-
Notifications
You must be signed in to change notification settings - Fork 0
/
firework_grammar.cpp
143 lines (111 loc) · 4.42 KB
/
firework_grammar.cpp
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
#include "parser.h"
namespace fireworkLang {
parse_return* expr (vector<token> tokens, int pos);
parse_return* assign(vector<token> tokens, int pos);
parse_return* prog(vector<token> tokens, int pos);
//positive number
parse_return * pos_num (vector<token> tokens, int pos) {
string to_match = tokens[pos].str;
if(all_of(to_match.begin(), to_match.end(), [](char a){return isdigit(a);})){
if(debug || match_only) cerr << "Matched number '" + (to_match == "\n" ? "\\n" : to_match) + "'." << endl;
return new parse_return(tokens[pos].str, tokens[pos].line, tokens[pos].col, pos + 1, "number");
}
if(debug) cerr << "Failed to match number '" + (to_match == "\n" ? "\\n" : to_match) + "' at position " << pos << endl;
return new parse_return("Failed to match number.");
}
//identifier
parse_return * identifier(vector<token> tokens, int pos) {
string to_match = tokens[pos].str;
if(to_match=="not") goto failure;
if(isalpha(to_match[0]) || to_match[0] == '_') {
if(all_of(to_match.begin(), to_match.end(), [](char a){
return a == '_' || isalnum(a);
})) {
if(debug || match_only) cerr << "Matched identifier '" + (to_match == "\n" ? "\\n" : to_match) + "' at position " << pos << endl;
return new parse_return(tokens[pos].str, tokens[pos].line, tokens[pos].col, pos + 1, "identifier");
}
}
failure:
if(debug) cerr << "Failed to match identifier '" + (to_match == "\n" ? "\\n" : to_match) + "' at position " << pos << endl;
return new parse_return("Failed to match identifier.");
}
//identifier list
prep(list_of_identifiers, identifier);
//negative number
popt(neg_sign, "-");
pconsec(num, neg_sign, pos_num);
//string
pexc(fw_string_elem, ANY, "\"");
prep(fw_string_content, fw_string_elem);
pconsec(fw_string, "\"", fw_string_content, "\"");
//decimal
pconsec(fw_float, num, ".", pos_num);
//value
por(value, fw_float, fw_string, num, identifier, pos_num);
//index access
pconsec(indexAccessor, "[", expr, "]");
//tuple (list of comma sep expressions)
plist(tuple_content, expr, ",");
pconsec(fw_tuple, "(", tuple_content, ")");
//dot operator, and -> operator
por(classAccessor, ".", "->");
plist(accessedMember, value, classAccessor);
//preunary
por(preUnarySyms, "++", "--", "~", "-", "+", "!", "not", "&", "*");
prep(preUnarySymRep, preUnarySyms);
//postunary
plist(arguments, expr, ",");
pconsec(functionCall, "(", arguments, ")");
por(postUnarySyms, "++", "--", indexAccessor, functionCall);
prep(postUnarySymRep, postUnarySyms);
//unary
pconsec(unary, preUnarySymRep, accessedMember, postUnarySymRep);
//exponents!!!
plist(exponent, unary, "**");
//multiplication
por(mul_div_symbol, "*", "/", "//", "%");
plist(mul_div, exponent, mul_div_symbol);
//addition
por(add_sub_symbol, "+", "-");
plist(add_sub, mul_div, add_sub_symbol);
//binary operators
por(binary_operator, "&", "^", "|", ">>", "<<");
plist(binary, add_sub, binary_operator);
//in operators
por(in_operator, "in", "not in");
plist(in_expression, binary, in_operator);
//comparisons
por(comparison_operator, ">", ">=", "<", "<=", "==", "!=");
plist(comparison, in_expression, comparison_operator);
//comparison and/or
por(logical_and_or_operator, "and", "&&", "or", "||");
plist(logical_and_or_expression, comparison, logical_and_or_operator);
//ternary
pconsec(ternary, logical_and_or_expression, "?", logical_and_or_expression, ":", logical_and_or_expression);
//expression
por(expression, ternary, logical_and_or_expression);
//assignment
por(assignment_symbol, "=", "-=", "+=", "~=", "%=", "^=", "&=", "*=", "**=", "&&=", "|=", "||=", "/=", "//=");
pconsec(assignment, list_of_identifiers, assignment_symbol, expr);
//statement
por(statement, assignment, expr, "");
//end statement
por(end_statement, "\n", ";");
//program
plist(program, statement, end_statement);
auto main = program;
parse_return* expr (vector<token> tokens, int pos){
return expression(tokens, pos);
}
parse_return* assign(vector<token> tokens, int pos) {
return assignment(tokens, pos);
}
parse_return* prog(vector<token> tokens, int pos) {
return program(tokens, pos);
}
}
parse_return * parse(vector<token> tokens, bool dbg, bool mtch) {
debug = dbg;
match_only = mtch;
return compress(fireworkLang::main(tokens, 0));
}