12#include <unordered_map>
13#include <unordered_set>
19 std::unordered_set<std::string>
vars;
20 std::unordered_set<std::string>
consts;
21 std::unordered_set<std::string>
types;
22 std::unordered_set<std::string>
funcs;
23 std::unordered_set<std::string>
procs;
24 std::unordered_set<std::string>
params;
47 bool validate(
const std::string &name);
130 bool match(
const std::string &s)
const;
134 bool peekIs(
const std::string &s);
137 void require(
const std::string &s);
144 void fail(
const std::string &msg);
146 void failHere(
const std::string &msg);
153 static std::string
lower(std::string s);
155 std::string
found()
const;
157 bool isKW(
const std::string &k)
const;
222 void parseExprStop(
const std::unordered_set<std::string> &stops);
235 static std::string
toLower(
const std::string &s);
void declareProc(const std::string &name, const scan::TToken *at)
Declare a procedure in the current scope.
void pushRecordFieldScope(const std::string &recordTypeName)
Push a record field scope for the given type.
bool isVariableInCurrentScope(const std::string &name) const
Check if a variable is declared in the current scope.
void parseUnit()
Validate a Pascal unit.
void parseWith()
Validate a with statement.
bool isBuiltinConst(const std::string &name) const
Check if a name is a built-in constant (true, false, maxint).
void parseVarSection()
Validate a var section.
void pushScope()
Push a new scope onto the scope stack.
int withDepth
nesting depth of with statements (suppress undeclared-id errors)
std::string currentRecordTypeName
name of record type currently being parsed
void require(const std::string &s)
Require the current token to match a string, or fail.
bool inRecordFieldScope() const
Check if currently inside a record field scope.
void declareType(const std::string &name, const scan::TToken *at)
Declare a user-defined type in the current scope.
bool isAddOp() const
Check if current token is an additive operator.
void parseExprStop(const std::unordered_set< std::string > &stops)
Validate an expression up to a set of stop tokens.
std::vector< Scope > scopeStack
stack of lexical scopes
bool isVarDeclaredHere(const std::string &name) const
Check if a variable is declared in the innermost scope.
void parseIf()
Validate an if statement.
void failHere(const std::string &msg)
Report a validation error at the current token.
void parseActualParams()
Validate actual parameter list.
bool isParamDeclaredHere(const std::string &name) const
Check if a parameter is declared in the innermost scope.
void parseDesignator()
Validate a designator (variable with selectors).
scan::Scanner scanner
underlying scanner
void parseType(const std::string &)
Validate a type specification.
void requireKW(const std::string &k)
Require the current token to be a keyword, or fail.
std::unordered_set< std::string > declaredVars
globally declared variables
bool isBuiltinTypeName(const std::string &key) const
Check if a lowercase key is a built-in type name.
void parseTypeName()
Validate a type name reference.
static std::string toLower(const std::string &s)
Convert a string to lowercase (static helper).
void parseConstSection()
Validate a const section.
void parseLabelSection()
Validate a label section.
const scan::TToken * token
current token pointer
void declareRecordField(const std::string &name, const scan::TToken *at)
Declare a record field in the current record scope.
void declareFunc(const std::string &name, const scan::TToken *at)
Declare a function in the current scope.
std::unordered_set< std::string > declaredProcs
globally declared procedures
void declareParam(const std::string &name, const scan::TToken *at)
Declare a parameter in the current scope.
static std::string lower(std::string s)
Convert a string to lowercase.
bool isProcDeclaredHere(const std::string &name) const
Check if a procedure is declared in the innermost scope.
std::unordered_set< std::string > declaredFuncs
globally declared functions
void parseCaseLabel()
Validate a single case label.
void checkConstOnly(const std::string &name, const scan::TToken *at)
Assert a name is a constant (used in const expressions).
void parseCase()
Validate a case statement.
std::unordered_set< std::string > declaredConsts
globally declared constants
std::unordered_map< std::string, std::unordered_set< std::string > > recordFieldScopesByType
field names per record type
void parseInterfaceSection()
Validate interface section declarations.
void declareConst(const std::string &name, const scan::TToken *at)
Declare a constant in the current scope.
bool match(const std::string &s) const
Check if the current token value matches a string.
void parseTypeSection()
Validate a type section.
void declareVar(const std::string &name, const scan::TToken *at)
Declare a variable in the current scope.
std::string filename
source filename for error messages
void parseSubprogram()
Validate a procedure or function declaration.
bool isSetOp() const
Check if current token is a set operator.
void parseGoto()
Validate a goto statement.
void parseUses()
Validate a uses clause.
void parseConstExpr(const std::unordered_set< std::string > &stops, bool constOnly)
Parse and validate a constant expression.
void parseConstSimple()
Validate a simple constant (number, string, or name).
void checkVarOrConst(const std::string &name, const scan::TToken *at)
Assert a name is a variable or constant in scope.
void parseRepeat()
Validate a repeat..until statement.
void parseCaseLabelList()
Validate a case label list.
void parseFor()
Validate a for statement.
std::unordered_set< std::string > importedUnits
unit names from uses clause
void popRecordFieldScope()
Pop the current record field scope.
static std::string tokenTypeToString(types::TokenType t)
Convert a TokenType to a display string.
void parseCompoundStatement()
Validate a compound statement (begin..end).
std::unordered_set< std::string > declaredTypes
globally declared type names
void parseIdentList()
Validate a comma-separated identifier list.
void popScope()
Pop the current scope from the stack.
bool peekIs(const std::string &s)
Check if the current token value matches (and optionally advance).
bool isRelOp() const
Check if current token is a relational operator.
void parseWhile()
Validate a while statement.
void checkType(const std::string &name, const scan::TToken *at)
Assert a type name is declared and accessible.
size_t index
current token index
bool isPascalKeyword(const std::string &s) const
Check if a string is a Pascal reserved keyword.
Scope * currentScope()
Get the current (top) scope.
void checkVar(const std::string &name, const scan::TToken *at)
Assert a variable is declared and accessible in scope.
std::string source
original source text
bool next()
Advance to the next token.
void parseFieldIdentList()
Parse a field identifier list within a record definition.
bool isMulOp() const
Check if current token is a multiplicative operator.
std::string currentScopeName() const
Return a display name for the current scope.
void parseFormalParams()
Validate formal parameter declarations.
void fail(const std::string &msg)
Report a validation error with a generic message.
TPValidator(const std::string &source_)
Construct a validator for the given source.
std::string found() const
Return a display string for the current token value.
bool isBuiltinType() const
Check if the current token is a built-in type name.
void parseSimpleOrCallOrAssign()
Validate a simple statement, procedure call, or assignment.
bool validate(const std::string &name)
Run semantic validation.
void parseStatement()
Validate a single statement.
void parseBlock()
Validate a block (declarations + compound statement).
void parseSubrange()
Validate a subrange type.
bool isTypeDeclaredHere(const std::string &name) const
Check if a type is declared in the innermost scope.
void predeclareType(const std::string &name)
Pre-declare a built-in or forward-declared type.
bool isKW(const std::string &k) const
Check if current token value (lowercased) matches a keyword.
void failAt(const scan::TToken *at, const std::string &msg)
Report a validation error at a specific token.
bool isFuncDeclaredHere(const std::string &name) const
Check if a function is declared in the innermost scope.
void parseConstant()
Validate a constant value.
void parseProgram()
Validate a complete Pascal program.
Lexical scanner that tokenizes source text.
token::Token< char > TToken
Default token type.
TokenType
Classification of scanned tokens.
Token, character, and operator type enumerations for the scanner.
Lexical scanner that tokenizes source text into typed tokens.
A single scope level, tracking declared identifiers by category.
std::unordered_set< std::string > consts
constant names
std::unordered_set< std::string > procs
procedure names
std::unordered_set< std::string > funcs
function names
std::unordered_set< std::string > params
parameter names
std::unordered_set< std::string > vars
variable names