1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
#pragma once
#include "cps/Identifier.h"
#include "cps/Lexer.h"
#include "cps/AST.h"
#include "cps/TypeAST.h"
#include "cps/FunctionAST.h"
#include <cstdint>
#include <vector>
#include <initializer_list>
#include <memory>
#include <map>
#include <optional>
#include <string>
#include <set>
namespace cps {
class Parser {
Lexer &Lex;
int CurTok = tok_eof;
bool StartsLine = true;
bool ParsingCaseLabel = false;
std::vector<unsigned> CaseBodyDepths;
unsigned NumErrors = 0;
unsigned BlockDepth = 0; // ParseBlock nesting; 1 = top level
std::map<int, int> BinopPrecedence;
IdentifierSet TypeNames = {
"INTEGER", "REAL", "BOOLEAN", "STRING", "CHAR", "VOID",
"C_INT", "C_CHAR", "C_STRING", "C_DOUBLE"
};
int getNextToken();
int GetTokPrecedence();
std::unique_ptr<ExprAST> ParseExpression();
std::unique_ptr<ExprAST> ParseAssignmentRHS();
std::unique_ptr<ExprAST> ParseUnaryImpl();
std::unique_ptr<ExprAST> ParseUnary();
std::unique_ptr<ExprAST> ParsePrimaryImpl();
std::unique_ptr<ExprAST> ParseResultMethods(std::unique_ptr<ExprAST> Expr, int Line);
std::unique_ptr<ExprAST> ParsePrimary();
std::unique_ptr<ExprAST> ParseBinOpRHS(int ExprPrec, std::unique_ptr<ExprAST> LHS);
std::unique_ptr<ExprAST> ParseNumberExpr();
std::unique_ptr<ExprAST> ParseIdentifierExpr();
std::string ParseOperatorName(int &Op);
std::unique_ptr<ExprAST> ParseParenExpr();
std::unique_ptr<MemberCallExprAST> ParseMemberCall(
const std::string &Name, std::vector<DesignatorAccess> Accesses, int Line);
std::string ParseTypeName(bool AllowVoid = false);
std::unique_ptr<StmtAST> ParseStatement();
std::unique_ptr<StmtAST> ParseStatementImpl();
std::vector<std::unique_ptr<StmtAST>> ParseBlock(std::initializer_list<int> Terminators, bool CaseBody = false);
bool isCaseLabel();
bool atCaseBoundary();
std::unique_ptr<StmtAST> ParseCaseStmt();
std::optional<std::vector<std::unique_ptr<ExprAST>>> ParseExprList(char CloseDelim, bool AllowEmpty);
void syncToStatementStart();
std::unique_ptr<StmtAST> ParseIfStmt();
std::unique_ptr<StmtAST> ParseTryStmt();
std::unique_ptr<StmtAST> ParseWhileStmt();
std::unique_ptr<StmtAST> ParseRepeatStmt();
std::unique_ptr<StmtAST> ParseForStmt();
std::unique_ptr<StmtAST> ParseDeclare();
std::unique_ptr<StmtAST> ParseConstant();
std::unique_ptr<StmtAST> ParseAssignStmt();
bool ParseDesignatorSuffix(std::vector<DesignatorAccess> &Out);
std::unique_ptr<StmtAST> ParseTypeDecl();
std::optional<std::vector<RecordFieldDecl>> ParseRecordBody(
const std::string &Name, int Line, int EndToken,
std::vector<std::unique_ptr<FunctionDefAST>> *Methods = nullptr);
bool ParseRecordField(std::vector<RecordFieldDecl> &Out, bool IsPublic);
std::unique_ptr<FunctionDefAST> ParseFunction(const std::string &OwnerClass = {}, bool IsPublic = true, bool IsOverload = false);
std::unique_ptr<FunctionDefAST> ParseProcedure(const std::string &OwnerClass = {}, bool IsPublic = true, bool IsOverload = false);
std::unique_ptr<StmtAST> ParseExternDecl();
std::unique_ptr<StmtAST> ParseCallStmt();
std::unique_ptr<StmtAST> ParseReturnStmt();
std::optional<std::vector<ParamDecl>> ParsePrototypeArgs(bool AllowByRef);
bool ParseIntegerValue(bool Negative, int64_t &Out);
bool ParseSignedIntBound(int64_t &Out);
std::unique_ptr<StmtAST> ParseOpenFile();
std::unique_ptr<StmtAST> ParseReadFile();
std::unique_ptr<StmtAST> ParseWriteFile();
std::unique_ptr<StmtAST> ParseCloseFile();
public:
Parser(Lexer &L);
std::vector<std::unique_ptr<StmtAST>> Parse();
bool hadError() const { return NumErrors != 0; }
};
} // namespace cps
|