MXVM 1.8.1
Virtual Machine, Compiler, and Pascal Frontend
Loading...
Searching...
No Matches
pascal::PascalParser Class Reference

Recursive-descent parser for the Pascal language. More...

#include <parser.hpp>

Inheritance diagram for pascal::PascalParser:

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

Detailed Description

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.

Constructor & Destructor Documentation

◆ PascalParser() [1/2]

pascal::PascalParser::PascalParser ( )
delete

◆ PascalParser() [2/2]

pascal::PascalParser::PascalParser ( const std::string & source)
inlineexplicit

Construct and prepare the parser.

Parameters
sourcePascal 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.

Member Function Documentation

◆ error()

void pascal::PascalParser::error ( const std::string & message)
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().

◆ expectToken() [1/2]

◆ expectToken() [2/2]

void pascal::PascalParser::expectToken ( types::TokenType expected)
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().

◆ getAddOp()

std::string pascal::PascalParser::getAddOp ( )
private

Consume and return an additive operator string.

◆ getArithmeticOperator()

BinaryOpNode::OpType pascal::PascalParser::getArithmeticOperator ( const std::string & op)
private

Map an arithmetic operator string to BinaryOpNode::OpType.

◆ getComparisonOperator()

BinaryOpNode::OpType pascal::PascalParser::getComparisonOperator ( const std::string & op)
private

Map a comparison operator string to BinaryOpNode::OpType.

◆ getLogicalOperator()

BinaryOpNode::OpType pascal::PascalParser::getLogicalOperator ( const std::string & op)
private

Map a logical operator string to BinaryOpNode::OpType.

◆ getMulOp()

std::string pascal::PascalParser::getMulOp ( )
private

Consume and return a multiplicative operator string.

◆ getRelationalOp()

std::string pascal::PascalParser::getRelationalOp ( )
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().

◆ getUnaryOp()

std::string pascal::PascalParser::getUnaryOp ( )
private

Consume and return a unary operator string.

◆ isAddOperator()

bool pascal::PascalParser::isAddOperator ( )
private

Check if current token is an additive operator.

◆ isArrayAccess()

bool pascal::PascalParser::isArrayAccess ( )
private

Check if the current context is an array access.

◆ isBuiltinFunction()

bool pascal::PascalParser::isBuiltinFunction ( const std::string & name)
private

Check if a name is a built-in function.

◆ isBuiltinProcedure()

bool pascal::PascalParser::isBuiltinProcedure ( const std::string & name)
private

Check if a name is a built-in procedure.

◆ isKeyword()

bool pascal::PascalParser::isKeyword ( const std::string & s)
private

Check if a string is a reserved keyword.

Definition at line 261 of file parser.cpp.

Referenced by parseConstDeclaration(), parseDeclarations(), parseInterfaceDeclarations(), and parseTypeDeclaration().

◆ isMulOperator()

bool pascal::PascalParser::isMulOperator ( )
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().

◆ isRelationalOperator()

bool pascal::PascalParser::isRelationalOperator ( )
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().

◆ isType()

bool pascal::PascalParser::isType ( const std::string & token)
private

Check if a string is a known type name.

References mxx::XParser::token.

◆ isUnitSource()

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().

◆ match() [1/2]

bool pascal::PascalParser::match ( const std::string & s)
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().

◆ match() [2/2]

bool pascal::PascalParser::match ( types::TokenType t)
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().

◆ parseArgumentList()

std::vector< std::unique_ptr< ASTNode > > pascal::PascalParser::parseArgumentList ( )
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().

◆ parseArrayDeclaration()

std::unique_ptr< ASTNode > pascal::PascalParser::parseArrayDeclaration ( const std::string & varName)
private

Parse an array variable declaration.

◆ parseArrayType()

std::unique_ptr< ASTNode > pascal::PascalParser::parseArrayType ( )
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().

◆ parseAssignmentOrProcCall()

std::unique_ptr< ASTNode > pascal::PascalParser::parseAssignmentOrProcCall ( )
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.

◆ parseBlock()

std::unique_ptr< BlockNode > pascal::PascalParser::parseBlock ( )
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().

◆ parseCaseStatement()

std::unique_ptr< ASTNode > pascal::PascalParser::parseCaseStatement ( )
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().

◆ parseCompoundStatement()

std::unique_ptr< CompoundStmtNode > pascal::PascalParser::parseCompoundStatement ( )
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().

◆ parseConstDeclaration()

std::unique_ptr< ASTNode > pascal::PascalParser::parseConstDeclaration ( )
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().

◆ parseDeclarations()

std::vector< std::unique_ptr< ASTNode > > pascal::PascalParser::parseDeclarations ( )
private

◆ parseExpression()

◆ parseFactor()

std::unique_ptr< ASTNode > pascal::PascalParser::parseFactor ( )
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().

◆ parseForStatement()

std::unique_ptr< ASTNode > pascal::PascalParser::parseForStatement ( )
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().

◆ parseFunctionCall()

std::unique_ptr< ASTNode > pascal::PascalParser::parseFunctionCall ( const std::string & name)
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().

◆ parseFunctionDeclaration()

std::unique_ptr< ASTNode > pascal::PascalParser::parseFunctionDeclaration ( )
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().

◆ parseFunctionForwardDecl()

std::unique_ptr< ASTNode > pascal::PascalParser::parseFunctionForwardDecl ( )
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().

◆ parseGotoStatement()

std::unique_ptr< ASTNode > pascal::PascalParser::parseGotoStatement ( )
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().

◆ parseIfStatement()

std::unique_ptr< ASTNode > pascal::PascalParser::parseIfStatement ( )
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().

◆ parseInterfaceDeclarations()

std::vector< std::unique_ptr< ASTNode > > pascal::PascalParser::parseInterfaceDeclarations ( )
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().

◆ parseLabelDeclaration()

void pascal::PascalParser::parseLabelDeclaration ( )
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().

◆ parseLValue()

std::unique_ptr< ASTNode > pascal::PascalParser::parseLValue ( )
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().

◆ parseParameter()

std::unique_ptr< ASTNode > pascal::PascalParser::parseParameter ( )
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().

◆ parseParameterList()

std::vector< std::unique_ptr< ASTNode > > pascal::PascalParser::parseParameterList ( )
private

◆ parseProcedureCall()

std::unique_ptr< ASTNode > pascal::PascalParser::parseProcedureCall ( const std::string & name)
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().

◆ parseProcedureDeclaration()

std::unique_ptr< ASTNode > pascal::PascalParser::parseProcedureDeclaration ( )
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().

◆ parseProcedureForwardDecl()

std::unique_ptr< ASTNode > pascal::PascalParser::parseProcedureForwardDecl ( )
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().

◆ parseProgram()

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().

◆ parseRecordType()

std::unique_ptr< ASTNode > pascal::PascalParser::parseRecordType ( )
private

◆ parseRepeatStatement()

std::unique_ptr< ASTNode > pascal::PascalParser::parseRepeatStatement ( )
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().

◆ parseSimpleExpression()

std::unique_ptr< ASTNode > pascal::PascalParser::parseSimpleExpression ( )
private

◆ parseStatement()

◆ parseStatementList()

std::vector< std::unique_ptr< ASTNode > > pascal::PascalParser::parseStatementList ( )
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().

◆ parseTerm()

std::unique_ptr< ASTNode > pascal::PascalParser::parseTerm ( )
private

◆ parseTypeDeclaration()

std::unique_ptr< ASTNode > pascal::PascalParser::parseTypeDeclaration ( )
private

◆ parseTypeSpec()

std::unique_ptr< ASTNode > pascal::PascalParser::parseTypeSpec ( )
private

◆ parseUnit()

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().

◆ parseVarDeclaration()

std::unique_ptr< ASTNode > pascal::PascalParser::parseVarDeclaration ( )
private

◆ parseWhileStatement()

std::unique_ptr< ASTNode > pascal::PascalParser::parseWhileStatement ( )
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().

◆ parseWithStatement()

std::unique_ptr< ASTNode > pascal::PascalParser::parseWithStatement ( )
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().

◆ removeBraceComments()

void pascal::PascalParser::removeBraceComments ( )
private

Remove {brace} comments from the token stream.

Definition at line 12 of file parser.cpp.

References mxx::XParser::scanner.

Referenced by PascalParser().

◆ tokenTypeToString()

std::string pascal::PascalParser::tokenTypeToString ( types::TokenType type)
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().

Member Data Documentation

◆ declaredLabels

std::unordered_set<std::string> pascal::PascalParser::declaredLabels
private

Set of user-declared goto labels.

Definition at line 203 of file parser.hpp.

Referenced by parseLabelDeclaration().

◆ validator

mxx::TPValidator pascal::PascalParser::validator

semantic validator

Definition at line 135 of file parser.hpp.

Referenced by PascalParser(), and compileFile().


The documentation for this class was generated from the following files: