-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathparser.h
148 lines (120 loc) · 3.42 KB
/
parser.h
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#ifndef JUCC_PARSER_PARSER_H
#define JUCC_PARSER_PARSER_H
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include "nlohmann/json.hpp"
#include "parser/parsing_table.h"
#include "utils/first_follow.h"
using json = nlohmann::ordered_json;
namespace jucc {
namespace parser {
class Parser {
/**
* json pretty print indentation for generated parse tree
*/
static const int INDENTATION = 4;
/**
* parse tree for Treant.js integration
*/
json parse_tree_;
/**
* A stack to put the symbols and perform the actual parsing
*/
std::stack<std::string> stack_;
/**
* The given input string to parse.
*/
std::vector<std::string> input_string_;
/**
* The start symbol for the grammar
*/
std::string start_symbol_;
/**
* Holds the current step of parsing.
*/
int current_step_{0};
/**
* Holds the build up parsing table object
*/
ParsingTable table_;
/**
* Holds the history of the productions parsed during parsing
*/
std::vector<int> production_history_;
/**
* Holds a copy of the input string initially
* and changes with each step of parsing.
*/
std::vector<std::string> current_string_;
/**
* Errors incurred during the parsing of the given input file.
*/
std::vector<std::string> parser_errors_;
/**
* Helper function to generate error messages for parsing.
*/
static std::string GenerateErrorMessage(const std::string ¤t_token);
/**
* Supportive function for Parser::FormattedJSON
* @param value
* @return { text: { name: "value" } }
*/
static json GetTextNode(const std::string & /* value */);
/**
* Utility recursive function for Parser::FormattedJSON
* @param body, a json
* @returns Treant.js formatted JSON
*/
static json RecRunner(const json & /*main*/, std::string /* key */);
public:
/**
* Constructor for initializing stack and other members.
*/
Parser();
/**
* Used for parsing the next token of the input string
*/
void ParseNextStep();
/**
* Resets the entire parsing process
*/
void ResetParsing();
/**
* Function that returns true when the parsing is completed
*/
bool IsComplete();
/**
* Completes a step of parsing
*/
void DoNextStep();
/**
* Build the parse tree from production history
* Parse tree not built if parser in error state
*/
void BuildParseTree();
/**
* Dumps the parse tree in given path in json format
* @param filepath
* @param formatted (Default is in Treant.js format else raw if value is set "false")
* @returns true on success
*/
bool WriteParseTree(const std::string &filepath, bool formatted = true);
/**
* Takes a json with no array, ideally received from parser::GetParseTree()
* Format is given here https://fperucic.github.io/treant-js/
* @returns a formatted JSON which acts as a input for Treant.js
*/
[[maybe_unused]] [[nodiscard]] static json FormattedJSON(const json & /* body */);
/* getters and setters*/
void SetInputString(std::vector<std::string> inps);
void SetParsingTable(ParsingTable table);
void SetStartSymbol(std::string start);
[[nodiscard]] const std::vector<int> &GetProductionHistory() { return production_history_; }
[[nodiscard]] const std::vector<std::string> &GetParserErrors() { return parser_errors_; }
[[nodiscard]] const json &GetParseTree() { return parse_tree_; }
};
} // namespace parser
} // namespace jucc
#endif