Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
Parser.h
Go to the documentation of this file.
1
6#pragma once
7
8#include <ext/algorithm>
9
10#include <ast/Statement.h>
11#include <ast/Option.h>
12#include <ast/Arg.h>
13#include <ast/Expression.h>
14
17
19
21
22#include <lexer/Lexer.h>
23
25
26namespace cli {
27
28class Parser {
29 cli::Lexer m_lexer;
30 cli::Lexer::Token m_current;
31 unsigned m_forceLexerReadNext = { 0 };
32
33 ext::set < cli::Lexer::Token > m_checkedOptions;
34
35 void clearCheckOptions ( ) {
36 m_checkedOptions.clear ( );
37 }
38
39 const ext::set < cli::Lexer::Token > & getCheckOptions ( ) {
40 return m_checkedOptions;
41 }
42
43 void restoreCheckOptions ( ext::set < cli::Lexer::Token > checkOptions ) {
44 m_checkedOptions = std::move ( checkOptions );
45 }
46
47 std::string lineInfo ( ) const {
48 return std::string ( "Line " ) + ext::to_string ( m_current.m_line ) + " at position " + ext::to_string ( m_current.m_position ) + ": ";
49 }
50
51public:
52 Parser ( cli::Lexer lexer ) : m_lexer ( std::move ( lexer ) ), m_current ( m_lexer.nextToken ( true ) ) {
53 }
54
56 m_lexer.setHint ( hint );
57 m_lexer.putback ( std::move ( m_current ) );
58 m_current = m_lexer.nextToken ( m_forceLexerReadNext > 0 );
59 m_lexer.setHint ( Lexer::Hint::NONE );
60 }
61
62 template < class ... TokenTypes >
63 bool check ( TokenTypes ... tokens ) {
64 m_checkedOptions.merge ( std::set < cli::Lexer::Token > { cli::Lexer::Token { "", "", tokens } ... } );
65 return ( ... || ( m_current.m_type == tokens ) );
66 }
67
68 template < class ... NonreservedTokens >
69 bool check_nonreserved_kw ( const NonreservedTokens & ... kw ) {
70 m_checkedOptions.merge ( std::set < cli::Lexer::Token > { cli::Lexer::Token { kw, "", cli::Lexer::TokenType::IDENTIFIER } ... } );
71 return m_current.m_type == Lexer::TokenType::IDENTIFIER && ( ... || ( m_current.m_value == kw ) );
72 }
73
74 template < class ... TokenTypes >
75 bool match ( cli::Lexer::TokenType token, TokenTypes ... tokens ) {
76 if ( ! check ( token, tokens ... ) )
77 throw exception::CommonException ( lineInfo ( ) + "Mismatched token while matching a token " + ( Lexer::tokenTypeToString ( token ) + ... + ( ", " + Lexer::tokenTypeToString ( tokens ) ) ) + ". Actual was " + Lexer::tokenTypeToString ( m_current.m_type ) + ". Tokens in active set " + ext::to_string ( getCheckOptions ( ) ) + "." );
78 m_current = m_lexer.nextToken ( m_forceLexerReadNext > 0 );
79 return true;
80 }
81
82 template < class ... NonreservedTokens >
83 bool match_nonreserved_kw ( const std::string & kw, const NonreservedTokens & ... kws ) {
84 if ( ! check_nonreserved_kw ( kw, kws ... ) )
85 throw exception::CommonException ( lineInfo ( ) + "Mismatched token while matching a non reseved keyword: " + ( kw + ... + ( ", " + kws ) ) + ". Tokens in active set " + ext::to_string ( getCheckOptions ( ) ) + "." );
86 m_current = m_lexer.nextToken ( m_forceLexerReadNext > 0 );
87 return true;
88 }
89
90 template < class ... TokenTypes >
91 bool check_then_match ( cli::Lexer::TokenType token, TokenTypes ... tokens ) {
92 if ( ! check ( token, tokens ... ) )
93 return false;
94
95 match ( token, tokens ... );
96 return true;
97 }
98
99 template < class ... NonreservedTokens >
100 bool check_then_match_nonreserved_kw ( const std::string & kw, const NonreservedTokens & ... kws ) {
101 if ( ! check_nonreserved_kw ( kw, kws ... ) )
102 return false;
103
104 match_nonreserved_kw ( kw, kws ... );
105 return true;
106 }
107
108 std::string matchIdentifier ( ) {
110 throw exception::CommonException ( lineInfo ( ) + "Mismatched token while matching an identifier. Tokens in active set " + ext::to_string ( getCheckOptions ( ) ) + "." );
111 std::string res = m_current.m_value;
112 m_current = m_lexer.nextToken ( m_forceLexerReadNext > 0 );
113 return res;
114 }
115
116 std::string matchString ( ) {
118 throw exception::CommonException ( lineInfo ( ) + "Mismatched token while matching a string. Tokens in active set " + ext::to_string ( getCheckOptions ( ) ) + "." );
119 std::string res = m_current.m_value;
120 m_current = m_lexer.nextToken ( m_forceLexerReadNext > 0 );
121 return res;
122 }
123
124 std::string matchType ( ) {
125 if ( ! check ( Lexer::TokenType::TYPE ) )
126 throw exception::CommonException ( lineInfo ( ) + "Mismatched token while matching a type. Tokens in active set " + ext::to_string ( getCheckOptions ( ) ) + "." );
127 std::string res = m_current.m_value;
128 m_current = m_lexer.nextToken ( m_forceLexerReadNext > 0 );
129 return res;
130 }
131
132 std::string matchFile ( ) {
133 if ( ! check ( Lexer::TokenType::FILE ) )
134 throw exception::CommonException ( lineInfo ( ) + "Mismatched token while matching a file. Tokens in active set " + ext::to_string ( getCheckOptions ( ) ) + "." );
135 std::string res = m_current.m_value;
136 m_current = m_lexer.nextToken ( m_forceLexerReadNext > 0 );
137 return res;
138 }
139
140 int matchInteger ( ) {
142 throw exception::CommonException ( lineInfo ( ) + "Mismatched token while matching an integer. Tokens in active set " + ext::to_string ( getCheckOptions ( ) ) + "." );
143 int res = ext::from_string < int > ( m_current.m_value );
144 m_current = m_lexer.nextToken ( m_forceLexerReadNext > 0 );
145 return res;
146 }
147
148 double matchDouble ( ) {
150 throw exception::CommonException ( lineInfo ( ) + "Mismatched token while matching a double. Tokens in active set " + ext::to_string ( getCheckOptions ( ) ) + "." );
151 double res = ext::from_string < double > ( m_current.m_value );
152 m_current = m_lexer.nextToken ( m_forceLexerReadNext > 0 );
153 return res;
154 }
155
156 const std::string & getTokenValue ( ) const {
157 return m_current.m_value;
158 }
159
160 void incNestedLevel ( ) {
161 ++ m_forceLexerReadNext;
162 }
163
164 void decNestedLevel ( ) {
165 -- m_forceLexerReadNext;
166 }
167
168 bool globalScope ( ) const {
169 return m_forceLexerReadNext == 0;
170 }
171
172 std::unique_ptr < Expression > assign_expression ( );
173
174 std::unique_ptr < Expression > or_expression ( );
175
176 std::unique_ptr < Expression > and_expression ( );
177
178 std::unique_ptr < Expression > bitwise_or_expression ( );
179
180 std::unique_ptr < Expression > bitwise_and_expression ( );
181
182 std::unique_ptr < Expression > bitwise_xor_expression ( );
183
184 std::unique_ptr < Expression > equality_expression ( );
185
186 std::unique_ptr < Expression > relational_expression ( );
187
188 std::unique_ptr < Expression > add_expression ( );
189
190 std::unique_ptr < Expression > mul_expression ( );
191
192 std::unique_ptr < Expression > cast_expression ( );
193
194 std::unique_ptr < Expression > prefix_expression ( );
195
196 std::unique_ptr < Expression > suffix_expression ( );
197
198 std::unique_ptr < Expression > atom ( );
199
200 std::vector < std::unique_ptr < Expression > > bracketed_expression_list ( );
201
202 std::unique_ptr < CategoryOption > category_option ( );
203
204 std::unique_ptr < TypeOption > type_option ( );
205
206 std::unique_ptr < TypeOption > optional_type_option ( );
207
208 std::unique_ptr < Arg > file ( );
209
210 std::unique_ptr < Arg > type ( );
211
212 std::unique_ptr < Arg > arg ( );
213
214 std::unique_ptr < Arg > optional_arg ( );
215
216 std::unique_ptr < Arg > template_arg ( );
217
218 std::unique_ptr < Arg > optional_variable ( );
219
220 std::unique_ptr < Arg > optional_binding ( );
221
222 std::shared_ptr < Statement > in_redirect ( );
223
224 std::shared_ptr < Statement > common ( );
225
226 std::shared_ptr < Statement > param ( );
227
228 std::shared_ptr < Statement > statement ( );
229
230 std::shared_ptr < StatementList > statement_list ( );
231
232 std::unique_ptr < Statement > out_redirect ( );
233
234 std::pair < bool, bool > introspect_cast_from_to ( );
235
236 std::unique_ptr < Command > introspect_command ( );
237
238 std::unique_ptr < CommandList > block ( );
239
240 std::unique_ptr < Expression > expression ( );
241
242 std::unique_ptr < Expression > batch ( );
243
244 std::unique_ptr < Expression > expression_or_batch ( );
245
246 std::unique_ptr < Expression > batch_or_expression ( );
247
248 std::pair < abstraction::TypeQualifiers::TypeQualifierSet, std::unique_ptr < Arg > > qualifiedType ( );
249
250 std::pair < abstraction::TypeQualifiers::TypeQualifierSet, std::unique_ptr < Arg > > runnableParam ( );
251
252 std::unique_ptr < Command > command ( );
253
254 std::unique_ptr < Command > semicolon_command ( );
255
256 std::unique_ptr < CommandList > parse ( );
257
258};
259
260} /* namespace cli */
261
Definition: Lexer.h:21
Hint
Definition: Lexer.h:23
void setHint(Hint hint)
Definition: Lexer.h:238
static std::string tokenTypeToString(TokenType type)
Definition: Lexer.h:90
void putback(Token &&token)
Definition: Lexer.h:234
Token nextToken(bool readNextLine=false)
Definition: Lexer.cpp:12
TokenType
Definition: Lexer.h:34
Definition: Parser.h:28
std::pair< abstraction::TypeQualifiers::TypeQualifierSet, std::unique_ptr< Arg > > runnableParam()
Definition: Parser.cpp:405
std::string matchString()
Definition: Parser.h:116
bool match_nonreserved_kw(const std::string &kw, const NonreservedTokens &... kws)
Definition: Parser.h:83
std::unique_ptr< Expression > assign_expression()
Definition: Parser.cpp:640
std::string matchFile()
Definition: Parser.h:132
std::string matchIdentifier()
Definition: Parser.h:108
std::unique_ptr< Arg > optional_variable()
Definition: Parser.cpp:137
bool check(TokenTypes ... tokens)
Definition: Parser.h:63
std::unique_ptr< Expression > equality_expression()
Definition: Parser.cpp:718
std::unique_ptr< Expression > atom()
Definition: Parser.cpp:836
std::unique_ptr< Expression > prefix_expression()
Definition: Parser.cpp:782
std::unique_ptr< Arg > template_arg()
Definition: Parser.cpp:132
std::unique_ptr< Expression > cast_expression()
std::unique_ptr< CommandList > parse()
Definition: Parser.cpp:624
std::pair< abstraction::TypeQualifiers::TypeQualifierSet, std::unique_ptr< Arg > > qualifiedType()
Definition: Parser.cpp:389
std::string matchType()
Definition: Parser.h:124
std::unique_ptr< Expression > expression()
Definition: Parser.cpp:362
void setHint(Lexer::Hint hint)
Definition: Parser.h:55
const std::string & getTokenValue() const
Definition: Parser.h:156
std::unique_ptr< Arg > arg()
Definition: Parser.cpp:114
std::pair< bool, bool > introspect_cast_from_to()
Definition: Parser.cpp:290
std::unique_ptr< Arg > type()
Definition: Parser.cpp:106
std::unique_ptr< Expression > bitwise_or_expression()
Definition: Parser.cpp:679
std::unique_ptr< Expression > relational_expression()
Definition: Parser.cpp:731
std::unique_ptr< Command > command()
Definition: Parser.cpp:414
std::vector< std::unique_ptr< Expression > > bracketed_expression_list()
Definition: Parser.cpp:868
bool check_then_match_nonreserved_kw(const std::string &kw, const NonreservedTokens &... kws)
Definition: Parser.h:100
int matchInteger()
Definition: Parser.h:140
std::unique_ptr< Command > introspect_command()
Definition: Parser.cpp:308
std::unique_ptr< Arg > optional_binding()
Definition: Parser.cpp:147
std::shared_ptr< StatementList > statement_list()
Definition: Parser.cpp:276
bool globalScope() const
Definition: Parser.h:168
std::unique_ptr< Expression > batch()
Definition: Parser.cpp:366
std::shared_ptr< Statement > param()
Definition: Parser.cpp:230
bool match(cli::Lexer::TokenType token, TokenTypes ... tokens)
Definition: Parser.h:75
std::unique_ptr< Statement > out_redirect()
Definition: Parser.cpp:179
std::unique_ptr< TypeOption > type_option()
Definition: Parser.cpp:76
bool check_then_match(cli::Lexer::TokenType token, TokenTypes ... tokens)
Definition: Parser.h:91
std::unique_ptr< Expression > and_expression()
Definition: Parser.cpp:666
Parser(cli::Lexer lexer)
Definition: Parser.h:52
bool check_nonreserved_kw(const NonreservedTokens &... kw)
Definition: Parser.h:69
std::unique_ptr< Expression > or_expression()
Definition: Parser.cpp:653
std::shared_ptr< Statement > statement()
Definition: Parser.cpp:249
double matchDouble()
Definition: Parser.h:148
void incNestedLevel()
Definition: Parser.h:160
std::unique_ptr< Expression > mul_expression()
Definition: Parser.cpp:764
std::unique_ptr< Arg > optional_arg()
Definition: Parser.cpp:125
std::unique_ptr< Expression > bitwise_and_expression()
Definition: Parser.cpp:692
std::unique_ptr< Expression > suffix_expression()
Definition: Parser.cpp:813
std::unique_ptr< CommandList > block()
Definition: Parser.cpp:341
std::unique_ptr< Arg > file()
Definition: Parser.cpp:93
std::shared_ptr< Statement > common()
Definition: Parser.cpp:195
std::unique_ptr< Expression > expression_or_batch()
Definition: Parser.cpp:371
std::shared_ptr< Statement > in_redirect()
Definition: Parser.cpp:157
std::unique_ptr< Expression > bitwise_xor_expression()
Definition: Parser.cpp:705
std::unique_ptr< Command > semicolon_command()
Definition: Parser.cpp:354
void decNestedLevel()
Definition: Parser.h:164
std::unique_ptr< TypeOption > optional_type_option()
Definition: Parser.cpp:86
std::unique_ptr< Expression > batch_or_expression()
Definition: Parser.cpp:380
std::unique_ptr< CategoryOption > category_option()
Definition: Parser.cpp:66
std::unique_ptr< Expression > add_expression()
Definition: Parser.cpp:748
Basic exception from which all other exceptions are derived.
Definition: CommonException.h:21
Definition: set.hpp:44
return res
Definition: MinimizeByPartitioning.h:145
Definition: Arg.h:11
std::string to_string(const T &value)
To string method designated for objects that can be casted to string.
Definition: string.hpp:131
void hint(Hint hint)
Definition: measurements.cpp:37
Definition: FordFulkerson.hpp:16
Definition: Lexer.h:193
TokenType m_type
Definition: Lexer.h:196
size_t m_line
Definition: Lexer.h:198
size_t m_position
Definition: Lexer.h:199
std::string m_value
Definition: Lexer.h:194