unlogic
Loading...
Searching...
No Matches
Parser.cpp
Go to the documentation of this file.
1#include "Parser.h"
2
3using namespace unlogic;
4
7
8bf::DefineTerminal<G, R"(given)"> KW_GIVEN(SyntaxKeyword);
9bf::DefineTerminal<G, R"(calc)"> KW_CALC(SyntaxKeyword);
10bf::DefineTerminal<G, R"(plot)"> KW_PLOT(SyntaxKeyword);
11
12bf::DefineTerminal<G, R"(on)"> KW_ON(SyntaxKeyword);
13bf::DefineTerminal<G, R"(as)"> KW_AS(SyntaxKeyword);
14
15bf::DefineTerminal<G, R"(\d+(\.\d+)?)", double> NUMBER(SyntaxLiteral, [](auto const &tok) -> ValueType {
16 return std::stod(std::string(tok.raw));
17});
18
19bf::DefineTerminal<G, R"([a-zA-Z]+)", std::string> IDENTIFIER(SyntaxIdentifier, [](auto const &tok) -> ValueType {
20 return std::string(tok.raw);
21});
22
23bf::DefineTerminal<G, R"(\^)"> OP_EXP(bf::Right, SyntaxOperator);
24
25bf::DefineTerminal<G, R"(\*)"> OP_MUL(bf::Left, SyntaxOperator);
26bf::DefineTerminal<G, R"(\/)"> OP_DIV(bf::Left, SyntaxOperator);
27bf::DefineTerminal<G, R"(\+)"> OP_ADD(bf::Left, SyntaxOperator);
28bf::DefineTerminal<G, R"(\-)"> OP_SUB(bf::Left, SyntaxOperator);
29
30bf::DefineTerminal<G, R"(=)"> OP_ASN(bf::Left, SyntaxOperator);
31
32bf::DefineTerminal<G, R"(\‍()"> PAR_OPEN(SyntaxGrouper);
33bf::DefineTerminal<G, R"(\))"> PAR_CLOSE(SyntaxGrouper);
34
35bf::DefineTerminal<G, R"(\[)"> BRK_OPEN(SyntaxGrouper);
36bf::DefineTerminal<G, R"(\])"> BRK_CLOSE(SyntaxGrouper);
37
38bf::DefineTerminal<G, R"(;)"> STMT_DELIMITER(SyntaxOperator);
39
40bf::DefineTerminal<G, R"(,)"> SEPARATOR(SyntaxOperator);
41
42bf::DefineNonTerminal<G, std::vector<std::string>> identifier_list
43 = bf::PR<G>(IDENTIFIER)<=>[](auto &$) -> ValueType
44 {
45 return std::vector<std::string>{ IDENTIFIER($[0]) };
46 }
47 | (identifier_list + SEPARATOR + IDENTIFIER)<=>[](auto &$) -> ValueType
48 {
49 auto list = identifier_list($[0]);
50 list.push_back(IDENTIFIER($[2]));
51
52 return std::move(list);
53 }
54 ;
55
56extern bf::DefineNonTerminal<G, std::unique_ptr<Node>> expression;
57
58bf::DefineNonTerminal<G, std::vector<std::unique_ptr<Node>>> expression_list
59 = bf::PR<G>(expression)<=>[](auto &$) -> ValueType
60 {
61 std::vector<UniqueNode> list;
62 list.push_back(std::move(expression($[0])));
63
64 return std::move(list);
65 }
66 | (expression_list + SEPARATOR + expression)<=>[](auto &$) -> ValueType
67 {
68 auto list = expression_list($[0]);
69 list.push_back(std::move(expression($[2])));
70
71 return std::move(list);
72 }
73 ;
74
75bf::DefineNonTerminal<G, std::unique_ptr<Node>> function_call
76 = (IDENTIFIER + PAR_OPEN + expression_list + PAR_CLOSE)<=>[](auto &$) -> ValueType
77 {
79 }
80 ;
81
82bf::DefineNonTerminal<G, std::unique_ptr<Node>> expression
83 = bf::PR<G>(NUMBER)<=>[](auto &$) -> ValueType
84 {
86 }
87 | bf::PR<G>(IDENTIFIER)<=>[](auto &$) -> ValueType
88 {
90 }
91 | bf::PR<G>(function_call)<=>[](auto &$) -> ValueType
92 {
93 return std::move($[0]);
94 }
95 | (PAR_OPEN + expression + PAR_CLOSE)<=>[](auto &$) -> ValueType
96 {
97 return std::move($[1]);
98 }
99 | (BRK_OPEN + expression + BRK_CLOSE)<=>[](auto &$) -> ValueType
100 {
101 return std::move($[1]);
102 }
103 | (expression + OP_EXP + expression)<=>[](auto &$) -> ValueType
104 {
106 }
107 | (expression + OP_MUL + expression)<=>[](auto &$) -> ValueType
108 {
110 }
111 | (expression + OP_DIV + expression)<=>[](auto &$) -> ValueType
112 {
114 }
115 | (expression + OP_ADD + expression)<=>[](auto &$) -> ValueType
116 {
118 }
119 | (expression + OP_SUB + expression)<=>[](auto &$) -> ValueType
120 {
122 }
123 ;
124
125bf::DefineNonTerminal<G, std::unique_ptr<Node>> function_definition
127 {
129 IDENTIFIER($[1]),
130 identifier_list($[3]),
131 expression($[6])
132 );
133 }
134 | (KW_GIVEN + IDENTIFIER + PAR_OPEN + PAR_CLOSE + OP_ASN + expression)<=>[](auto &$) -> ValueType
135 {
137 IDENTIFIER($[1]),
138 std::vector<std::string>{},
139 expression($[5])
140 );
141 }
142 ;
143
144bf::DefineNonTerminal<G> variable_definition
146 ;
147
148bf::DefineNonTerminal<G, std::unique_ptr<Node>> plot_command
149 = (KW_PLOT + IDENTIFIER)<=>[](auto &$) -> ValueType
150 {
152 }
153 ;
154
155bf::DefineNonTerminal<G, std::unique_ptr<Node>> statement
156 = (function_definition + STMT_DELIMITER)<=>[](auto &$) -> ValueType
157 {
158 return std::move($[0]);
159 }
160 | (plot_command + STMT_DELIMITER)<=>[](auto &$) -> ValueType
161 {
162 return std::move($[0]);
163 }
164 ;
165
166bf::DefineNonTerminal<G, std::vector<std::unique_ptr<Node>>> statement_list
167 = bf::PR<G>(statement)<=>[](auto &$) -> ValueType
168 {
169 std::vector<std::unique_ptr<Node>> list;
170 list.push_back(std::move(statement($[0])));
171
172 return std::move(list);
173 }
174 | (statement_list + statement)<=>[](auto &$) -> ValueType
175 {
176 auto list = statement_list($[0]);
177 list.push_back(std::move(statement($[1])));
178
179 return std::move(list);
180 }
181 ;
182
183bf::DefineNonTerminal<G, std::unique_ptr<Node>> scoped_block
184 = bf::PR<G>(statement_list)<=>[](auto &$) -> ValueType
185 {
187 }
188 ;
189
190bf::DefineNonTerminal<G, std::unique_ptr<Node>> program
191 = bf::PR<G>(scoped_block)<=>[](auto &$) -> ValueType
192 {
194 }
195 ;
196
197/*
198 * Exports
199 */
200bf::NonTerminal<G> &unlogic::unlogic_program = program;
bf::DefineTerminal< G, R"(\*)"> OP_MUL(bf::Left, SyntaxOperator)
bf::DefineTerminal< G, R"(,)"> SEPARATOR(SyntaxOperator)
bf::DefineNonTerminal< G, std::unique_ptr< Node > > program
Definition Parser.cpp:191
bf::DefineNonTerminal< G, std::vector< std::string > > identifier_list
Definition Parser.cpp:43
bf::DefineTerminal< G, R"(plot)"> KW_PLOT(SyntaxKeyword)
bf::DefineNonTerminal< G, std::vector< std::unique_ptr< Node > > > statement_list
Definition Parser.cpp:167
bf::DefineTerminal< G, R"(\‍()"> PAR_OPEN(SyntaxGrouper);bf::DefineTerminal< G, R"(\‍))"> PAR_CLOSE(SyntaxGrouper)
bf::DefineTerminal< G, R"(as)"> KW_AS(SyntaxKeyword)
bf::DefineTerminal< G, R"(;)"> STMT_DELIMITER(SyntaxOperator)
bf::DefineNonTerminal< G > variable_definition
Definition Parser.cpp:145
bf::DefineTerminal< G, R"(\d+(\.\d+)?)", double > NUMBER(SyntaxLiteral, [](auto const &tok) -> ValueType { return std::stod(std::string(tok.raw));})
bf::DefineTerminal< G, R"(given)"> KW_GIVEN(SyntaxKeyword)
bf::DefineTerminal< G, R"(on)"> KW_ON(SyntaxKeyword)
bf::DefineNonTerminal< G, std::unique_ptr< Node > > plot_command
Definition Parser.cpp:149
bf::DefineNonTerminal< G, std::vector< std::unique_ptr< Node > > > expression_list
Definition Parser.cpp:59
bf::DefineTerminal< G, R"(\-)"> OP_SUB(bf::Left, SyntaxOperator)
bf::DefineTerminal< G, R"(calc)"> KW_CALC(SyntaxKeyword)
bf::DefineTerminal< G, R"(=)"> OP_ASN(bf::Left, SyntaxOperator)
bf::DefineTerminal< G, R"(\/)"> OP_DIV(bf::Left, SyntaxOperator)
bf::DefineTerminal< G, R"(\^)"> OP_EXP(bf::Right, SyntaxOperator)
ParserValueType ValueType
Definition Parser.cpp:6
bf::DefineNonTerminal< G, std::unique_ptr< Node > > statement
Definition Parser.cpp:156
bf::DefineTerminal< G, R"(\])"> BRK_CLOSE(SyntaxGrouper)
bf::DefineNonTerminal< G, std::unique_ptr< Node > > scoped_block
Definition Parser.cpp:184
bf::DefineNonTerminal< G, std::unique_ptr< Node > > function_call
Definition Parser.cpp:76
bf::DefineNonTerminal< G, std::unique_ptr< Node > > function_definition
Definition Parser.cpp:126
bf::DefineTerminal< G, R"([a-zA-Z]+)", std::string > IDENTIFIER(SyntaxIdentifier, [](auto const &tok) -> ValueType { return std::string(tok.raw);})
bf::DefineNonTerminal< G, std::unique_ptr< Node > > expression
Definition Parser.cpp:83
ParserGrammarType G
Definition Parser.cpp:5
bf::DefineTerminal< G, R"(\[)"> BRK_OPEN(SyntaxGrouper)
bf::DefineTerminal< G, R"(\+)"> OP_ADD(bf::Left, SyntaxOperator)
UniqueNode unique_node(Args &&... args)
Definition Node.h:144
std::variant< double, std::string, std::vector< std::string >, UniqueNode, std::vector< UniqueNode > > ParserValueType
Definition Parser.h:19
bf::GrammarDefinition< ParserValueType, SyntaxHighlightingGroup > ParserGrammarType
Definition Parser.h:20
@ SyntaxKeyword
Definition Parser.h:13
@ SyntaxOperator
Definition Parser.h:12
@ SyntaxIdentifier
Definition Parser.h:14
@ SyntaxLiteral
Definition Parser.h:15
@ SyntaxGrouper
Definition Parser.h:16
bf::NonTerminal< ParserGrammarType > & unlogic_program
Definition Parser.cpp:200