Cleanup and removal of globals.
[external/ragel.git] / ragel / rlparse.kl
1 /*
2  *  Copyright 2001-2007 Adrian Thurston <thurston@cs.queensu.ca>
3  */
4
5 /*  This file is part of Ragel.
6  *
7  *  Ragel is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  (at your option) any later version.
11  * 
12  *  Ragel is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  * 
17  *  You should have received a copy of the GNU General Public License
18  *  along with Ragel; if not, write to the Free Software
19  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
20  */
21
22 #include "rlparse.h"
23 #include "ragel.h"
24 #include <iostream>
25 #include <errno.h>
26
27 using std::cout;
28 using std::cerr;
29 using std::endl;
30
31 ParserDict parserDict;
32
33 %%{
34
35 parser Parser;
36
37 include "rlparse.kh";
38
39 start: section_list;
40
41 section_list: section_list statement_list TK_EndSection;
42 section_list: ;
43
44 statement_list: statement_list statement;
45 statement_list: ;
46
47 statement: assignment commit;
48 statement: instantiation commit;
49 statement: action_spec commit;
50 statement: alphtype_spec commit;
51 statement: range_spec commit;
52 statement: getkey_spec commit;
53 statement: access_spec commit;
54 statement: variable_spec commit;
55
56 assignment:
57         machine_name '=' join ';' final {
58                 /* Main machine must be an instance. */
59                 bool isInstance = false;
60                 if ( strcmp($1->token.data, machineMain) == 0 ) {
61                         warning($1->token.loc) << 
62                                         "main machine will be implicitly instantiated" << endl;
63                         isInstance = true;
64                 }
65
66                 /* Generic creation of machine for instantiation and assignment. */
67                 JoinOrLm *joinOrLm = new JoinOrLm( $3->join );
68                 tryMachineDef( $1->token.loc, $1->token.data, joinOrLm, isInstance );
69         };
70
71 instantiation: 
72         machine_name TK_ColonEquals join_or_lm ';' final {
73                 /* Generic creation of machine for instantiation and assignment. */
74                 tryMachineDef( $1->token.loc, $1->token.data, $3->joinOrLm, true );
75         };
76
77 type token_type
78 {
79         Token token;
80 };
81
82 nonterm machine_name uses token_type;
83
84 machine_name: 
85         TK_Word final {
86                 /* Make/get the priority key. The name may have already been referenced
87                  * and therefore exist. */
88                 PriorDictEl *priorDictEl;
89                 if ( pd->priorDict.insert( $1->data, pd->nextPriorKey, &priorDictEl ) )
90                         pd->nextPriorKey += 1;
91                 pd->curDefPriorKey = priorDictEl->value;
92
93                 /* Make/get the local error key. */
94                 LocalErrDictEl *localErrDictEl;
95                 if ( pd->localErrDict.insert( $1->data, pd->nextLocalErrKey, &localErrDictEl ) )
96                         pd->nextLocalErrKey += 1;
97                 pd->curDefLocalErrKey = localErrDictEl->value;
98
99                 $$->token = *$1;
100         };
101
102 action_spec:
103         KW_Action TK_Word '{' inline_block '}' final {
104                 if ( pd->actionDict.find( $2->data ) ) {
105                         /* Recover by just ignoring the duplicate. */
106                         error($2->loc) << "action \"" << $2->data << "\" already defined" << endl;
107                 }
108                 else {
109                         //cerr << "NEW ACTION " << $2->data << " " << $4->inlineList << endl;
110                         /* Add the action to the list of actions. */
111                         Action *newAction = new Action( $3->loc, $2->data, $4->inlineList );
112
113                         /* Insert to list and dict. */
114                         pd->actionList.append( newAction );
115                         pd->actionDict.insert( newAction );
116                 }
117         };
118
119 # Specifies the data type of the input alphabet. One or two words followed by a
120 # semi-colon.
121 alphtype_spec:
122         KW_AlphType TK_Word TK_Word ';' final {
123                 if ( ! pd->setAlphType( $2->data, $3->data ) ) {
124                         // Recover by ignoring the alphtype statement.
125                         error($2->loc) << "\"" << $2->data << 
126                                         " " << $3->data << "\" is not a valid alphabet type" << endl;
127                 }
128         };
129
130 alphtype_spec:
131         KW_AlphType TK_Word ';' final {
132                 if ( ! pd->setAlphType( $2->data ) ) {
133                         // Recover by ignoring the alphtype statement.
134                         error($2->loc) << "\"" << $2->data << 
135                                         "\" is not a valid alphabet type" << endl;
136                 }
137         };
138
139 # Specifies a range to assume that the input characters will fall into.
140 range_spec:
141         KW_Range alphabet_num alphabet_num ';' final {
142                 // Save the upper and lower ends of the range and emit the line number.
143                 pd->lowerNum = $2->token.data;
144                 pd->upperNum = $3->token.data;
145                 pd->rangeLowLoc = $2->token.loc;
146                 pd->rangeHighLoc = $3->token.loc;
147         };
148
149 getkey_spec:
150         KW_GetKey inline_expr ';' final {
151                 pd->getKeyExpr = $2->inlineList;
152         };
153
154 access_spec:
155         KW_Access inline_expr ';' final {
156                 pd->accessExpr = $2->inlineList;
157         };
158
159 variable_spec:
160         KW_Variable opt_whitespace TK_Word inline_expr ';' final {
161                 /* FIXME: Need to implement the rest of this. */
162                 if ( strcmp( $3->data, "curstate" ) == 0 )
163                         pd->curStateExpr = $4->inlineList;
164                 else {
165                         error($3->loc) << "sorry, unimplementd" << endl;
166                 }
167         };
168
169 opt_whitespace: opt_whitespace IL_WhiteSpace;
170 opt_whitespace: ;
171
172 #
173 # Expressions
174 #
175
176 nonterm join_or_lm
177 {
178         JoinOrLm *joinOrLm;
179 };
180
181 join_or_lm: 
182         join final {
183                 $$->joinOrLm = new JoinOrLm( $1->join );
184         };
185 join_or_lm:
186         TK_BarStar lm_part_list '*' '|' final {
187                 /* Create a new factor going to a longest match structure. Record
188                  * in the parse data that we have a longest match. */
189                 LongestMatch *lm = new LongestMatch( $1->loc, $2->lmPartList );
190                 pd->lmList.append( lm );
191                 for ( LmPartList::Iter lmp = *($2->lmPartList); lmp.lte(); lmp++ )
192                         lmp->longestMatch = lm;
193                 $$->joinOrLm = new JoinOrLm( lm );
194         };
195
196 nonterm lm_part_list
197 {
198         LmPartList *lmPartList;
199 };
200
201 lm_part_list:
202         lm_part_list longest_match_part final {
203                 if ( $2->lmPart != 0 ) 
204                         $1->lmPartList->append( $2->lmPart );
205                 $$->lmPartList = $1->lmPartList;
206         };
207 lm_part_list:
208         longest_match_part final {
209                 /* Create a new list with the part. */
210                 $$->lmPartList = new LmPartList;
211                 if ( $1->lmPart != 0 )
212                         $$->lmPartList->append( $1->lmPart );
213         };
214
215 nonterm longest_match_part
216 {
217         LongestMatchPart *lmPart;
218 };
219
220 longest_match_part: 
221         action_spec final { $$->lmPart = 0; };
222 longest_match_part: 
223         assignment final { $$->lmPart = 0; };
224 longest_match_part: 
225         join opt_lm_part_action ';' final {
226                 $$->lmPart = 0;
227                 Action *action = $2->action;
228                 if ( action != 0 )
229                         action->isLmAction = true;
230                 $$->lmPart = new LongestMatchPart( $1->join, action, 
231                                 $3->loc, pd->nextLongestMatchId++ );
232         };
233
234 nonterm opt_lm_part_action
235 {
236         Action *action;
237 };
238
239 opt_lm_part_action:
240         TK_DoubleArrow action_embed final { 
241                 $$->action = $2->action;
242         };
243 opt_lm_part_action:
244         action_embed_block final {
245                 $$->action = $1->action;
246         };
247 opt_lm_part_action:
248         final {
249                 $$->action = 0;
250         };
251
252
253 nonterm join
254 {
255         Join *join;
256 };
257
258 join: 
259         join ',' expression final {
260                 /* Append the expression to the list and return it. */
261                 $1->join->exprList.append( $3->expression );
262                 $$->join = $1->join;
263         };
264 join: 
265         expression final {
266                 $$->join = new Join( $1->expression );
267         };
268
269 nonterm expression
270 {
271         Expression *expression;
272 };
273
274 expression: 
275         expression '|' term final {
276                 $$->expression = new Expression( $1->expression, 
277                                 $3->term, Expression::OrType );
278         };
279 expression: 
280         expression '&' term final {
281                 $$->expression = new Expression( $1->expression, 
282                                 $3->term, Expression::IntersectType );
283         };
284 # This priority specification overrides the innermost parsing strategy which
285 # results ordered choice interpretation of the grammar.
286 expression: 
287         expression pri(1) '-' term final {
288                 $$->expression = new Expression( $1->expression, 
289                                 $3->term, Expression::SubtractType );
290         };
291 expression: 
292         expression TK_DashDash term final {
293                 $$->expression = new Expression( $1->expression, 
294                                 $3->term, Expression::StrongSubtractType );
295         };
296 expression: 
297         term final {
298                 $$->expression = new Expression( $1->term );
299         };
300
301 nonterm term
302 {
303         Term *term;
304 };
305
306 term:
307         term factor_with_label final {
308                 $$->term = new Term( $1->term, $2->factorWithAug );
309         };
310 term:
311         term '.' factor_with_label final {
312                 $$->term = new Term( $1->term, $3->factorWithAug );
313         };
314 term:
315         term TK_ColonGt factor_with_label final {
316                 $$->term = new Term( $1->term, $3->factorWithAug, Term::RightStartType );
317         };
318 term:
319         term TK_ColonGtGt factor_with_label final {
320                 $$->term = new Term( $1->term, $3->factorWithAug, Term::RightFinishType );
321         };
322 term:
323         term TK_LtColon factor_with_label final {
324                 $$->term = new Term( $1->term, 
325                                 $3->factorWithAug, Term::LeftType );
326         };
327 term:
328         factor_with_label final {
329                 $$->term = new Term( $1->factorWithAug );
330         };
331
332 nonterm factor_with_label
333 {
334         FactorWithAug *factorWithAug;
335 };
336
337 factor_with_label: 
338         TK_Word ':' factor_with_label final { 
339                 /* Add the label to the list and pass the factor up. */
340                 $3->factorWithAug->labels.prepend( Label($1->loc, $1->data) );
341                 $$->factorWithAug = $3->factorWithAug; 
342         };
343 factor_with_label: 
344         factor_with_ep final {
345                 $$->factorWithAug = $1->factorWithAug;
346         };
347
348 nonterm factor_with_ep
349 {
350         FactorWithAug *factorWithAug;
351 };
352
353 factor_with_ep: 
354         factor_with_ep TK_Arrow local_state_ref final { 
355                 /* Add the target to the list and return the factor object. */
356                 $1->factorWithAug->epsilonLinks.append( EpsilonLink( $2->loc, nameRef ) );
357                 $$->factorWithAug = $1->factorWithAug; 
358         };
359 factor_with_ep: 
360         factor_with_aug final {
361                 $$->factorWithAug = $1->factorWithAug;
362         };
363
364 nonterm factor_with_aug
365 {
366         FactorWithAug *factorWithAug;
367 };
368
369 factor_with_aug:
370         factor_with_aug aug_type_base action_embed final {
371                 /* Append the action to the factorWithAug, record the refernce from 
372                  * factorWithAug to the action and pass up the factorWithAug. */
373                 $1->factorWithAug->actions.append( 
374                                 ParserAction( $2->loc, $2->augType, 0, $3->action ) );
375                 $$->factorWithAug = $1->factorWithAug;
376         };
377 factor_with_aug:
378         factor_with_aug aug_type_base priority_aug final {
379                 /* Append the named priority to the factorWithAug and pass it up. */
380                 $1->factorWithAug->priorityAugs.append( 
381                                 PriorityAug( $2->augType, pd->curDefPriorKey, $3->priorityNum ) );
382                 $$->factorWithAug = $1->factorWithAug;
383         };
384 factor_with_aug:
385         factor_with_aug aug_type_base '(' priority_name ',' priority_aug ')' final {
386                 /* Append the priority using a default name. */
387                 $1->factorWithAug->priorityAugs.append( 
388                                 PriorityAug( $2->augType, $4->priorityName, $6->priorityNum ) );
389                 $$->factorWithAug = $1->factorWithAug;
390         };
391 factor_with_aug:
392         factor_with_aug aug_type_cond action_embed final {
393                 $1->factorWithAug->conditions.append( ParserAction( $2->loc, 
394                                 $2->augType, 0, $3->action ) );
395                 $$->factorWithAug = $1->factorWithAug;
396         };
397 factor_with_aug:
398         factor_with_aug aug_type_to_state action_embed final {
399                 /* Append the action, pass it up. */
400                 $1->factorWithAug->actions.append( ParserAction( $2->loc, 
401                                 $2->augType, 0, $3->action ) );
402                 $$->factorWithAug = $1->factorWithAug;
403         };
404 factor_with_aug:
405         factor_with_aug aug_type_from_state action_embed final {
406                 /* Append the action, pass it up. */
407                 $1->factorWithAug->actions.append( ParserAction( $2->loc,
408                                 $2->augType, 0, $3->action ) );
409                 $$->factorWithAug = $1->factorWithAug;
410         };
411 factor_with_aug:
412         factor_with_aug aug_type_eof action_embed final {
413                 /* Append the action, pass it up. */
414                 $1->factorWithAug->actions.append( ParserAction( $2->loc,
415                                 $2->augType, 0, $3->action ) );
416                 $$->factorWithAug = $1->factorWithAug;
417         };
418 factor_with_aug:
419         factor_with_aug aug_type_gbl_error action_embed final {
420                 /* Append the action to the factorWithAug, record the refernce from 
421                  * factorWithAug to the action and pass up the factorWithAug. */
422                 $1->factorWithAug->actions.append( ParserAction( $2->loc,
423                                 $2->augType, pd->curDefLocalErrKey, $3->action ) );
424                 $$->factorWithAug = $1->factorWithAug;
425         };
426 factor_with_aug:
427         factor_with_aug aug_type_local_error action_embed final {
428                 /* Append the action to the factorWithAug, record the refernce from 
429                  * factorWithAug to the action and pass up the factorWithAug. */
430                 $1->factorWithAug->actions.append( ParserAction( $2->loc, 
431                                 $2->augType, pd->curDefLocalErrKey, $3->action ) );
432                 $$->factorWithAug = $1->factorWithAug;
433         };
434 factor_with_aug:
435         factor_with_aug aug_type_local_error '(' local_err_name ',' action_embed ')' final {
436                 /* Append the action to the factorWithAug, record the refernce from
437                  * factorWithAug to the action and pass up the factorWithAug. */
438                 $1->factorWithAug->actions.append( ParserAction( $2->loc,
439                                 $2->augType, $4->error_name, $6->action ) );
440                 $$->factorWithAug = $1->factorWithAug;
441         };
442 factor_with_aug:
443         factor_with_rep final {
444                 $$->factorWithAug = new FactorWithAug( $1->factorWithRep );
445         };
446
447 type aug_type
448 {
449         InputLoc loc;
450         AugType augType;
451 };
452
453 #  Classes of transtions on which to embed actions or change priorities.
454 nonterm aug_type_base uses aug_type;
455
456 aug_type_base: '@' final { $$->loc = $1->loc; $$->augType = at_finish; };
457 aug_type_base: '%' final { $$->loc = $1->loc; $$->augType = at_leave; };
458 aug_type_base: '$' final { $$->loc = $1->loc; $$->augType = at_all; };
459 aug_type_base: '>' final { $$->loc = $1->loc; $$->augType = at_start; };
460
461 # Embedding conditions.
462 nonterm aug_type_cond uses aug_type;
463
464 aug_type_cond: TK_StartCond final { $$->loc = $1->loc; $$->augType = at_start; };
465 aug_type_cond: '>' KW_When final { $$->loc = $1->loc; $$->augType = at_start; };
466 aug_type_cond: TK_AllCond final { $$->loc = $1->loc; $$->augType = at_all; };
467 aug_type_cond: '$' KW_When final { $$->loc = $1->loc; $$->augType = at_all; };
468 aug_type_cond: TK_LeavingCond final { $$->loc = $1->loc; $$->augType = at_leave; };
469 aug_type_cond: '%' KW_When final { $$->loc = $1->loc; $$->augType = at_leave; };
470 aug_type_cond: KW_When final { $$->loc = $1->loc; $$->augType = at_all; };
471
472 #
473 # To state actions.
474 #
475
476 nonterm aug_type_to_state uses aug_type;
477
478 aug_type_to_state: TK_StartToState 
479                 final { $$->loc = $1->loc; $$->augType = at_start_to_state; };
480 aug_type_to_state: '>' KW_To 
481                 final { $$->loc = $1->loc; $$->augType = at_start_to_state; };
482
483 aug_type_to_state: TK_NotStartToState 
484                 final { $$->loc = $1->loc; $$->augType = at_not_start_to_state; };
485 aug_type_to_state: '<' KW_To 
486                 final { $$->loc = $1->loc; $$->augType = at_not_start_to_state; };
487
488 aug_type_to_state: TK_AllToState 
489                 final { $$->loc = $1->loc; $$->augType = at_all_to_state; };
490 aug_type_to_state: '$' KW_To 
491                 final { $$->loc = $1->loc; $$->augType = at_all_to_state; };
492
493 aug_type_to_state: TK_FinalToState
494                 final { $$->loc = $1->loc; $$->augType = at_final_to_state; };
495 aug_type_to_state: '%' KW_To
496                 final { $$->loc = $1->loc; $$->augType = at_final_to_state; };
497
498 aug_type_to_state: TK_NotFinalToState
499                 final { $$->loc = $1->loc; $$->augType = at_not_final_to_state; };
500 aug_type_to_state: '@' KW_To
501                 final { $$->loc = $1->loc; $$->augType = at_not_final_to_state; };
502
503 aug_type_to_state: TK_MiddleToState
504                 final { $$->loc = $1->loc; $$->augType = at_middle_to_state; };
505 aug_type_to_state: TK_Middle KW_To
506                 final { $$->loc = $1->loc; $$->augType = at_middle_to_state; };
507
508 #
509 # From state actions.
510 #
511
512 nonterm aug_type_from_state uses aug_type;
513
514 aug_type_from_state: TK_StartFromState 
515                 final { $$->loc = $1->loc; $$->augType = at_start_from_state; };
516 aug_type_from_state: '>' KW_From 
517                 final { $$->loc = $1->loc; $$->augType = at_start_from_state; };
518
519 aug_type_from_state: TK_NotStartFromState 
520                 final { $$->loc = $1->loc; $$->augType = at_not_start_from_state; };
521 aug_type_from_state: '<' KW_From 
522                 final { $$->loc = $1->loc; $$->augType = at_not_start_from_state; };
523
524 aug_type_from_state: TK_AllFromState 
525                 final { $$->loc = $1->loc; $$->augType = at_all_from_state; };
526 aug_type_from_state: '$' KW_From 
527                 final { $$->loc = $1->loc; $$->augType = at_all_from_state; };
528
529 aug_type_from_state: TK_FinalFromState 
530                 final { $$->loc = $1->loc; $$->augType = at_final_from_state; };
531 aug_type_from_state: '%' KW_From 
532                 final { $$->loc = $1->loc; $$->augType = at_final_from_state; };
533
534 aug_type_from_state: TK_NotFinalFromState 
535                 final { $$->loc = $1->loc; $$->augType = at_not_final_from_state; };
536 aug_type_from_state: '@' KW_From 
537                 final { $$->loc = $1->loc; $$->augType = at_not_final_from_state; };
538
539 aug_type_from_state: TK_MiddleFromState 
540                 final { $$->loc = $1->loc; $$->augType = at_middle_from_state; };
541 aug_type_from_state: TK_Middle KW_From 
542                 final { $$->loc = $1->loc; $$->augType = at_middle_from_state; };
543
544 #
545 # Eof state actions.
546 #
547
548 nonterm aug_type_eof uses aug_type;
549
550 aug_type_eof: TK_StartEOF 
551                 final { $$->loc = $1->loc; $$->augType = at_start_eof; };
552 aug_type_eof: '>' KW_Eof
553                 final { $$->loc = $1->loc; $$->augType = at_start_eof; };
554
555 aug_type_eof: TK_NotStartEOF
556                 final { $$->loc = $1->loc; $$->augType = at_not_start_eof; };
557 aug_type_eof: '<' KW_Eof
558                 final { $$->loc = $1->loc; $$->augType = at_not_start_eof; };
559
560 aug_type_eof: TK_AllEOF
561                 final { $$->loc = $1->loc; $$->augType = at_all_eof; };
562 aug_type_eof: '$' KW_Eof
563                 final { $$->loc = $1->loc; $$->augType = at_all_eof; };
564
565 aug_type_eof: TK_FinalEOF
566                 final { $$->loc = $1->loc; $$->augType = at_final_eof; };
567 aug_type_eof: '%' KW_Eof
568                 final { $$->loc = $1->loc; $$->augType = at_final_eof; };
569
570 aug_type_eof: TK_NotFinalEOF
571                 final { $$->loc = $1->loc; $$->augType = at_not_final_eof; };
572 aug_type_eof: '@' KW_Eof
573                 final { $$->loc = $1->loc; $$->augType = at_not_final_eof; };
574
575 aug_type_eof: TK_MiddleEOF
576                 final { $$->loc = $1->loc; $$->augType = at_middle_eof; };
577 aug_type_eof: TK_Middle KW_Eof
578                 final { $$->loc = $1->loc; $$->augType = at_middle_eof; };
579
580 #
581 # Global error actions.
582 #
583
584 nonterm aug_type_gbl_error uses aug_type;
585
586 aug_type_gbl_error: TK_StartGblError 
587                 final { $$->loc = $1->loc; $$->augType = at_start_gbl_error; };
588 aug_type_gbl_error: '>' KW_Err 
589                 final { $$->loc = $1->loc; $$->augType = at_start_gbl_error; };
590
591 aug_type_gbl_error: TK_NotStartGblError 
592                 final { $$->loc = $1->loc; $$->augType = at_not_start_gbl_error; };
593 aug_type_gbl_error: '<' KW_Err
594                 final { $$->loc = $1->loc; $$->augType = at_not_start_gbl_error; };
595
596 aug_type_gbl_error: TK_AllGblError
597                 final { $$->loc = $1->loc; $$->augType = at_all_gbl_error; };
598 aug_type_gbl_error: '$' KW_Err
599                 final { $$->loc = $1->loc; $$->augType = at_all_gbl_error; };
600
601 aug_type_gbl_error: TK_FinalGblError
602                 final { $$->loc = $1->loc; $$->augType = at_final_gbl_error; };
603 aug_type_gbl_error: '%' KW_Err
604                 final { $$->loc = $1->loc; $$->augType = at_final_gbl_error; };
605
606 aug_type_gbl_error: TK_NotFinalGblError
607                 final { $$->loc = $1->loc; $$->augType = at_not_final_gbl_error; };
608 aug_type_gbl_error: '@' KW_Err
609                 final { $$->loc = $1->loc; $$->augType = at_not_final_gbl_error; };
610
611 aug_type_gbl_error: TK_MiddleGblError
612                 final { $$->loc = $1->loc; $$->augType = at_middle_gbl_error; };
613 aug_type_gbl_error: TK_Middle KW_Err
614                 final { $$->loc = $1->loc; $$->augType = at_middle_gbl_error; };
615
616
617 #
618 # Local error actions.
619 #
620
621 nonterm aug_type_local_error uses aug_type;
622
623 aug_type_local_error: TK_StartLocalError
624                 final { $$->loc = $1->loc; $$->augType = at_start_local_error; };
625 aug_type_local_error: '>' KW_Lerr
626                 final { $$->loc = $1->loc; $$->augType = at_start_local_error; };
627
628 aug_type_local_error: TK_NotStartLocalError
629                 final { $$->loc = $1->loc; $$->augType = at_not_start_local_error; };
630 aug_type_local_error: '<' KW_Lerr
631                 final { $$->loc = $1->loc; $$->augType = at_not_start_local_error; };
632
633 aug_type_local_error: TK_AllLocalError
634                 final { $$->loc = $1->loc; $$->augType = at_all_local_error; };
635 aug_type_local_error: '$' KW_Lerr
636                 final { $$->loc = $1->loc; $$->augType = at_all_local_error; };
637
638 aug_type_local_error: TK_FinalLocalError
639                 final { $$->loc = $1->loc; $$->augType = at_final_local_error; };
640 aug_type_local_error: '%' KW_Lerr
641                 final { $$->loc = $1->loc; $$->augType = at_final_local_error; };
642
643 aug_type_local_error: TK_NotFinalLocalError
644                 final { $$->loc = $1->loc; $$->augType = at_not_final_local_error; };
645 aug_type_local_error: '@' KW_Lerr
646                 final { $$->loc = $1->loc; $$->augType = at_not_final_local_error; };
647
648 aug_type_local_error: TK_MiddleLocalError
649                 final { $$->loc = $1->loc; $$->augType = at_middle_local_error; };
650 aug_type_local_error: TK_Middle KW_Lerr
651                 final { $$->loc = $1->loc; $$->augType = at_middle_local_error; };
652
653
654 type action_ref
655 {
656         Action *action;
657 };
658
659 # Different ways to embed actions. A TK_Word is reference to an action given by
660 # the user as a statement in the fsm specification. An action can also be
661 # specified immediately.
662 nonterm action_embed uses action_ref;
663
664 action_embed: action_embed_word final { $$->action = $1->action; };
665 action_embed: action_embed_block final { $$->action = $1->action; };
666
667 nonterm action_embed_word uses action_ref;
668
669 action_embed_word:
670         TK_Word final {
671                 /* Set the name in the actionDict. */
672                 Action *action = pd->actionDict.find( $1->data );
673                 if ( action != 0 ) {
674                         /* Pass up the action element */
675                         $$->action = action;
676                 }
677                 else {
678                         /* Will recover by returning null as the action. */
679                         error($1->loc) << "action lookup of \"" << $1->data << "\" failed" << endl;
680                         $$->action = 0;
681                 }
682         };
683
684 nonterm action_embed_block uses action_ref;
685
686 action_embed_block:
687         '{' inline_block '}' final {
688                 /* Create the action, add it to the list and pass up. */
689                 Action *newAction = new Action( $1->loc, 0, $2->inlineList );
690                 pd->actionList.append( newAction );
691                 $$->action = newAction;
692         };
693
694 nonterm priority_name
695 {
696         int priorityName;
697 };
698
699 # A specified priority name. Looks up the name in the current priority
700 # dictionary.
701 priority_name:
702         TK_Word final {
703                 // Lookup/create the priority key.
704                 PriorDictEl *priorDictEl;
705                 if ( pd->priorDict.insert( $1->data, pd->nextPriorKey, &priorDictEl ) )
706                         pd->nextPriorKey += 1;
707
708                 // Use the inserted/found priority key.
709                 $$->priorityName = priorDictEl->value;
710         };
711
712 nonterm priority_aug
713 {
714         int priorityNum;
715 };
716
717 # Priority change specs.
718 priority_aug: 
719         priority_aug_num final {
720                 // Convert the priority number to a long. Check for overflow.
721                 errno = 0;
722                 //cerr << "PRIOR AUG: " << $1->token.data << endl;
723                 int aug = strtol( $1->token.data, 0, 10 );
724                 if ( errno == ERANGE && aug == LONG_MAX ) {
725                         /* Priority number too large. Recover by setting the priority to 0. */
726                         error($1->token.loc) << "priority number " << $1->token.data << 
727                                         " overflows" << endl;
728                         $$->priorityNum = 0;
729                 }
730                 else if ( errno == ERANGE && aug == LONG_MIN ) {
731                         /* Priority number too large in the neg. Recover by using 0. */
732                         error($1->token.loc) << "priority number " << $1->token.data << 
733                                         " underflows" << endl;
734                         $$->priorityNum = 0;
735                 }
736                 else {
737                         /* No overflow or underflow. */
738                         $$->priorityNum = aug;
739                 }
740         };
741
742 nonterm priority_aug_num uses token_type;
743
744 priority_aug_num:
745         TK_UInt final {
746                 $$->token = *$1;
747         };
748 priority_aug_num:
749         '+' TK_UInt final {
750                 $$->token.set( "+", 1 );
751                 $$->token.loc = $1->loc;
752                 $$->token.append( *$2 );
753         };
754 priority_aug_num:
755         '-' TK_UInt final {
756                 $$->token.set( "-", 1 );
757                 $$->token.loc = $1->loc;
758                 $$->token.append( *$2 );
759         };
760
761 nonterm local_err_name
762 {
763         int error_name;
764 };
765
766 local_err_name:
767         TK_Word final {
768                 /* Lookup/create the priority key. */
769                 LocalErrDictEl *localErrDictEl;
770                 if ( pd->localErrDict.insert( $1->data, pd->nextLocalErrKey, &localErrDictEl ) )
771                         pd->nextLocalErrKey += 1;
772
773                 /* Use the inserted/found priority key. */
774                 $$->error_name = localErrDictEl->value;
775         };
776
777
778
779 # The fourth level of precedence. These are the trailing unary operators that
780 # allow for repetition.
781
782 nonterm factor_with_rep
783 {
784         FactorWithRep *factorWithRep;
785 };
786
787 factor_with_rep:
788         factor_with_rep '*' final {
789                 $$->factorWithRep = new FactorWithRep( $2->loc, $1->factorWithRep, 
790                                 0, 0, FactorWithRep::StarType );
791         };
792 factor_with_rep:
793         factor_with_rep TK_StarStar final {
794                 $$->factorWithRep = new FactorWithRep( $2->loc, $1->factorWithRep, 
795                                 0, 0, FactorWithRep::StarStarType );
796         };
797 factor_with_rep:
798         factor_with_rep '?' final {
799                 $$->factorWithRep = new FactorWithRep( $2->loc, $1->factorWithRep, 
800                                 0, 0, FactorWithRep::OptionalType );
801         };
802 factor_with_rep:
803         factor_with_rep '+' final {
804                 $$->factorWithRep = new FactorWithRep( $2->loc, $1->factorWithRep, 
805                                 0, 0, FactorWithRep::PlusType );
806         };
807 factor_with_rep:
808         factor_with_rep '{' factor_rep_num '}' final {
809                 $$->factorWithRep = new FactorWithRep( $2->loc, $1->factorWithRep, 
810                                 $3->rep, 0, FactorWithRep::ExactType );
811         };
812 factor_with_rep:
813         factor_with_rep '{' ',' factor_rep_num '}' final {
814                 $$->factorWithRep = new FactorWithRep( $2->loc, $1->factorWithRep, 
815                                 0, $4->rep, FactorWithRep::MaxType );
816         };
817 factor_with_rep:
818         factor_with_rep '{' factor_rep_num ',' '}' final {
819                 $$->factorWithRep = new FactorWithRep( $2->loc, $1->factorWithRep,
820                                 $3->rep, 0, FactorWithRep::MinType );
821         };
822 factor_with_rep:
823         factor_with_rep '{' factor_rep_num ',' factor_rep_num '}' final {
824                 $$->factorWithRep = new FactorWithRep( $2->loc, $1->factorWithRep, 
825                                 $3->rep, $5->rep, FactorWithRep::RangeType );
826         };
827 factor_with_rep:
828         factor_with_neg final {
829                 $$->factorWithRep = new FactorWithRep( 
830                                 $1->factorWithNeg->loc, $1->factorWithNeg );
831         };
832
833 nonterm factor_rep_num
834 {
835         int rep;
836 };
837
838 factor_rep_num:
839         TK_UInt final {
840                 // Convert the priority number to a long. Check for overflow.
841                 errno = 0;
842                 int rep = strtol( $1->data, 0, 10 );
843                 if ( errno == ERANGE && rep == LONG_MAX ) {
844                         // Repetition too large. Recover by returing repetition 1. */
845                         error($1->loc) << "repetition number " << $1->data << " overflows" << endl;
846                         $$->rep = 1;
847                 }
848                 else {
849                         // Cannot be negative, so no overflow.
850                         $$->rep = rep;
851                 }
852         };
853
854
855 #
856 # The fifth level up in precedence. Negation.
857 #
858
859 nonterm factor_with_neg
860 {
861         FactorWithNeg *factorWithNeg;
862 };
863
864 factor_with_neg:
865         '!' factor_with_neg final {
866                 $$->factorWithNeg = new FactorWithNeg( $1->loc,
867                                 $2->factorWithNeg, FactorWithNeg::NegateType );
868         };
869 factor_with_neg:
870         '^' factor_with_neg final {
871                 $$->factorWithNeg = new FactorWithNeg( $1->loc,
872                                 $2->factorWithNeg, FactorWithNeg::CharNegateType );
873         };
874 factor_with_neg:
875         factor final {
876                 $$->factorWithNeg = new FactorWithNeg( $1->factor->loc, $1->factor );
877         };
878
879 nonterm factor
880 {
881         Factor *factor;
882 };
883
884 factor: 
885         TK_Literal final {
886                 /* Create a new factor node going to a concat literal. */
887                 $$->factor = new Factor( new Literal( *$1, Literal::LitString ) );
888         };
889 factor: 
890         alphabet_num final {
891                 /* Create a new factor node going to a literal number. */
892                 $$->factor = new Factor( new Literal( $1->token, Literal::Number ) );
893         };
894 factor:
895         TK_Word final {
896                 /* Find the named graph. */
897                 GraphDictEl *gdNode = pd->graphDict.find( $1->data );
898                 if ( gdNode == 0 ) {
899                         /* Recover by returning null as the factor node. */
900                         error($1->loc) << "graph lookup of \"" << $1->data << "\" failed" << endl;
901                         $$->factor = 0;
902                 }
903                 else if ( gdNode->isInstance ) {
904                         /* Recover by retuning null as the factor node. */
905                         error($1->loc) << "references to graph instantiations not allowed "
906                                         "in expressions" << endl;
907                         $$->factor = 0;
908                 }
909                 else {
910                         /* Create a factor node that is a lookup of an expression. */
911                         $$->factor = new Factor( $1->loc, gdNode->value );
912                 }
913         };
914 factor:
915         RE_SqOpen regular_expr_or_data RE_SqClose final {
916                 /* Create a new factor node going to an OR expression. */
917                 $$->factor = new Factor( new ReItem( $1->loc, $2->reOrBlock, ReItem::OrBlock ) );
918         };
919 factor:
920         RE_SqOpenNeg regular_expr_or_data RE_SqClose final {
921                 /* Create a new factor node going to a negated OR expression. */
922                 $$->factor = new Factor( new ReItem( $1->loc, $2->reOrBlock, ReItem::NegOrBlock ) );
923         };
924 factor:
925         RE_Slash regular_expr RE_Slash final {
926                 if ( $3->length > 1 ) {
927                         for ( char *p = $3->data; *p != 0; p++ ) {
928                                 if ( *p == 'i' )
929                                         $2->regExpr->caseInsensitive = true;
930                         }
931                 }
932
933                 /* Create a new factor node going to a regular exp. */
934                 $$->factor = new Factor( $2->regExpr );
935         };
936 factor:
937         range_lit TK_DotDot range_lit final {
938                 /* Create a new factor node going to a range. */
939                 $$->factor = new Factor( new Range( $1->literal, $3->literal ) );
940         };
941 factor:
942         '(' join ')' final {
943                 /* Create a new factor going to a parenthesized join. */
944                 $$->factor = new Factor( $2->join );
945         };
946
947 nonterm range_lit
948 {
949         Literal *literal;
950 };
951
952 # Literals which can be the end points of ranges.
953 range_lit:
954         TK_Literal final {
955                 /* Range literas must have only one char. We restrict this in the parse tree. */
956                 $$->literal = new Literal( *$1, Literal::LitString );
957         };
958 range_lit:
959         alphabet_num final {
960                 /* Create a new literal number. */
961                 $$->literal = new Literal( $1->token, Literal::Number );
962         };
963
964 nonterm alphabet_num uses token_type;
965
966 # Any form of a number that can be used as a basic machine. */
967 alphabet_num:
968         TK_UInt final { 
969                 $$->token = *$1;
970         };
971 alphabet_num: 
972         '-' TK_UInt final { 
973                 $$->token.set( "-", 1 );
974                 $$->token.loc = $1->loc;
975                 $$->token.append( *$2 );
976         };
977 alphabet_num: 
978         TK_Hex final { 
979                 $$->token = *$1;
980         };
981 #
982 # Regular Expressions.
983 #
984
985 nonterm regular_expr
986 {
987         RegExpr *regExpr;
988 };
989
990 # Parser for regular expression fsms. Any number of expression items which
991 # generally gives a machine one character long or one character long stared.
992 regular_expr:
993         regular_expr regular_expr_item final {
994                 /* An optimization to lessen the tree size. If a non-starred char is
995                  * directly under the left side on the right and the right side is
996                  * another non-starred char then paste them together and return the
997                  * left side. Otherwise just put the two under a new reg exp node. */
998                 if ( $2->reItem->type == ReItem::Data && !$2->reItem->star &&
999                         $1->regExpr->type == RegExpr::RecurseItem &&
1000                         $1->regExpr->item->type == ReItem::Data && !$1->regExpr->item->star )
1001                 {
1002                         /* Append the right side to the right side of the left and toss the
1003                          * right side. */
1004                         $1->regExpr->item->token.append( $2->reItem->token );
1005                         delete $2->reItem;
1006                         $$->regExpr = $1->regExpr;
1007                 }
1008                 else {
1009                         $$->regExpr = new RegExpr( $1->regExpr, $2->reItem );
1010                 }
1011         };
1012 regular_expr:
1013         final {
1014                 /* Can't optimize the tree. */
1015                 $$->regExpr = new RegExpr();
1016         };
1017
1018 nonterm regular_expr_item
1019 {
1020         ReItem *reItem;
1021 };
1022
1023 # RegularExprItems can be a character spec with an optional staring of the char.
1024 regular_expr_item:
1025         regular_expr_char RE_Star final {
1026                 $1->reItem->star = true;
1027                 $$->reItem = $1->reItem;
1028         };
1029 regular_expr_item:
1030         regular_expr_char final {
1031                 $$->reItem = $1->reItem;
1032         };
1033
1034 nonterm regular_expr_char
1035 {
1036         ReItem *reItem;
1037 };
1038
1039 # A character spec can be a set of characters inside of square parenthesis, a
1040 # dot specifying any character or some explicitly stated character.
1041 regular_expr_char:
1042         RE_SqOpen regular_expr_or_data RE_SqClose final {
1043                 $$->reItem = new ReItem( $1->loc, $2->reOrBlock, ReItem::OrBlock );
1044         };
1045 regular_expr_char:
1046         RE_SqOpenNeg regular_expr_or_data RE_SqClose final {
1047                 $$->reItem = new ReItem( $1->loc, $2->reOrBlock, ReItem::NegOrBlock );
1048         };
1049 regular_expr_char:
1050         RE_Dot final {
1051                 $$->reItem = new ReItem( $1->loc, ReItem::Dot );
1052         };
1053 regular_expr_char:
1054         RE_Char final {
1055                 $$->reItem = new ReItem( $1->loc, *$1 );
1056         };
1057
1058 # The data inside of a [] expression in a regular expression. Accepts any
1059 # number of characters or ranges. */
1060 nonterm regular_expr_or_data
1061 {
1062         ReOrBlock *reOrBlock;
1063 };
1064
1065 regular_expr_or_data:
1066         regular_expr_or_data regular_expr_or_char final {
1067                 /* An optimization to lessen the tree size. If an or char is directly
1068                  * under the left side on the right and the right side is another or
1069                  * char then paste them together and return the left side. Otherwise
1070                  * just put the two under a new or data node. */
1071                 if ( $2->reOrItem->type == ReOrItem::Data &&
1072                                 $1->reOrBlock->type == ReOrBlock::RecurseItem &&
1073                                 $1->reOrBlock->item->type == ReOrItem::Data )
1074                 {
1075                         /* Append the right side to right side of the left and toss the
1076                          * right side. */
1077                         $1->reOrBlock->item->token.append( $2->reOrItem->token );
1078                         delete $2->reOrItem;
1079                         $$->reOrBlock = $1->reOrBlock;
1080                 }
1081                 else {
1082                         /* Can't optimize, put the left and right under a new node. */
1083                         $$->reOrBlock = new ReOrBlock( $1->reOrBlock, $2->reOrItem );
1084                 }
1085         };
1086 regular_expr_or_data:
1087         final {
1088                 $$->reOrBlock = new ReOrBlock();
1089         };
1090
1091 # A single character inside of an or expression. Can either be a character or a
1092 # set of characters.
1093 nonterm regular_expr_or_char
1094 {
1095         ReOrItem *reOrItem;
1096 };
1097
1098 regular_expr_or_char:
1099         RE_Char final {
1100                 $$->reOrItem = new ReOrItem( $1->loc, *$1 );
1101         };
1102 regular_expr_or_char:
1103         RE_Char RE_Dash RE_Char final {
1104                 $$->reOrItem = new ReOrItem( $2->loc, $1->data[0], $3->data[0] );
1105         };
1106
1107 #
1108 # Inline Lists for inline host code.
1109 #
1110
1111 type inline_list
1112 {
1113         InlineList *inlineList;
1114 };
1115
1116 nonterm inline_block uses inline_list;
1117
1118 inline_block:
1119         inline_block inline_block_item 
1120         final {
1121                 /* Append the item to the list, return the list. */
1122                 $$->inlineList = $1->inlineList;
1123                 $$->inlineList->append( $2->inlineItem );
1124         };
1125
1126 inline_block:
1127         final {
1128                 /* Start with empty list. */
1129                 $$->inlineList = new InlineList;
1130         };
1131
1132 type inline_item
1133 {
1134         InlineItem *inlineItem;
1135 };
1136
1137 nonterm inline_block_item uses inline_item;
1138 nonterm inline_block_interpret uses inline_item;
1139
1140 inline_block_item:
1141         inline_expr_any 
1142         final {
1143                 $$->inlineItem = new InlineItem( $1->token.loc, $1->token.data, InlineItem::Text );
1144         };
1145
1146 inline_block_item:
1147         inline_block_symbol 
1148         final {
1149                 $$->inlineItem = new InlineItem( $1->token.loc, $1->token.data, InlineItem::Text );
1150         };
1151
1152 inline_block_item:
1153         inline_block_interpret
1154         final {
1155                 /* Pass the inline item up. */
1156                 $$->inlineItem = $1->inlineItem;
1157         };
1158
1159 nonterm inline_block_symbol uses token_type;
1160
1161 inline_block_symbol: ',' final { $$->token = *$1; };
1162 inline_block_symbol: ';' final { $$->token = *$1; };
1163 inline_block_symbol: '(' final { $$->token = *$1; };
1164 inline_block_symbol: ')' final { $$->token = *$1; };
1165 inline_block_symbol: '*' final { $$->token = *$1; };
1166 inline_block_symbol: TK_NameSep final { $$->token = *$1; };
1167
1168 # Interpreted statements in a struct block. */
1169 inline_block_interpret:
1170         inline_expr_interpret final {
1171                 /* Pass up interpreted items of inline expressions. */
1172                 $$->inlineItem = $1->inlineItem;
1173         };
1174 inline_block_interpret:
1175         KW_Hold ';' final {
1176                 $$->inlineItem = new InlineItem( $1->loc, InlineItem::Hold );
1177         };
1178 inline_block_interpret:
1179         KW_Exec inline_expr ';' final {
1180                 $$->inlineItem = new InlineItem( $1->loc, InlineItem::Exec );
1181                 $$->inlineItem->children = $2->inlineList;
1182         };
1183 inline_block_interpret:
1184         KW_Goto state_ref ';' final { 
1185                 $$->inlineItem = new InlineItem( $1->loc, 
1186                                 new NameRef(nameRef), InlineItem::Goto );
1187         };
1188 inline_block_interpret:
1189         KW_Goto '*' inline_expr ';' final {
1190                 $$->inlineItem = new InlineItem( $1->loc, InlineItem::GotoExpr );
1191                 $$->inlineItem->children = $3->inlineList;
1192         };
1193 inline_block_interpret:
1194         KW_Next state_ref ';' final { 
1195                 $$->inlineItem = new InlineItem( $1->loc, new NameRef(nameRef), InlineItem::Next );
1196         };
1197 inline_block_interpret:
1198         KW_Next '*' inline_expr ';' final {
1199                 $$->inlineItem = new InlineItem( $1->loc, InlineItem::NextExpr );
1200                 $$->inlineItem->children = $3->inlineList;
1201         };
1202 inline_block_interpret:
1203         KW_Call state_ref ';' final {
1204                 $$->inlineItem = new InlineItem( $1->loc, new NameRef(nameRef), InlineItem::Call );
1205         };
1206 inline_block_interpret:
1207         KW_Call '*' inline_expr ';' final {
1208                 $$->inlineItem = new InlineItem( $1->loc, InlineItem::CallExpr );
1209                 $$->inlineItem->children = $3->inlineList;
1210         };
1211 inline_block_interpret:
1212         KW_Ret ';' final {
1213                 $$->inlineItem = new InlineItem( $1->loc, InlineItem::Ret );
1214         };
1215 inline_block_interpret:
1216         KW_Break ';' final {
1217                 $$->inlineItem = new InlineItem( $1->loc, InlineItem::Break );
1218         };
1219
1220 nonterm inline_expr uses inline_list;
1221
1222 inline_expr:
1223         inline_expr inline_expr_item 
1224         final {
1225                 $$->inlineList = $1->inlineList;
1226                 $$->inlineList->append( $2->inlineItem );
1227         };
1228 inline_expr:
1229         final {
1230                 /* Init the list used for this expr. */
1231                 $$->inlineList = new InlineList;
1232         };
1233
1234 nonterm inline_expr_item uses inline_item;
1235
1236 inline_expr_item: 
1237         inline_expr_any 
1238         final {
1239                 /* Return a text segment. */
1240                 $$->inlineItem = new InlineItem( $1->token.loc, $1->token.data, InlineItem::Text );
1241         };
1242 inline_expr_item:
1243         inline_expr_symbol
1244         final {
1245                 /* Return a text segment, must heap alloc the text. */
1246                 $$->inlineItem = new InlineItem( $1->token.loc, $1->token.data, InlineItem::Text );
1247         };
1248 inline_expr_item:
1249         inline_expr_interpret
1250         final{
1251                 /* Pass the inline item up. */
1252                 $$->inlineItem = $1->inlineItem;
1253         };
1254
1255 nonterm inline_expr_any uses token_type;
1256
1257 inline_expr_any: IL_WhiteSpace try { $$->token = *$1; };
1258 inline_expr_any: IL_Comment try { $$->token = *$1; };
1259 inline_expr_any: IL_Literal try { $$->token = *$1; };
1260 inline_expr_any: IL_Symbol try { $$->token = *$1; };
1261 inline_expr_any: TK_UInt try { $$->token = *$1; };
1262 inline_expr_any: TK_Hex try { $$->token = *$1; };
1263 inline_expr_any: TK_Word try { $$->token = *$1; };
1264
1265 # Anything in a ExecValExpr that is not dynamically allocated. This includes
1266 # all special symbols caught in inline code except the semi.
1267
1268 nonterm inline_expr_symbol uses token_type;
1269
1270 inline_expr_symbol: ',' try { $$->token = *$1; };
1271 inline_expr_symbol: '(' try { $$->token = *$1; };
1272 inline_expr_symbol: ')' try { $$->token = *$1; };
1273 inline_expr_symbol: '*' try { $$->token = *$1; };
1274 inline_expr_symbol: TK_NameSep try { $$->token = *$1; };
1275
1276 nonterm inline_expr_interpret uses inline_item;
1277
1278 inline_expr_interpret:
1279         KW_PChar 
1280         final {
1281                 $$->inlineItem = new InlineItem( $1->loc, InlineItem::PChar );
1282         };
1283 inline_expr_interpret:
1284         KW_Char 
1285         final {
1286                 $$->inlineItem = new InlineItem( $1->loc, InlineItem::Char );
1287         };
1288 inline_expr_interpret:
1289         KW_CurState 
1290         final {
1291                 $$->inlineItem = new InlineItem( $1->loc, InlineItem::Curs );
1292         };
1293 inline_expr_interpret:
1294         KW_TargState 
1295         final {
1296                 $$->inlineItem = new InlineItem( $1->loc, InlineItem::Targs );
1297         };
1298 inline_expr_interpret:
1299         KW_Entry '(' state_ref ')' 
1300         final {
1301                 $$->inlineItem = new InlineItem( $1->loc, 
1302                         new NameRef(nameRef), InlineItem::Entry );
1303         };
1304
1305 #  A local state reference. Cannot have :: prefix.
1306 local_state_ref:
1307         no_name_sep state_ref_names;
1308
1309 # Clear the name ref structure.
1310 no_name_sep:
1311         final {
1312                 nameRef.empty();
1313         };
1314
1315 # A qualified state reference.
1316 state_ref: opt_name_sep state_ref_names;
1317
1318 # Optional leading name separator.
1319 opt_name_sep:
1320         TK_NameSep 
1321         final {
1322                 /* Insert an initial null pointer val to indicate the existence of the
1323                  * initial name seperator. */
1324                 nameRef.setAs( 0 );
1325         };
1326 opt_name_sep:
1327         final {
1328                 nameRef.empty();
1329         };
1330
1331 # List of names separated by ::
1332 state_ref_names:
1333         state_ref_names TK_NameSep TK_Word
1334         final {
1335                 nameRef.append( $3->data );
1336         };
1337 state_ref_names:
1338         TK_Word 
1339         final {
1340                 nameRef.append( $1->data );
1341         };
1342
1343 }%%
1344
1345 %%{
1346         write types;
1347         write data;
1348 }%%
1349
1350 void Parser::init()
1351 {
1352         %% write init;
1353 }
1354
1355 int Parser::parseLangEl( int type, const Token *token )
1356 {
1357         %% write exec;
1358         return errCount == 0 ? 0 : -1;
1359 }
1360
1361 void Parser::tryMachineDef( InputLoc &loc, char *name, 
1362                 JoinOrLm *joinOrLm, bool isInstance )
1363 {
1364         GraphDictEl *newEl = pd->graphDict.insert( name );
1365         if ( newEl != 0 ) {
1366                 /* New element in the dict, all good. */
1367                 newEl->value = new VarDef( name, joinOrLm );
1368                 newEl->isInstance = isInstance;
1369                 newEl->loc = loc;
1370
1371                 /* It it is an instance, put on the instance list. */
1372                 if ( isInstance )
1373                         pd->instanceList.append( newEl );
1374         }
1375         else {
1376                 // Recover by ignoring the duplicate.
1377                 error(loc) << "fsm \"" << name << "\" previously defined" << endl;
1378         }
1379 }
1380
1381 ostream &Parser::parse_error( int tokId, Token &token )
1382 {
1383         /* Maintain the error count. */
1384         gblErrorCount += 1;
1385
1386         cerr << token.loc.fileName << ":" << token.loc.line << ":" << token.loc.col << ": ";
1387         cerr << "at token ";
1388         if ( tokId < 128 )
1389                 cerr << "\"" << Parser_lelNames[tokId] << "\"";
1390         else 
1391                 cerr << Parser_lelNames[tokId];
1392         if ( token.data != 0 )
1393                 cerr << " with data \"" << token.data << "\"";
1394         cerr << ": ";
1395         
1396         return cerr;
1397 }
1398
1399 int Parser::token( InputLoc &loc, int tokId, char *tokstart, int toklen )
1400 {
1401         Token token;
1402         token.data = tokstart;
1403         token.length = toklen;
1404         token.loc = loc;
1405         int res = parseLangEl( tokId, &token );
1406         if ( res < 0 ) {
1407                 parse_error(tokId, token) << "parse error" << endl;
1408                 exit(1);
1409         }
1410         return res;
1411 }