MXVM 1.8.1
Virtual Machine, Compiler, and Pascal Frontend
Loading...
Searching...
No Matches
parser.hpp
Go to the documentation of this file.
1
6#ifndef _PARSER_H__H
7#define _PARSER_H__H
8#include "ast.hpp"
9#include "scanner/scanner.hpp"
10#include "validator.hpp"
11#include <memory>
12#include <stdexcept>
13#include <string>
14#include <vector>
15
16namespace pascal {
18 class ParseException : public std::runtime_error {
19 public:
21 explicit ParseException(const std::string &msg) : std::runtime_error(msg) {}
22 };
23} // namespace pascal
24
25namespace mxx {
32 class XParser {
33 public:
34 XParser() = delete;
39 explicit XParser(const std::string &source)
40 : scanner(source), token(nullptr), index(0) {
41 scanner.scan();
42 scanner.removeEOL();
43 if (scanner.size() > 0) {
44 token = &scanner[0];
45 index = 0;
46 }
47 }
48
50 bool next() {
51 if (index + 1 < scanner.size()) {
52 index++;
54 return true;
55 }
56 token = nullptr;
57 return false;
58 }
59
61 bool peekIs(const std::string &s) {
62 if (!token) return false;
63 const auto &v = token->getTokenValue();
64 if (v.size() != s.size()) return false;
65 for (size_t i = 0; i < v.size(); ++i)
66 if (std::tolower(static_cast<unsigned char>(v[i])) !=
67 std::tolower(static_cast<unsigned char>(s[i])))
68 return false;
69 return true;
70 }
71
72 bool peekIs(const types::TokenType &t) {
73 return token && token->getTokenType() == t;
74 }
75
81 static std::string tokenTypeToString(types::TokenType t) {
82 switch (t) {
84 return "IDENTIFIER";
86 return "NUMBER";
88 return "STRING";
90 return "SYMBOL";
91 default:
92 return "UNKNOWN";
93 }
94 }
95
96 std::string filename;
97
98 protected:
100 scan::TToken *token = nullptr;
101 size_t index = 0;
102 };
103} // namespace mxx
104
105namespace pascal {
112 class PascalParser : public mxx::XParser {
113 public:
114 PascalParser() = delete;
119 explicit PascalParser(const std::string &source) : mxx::XParser(source), validator(source) {
121 if (scanner.size() > 0) {
122 token = &scanner[0];
123 index = 0;
124 } else {
125 token = nullptr;
126 }
127 }
128
130 std::unique_ptr<ProgramNode> parseProgram();
132 std::unique_ptr<UnitNode> parseUnit();
134 bool isUnitSource() const;
136
137 private:
139 void removeBraceComments();
141 void error(const std::string &message);
143 void expectToken(const std::string &expected);
145 void expectToken(types::TokenType expected);
147 bool match(const std::string &s);
149 bool match(types::TokenType t);
151 std::unique_ptr<ASTNode> parseLValue();
153 std::unique_ptr<BlockNode> parseBlock();
155 std::unique_ptr<CompoundStmtNode> parseCompoundStatement();
157 std::vector<std::unique_ptr<ASTNode>> parseDeclarations();
159 std::unique_ptr<ASTNode> parseVarDeclaration();
161 std::unique_ptr<ASTNode> parseConstDeclaration();
163 std::unique_ptr<ASTNode> parseProcedureDeclaration();
165 std::unique_ptr<ASTNode> parseFunctionDeclaration();
167 std::vector<std::unique_ptr<ASTNode>> parseInterfaceDeclarations();
169 std::unique_ptr<ASTNode> parseProcedureForwardDecl();
171 std::unique_ptr<ASTNode> parseFunctionForwardDecl();
173 std::vector<std::unique_ptr<ASTNode>> parseParameterList();
175 std::vector<std::unique_ptr<ASTNode>> parseStatementList();
177 std::unique_ptr<ASTNode> parseParameter();
179 std::unique_ptr<ASTNode> parseRecordType();
181 std::unique_ptr<ASTNode> parseTypeDeclaration();
183 std::unique_ptr<ASTNode> parseStatement();
185 std::unique_ptr<ASTNode> parseAssignmentOrProcCall();
187 std::unique_ptr<ASTNode> parseIfStatement();
189 std::unique_ptr<ASTNode> parseWhileStatement();
191 std::unique_ptr<ASTNode> parseForStatement();
193 std::unique_ptr<ASTNode> parseRepeatStatement();
195 std::unique_ptr<ASTNode> parseCaseStatement();
197 std::unique_ptr<ASTNode> parseWithStatement();
199 std::unique_ptr<ASTNode> parseGotoStatement();
203 std::unordered_set<std::string> declaredLabels;
205 std::unique_ptr<ASTNode> parseExpression();
207 std::unique_ptr<ASTNode> parseSimpleExpression();
209 std::unique_ptr<ASTNode> parseTerm();
211 std::unique_ptr<ASTNode> parseFactor();
213 std::unique_ptr<ASTNode> parseProcedureCall(const std::string &name);
215 std::unique_ptr<ASTNode> parseFunctionCall(const std::string &name);
217 std::vector<std::unique_ptr<ASTNode>> parseArgumentList();
218
224 bool isMulOperator();
225
227 std::unique_ptr<ASTNode> parseTypeSpec();
228
230 std::string getRelationalOp();
232 std::string getAddOp();
234 std::string getMulOp();
236 std::string getUnaryOp();
244 std::string tokenTypeToString(types::TokenType type);
246 bool isType(const std::string &token);
248 bool isBuiltinProcedure(const std::string &name);
250 bool isBuiltinFunction(const std::string &name);
252 bool isKeyword(const std::string &s);
254 std::unique_ptr<ASTNode> parseArrayType();
256 std::unique_ptr<ASTNode> parseArrayDeclaration(const std::string &varName);
259 };
260} // namespace pascal
261
262#endif
Semantic validator for Pascal programs.
Definition validator.hpp:34
Base parser providing token-stream navigation.
Definition parser.hpp:32
std::string filename
source filename for error messages
Definition parser.hpp:96
scan::TToken * token
current token pointer
Definition parser.hpp:100
bool peekIs(const types::TokenType &t)
Check if the current token's type matches.
Definition parser.hpp:72
XParser()=delete
scan::Scanner scanner
underlying scanner
Definition parser.hpp:99
bool peekIs(const std::string &s)
Check if the current token's value matches a string (case-insensitive).
Definition parser.hpp:61
XParser(const std::string &source)
Construct and scan the source text.
Definition parser.hpp:39
size_t index
current token index
Definition parser.hpp:101
bool next()
Advance to the next token; returns false at end of stream.
Definition parser.hpp:50
static std::string tokenTypeToString(types::TokenType t)
Convert a TokenType to a human-readable string.
Definition parser.hpp:81
OpType
Binary operator kinds.
Definition ast.hpp:441
ParseException(const std::string &msg)
Construct with an error message.
Definition parser.hpp:21
bool isBuiltinProcedure(const std::string &name)
Check if a name is a built-in procedure.
bool isAddOperator()
Check if current token is an additive operator.
std::unique_ptr< ASTNode > parseWithStatement()
Parse a with statement.
Definition parser.cpp:1310
bool isType(const std::string &token)
Check if a string is a known type name.
PascalParser(const std::string &source)
Construct and prepare the parser.
Definition parser.hpp:119
bool match(const std::string &s)
Try to match and consume a token by value.
Definition parser.cpp:79
std::unique_ptr< ASTNode > parseTypeDeclaration()
Parse a type declaration section.
Definition parser.cpp:984
std::vector< std::unique_ptr< ASTNode > > parseStatementList()
Parse a list of statements (separated by semicolons).
Definition parser.cpp:924
void error(const std::string &message)
Report a parse error with the given message.
Definition parser.cpp:33
std::unique_ptr< ASTNode > parseVarDeclaration()
Parse a var declaration section.
Definition parser.cpp:1159
std::unique_ptr< ASTNode > parseExpression()
Parse a full expression (simple expression with optional relational op).
Definition parser.cpp:615
std::unique_ptr< ASTNode > parseGotoStatement()
Parse a goto statement.
Definition parser.cpp:1325
std::unique_ptr< ASTNode > parseProcedureDeclaration()
Parse a procedure declaration.
Definition parser.cpp:279
std::unique_ptr< ASTNode > parseParameter()
Parse a single formal parameter.
Definition parser.cpp:360
std::unique_ptr< ASTNode > parseTypeSpec()
Parse a type specifier.
Definition parser.cpp:1192
void expectToken(const std::string &expected)
Consume a token matching the expected string, or throw.
Definition parser.cpp:37
void parseLabelDeclaration()
Parse a label declaration section (label 100, 200;).
Definition parser.cpp:1337
std::unique_ptr< ProgramNode > parseProgram()
Parse a complete Pascal program and return the root AST node.
Definition parser.cpp:47
bool isKeyword(const std::string &s)
Check if a string is a reserved keyword.
Definition parser.cpp:261
std::string getAddOp()
Consume and return an additive operator string.
std::unique_ptr< ASTNode > parseRecordType()
Parse a record type definition.
Definition parser.cpp:1070
std::string getUnaryOp()
Consume and return a unary operator string.
std::unique_ptr< ASTNode > parseForStatement()
Parse a for-to/downto-do statement.
Definition parser.cpp:566
BinaryOpNode::OpType getLogicalOperator(const std::string &op)
Map a logical operator string to BinaryOpNode::OpType.
std::unique_ptr< ASTNode > parseIfStatement()
Parse an if-then-else statement.
Definition parser.cpp:535
std::unique_ptr< ASTNode > parseSimpleExpression()
Parse a simple expression (terms combined by +, -, or).
Definition parser.cpp:662
bool isBuiltinFunction(const std::string &name)
Check if a name is a built-in function.
std::unique_ptr< ASTNode > parseConstDeclaration()
Parse a const declaration section.
Definition parser.cpp:878
std::vector< std::unique_ptr< ASTNode > > parseParameterList()
Parse a formal parameter list.
Definition parser.cpp:348
std::unique_ptr< ASTNode > parseAssignmentOrProcCall()
Parse an assignment or procedure call starting with an identifier.
Definition parser.cpp:517
std::unique_ptr< BlockNode > parseBlock()
Parse a block (declarations + compound statement).
Definition parser.cpp:249
std::unique_ptr< UnitNode > parseUnit()
Parse a Pascal unit and return the root AST node.
Definition parser.cpp:95
std::unique_ptr< ASTNode > parseProcedureForwardDecl()
Parse a procedure forward declaration (signature only, no body).
Definition parser.cpp:186
std::unordered_set< std::string > declaredLabels
Set of user-declared goto labels.
Definition parser.hpp:203
std::vector< std::unique_ptr< ASTNode > > parseArgumentList()
Parse a comma-separated argument list.
Definition parser.cpp:939
bool isUnitSource() const
Check if the source starts with a 'unit' keyword.
Definition parser.cpp:87
std::unique_ptr< ASTNode > parseArrayDeclaration(const std::string &varName)
Parse an array variable declaration.
bool isMulOperator()
Check if current token is a multiplicative operator.
Definition parser.cpp:951
std::vector< std::unique_ptr< ASTNode > > parseInterfaceDeclarations()
Parse interface forward declarations (procedure/function signatures only).
Definition parser.cpp:163
std::string tokenTypeToString(types::TokenType type)
Convert a TokenType to its display string.
Definition parser.cpp:1260
std::unique_ptr< ASTNode > parseFunctionDeclaration()
Parse a function declaration.
Definition parser.cpp:303
std::vector< std::unique_ptr< ASTNode > > parseDeclarations()
Parse the declarations section.
Definition parser.cpp:899
std::unique_ptr< ASTNode > parseFunctionForwardDecl()
Parse a function forward declaration (signature only, no body).
Definition parser.cpp:207
std::unique_ptr< ASTNode > parseFunctionCall(const std::string &name)
Parse a function call given its already-parsed name.
Definition parser.cpp:829
void removeBraceComments()
Remove {brace} comments from the token stream.
Definition parser.cpp:12
std::unique_ptr< ASTNode > parseArrayType()
Parse an array type specification.
Definition parser.cpp:1230
std::unique_ptr< ASTNode > parseCaseStatement()
Parse a case statement.
Definition parser.cpp:842
std::unique_ptr< ASTNode > parseLValue()
Parse an lvalue (variable, array access, pointer deref, field access).
Definition parser.cpp:1275
std::string getMulOp()
Consume and return a multiplicative operator string.
BinaryOpNode::OpType getComparisonOperator(const std::string &op)
Map a comparison operator string to BinaryOpNode::OpType.
std::unique_ptr< ASTNode > parseProcedureCall(const std::string &name)
Parse a procedure call given its already-parsed name.
Definition parser.cpp:815
std::unique_ptr< ASTNode > parseFactor()
Parse a factor (literal, variable, function call, sub-expression).
Definition parser.cpp:715
std::unique_ptr< ASTNode > parseWhileStatement()
Parse a while-do statement.
Definition parser.cpp:553
mxx::TPValidator validator
semantic validator
Definition parser.hpp:135
BinaryOpNode::OpType getArithmeticOperator(const std::string &op)
Map an arithmetic operator string to BinaryOpNode::OpType.
std::unique_ptr< ASTNode > parseTerm()
Parse a term (factors combined by *, /, div, mod, and).
Definition parser.cpp:694
std::unique_ptr< ASTNode > parseRepeatStatement()
Parse a repeat-until statement.
Definition parser.cpp:594
std::string getRelationalOp()
Consume and return a relational operator string.
Definition parser.cpp:965
bool isArrayAccess()
Check if the current context is an array access.
std::unique_ptr< CompoundStmtNode > parseCompoundStatement()
Parse a compound statement (begin..end).
Definition parser.cpp:400
std::unique_ptr< ASTNode > parseStatement()
Parse a single statement.
Definition parser.cpp:412
bool isRelationalOperator()
Check if current token is a relational operator.
Definition parser.cpp:957
Lexical scanner that tokenizes source text.
Definition scanner.hpp:28
Definition expr.cpp:8
Definition ast.cpp:9
token::Token< char > TToken
Default token type.
Definition scanner.hpp:19
TokenType
Classification of scanned tokens.
Definition types.hpp:16
@ TT_SYM
symbol / operator token
Definition types.hpp:19
@ TT_STR
string literal
Definition types.hpp:20
@ TT_ID
identifier or keyword
Definition types.hpp:17
@ TT_NUM
decimal numeric literal
Definition types.hpp:21
Lexical scanner that tokenizes source text into typed tokens.
AST node hierarchy for the Pascal-to-MXVM frontend parser.
Semantic validator for Pascal programs — scope, type, and declaration checking.