39
94
Semantic &_semantic;
97
/** Token provider for getting the tokens to parse. */
42
98
TokenProvider *token_provider;
101
/** \class State Syntax.h Puma/Syntax.h
102
* Parser state, the current position in the token stream. */
45
103
struct State : public TokenProvider::State {
108
/** Copy constructor.
109
* \param s The parser state to copy. */
48
110
State (const TokenProvider::State &s) : TokenProvider::State (s) {}
52
Syntax (Builder &, Semantic &);
115
* \param b The syntax tree builder.
116
* \param s The semantic analysis object. */
117
Syntax (Builder &b, Semantic &s);
53
119
virtual ~Syntax () {}
56
CTree *run (TokenProvider &);
57
template <class T> CTree *run (TokenProvider &, CTree *(T::*)());
58
virtual void configure (Config &) {}
122
/** Start the parse process.
123
* \param tp The token provider from where to get the tokens to parse.
124
* \return The resulting syntax tree. */
125
CTree *run (TokenProvider &tp);
126
/** Start the parse process at a specific grammar rule.
127
* \param tp The token provider from where to get the tokens to parse.
128
* \param rule The grammar rule where to start.
129
* \return The resulting syntax tree. */
130
template <class T> CTree *run (TokenProvider &tp, CTree *(T::*rule)());
131
/** Configure the syntactic analysis object.
132
* \param c The configuration object. */
133
virtual void configure (Config &c) {}
134
/** Get the token provider from which the parsed tokens are read. */
59
135
TokenProvider *provider () const { return token_provider; }
136
/** Get the last token that could not be parsed. */
60
137
Token *problem () const;
138
/** Check if errors occured during the parse process. */
61
139
bool error () const;
62
bool look_ahead (int, unsigned = 1);
63
bool look_ahead (int*, unsigned = 1);
140
/** Look-ahead n core language tokens and check if
141
* the n-th token has the given type.
142
* \param token_type The type of the n-th token.
143
* \param n The number of tokens to look-ahead.
144
* \return True if the n-th token has the given type. */
145
bool look_ahead (int token_type, unsigned n = 1);
146
/** Look-ahead n core language tokens and check if
147
* the n-th token has one of the given types.
148
* \param token_types The possible types of the n-th token.
149
* \param n The number of tokens to look-ahead.
150
* \return True if the n-th token has one of the given types. */
151
bool look_ahead (int* token_types, unsigned n = 1);
152
/** Look-ahead one core language token.
153
* \return The type of the next core language token. */
64
154
inline int look_ahead () const;
155
/** Consume all tokens until the next core language token. */
65
156
inline bool consume ();
68
template <class T> bool parse (CTree *(T::*)());
69
template <class T> bool seq (CTree *(T::*)());
70
template <class T> bool seq (bool (T::*)());
71
template <class T> bool list (CTree *(T::*)(), int, bool = false);
72
template <class T> bool list (CTree *(T::*)(), int*, bool = false);
73
template <class T> bool list (bool (T::*)(), int, bool = false);
74
template <class T> bool list (bool (T::*)(), int*, bool = false);
75
template <class T> bool catch_error (CTree *(T::*)(), const char*, int*, int*);
78
bool parse_token (int);
79
bool opt (bool) const;
159
/** Parse the given grammar rule. Saves the current state of
160
* the builder, semantic, and token provider objects.
161
* \param rule The rule to parse.
162
* \return True if parsed successfully. */
163
template <class T> bool parse (CTree *(T::*rule)());
164
/** Parse a sequence of the given grammar rule.
165
* \param rule The rule to parse at least once.
166
* \return True if parsed successfully. */
167
template <class T> bool seq (CTree *(T::*rule)());
168
/** Parse a sequence of the given grammar rule.
169
* \param rule The rule to parse at least once.
170
* \return True if parsed successfully. */
171
template <class T> bool seq (bool (T::*rule)());
172
/** Parse a sequence of rule-separator pairs.
173
* \param rule The rule to parse at least once.
174
* \param separator The separator token.
175
* \param trailing_separator True if a trailing separator token is allowed.
176
* \return True if parsed successfully. */
177
template <class T> bool list (CTree *(T::*rule)(), int separator, bool trailing_separator = false);
178
/** Parse a sequence of rule-separator pairs.
179
* \param rule The rule to parse at least once.
180
* \param separators The separator tokens.
181
* \param trailing_separator True if a trailing separator token is allowed.
182
* \return True if parsed successfully. */
183
template <class T> bool list (CTree *(T::*rule)(), int* separators, bool trailing_separator = false);
184
/** Parse a sequence of rule-separator pairs.
185
* \param rule The rule to parse at least once.
186
* \param separator The separator token.
187
* \param trailing_separator True if a trailing separator token is allowed.
188
* \return True if parsed successfully. */
189
template <class T> bool list (bool (T::*rule)(), int separator, bool trailing_separator = false);
190
/** Parse a sequence of rule-separator pairs.
191
* \param rule The rule to parse at least once.
192
* \param separators The separator tokens.
193
* \param trailing_separator True if a trailing separator token is allowed.
194
* \return True if parsed successfully. */
195
template <class T> bool list (bool (T::*rule)(), int* separators, bool trailing_separator = false);
196
/** Parse a grammar rule automatically catching parse errors.
197
* \param rule The rule to parse.
198
* \param msg The error message to show if the rule fails.
199
* \param finish_tokens Set of token types that abort parsing the rule.
200
* \param skip_tokens If the rule fails skip all tokens until a token is read
201
* that has one of the types given here.
202
* \return False if at EOF or a finish_token is read, true otherwise. */
203
template <class T> bool catch_error (CTree *(T::*rule)(), const char* msg, int* finish_tokens, int* skip_tokens);
204
/** Parse a token with the given type.
205
* \param token_type The token type.
206
* \return True a corresponding token was parsed. */
207
bool parse (int token_type);
208
/** Parse a token with one of the given types.
209
* \param token_types The token types.
210
* \return True a corresponding token was parsed. */
211
bool parse (int *token_types);
212
/** Parse a token with the given type.
213
* \param token_type The token type.
214
* \return True a corresponding token was parsed. */
215
bool parse_token (int token_type);
216
/** Optional rule parsing. Always succeeds regardless
218
* \param dummy Dummy parameter, is not evaluated.
220
bool opt (bool dummy) const;
223
/** Get the syntax tree builder. */
82
224
Builder &builder () const;
225
/** Get the semantic analysis object. */
83
226
Semantic &semantic () const;
229
/** Top parse rule to be reimplemented for a specific grammar.
230
* \return The root node of the syntax tree, or NULL. */
87
231
virtual CTree *trans_unit ();
89
// handle a compiler directive token
233
/** Handle a compiler directive token. The default handling is
234
* to skip the compiler directive. */
90
235
virtual void handle_directive ();
238
/** Save the current parser state. Calls save_state() on the
239
* builder, semantic, and token provider objects.
240
* \return The current parser state. */
93
241
State save_state ();
242
/** Forget the saved parser state. */
94
243
void forget_state ();
244
/** Restore the saved parser state. Triggers restoring the
245
* syntax and semantic trees to the saved state. */
95
246
void restore_state ();
96
void restore_state (State);
97
void set_state (State);
99
bool accept (CTree *, State);
247
/** Restore the saved parser state to the given state.
248
* Triggers restoring the syntax and semantic trees.
249
* \param state The state to which to restore. */
250
void restore_state (State state);
251
/** Overwrite the parser state with the given state.
252
* \param state The new parser state. */
253
void set_state (State state);
255
/** Accept the given syntax tree node. If the node is NULL
256
* then the parser state is restored to the given state.
257
* Otherwise all saved states are discarded.
258
* \param tree Tree to accept.
259
* \param state The saved state. */
260
bool accept (CTree *tree, State state);
262
/** Skip all non-core language tokens until the next
263
* core-language token is read.
264
* \return The next core-language token. */
101
265
Token *locate_token ();
266
/** Skip the current token. */
103
void skip_block (int, int);
268
/** Skip all tokens between start and end, including
269
* start and end token.
270
* \param start The start token type.
271
* \param end The end token type. */
272
void skip_block (int start, int end);
273
/** Skip all tokens between '{' and '}', including
104
275
void skip_curly_block ();
276
/** Skip all tokens between '(' and ')', including
105
278
void skip_round_block ();
106
void parse_block (int, int);
279
/** Parse all tokens between start and end, including
280
* start and end token.
281
* \param start The start token type.
282
* \param end The end token type. */
283
void parse_block (int start, int end);
284
/** Parse all tokens between '{' and '}', including
107
286
void parse_curly_block ();
287
/** Parse all tokens between '(' and ')', including
108
289
void parse_round_block ();
109
bool skip (int, bool = true);
110
bool skip (int *, bool = true);
111
bool is_in (int, int *) const;
290
/** Skip all tokens until a token with the given type is read.
291
* \param stop_token The type of the token to stop.
292
* \param inclusive If true, the stop token is skipped too.
293
* \return False if the stop token is not found, true otherwise. */
294
bool skip (int stop_token, bool inclusive = true);
295
/** Skip all tokens until a token with one of the given types is read.
296
* \param stop_tokens The types of the token to stop.
297
* \param inclusive If true, the stop token is skipped too.
298
* \return False if the stop token is not found, true otherwise. */
299
bool skip (int *stop_tokens, bool inclusive = true);
300
/** Check if the given token type is in the set of given token types.
301
* \param token_type The token type to check.
302
* \param token_types The set of token types. */
303
bool is_in (int token_type, int *token_types) const;
114
306
inline Syntax::Syntax (Builder &b, Semantic &s) :