|
MXVM 1.8.1
Virtual Machine, Compiler, and Pascal Frontend
|
Recursive-descent parser for the Pascal language. More...
#include <parser.hpp>

Public Member Functions | |
| PascalParser ()=delete | |
| PascalParser (const std::string &source) | |
| Construct and prepare the parser. | |
| std::unique_ptr< ProgramNode > | parseProgram () |
| Parse a complete Pascal program and return the root AST node. | |
| std::unique_ptr< UnitNode > | parseUnit () |
| Parse a Pascal unit and return the root AST node. | |
| bool | isUnitSource () const |
| Check if the source starts with a 'unit' keyword. | |
| Public Member Functions inherited from mxx::XParser | |
| XParser ()=delete | |
| XParser (const std::string &source) | |
| Construct and scan the source text. | |
| bool | next () |
| Advance to the next token; returns false at end of stream. | |
| bool | peekIs (const std::string &s) |
| Check if the current token's value matches a string (case-insensitive). | |
| bool | peekIs (const types::TokenType &t) |
| Check if the current token's type matches. | |
Public Attributes | |
| mxx::TPValidator | validator |
| semantic validator | |
| Public Attributes inherited from mxx::XParser | |
| std::string | filename |
| source filename for error messages | |
Private Member Functions | |
| void | removeBraceComments () |
| Remove {brace} comments from the token stream. | |
| void | error (const std::string &message) |
| Report a parse error with the given message. | |
| void | expectToken (const std::string &expected) |
| Consume a token matching the expected string, or throw. | |
| void | expectToken (types::TokenType expected) |
| Consume a token matching the expected type, or throw. | |
| bool | match (const std::string &s) |
| Try to match and consume a token by value. | |
| bool | match (types::TokenType t) |
| Try to match and consume a token by type. | |
| std::unique_ptr< ASTNode > | parseLValue () |
| Parse an lvalue (variable, array access, pointer deref, field access). | |
| std::unique_ptr< BlockNode > | parseBlock () |
| Parse a block (declarations + compound statement). | |
| std::unique_ptr< CompoundStmtNode > | parseCompoundStatement () |
| Parse a compound statement (begin..end). | |
| std::vector< std::unique_ptr< ASTNode > > | parseDeclarations () |
| Parse the declarations section. | |
| std::unique_ptr< ASTNode > | parseVarDeclaration () |
| Parse a var declaration section. | |
| std::unique_ptr< ASTNode > | parseConstDeclaration () |
| Parse a const declaration section. | |
| std::unique_ptr< ASTNode > | parseProcedureDeclaration () |
| Parse a procedure declaration. | |
| std::unique_ptr< ASTNode > | parseFunctionDeclaration () |
| Parse a function declaration. | |
| std::vector< std::unique_ptr< ASTNode > > | parseInterfaceDeclarations () |
| Parse interface forward declarations (procedure/function signatures only). | |
| std::unique_ptr< ASTNode > | parseProcedureForwardDecl () |
| Parse a procedure forward declaration (signature only, no body). | |
| std::unique_ptr< ASTNode > | parseFunctionForwardDecl () |
| Parse a function forward declaration (signature only, no body). | |
| std::vector< std::unique_ptr< ASTNode > > | parseParameterList () |
| Parse a formal parameter list. | |
| std::vector< std::unique_ptr< ASTNode > > | parseStatementList () |
| Parse a list of statements (separated by semicolons). | |
| std::unique_ptr< ASTNode > | parseParameter () |
| Parse a single formal parameter. | |
| std::unique_ptr< ASTNode > | parseRecordType () |
| Parse a record type definition. | |
| std::unique_ptr< ASTNode > | parseTypeDeclaration () |
| Parse a type declaration section. | |
| std::unique_ptr< ASTNode > | parseStatement () |
| Parse a single statement. | |
| std::unique_ptr< ASTNode > | parseAssignmentOrProcCall () |
| Parse an assignment or procedure call starting with an identifier. | |
| std::unique_ptr< ASTNode > | parseIfStatement () |
| Parse an if-then-else statement. | |
| std::unique_ptr< ASTNode > | parseWhileStatement () |
| Parse a while-do statement. | |
| std::unique_ptr< ASTNode > | parseForStatement () |
| Parse a for-to/downto-do statement. | |
| std::unique_ptr< ASTNode > | parseRepeatStatement () |
| Parse a repeat-until statement. | |
| std::unique_ptr< ASTNode > | parseCaseStatement () |
| Parse a case statement. | |
| std::unique_ptr< ASTNode > | parseWithStatement () |
| Parse a with statement. | |
| std::unique_ptr< ASTNode > | parseGotoStatement () |
| Parse a goto statement. | |
| void | parseLabelDeclaration () |
| Parse a label declaration section (label 100, 200;). | |
| std::unique_ptr< ASTNode > | parseExpression () |
| Parse a full expression (simple expression with optional relational op). | |
| std::unique_ptr< ASTNode > | parseSimpleExpression () |
| Parse a simple expression (terms combined by +, -, or). | |
| std::unique_ptr< ASTNode > | parseTerm () |
| Parse a term (factors combined by *, /, div, mod, and). | |
| std::unique_ptr< ASTNode > | parseFactor () |
| Parse a factor (literal, variable, function call, sub-expression). | |
| std::unique_ptr< ASTNode > | parseProcedureCall (const std::string &name) |
| Parse a procedure call given its already-parsed name. | |
| std::unique_ptr< ASTNode > | parseFunctionCall (const std::string &name) |
| Parse a function call given its already-parsed name. | |
| std::vector< std::unique_ptr< ASTNode > > | parseArgumentList () |
| Parse a comma-separated argument list. | |
| bool | isRelationalOperator () |
| Check if current token is a relational operator. | |
| bool | isAddOperator () |
| Check if current token is an additive operator. | |
| bool | isMulOperator () |
| Check if current token is a multiplicative operator. | |
| std::unique_ptr< ASTNode > | parseTypeSpec () |
| Parse a type specifier. | |
| std::string | getRelationalOp () |
| Consume and return a relational operator string. | |
| std::string | getAddOp () |
| Consume and return an additive operator string. | |
| std::string | getMulOp () |
| Consume and return a multiplicative operator string. | |
| std::string | getUnaryOp () |
| Consume and return a unary operator string. | |
| BinaryOpNode::OpType | getComparisonOperator (const std::string &op) |
| Map a comparison operator string to BinaryOpNode::OpType. | |
| BinaryOpNode::OpType | getLogicalOperator (const std::string &op) |
| Map a logical operator string to BinaryOpNode::OpType. | |
| BinaryOpNode::OpType | getArithmeticOperator (const std::string &op) |
| Map an arithmetic operator string to BinaryOpNode::OpType. | |
| std::string | tokenTypeToString (types::TokenType type) |
| Convert a TokenType to its display string. | |
| bool | isType (const std::string &token) |
| Check if a string is a known type name. | |
| bool | isBuiltinProcedure (const std::string &name) |
| Check if a name is a built-in procedure. | |
| bool | isBuiltinFunction (const std::string &name) |
| Check if a name is a built-in function. | |
| bool | isKeyword (const std::string &s) |
| Check if a string is a reserved keyword. | |
| std::unique_ptr< ASTNode > | parseArrayType () |
| Parse an array type specification. | |
| std::unique_ptr< ASTNode > | parseArrayDeclaration (const std::string &varName) |
| Parse an array variable declaration. | |
| bool | isArrayAccess () |
| Check if the current context is an array access. | |
Private Attributes | |
| std::unordered_set< std::string > | declaredLabels |
| Set of user-declared goto labels. | |
Additional Inherited Members | |
| Static Public Member Functions inherited from mxx::XParser | |
| static std::string | tokenTypeToString (types::TokenType t) |
| Convert a TokenType to a human-readable string. | |
| Protected Attributes inherited from mxx::XParser | |
| scan::Scanner | scanner |
| underlying scanner | |
| scan::TToken * | token = nullptr |
| current token pointer | |
| size_t | index = 0 |
| current token index | |
Recursive-descent parser for the Pascal language.
Parses Pascal source code into an AST rooted at a ProgramNode. Includes a TPValidator for semantic checks during parsing.
Definition at line 112 of file parser.hpp.
|
delete |
|
inlineexplicit |
Construct and prepare the parser.
| source | Pascal source code string |
Definition at line 119 of file parser.hpp.
References mxx::XParser::XParser(), mxx::XParser::index, removeBraceComments(), mxx::XParser::scanner, mxx::XParser::token, and validator.
|
private |
Report a parse error with the given message.
Definition at line 33 of file parser.cpp.
References mxx::XParser::token.
Referenced by expectToken(), expectToken(), getRelationalOp(), parseFactor(), parseForStatement(), parseRepeatStatement(), and parseStatement().
|
private |
Consume a token matching the expected string, or throw.
Definition at line 37 of file parser.cpp.
References error(), mxx::XParser::peekIs(), and mxx::XParser::token.
Referenced by parseArrayType(), parseAssignmentOrProcCall(), parseCaseStatement(), parseCompoundStatement(), parseConstDeclaration(), parseFactor(), parseForStatement(), parseFunctionCall(), parseFunctionDeclaration(), parseFunctionForwardDecl(), parseGotoStatement(), parseIfStatement(), parseLabelDeclaration(), parseLValue(), parseParameter(), parseProcedureCall(), parseProcedureDeclaration(), parseProcedureForwardDecl(), parseProgram(), parseRecordType(), parseRepeatStatement(), parseStatement(), parseTypeDeclaration(), parseTypeSpec(), parseUnit(), parseVarDeclaration(), parseWhileStatement(), and parseWithStatement().
|
private |
Consume a token matching the expected type, or throw.
Definition at line 42 of file parser.cpp.
References error(), mxx::XParser::peekIs(), and tokenTypeToString().
|
private |
Consume and return an additive operator string.
|
private |
Map an arithmetic operator string to BinaryOpNode::OpType.
|
private |
Map a comparison operator string to BinaryOpNode::OpType.
|
private |
Map a logical operator string to BinaryOpNode::OpType.
|
private |
Consume and return a multiplicative operator string.
|
private |
Consume and return a relational operator string.
Definition at line 965 of file parser.cpp.
References error(), and mxx::XParser::peekIs().
Referenced by parseExpression().
|
private |
Consume and return a unary operator string.
|
private |
Check if current token is an additive operator.
|
private |
Check if the current context is an array access.
|
private |
Check if a name is a built-in function.
|
private |
Check if a name is a built-in procedure.
|
private |
Check if a string is a reserved keyword.
Definition at line 261 of file parser.cpp.
Referenced by parseConstDeclaration(), parseDeclarations(), parseInterfaceDeclarations(), and parseTypeDeclaration().
|
private |
Check if current token is a multiplicative operator.
Definition at line 951 of file parser.cpp.
References mxx::XParser::peekIs(), types::TT_ID, and types::TT_SYM.
Referenced by parseTerm().
|
private |
Check if current token is a relational operator.
Definition at line 957 of file parser.cpp.
References mxx::XParser::peekIs(), and types::TT_SYM.
Referenced by parseExpression().
|
private |
Check if a string is a known type name.
References mxx::XParser::token.
| bool pascal::PascalParser::isUnitSource | ( | ) | const |
Check if the source starts with a 'unit' keyword.
Definition at line 87 of file parser.cpp.
References mxx::XParser::token.
Referenced by compileFile().
|
private |
Try to match and consume a token by value.
Definition at line 79 of file parser.cpp.
References mxx::XParser::next(), and mxx::XParser::peekIs().
Referenced by parseDeclarations(), and parseInterfaceDeclarations().
|
private |
Try to match and consume a token by type.
Definition at line 241 of file parser.cpp.
References mxx::XParser::next(), and mxx::XParser::peekIs().
|
private |
Parse a comma-separated argument list.
Definition at line 939 of file parser.cpp.
References mxx::XParser::next(), parseExpression(), and mxx::XParser::peekIs().
Referenced by parseFunctionCall(), and parseProcedureCall().
|
private |
Parse an array variable declaration.
|
private |
Parse an array type specification.
Definition at line 1230 of file parser.cpp.
References expectToken(), mxx::XParser::next(), parseExpression(), parseTypeSpec(), and mxx::XParser::peekIs().
Referenced by parseTypeDeclaration(), and parseTypeSpec().
|
private |
Parse an assignment or procedure call starting with an identifier.
Definition at line 517 of file parser.cpp.
References expectToken(), mxx::XParser::next(), parseExpression(), parseProcedureCall(), mxx::XParser::peekIs(), mxx::XParser::token, and types::TT_ID.
|
private |
Parse a block (declarations + compound statement).
Definition at line 249 of file parser.cpp.
References parseCompoundStatement(), parseDeclarations(), parseLabelDeclaration(), mxx::XParser::peekIs(), and mxx::XParser::token.
Referenced by parseFunctionDeclaration(), parseProcedureDeclaration(), and parseProgram().
|
private |
Parse a case statement.
Definition at line 842 of file parser.cpp.
References expectToken(), mxx::XParser::next(), parseExpression(), parseStatement(), mxx::XParser::peekIs(), and mxx::XParser::token.
Referenced by parseStatement().
|
private |
Parse a compound statement (begin..end).
Definition at line 400 of file parser.cpp.
References expectToken(), mxx::XParser::next(), parseStatementList(), and mxx::XParser::token.
Referenced by parseBlock(), and parseStatement().
|
private |
Parse a const declaration section.
Definition at line 878 of file parser.cpp.
References expectToken(), isKeyword(), mxx::XParser::next(), parseExpression(), mxx::XParser::peekIs(), mxx::XParser::token, and types::TT_ID.
Referenced by parseDeclarations(), and parseInterfaceDeclarations().
|
private |
Parse the declarations section.
Definition at line 899 of file parser.cpp.
References isKeyword(), match(), parseConstDeclaration(), parseFunctionDeclaration(), parseLabelDeclaration(), parseProcedureDeclaration(), parseTypeDeclaration(), parseVarDeclaration(), mxx::XParser::peekIs(), mxx::XParser::token, and types::TT_ID.
Referenced by parseBlock(), and parseUnit().
|
private |
Parse a full expression (simple expression with optional relational op).
Definition at line 615 of file parser.cpp.
References pascal::BinaryOpNode::AND, pascal::BinaryOpNode::EQUAL, getRelationalOp(), pascal::BinaryOpNode::GREATER, pascal::BinaryOpNode::GREATER_EQUAL, pascal::BinaryOpNode::IN, isRelationalOperator(), pascal::BinaryOpNode::LESS, pascal::BinaryOpNode::LESS_EQUAL, mxx::XParser::next(), pascal::BinaryOpNode::NOT_EQUAL, pascal::BinaryOpNode::OR, parseSimpleExpression(), mxx::XParser::peekIs(), and mxx::XParser::token.
Referenced by parseArgumentList(), parseArrayType(), parseAssignmentOrProcCall(), parseCaseStatement(), parseConstDeclaration(), parseFactor(), parseForStatement(), parseIfStatement(), parseLValue(), parseRecordType(), parseRepeatStatement(), parseStatement(), parseVarDeclaration(), and parseWhileStatement().
|
private |
Parse a factor (literal, variable, function call, sub-expression).
Definition at line 715 of file parser.cpp.
References error(), expectToken(), mxx::XParser::index, mxx::XParser::next(), parseExpression(), parseFactor(), parseFunctionCall(), mxx::XParser::peekIs(), mxx::XParser::token, types::TT_ID, types::TT_NUM, and types::TT_STR.
Referenced by parseFactor(), and parseTerm().
|
private |
Parse a for-to/downto-do statement.
Definition at line 566 of file parser.cpp.
References error(), expectToken(), mxx::XParser::next(), parseExpression(), parseStatement(), mxx::XParser::peekIs(), mxx::XParser::token, and types::TT_ID.
Referenced by parseStatement().
|
private |
Parse a function call given its already-parsed name.
Definition at line 829 of file parser.cpp.
References expectToken(), mxx::XParser::next(), parseArgumentList(), and mxx::XParser::token.
Referenced by parseFactor().
|
private |
Parse a function declaration.
Definition at line 303 of file parser.cpp.
References expectToken(), mxx::XParser::next(), parseBlock(), parseParameterList(), mxx::XParser::peekIs(), mxx::XParser::token, and types::TT_ID.
Referenced by parseDeclarations().
|
private |
Parse a function forward declaration (signature only, no body).
Definition at line 207 of file parser.cpp.
References expectToken(), mxx::XParser::next(), parseParameterList(), mxx::XParser::peekIs(), mxx::XParser::token, and types::TT_ID.
Referenced by parseInterfaceDeclarations().
|
private |
Parse a goto statement.
Definition at line 1325 of file parser.cpp.
References expectToken(), mxx::XParser::next(), mxx::XParser::token, and types::TT_NUM.
Referenced by parseStatement().
|
private |
Parse an if-then-else statement.
Definition at line 535 of file parser.cpp.
References expectToken(), mxx::XParser::next(), parseExpression(), parseStatement(), mxx::XParser::peekIs(), and mxx::XParser::token.
Referenced by parseStatement().
|
private |
Parse interface forward declarations (procedure/function signatures only).
Definition at line 163 of file parser.cpp.
References isKeyword(), match(), parseConstDeclaration(), parseFunctionForwardDecl(), parseProcedureForwardDecl(), parseTypeDeclaration(), parseVarDeclaration(), mxx::XParser::peekIs(), mxx::XParser::token, and types::TT_ID.
Referenced by parseUnit().
|
private |
Parse a label declaration section (label 100, 200;).
Definition at line 1337 of file parser.cpp.
References declaredLabels, expectToken(), mxx::XParser::next(), mxx::XParser::peekIs(), mxx::XParser::token, and types::TT_NUM.
Referenced by parseBlock(), and parseDeclarations().
|
private |
Parse an lvalue (variable, array access, pointer deref, field access).
Definition at line 1275 of file parser.cpp.
References expectToken(), mxx::XParser::index, mxx::XParser::next(), parseExpression(), mxx::XParser::peekIs(), mxx::XParser::token, and types::TT_ID.
Referenced by parseStatement().
|
private |
Parse a single formal parameter.
Definition at line 360 of file parser.cpp.
References expectToken(), mxx::XParser::next(), mxx::XParser::peekIs(), mxx::XParser::token, and types::TT_ID.
Referenced by parseParameterList().
|
private |
Parse a formal parameter list.
Definition at line 348 of file parser.cpp.
References mxx::XParser::next(), parseParameter(), and mxx::XParser::peekIs().
Referenced by parseFunctionDeclaration(), parseFunctionForwardDecl(), parseProcedureDeclaration(), and parseProcedureForwardDecl().
|
private |
Parse a procedure call given its already-parsed name.
Definition at line 815 of file parser.cpp.
References expectToken(), mxx::XParser::next(), parseArgumentList(), mxx::XParser::peekIs(), and mxx::XParser::token.
Referenced by parseAssignmentOrProcCall(), and parseStatement().
|
private |
Parse a procedure declaration.
Definition at line 279 of file parser.cpp.
References expectToken(), mxx::XParser::next(), parseBlock(), parseParameterList(), mxx::XParser::peekIs(), mxx::XParser::token, and types::TT_ID.
Referenced by parseDeclarations().
|
private |
Parse a procedure forward declaration (signature only, no body).
Definition at line 186 of file parser.cpp.
References expectToken(), mxx::XParser::next(), parseParameterList(), mxx::XParser::peekIs(), mxx::XParser::token, and types::TT_ID.
Referenced by parseInterfaceDeclarations().
| std::unique_ptr< ProgramNode > pascal::PascalParser::parseProgram | ( | ) |
Parse a complete Pascal program and return the root AST node.
Definition at line 47 of file parser.cpp.
References expectToken(), mxx::XParser::next(), parseBlock(), mxx::XParser::peekIs(), mxx::XParser::token, and types::TT_ID.
Referenced by compileFile().
|
private |
Parse a record type definition.
Definition at line 1070 of file parser.cpp.
References pascal::VariantArm::caseLabels, expectToken(), pascal::VariantArm::fields, mxx::XParser::next(), parseExpression(), parseTypeSpec(), mxx::XParser::peekIs(), mxx::XParser::token, and types::TT_ID.
Referenced by parseTypeDeclaration(), and parseTypeSpec().
|
private |
Parse a repeat-until statement.
Definition at line 594 of file parser.cpp.
References error(), expectToken(), mxx::XParser::next(), parseExpression(), parseStatement(), mxx::XParser::peekIs(), and mxx::XParser::token.
Referenced by parseStatement().
|
private |
Parse a simple expression (terms combined by +, -, or).
Definition at line 662 of file parser.cpp.
References pascal::BinaryOpNode::MINUS, pascal::UnaryOpNode::MINUS, mxx::XParser::next(), pascal::UnaryOpNode::NOT, parseTerm(), mxx::XParser::peekIs(), pascal::BinaryOpNode::PLUS, pascal::UnaryOpNode::PLUS, mxx::XParser::token, and types::TT_SYM.
Referenced by parseExpression().
|
private |
Parse a single statement.
Definition at line 412 of file parser.cpp.
References error(), expectToken(), mxx::XParser::next(), parseCaseStatement(), parseCompoundStatement(), parseExpression(), parseForStatement(), parseGotoStatement(), parseIfStatement(), parseLValue(), parseProcedureCall(), parseRepeatStatement(), parseStatement(), parseWhileStatement(), parseWithStatement(), mxx::XParser::peekIs(), mxx::XParser::token, types::TT_ID, and types::TT_NUM.
Referenced by parseCaseStatement(), parseForStatement(), parseIfStatement(), parseRepeatStatement(), parseStatement(), parseStatementList(), parseWhileStatement(), and parseWithStatement().
|
private |
Parse a list of statements (separated by semicolons).
Definition at line 924 of file parser.cpp.
References mxx::XParser::next(), parseStatement(), and mxx::XParser::peekIs().
Referenced by parseCompoundStatement().
|
private |
Parse a term (factors combined by *, /, div, mod, and).
Definition at line 694 of file parser.cpp.
References pascal::BinaryOpNode::DIV, pascal::BinaryOpNode::DIVIDE, isMulOperator(), pascal::BinaryOpNode::MOD, pascal::BinaryOpNode::MULTIPLY, mxx::XParser::next(), parseFactor(), mxx::XParser::peekIs(), and mxx::XParser::token.
Referenced by parseSimpleExpression().
|
private |
Parse a type declaration section.
Definition at line 984 of file parser.cpp.
References expectToken(), isKeyword(), mxx::XParser::next(), parseArrayType(), parseRecordType(), mxx::XParser::peekIs(), mxx::XParser::token, and types::TT_ID.
Referenced by parseDeclarations(), and parseInterfaceDeclarations().
|
private |
Parse a type specifier.
Definition at line 1192 of file parser.cpp.
References expectToken(), mxx::XParser::next(), parseArrayType(), parseRecordType(), mxx::XParser::peekIs(), mxx::XParser::token, and types::TT_ID.
Referenced by parseArrayType(), parseRecordType(), and parseVarDeclaration().
| std::unique_ptr< UnitNode > pascal::PascalParser::parseUnit | ( | ) |
Parse a Pascal unit and return the root AST node.
Definition at line 95 of file parser.cpp.
References expectToken(), mxx::XParser::next(), parseDeclarations(), parseInterfaceDeclarations(), mxx::XParser::peekIs(), mxx::XParser::token, and types::TT_ID.
Referenced by compileFile().
|
private |
Parse a var declaration section.
Definition at line 1159 of file parser.cpp.
References expectToken(), mxx::XParser::next(), parseExpression(), parseTypeSpec(), mxx::XParser::peekIs(), mxx::XParser::token, and types::TT_ID.
Referenced by parseDeclarations(), and parseInterfaceDeclarations().
|
private |
Parse a while-do statement.
Definition at line 553 of file parser.cpp.
References expectToken(), mxx::XParser::next(), parseExpression(), parseStatement(), and mxx::XParser::token.
Referenced by parseStatement().
|
private |
Parse a with statement.
Definition at line 1310 of file parser.cpp.
References expectToken(), mxx::XParser::next(), parseStatement(), mxx::XParser::token, and types::TT_ID.
Referenced by parseStatement().
|
private |
Remove {brace} comments from the token stream.
Definition at line 12 of file parser.cpp.
References mxx::XParser::scanner.
Referenced by PascalParser().
|
private |
Convert a TokenType to its display string.
Definition at line 1260 of file parser.cpp.
References types::TT_ID, types::TT_NUM, types::TT_STR, and types::TT_SYM.
Referenced by expectToken().
|
private |
Set of user-declared goto labels.
Definition at line 203 of file parser.hpp.
Referenced by parseLabelDeclaration().
| mxx::TPValidator pascal::PascalParser::validator |
semantic validator
Definition at line 135 of file parser.hpp.
Referenced by PascalParser(), and compileFile().