2 * Copyright 2001-2007 Adrian Thurston <thurston@complang.org>
5 /* This file is part of Ragel.
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.
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.
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
40 section_list: section_list statement_list TK_EndSection;
43 statement_list: statement_list statement;
46 statement: assignment commit;
47 statement: instantiation commit;
48 statement: action_spec commit;
49 statement: alphtype_spec commit;
50 statement: range_spec commit;
51 statement: getkey_spec commit;
52 statement: access_spec commit;
53 statement: variable_spec commit;
54 statement: export_block commit;
55 statement: pre_push_spec commit;
56 statement: post_pop_spec commit;
59 KW_PrePush '{' inline_block '}'
61 if ( pd->prePushExpr != 0 ) {
62 /* Recover by just ignoring the duplicate. */
63 error($2->loc) << "pre_push code already defined" << endl;
66 pd->prePushExpr = $3->inlineList;
71 KW_PostPop '{' inline_block '}'
73 if ( pd->postPopExpr != 0 ) {
74 /* Recover by just ignoring the duplicate. */
75 error($2->loc) << "post_pop code already defined" << endl;
78 pd->postPopExpr = $3->inlineList;
82 export_open: KW_Export
84 exportContext.append( true );
92 opt_export: export_open final { $$->isSet = true; };
93 opt_export: final { $$->isSet = false; };
95 export_block: export_open '{' statement_list '}'
97 exportContext.remove( exportContext.length()-1 );
101 opt_export machine_name '=' join ';' final {
102 /* Main machine must be an instance. */
103 bool isInstance = false;
104 if ( strcmp($2->token.data, mainMachine) == 0 ) {
105 warning($2->token.loc) <<
106 "main machine will be implicitly instantiated" << endl;
110 /* Generic creation of machine for instantiation and assignment. */
111 JoinOrLm *joinOrLm = new JoinOrLm( $4->join );
112 tryMachineDef( $2->token.loc, $2->token.data, joinOrLm, isInstance );
115 exportContext.remove( exportContext.length()-1 );
117 $4->join->loc = $3->loc;
121 opt_export machine_name TK_ColonEquals join_or_lm ';' final {
122 /* Generic creation of machine for instantiation and assignment. */
123 tryMachineDef( $2->token.loc, $2->token.data, $4->joinOrLm, true );
126 exportContext.remove( exportContext.length()-1 );
128 /* Pass a location to join_or_lm */
129 if ( $4->joinOrLm->join != 0 )
130 $4->joinOrLm->join->loc = $3->loc;
138 nonterm machine_name uses token_type;
142 /* Make/get the priority key. The name may have already been referenced
143 * and therefore exist. */
144 PriorDictEl *priorDictEl;
145 if ( pd->priorDict.insert( $1->data, pd->nextPriorKey, &priorDictEl ) )
146 pd->nextPriorKey += 1;
147 pd->curDefPriorKey = priorDictEl->value;
149 /* Make/get the local error key. */
150 LocalErrDictEl *localErrDictEl;
151 if ( pd->localErrDict.insert( $1->data, pd->nextLocalErrKey, &localErrDictEl ) )
152 pd->nextLocalErrKey += 1;
153 pd->curDefLocalErrKey = localErrDictEl->value;
159 KW_Action TK_Word '{' inline_block '}' final {
160 if ( pd->actionDict.find( $2->data ) ) {
161 /* Recover by just ignoring the duplicate. */
162 error($2->loc) << "action \"" << $2->data << "\" already defined" << endl;
165 //cerr << "NEW ACTION " << $2->data << " " << $4->inlineList << endl;
166 /* Add the action to the list of actions. */
167 Action *newAction = new Action( $3->loc, $2->data,
168 $4->inlineList, pd->nextCondId++ );
170 /* Insert to list and dict. */
171 pd->actionList.append( newAction );
172 pd->actionDict.insert( newAction );
176 # Specifies the data type of the input alphabet. One or two words followed by a
179 KW_AlphType TK_Word TK_Word ';' final {
180 if ( ! pd->setAlphType( $1->loc, $2->data, $3->data ) ) {
181 // Recover by ignoring the alphtype statement.
182 error($2->loc) << "\"" << $2->data <<
183 " " << $3->data << "\" is not a valid alphabet type" << endl;
188 KW_AlphType TK_Word ';' final {
189 if ( ! pd->setAlphType( $1->loc, $2->data ) ) {
190 // Recover by ignoring the alphtype statement.
191 error($2->loc) << "\"" << $2->data <<
192 "\" is not a valid alphabet type" << endl;
196 # Specifies a range to assume that the input characters will fall into.
198 KW_Range alphabet_num alphabet_num ';' final {
199 // Save the upper and lower ends of the range and emit the line number.
200 pd->lowerNum = $2->token.data;
201 pd->upperNum = $3->token.data;
202 pd->rangeLowLoc = $2->token.loc;
203 pd->rangeHighLoc = $3->token.loc;
207 KW_GetKey inline_expr ';' final {
208 pd->getKeyExpr = $2->inlineList;
212 KW_Access inline_expr ';' final {
213 pd->accessExpr = $2->inlineList;
217 KW_Variable opt_whitespace TK_Word inline_expr ';' final {
218 /* FIXME: Need to implement the rest of this. */
219 bool wasSet = pd->setVariable( $3->data, $4->inlineList );
221 error($3->loc) << "bad variable name" << endl;
224 opt_whitespace: opt_whitespace IL_WhiteSpace;
238 $$->joinOrLm = new JoinOrLm( $1->join );
241 TK_BarStar lm_part_list '*' '|' final {
242 /* Create a new factor going to a longest match structure. Record
243 * in the parse data that we have a longest match. */
244 LongestMatch *lm = new LongestMatch( $1->loc, $2->lmPartList );
245 pd->lmList.append( lm );
246 for ( LmPartList::Iter lmp = *($2->lmPartList); lmp.lte(); lmp++ )
247 lmp->longestMatch = lm;
248 $$->joinOrLm = new JoinOrLm( lm );
253 LmPartList *lmPartList;
257 lm_part_list longest_match_part
259 if ( $2->lmPart != 0 )
260 $1->lmPartList->append( $2->lmPart );
261 $$->lmPartList = $1->lmPartList;
266 /* Create a new list with the part. */
267 $$->lmPartList = new LmPartList;
268 if ( $1->lmPart != 0 )
269 $$->lmPartList->append( $1->lmPart );
272 nonterm longest_match_part
274 LongestMatchPart *lmPart;
278 action_spec final { $$->lmPart = 0; };
280 assignment final { $$->lmPart = 0; };
282 join opt_lm_part_action ';' final {
284 Action *action = $2->action;
286 action->isLmAction = true;
287 $$->lmPart = new LongestMatchPart( $1->join, action,
288 $3->loc, pd->nextLongestMatchId++ );
290 /* Provide a location to join. Unfortunately We don't
291 * have the start of the join as in other occurances. Use the end. */
292 $1->join->loc = $3->loc;
295 nonterm opt_lm_part_action
301 TK_DoubleArrow action_embed final {
302 $$->action = $2->action;
305 action_embed_block final {
306 $$->action = $1->action;
320 join ',' expression final {
321 /* Append the expression to the list and return it. */
322 $1->join->exprList.append( $3->expression );
327 $$->join = new Join( $1->expression );
332 Expression *expression;
336 expression '|' term_short final {
337 $$->expression = new Expression( $1->expression,
338 $3->term, Expression::OrType );
341 expression '&' term_short final {
342 $$->expression = new Expression( $1->expression,
343 $3->term, Expression::IntersectType );
346 expression '-' term_short final {
347 $$->expression = new Expression( $1->expression,
348 $3->term, Expression::SubtractType );
351 expression TK_DashDash term_short final {
352 $$->expression = new Expression( $1->expression,
353 $3->term, Expression::StrongSubtractType );
357 $$->expression = new Expression( $1->term );
360 # This is where we resolve the ambiguity involving -. By default ragel tries to
361 # do a longest match, which gives precedence to a concatenation because it is
362 # innermost. What we need is to force term into a shortest match so that when -
363 # is seen it doesn't try to extend term with a concatenation, but ends term and
364 # goes for a subtraction.
366 # The shortest tag overrides the default longest match action ordering strategy
367 # and instead forces a shortest match stragegy. The wrap the term production in
368 # a new nonterminal 'term_short' to guarantee the shortest match behaviour.
387 term factor_with_label final {
388 $$->term = new Term( $1->term, $2->factorWithAug );
391 term '.' factor_with_label final {
392 $$->term = new Term( $1->term, $3->factorWithAug );
395 term TK_ColonGt factor_with_label final {
396 $$->term = new Term( $1->term, $3->factorWithAug, Term::RightStartType );
399 term TK_ColonGtGt factor_with_label final {
400 $$->term = new Term( $1->term, $3->factorWithAug, Term::RightFinishType );
403 term TK_LtColon factor_with_label final {
404 $$->term = new Term( $1->term,
405 $3->factorWithAug, Term::LeftType );
408 factor_with_label final {
409 $$->term = new Term( $1->factorWithAug );
412 nonterm factor_with_label
414 FactorWithAug *factorWithAug;
418 TK_Word ':' factor_with_label final {
419 /* Add the label to the list and pass the factor up. */
420 $3->factorWithAug->labels.prepend( Label($1->loc, $1->data) );
421 $$->factorWithAug = $3->factorWithAug;
424 factor_with_ep final {
425 $$->factorWithAug = $1->factorWithAug;
428 nonterm factor_with_ep
430 FactorWithAug *factorWithAug;
434 factor_with_ep TK_Arrow local_state_ref final {
435 /* Add the target to the list and return the factor object. */
436 $1->factorWithAug->epsilonLinks.append( EpsilonLink( $2->loc, nameRef ) );
437 $$->factorWithAug = $1->factorWithAug;
440 factor_with_aug final {
441 $$->factorWithAug = $1->factorWithAug;
444 nonterm factor_with_aug
446 FactorWithAug *factorWithAug;
450 factor_with_aug aug_type_base action_embed final {
451 /* Append the action to the factorWithAug, record the refernce from
452 * factorWithAug to the action and pass up the factorWithAug. */
453 $1->factorWithAug->actions.append(
454 ParserAction( $2->loc, $2->augType, 0, $3->action ) );
455 $$->factorWithAug = $1->factorWithAug;
458 factor_with_aug aug_type_base priority_aug final {
459 /* Append the named priority to the factorWithAug and pass it up. */
460 $1->factorWithAug->priorityAugs.append(
461 PriorityAug( $2->augType, pd->curDefPriorKey, $3->priorityNum ) );
462 $$->factorWithAug = $1->factorWithAug;
465 factor_with_aug aug_type_base '(' priority_name ',' priority_aug ')' final {
466 /* Append the priority using a default name. */
467 $1->factorWithAug->priorityAugs.append(
468 PriorityAug( $2->augType, $4->priorityName, $6->priorityNum ) );
469 $$->factorWithAug = $1->factorWithAug;
472 factor_with_aug aug_type_cond action_embed final {
473 $1->factorWithAug->conditions.append( ConditionTest( $2->loc,
474 $2->augType, $3->action, true ) );
475 $$->factorWithAug = $1->factorWithAug;
478 factor_with_aug aug_type_cond '!' action_embed final {
479 $1->factorWithAug->conditions.append( ConditionTest( $2->loc,
480 $2->augType, $4->action, false ) );
481 $$->factorWithAug = $1->factorWithAug;
484 factor_with_aug aug_type_to_state action_embed final {
485 /* Append the action, pass it up. */
486 $1->factorWithAug->actions.append( ParserAction( $2->loc,
487 $2->augType, 0, $3->action ) );
488 $$->factorWithAug = $1->factorWithAug;
491 factor_with_aug aug_type_from_state action_embed final {
492 /* Append the action, pass it up. */
493 $1->factorWithAug->actions.append( ParserAction( $2->loc,
494 $2->augType, 0, $3->action ) );
495 $$->factorWithAug = $1->factorWithAug;
498 factor_with_aug aug_type_eof action_embed final {
499 /* Append the action, pass it up. */
500 $1->factorWithAug->actions.append( ParserAction( $2->loc,
501 $2->augType, 0, $3->action ) );
502 $$->factorWithAug = $1->factorWithAug;
505 factor_with_aug aug_type_gbl_error action_embed final {
506 /* Append the action to the factorWithAug, record the refernce from
507 * factorWithAug to the action and pass up the factorWithAug. */
508 $1->factorWithAug->actions.append( ParserAction( $2->loc,
509 $2->augType, pd->curDefLocalErrKey, $3->action ) );
510 $$->factorWithAug = $1->factorWithAug;
513 factor_with_aug aug_type_local_error action_embed final {
514 /* Append the action to the factorWithAug, record the refernce from
515 * factorWithAug to the action and pass up the factorWithAug. */
516 $1->factorWithAug->actions.append( ParserAction( $2->loc,
517 $2->augType, pd->curDefLocalErrKey, $3->action ) );
518 $$->factorWithAug = $1->factorWithAug;
521 factor_with_aug aug_type_local_error '(' local_err_name ',' action_embed ')' final {
522 /* Append the action to the factorWithAug, record the refernce from
523 * factorWithAug to the action and pass up the factorWithAug. */
524 $1->factorWithAug->actions.append( ParserAction( $2->loc,
525 $2->augType, $4->error_name, $6->action ) );
526 $$->factorWithAug = $1->factorWithAug;
529 factor_with_rep final {
530 $$->factorWithAug = new FactorWithAug( $1->factorWithRep );
539 # Classes of transtions on which to embed actions or change priorities.
540 nonterm aug_type_base uses aug_type;
542 aug_type_base: '@' final { $$->loc = $1->loc; $$->augType = at_finish; };
543 aug_type_base: '%' final { $$->loc = $1->loc; $$->augType = at_leave; };
544 aug_type_base: '$' final { $$->loc = $1->loc; $$->augType = at_all; };
545 aug_type_base: '>' final { $$->loc = $1->loc; $$->augType = at_start; };
547 # Embedding conditions.
548 nonterm aug_type_cond uses aug_type;
550 aug_type_cond: TK_StartCond final { $$->loc = $1->loc; $$->augType = at_start; };
551 aug_type_cond: '>' KW_When final { $$->loc = $1->loc; $$->augType = at_start; };
552 aug_type_cond: TK_AllCond final { $$->loc = $1->loc; $$->augType = at_all; };
553 aug_type_cond: '$' KW_When final { $$->loc = $1->loc; $$->augType = at_all; };
554 aug_type_cond: TK_LeavingCond final { $$->loc = $1->loc; $$->augType = at_leave; };
555 aug_type_cond: '%' KW_When final { $$->loc = $1->loc; $$->augType = at_leave; };
556 aug_type_cond: KW_When final { $$->loc = $1->loc; $$->augType = at_all; };
557 aug_type_cond: KW_InWhen final { $$->loc = $1->loc; $$->augType = at_start; };
558 aug_type_cond: KW_OutWhen final { $$->loc = $1->loc; $$->augType = at_leave; };
564 nonterm aug_type_to_state uses aug_type;
566 aug_type_to_state: TK_StartToState
567 final { $$->loc = $1->loc; $$->augType = at_start_to_state; };
568 aug_type_to_state: '>' KW_To
569 final { $$->loc = $1->loc; $$->augType = at_start_to_state; };
571 aug_type_to_state: TK_NotStartToState
572 final { $$->loc = $1->loc; $$->augType = at_not_start_to_state; };
573 aug_type_to_state: '<' KW_To
574 final { $$->loc = $1->loc; $$->augType = at_not_start_to_state; };
576 aug_type_to_state: TK_AllToState
577 final { $$->loc = $1->loc; $$->augType = at_all_to_state; };
578 aug_type_to_state: '$' KW_To
579 final { $$->loc = $1->loc; $$->augType = at_all_to_state; };
581 aug_type_to_state: TK_FinalToState
582 final { $$->loc = $1->loc; $$->augType = at_final_to_state; };
583 aug_type_to_state: '%' KW_To
584 final { $$->loc = $1->loc; $$->augType = at_final_to_state; };
586 aug_type_to_state: TK_NotFinalToState
587 final { $$->loc = $1->loc; $$->augType = at_not_final_to_state; };
588 aug_type_to_state: '@' KW_To
589 final { $$->loc = $1->loc; $$->augType = at_not_final_to_state; };
591 aug_type_to_state: TK_MiddleToState
592 final { $$->loc = $1->loc; $$->augType = at_middle_to_state; };
593 aug_type_to_state: TK_Middle KW_To
594 final { $$->loc = $1->loc; $$->augType = at_middle_to_state; };
597 # From state actions.
600 nonterm aug_type_from_state uses aug_type;
602 aug_type_from_state: TK_StartFromState
603 final { $$->loc = $1->loc; $$->augType = at_start_from_state; };
604 aug_type_from_state: '>' KW_From
605 final { $$->loc = $1->loc; $$->augType = at_start_from_state; };
607 aug_type_from_state: TK_NotStartFromState
608 final { $$->loc = $1->loc; $$->augType = at_not_start_from_state; };
609 aug_type_from_state: '<' KW_From
610 final { $$->loc = $1->loc; $$->augType = at_not_start_from_state; };
612 aug_type_from_state: TK_AllFromState
613 final { $$->loc = $1->loc; $$->augType = at_all_from_state; };
614 aug_type_from_state: '$' KW_From
615 final { $$->loc = $1->loc; $$->augType = at_all_from_state; };
617 aug_type_from_state: TK_FinalFromState
618 final { $$->loc = $1->loc; $$->augType = at_final_from_state; };
619 aug_type_from_state: '%' KW_From
620 final { $$->loc = $1->loc; $$->augType = at_final_from_state; };
622 aug_type_from_state: TK_NotFinalFromState
623 final { $$->loc = $1->loc; $$->augType = at_not_final_from_state; };
624 aug_type_from_state: '@' KW_From
625 final { $$->loc = $1->loc; $$->augType = at_not_final_from_state; };
627 aug_type_from_state: TK_MiddleFromState
628 final { $$->loc = $1->loc; $$->augType = at_middle_from_state; };
629 aug_type_from_state: TK_Middle KW_From
630 final { $$->loc = $1->loc; $$->augType = at_middle_from_state; };
636 nonterm aug_type_eof uses aug_type;
638 aug_type_eof: TK_StartEOF
639 final { $$->loc = $1->loc; $$->augType = at_start_eof; };
640 aug_type_eof: '>' KW_Eof
641 final { $$->loc = $1->loc; $$->augType = at_start_eof; };
643 aug_type_eof: TK_NotStartEOF
644 final { $$->loc = $1->loc; $$->augType = at_not_start_eof; };
645 aug_type_eof: '<' KW_Eof
646 final { $$->loc = $1->loc; $$->augType = at_not_start_eof; };
648 aug_type_eof: TK_AllEOF
649 final { $$->loc = $1->loc; $$->augType = at_all_eof; };
650 aug_type_eof: '$' KW_Eof
651 final { $$->loc = $1->loc; $$->augType = at_all_eof; };
653 aug_type_eof: TK_FinalEOF
654 final { $$->loc = $1->loc; $$->augType = at_final_eof; };
655 aug_type_eof: '%' KW_Eof
656 final { $$->loc = $1->loc; $$->augType = at_final_eof; };
658 aug_type_eof: TK_NotFinalEOF
659 final { $$->loc = $1->loc; $$->augType = at_not_final_eof; };
660 aug_type_eof: '@' KW_Eof
661 final { $$->loc = $1->loc; $$->augType = at_not_final_eof; };
663 aug_type_eof: TK_MiddleEOF
664 final { $$->loc = $1->loc; $$->augType = at_middle_eof; };
665 aug_type_eof: TK_Middle KW_Eof
666 final { $$->loc = $1->loc; $$->augType = at_middle_eof; };
669 # Global error actions.
672 nonterm aug_type_gbl_error uses aug_type;
674 aug_type_gbl_error: TK_StartGblError
675 final { $$->loc = $1->loc; $$->augType = at_start_gbl_error; };
676 aug_type_gbl_error: '>' KW_Err
677 final { $$->loc = $1->loc; $$->augType = at_start_gbl_error; };
679 aug_type_gbl_error: TK_NotStartGblError
680 final { $$->loc = $1->loc; $$->augType = at_not_start_gbl_error; };
681 aug_type_gbl_error: '<' KW_Err
682 final { $$->loc = $1->loc; $$->augType = at_not_start_gbl_error; };
684 aug_type_gbl_error: TK_AllGblError
685 final { $$->loc = $1->loc; $$->augType = at_all_gbl_error; };
686 aug_type_gbl_error: '$' KW_Err
687 final { $$->loc = $1->loc; $$->augType = at_all_gbl_error; };
689 aug_type_gbl_error: TK_FinalGblError
690 final { $$->loc = $1->loc; $$->augType = at_final_gbl_error; };
691 aug_type_gbl_error: '%' KW_Err
692 final { $$->loc = $1->loc; $$->augType = at_final_gbl_error; };
694 aug_type_gbl_error: TK_NotFinalGblError
695 final { $$->loc = $1->loc; $$->augType = at_not_final_gbl_error; };
696 aug_type_gbl_error: '@' KW_Err
697 final { $$->loc = $1->loc; $$->augType = at_not_final_gbl_error; };
699 aug_type_gbl_error: TK_MiddleGblError
700 final { $$->loc = $1->loc; $$->augType = at_middle_gbl_error; };
701 aug_type_gbl_error: TK_Middle KW_Err
702 final { $$->loc = $1->loc; $$->augType = at_middle_gbl_error; };
706 # Local error actions.
709 nonterm aug_type_local_error uses aug_type;
711 aug_type_local_error: TK_StartLocalError
712 final { $$->loc = $1->loc; $$->augType = at_start_local_error; };
713 aug_type_local_error: '>' KW_Lerr
714 final { $$->loc = $1->loc; $$->augType = at_start_local_error; };
716 aug_type_local_error: TK_NotStartLocalError
717 final { $$->loc = $1->loc; $$->augType = at_not_start_local_error; };
718 aug_type_local_error: '<' KW_Lerr
719 final { $$->loc = $1->loc; $$->augType = at_not_start_local_error; };
721 aug_type_local_error: TK_AllLocalError
722 final { $$->loc = $1->loc; $$->augType = at_all_local_error; };
723 aug_type_local_error: '$' KW_Lerr
724 final { $$->loc = $1->loc; $$->augType = at_all_local_error; };
726 aug_type_local_error: TK_FinalLocalError
727 final { $$->loc = $1->loc; $$->augType = at_final_local_error; };
728 aug_type_local_error: '%' KW_Lerr
729 final { $$->loc = $1->loc; $$->augType = at_final_local_error; };
731 aug_type_local_error: TK_NotFinalLocalError
732 final { $$->loc = $1->loc; $$->augType = at_not_final_local_error; };
733 aug_type_local_error: '@' KW_Lerr
734 final { $$->loc = $1->loc; $$->augType = at_not_final_local_error; };
736 aug_type_local_error: TK_MiddleLocalError
737 final { $$->loc = $1->loc; $$->augType = at_middle_local_error; };
738 aug_type_local_error: TK_Middle KW_Lerr
739 final { $$->loc = $1->loc; $$->augType = at_middle_local_error; };
747 # Different ways to embed actions. A TK_Word is reference to an action given by
748 # the user as a statement in the fsm specification. An action can also be
749 # specified immediately.
750 nonterm action_embed uses action_ref;
752 action_embed: action_embed_word final { $$->action = $1->action; };
753 action_embed: '(' action_embed_word ')' final { $$->action = $2->action; };
754 action_embed: action_embed_block final { $$->action = $1->action; };
756 nonterm action_embed_word uses action_ref;
760 /* Set the name in the actionDict. */
761 Action *action = pd->actionDict.find( $1->data );
763 /* Pass up the action element */
767 /* Will recover by returning null as the action. */
768 error($1->loc) << "action lookup of \"" << $1->data << "\" failed" << endl;
773 nonterm action_embed_block uses action_ref;
776 '{' inline_block '}' final {
777 /* Create the action, add it to the list and pass up. */
778 Action *newAction = new Action( $1->loc, 0, $2->inlineList, pd->nextCondId++ );
779 pd->actionList.append( newAction );
780 $$->action = newAction;
783 nonterm priority_name
788 # A specified priority name. Looks up the name in the current priority
792 // Lookup/create the priority key.
793 PriorDictEl *priorDictEl;
794 if ( pd->priorDict.insert( $1->data, pd->nextPriorKey, &priorDictEl ) )
795 pd->nextPriorKey += 1;
797 // Use the inserted/found priority key.
798 $$->priorityName = priorDictEl->value;
806 # Priority change specs.
808 priority_aug_num final {
809 // Convert the priority number to a long. Check for overflow.
811 //cerr << "PRIOR AUG: " << $1->token.data << endl;
812 long aug = strtol( $1->token.data, 0, 10 );
813 if ( errno == ERANGE && aug == LONG_MAX ) {
814 /* Priority number too large. Recover by setting the priority to 0. */
815 error($1->token.loc) << "priority number " << $1->token.data <<
816 " overflows" << endl;
819 else if ( errno == ERANGE && aug == LONG_MIN ) {
820 /* Priority number too large in the neg. Recover by using 0. */
821 error($1->token.loc) << "priority number " << $1->token.data <<
822 " underflows" << endl;
826 /* No overflow or underflow. */
827 $$->priorityNum = aug;
831 nonterm priority_aug_num uses token_type;
839 $$->token.set( "+", 1 );
840 $$->token.loc = $1->loc;
841 $$->token.append( *$2 );
845 $$->token.set( "-", 1 );
846 $$->token.loc = $1->loc;
847 $$->token.append( *$2 );
850 nonterm local_err_name
857 /* Lookup/create the priority key. */
858 LocalErrDictEl *localErrDictEl;
859 if ( pd->localErrDict.insert( $1->data, pd->nextLocalErrKey, &localErrDictEl ) )
860 pd->nextLocalErrKey += 1;
862 /* Use the inserted/found priority key. */
863 $$->error_name = localErrDictEl->value;
868 # The fourth level of precedence. These are the trailing unary operators that
869 # allow for repetition.
871 nonterm factor_with_rep
873 FactorWithRep *factorWithRep;
877 factor_with_rep '*' final {
878 $$->factorWithRep = new FactorWithRep( $2->loc, $1->factorWithRep,
879 0, 0, FactorWithRep::StarType );
882 factor_with_rep TK_StarStar final {
883 $$->factorWithRep = new FactorWithRep( $2->loc, $1->factorWithRep,
884 0, 0, FactorWithRep::StarStarType );
887 factor_with_rep '?' final {
888 $$->factorWithRep = new FactorWithRep( $2->loc, $1->factorWithRep,
889 0, 0, FactorWithRep::OptionalType );
892 factor_with_rep '+' final {
893 $$->factorWithRep = new FactorWithRep( $2->loc, $1->factorWithRep,
894 0, 0, FactorWithRep::PlusType );
897 factor_with_rep '{' factor_rep_num '}' final {
898 $$->factorWithRep = new FactorWithRep( $2->loc, $1->factorWithRep,
899 $3->rep, 0, FactorWithRep::ExactType );
902 factor_with_rep '{' ',' factor_rep_num '}' final {
903 $$->factorWithRep = new FactorWithRep( $2->loc, $1->factorWithRep,
904 0, $4->rep, FactorWithRep::MaxType );
907 factor_with_rep '{' factor_rep_num ',' '}' final {
908 $$->factorWithRep = new FactorWithRep( $2->loc, $1->factorWithRep,
909 $3->rep, 0, FactorWithRep::MinType );
912 factor_with_rep '{' factor_rep_num ',' factor_rep_num '}' final {
913 $$->factorWithRep = new FactorWithRep( $2->loc, $1->factorWithRep,
914 $3->rep, $5->rep, FactorWithRep::RangeType );
917 factor_with_neg final {
918 $$->factorWithRep = new FactorWithRep( $1->factorWithNeg );
921 nonterm factor_rep_num
928 // Convert the priority number to a long. Check for overflow.
930 long rep = strtol( $1->data, 0, 10 );
931 if ( errno == ERANGE && rep == LONG_MAX ) {
932 // Repetition too large. Recover by returing repetition 1. */
933 error($1->loc) << "repetition number " << $1->data << " overflows" << endl;
937 // Cannot be negative, so no overflow.
944 # The fifth level up in precedence. Negation.
947 nonterm factor_with_neg
949 FactorWithNeg *factorWithNeg;
953 '!' factor_with_neg final {
954 $$->factorWithNeg = new FactorWithNeg( $1->loc,
955 $2->factorWithNeg, FactorWithNeg::NegateType );
958 '^' factor_with_neg final {
959 $$->factorWithNeg = new FactorWithNeg( $1->loc,
960 $2->factorWithNeg, FactorWithNeg::CharNegateType );
964 $$->factorWithNeg = new FactorWithNeg( $1->factor );
974 /* Create a new factor node going to a concat literal. */
975 $$->factor = new Factor( new Literal( *$1, Literal::LitString ) );
979 /* Create a new factor node going to a literal number. */
980 $$->factor = new Factor( new Literal( $1->token, Literal::Number ) );
984 /* Find the named graph. */
985 GraphDictEl *gdNode = pd->graphDict.find( $1->data );
987 /* Recover by returning null as the factor node. */
988 error($1->loc) << "graph lookup of \"" << $1->data << "\" failed" << endl;
991 else if ( gdNode->isInstance ) {
992 /* Recover by retuning null as the factor node. */
993 error($1->loc) << "references to graph instantiations not allowed "
994 "in expressions" << endl;
998 /* Create a factor node that is a lookup of an expression. */
999 $$->factor = new Factor( $1->loc, gdNode->value );
1003 RE_SqOpen regular_expr_or_data RE_SqClose final {
1004 /* Create a new factor node going to an OR expression. */
1005 $$->factor = new Factor( new ReItem( $1->loc, $2->reOrBlock, ReItem::OrBlock ) );
1008 RE_SqOpenNeg regular_expr_or_data RE_SqClose final {
1009 /* Create a new factor node going to a negated OR expression. */
1010 $$->factor = new Factor( new ReItem( $1->loc, $2->reOrBlock, ReItem::NegOrBlock ) );
1013 RE_Slash regular_expr RE_Slash final {
1014 if ( $3->length > 1 ) {
1015 for ( char *p = $3->data; *p != 0; p++ ) {
1017 $2->regExpr->caseInsensitive = true;
1021 /* Create a new factor node going to a regular exp. */
1022 $$->factor = new Factor( $2->regExpr );
1025 range_lit TK_DotDot range_lit final {
1026 /* Create a new factor node going to a range. */
1027 $$->factor = new Factor( new Range( $1->literal, $3->literal ) );
1030 '(' join ')' final {
1031 /* Create a new factor going to a parenthesized join. */
1032 $$->factor = new Factor( $2->join );
1033 $2->join->loc = $1->loc;
1041 # Literals which can be the end points of ranges.
1044 /* Range literas must have only one char. We restrict this in the parse tree. */
1045 $$->literal = new Literal( *$1, Literal::LitString );
1048 alphabet_num final {
1049 /* Create a new literal number. */
1050 $$->literal = new Literal( $1->token, Literal::Number );
1053 nonterm alphabet_num uses token_type;
1055 # Any form of a number that can be used as a basic machine. */
1062 $$->token.set( "-", 1 );
1063 $$->token.loc = $1->loc;
1064 $$->token.append( *$2 );
1071 # Regular Expressions.
1074 nonterm regular_expr
1079 # Parser for regular expression fsms. Any number of expression items which
1080 # generally gives a machine one character long or one character long stared.
1082 regular_expr regular_expr_item final {
1083 /* An optimization to lessen the tree size. If a non-starred char is
1084 * directly under the left side on the right and the right side is
1085 * another non-starred char then paste them together and return the
1086 * left side. Otherwise just put the two under a new reg exp node. */
1087 if ( $2->reItem->type == ReItem::Data && !$2->reItem->star &&
1088 $1->regExpr->type == RegExpr::RecurseItem &&
1089 $1->regExpr->item->type == ReItem::Data && !$1->regExpr->item->star )
1091 /* Append the right side to the right side of the left and toss the
1093 $1->regExpr->item->token.append( $2->reItem->token );
1095 $$->regExpr = $1->regExpr;
1098 $$->regExpr = new RegExpr( $1->regExpr, $2->reItem );
1103 /* Can't optimize the tree. */
1104 $$->regExpr = new RegExpr();
1107 nonterm regular_expr_item
1112 # RegularExprItems can be a character spec with an optional staring of the char.
1114 regular_expr_char RE_Star final {
1115 $1->reItem->star = true;
1116 $$->reItem = $1->reItem;
1119 regular_expr_char final {
1120 $$->reItem = $1->reItem;
1123 nonterm regular_expr_char
1128 # A character spec can be a set of characters inside of square parenthesis, a
1129 # dot specifying any character or some explicitly stated character.
1131 RE_SqOpen regular_expr_or_data RE_SqClose final {
1132 $$->reItem = new ReItem( $1->loc, $2->reOrBlock, ReItem::OrBlock );
1135 RE_SqOpenNeg regular_expr_or_data RE_SqClose final {
1136 $$->reItem = new ReItem( $1->loc, $2->reOrBlock, ReItem::NegOrBlock );
1140 $$->reItem = new ReItem( $1->loc, ReItem::Dot );
1144 $$->reItem = new ReItem( $1->loc, *$1 );
1147 # The data inside of a [] expression in a regular expression. Accepts any
1148 # number of characters or ranges. */
1149 nonterm regular_expr_or_data
1151 ReOrBlock *reOrBlock;
1154 regular_expr_or_data:
1155 regular_expr_or_data regular_expr_or_char final {
1156 /* An optimization to lessen the tree size. If an or char is directly
1157 * under the left side on the right and the right side is another or
1158 * char then paste them together and return the left side. Otherwise
1159 * just put the two under a new or data node. */
1160 if ( $2->reOrItem->type == ReOrItem::Data &&
1161 $1->reOrBlock->type == ReOrBlock::RecurseItem &&
1162 $1->reOrBlock->item->type == ReOrItem::Data )
1164 /* Append the right side to right side of the left and toss the
1166 $1->reOrBlock->item->token.append( $2->reOrItem->token );
1167 delete $2->reOrItem;
1168 $$->reOrBlock = $1->reOrBlock;
1171 /* Can't optimize, put the left and right under a new node. */
1172 $$->reOrBlock = new ReOrBlock( $1->reOrBlock, $2->reOrItem );
1175 regular_expr_or_data:
1177 $$->reOrBlock = new ReOrBlock();
1180 # A single character inside of an or expression. Can either be a character or a
1181 # set of characters.
1182 nonterm regular_expr_or_char
1187 regular_expr_or_char:
1189 $$->reOrItem = new ReOrItem( $1->loc, *$1 );
1191 regular_expr_or_char:
1192 RE_Char RE_Dash RE_Char final {
1193 $$->reOrItem = new ReOrItem( $2->loc, $1->data[0], $3->data[0] );
1197 # Inline Lists for inline host code.
1202 InlineList *inlineList;
1205 nonterm inline_block uses inline_list;
1208 inline_block inline_block_item
1210 /* Append the item to the list, return the list. */
1211 $$->inlineList = $1->inlineList;
1212 $$->inlineList->append( $2->inlineItem );
1217 /* Start with empty list. */
1218 $$->inlineList = new InlineList;
1223 InlineItem *inlineItem;
1226 nonterm inline_block_item uses inline_item;
1227 nonterm inline_block_interpret uses inline_item;
1232 $$->inlineItem = new InlineItem( $1->token.loc, $1->token.data, InlineItem::Text );
1238 $$->inlineItem = new InlineItem( $1->token.loc, $1->token.data, InlineItem::Text );
1242 inline_block_interpret
1244 /* Pass the inline item up. */
1245 $$->inlineItem = $1->inlineItem;
1248 nonterm inline_block_symbol uses token_type;
1250 inline_block_symbol: ',' final { $$->token = *$1; };
1251 inline_block_symbol: ';' final { $$->token = *$1; };
1252 inline_block_symbol: '(' final { $$->token = *$1; };
1253 inline_block_symbol: ')' final { $$->token = *$1; };
1254 inline_block_symbol: '*' final { $$->token = *$1; };
1255 inline_block_symbol: TK_NameSep final { $$->token = *$1; };
1257 # Interpreted statements in a struct block. */
1258 inline_block_interpret:
1259 inline_expr_interpret final {
1260 /* Pass up interpreted items of inline expressions. */
1261 $$->inlineItem = $1->inlineItem;
1263 inline_block_interpret:
1265 $$->inlineItem = new InlineItem( $1->loc, InlineItem::Hold );
1267 inline_block_interpret:
1268 KW_Exec inline_expr ';' final {
1269 $$->inlineItem = new InlineItem( $1->loc, InlineItem::Exec );
1270 $$->inlineItem->children = $2->inlineList;
1272 inline_block_interpret:
1273 KW_Goto state_ref ';' final {
1274 $$->inlineItem = new InlineItem( $1->loc,
1275 new NameRef(nameRef), InlineItem::Goto );
1277 inline_block_interpret:
1278 KW_Goto '*' inline_expr ';' final {
1279 $$->inlineItem = new InlineItem( $1->loc, InlineItem::GotoExpr );
1280 $$->inlineItem->children = $3->inlineList;
1282 inline_block_interpret:
1283 KW_Next state_ref ';' final {
1284 $$->inlineItem = new InlineItem( $1->loc, new NameRef(nameRef), InlineItem::Next );
1286 inline_block_interpret:
1287 KW_Next '*' inline_expr ';' final {
1288 $$->inlineItem = new InlineItem( $1->loc, InlineItem::NextExpr );
1289 $$->inlineItem->children = $3->inlineList;
1291 inline_block_interpret:
1292 KW_Call state_ref ';' final {
1293 $$->inlineItem = new InlineItem( $1->loc, new NameRef(nameRef), InlineItem::Call );
1295 inline_block_interpret:
1296 KW_Call '*' inline_expr ';' final {
1297 $$->inlineItem = new InlineItem( $1->loc, InlineItem::CallExpr );
1298 $$->inlineItem->children = $3->inlineList;
1300 inline_block_interpret:
1302 $$->inlineItem = new InlineItem( $1->loc, InlineItem::Ret );
1304 inline_block_interpret:
1305 KW_Break ';' final {
1306 $$->inlineItem = new InlineItem( $1->loc, InlineItem::Break );
1309 nonterm inline_expr uses inline_list;
1312 inline_expr inline_expr_item
1314 $$->inlineList = $1->inlineList;
1315 $$->inlineList->append( $2->inlineItem );
1319 /* Init the list used for this expr. */
1320 $$->inlineList = new InlineList;
1323 nonterm inline_expr_item uses inline_item;
1328 /* Return a text segment. */
1329 $$->inlineItem = new InlineItem( $1->token.loc, $1->token.data, InlineItem::Text );
1334 /* Return a text segment, must heap alloc the text. */
1335 $$->inlineItem = new InlineItem( $1->token.loc, $1->token.data, InlineItem::Text );
1338 inline_expr_interpret
1340 /* Pass the inline item up. */
1341 $$->inlineItem = $1->inlineItem;
1344 nonterm inline_expr_any uses token_type;
1346 inline_expr_any: IL_WhiteSpace try { $$->token = *$1; };
1347 inline_expr_any: IL_Comment try { $$->token = *$1; };
1348 inline_expr_any: IL_Literal try { $$->token = *$1; };
1349 inline_expr_any: IL_Symbol try { $$->token = *$1; };
1350 inline_expr_any: TK_UInt try { $$->token = *$1; };
1351 inline_expr_any: TK_Hex try { $$->token = *$1; };
1352 inline_expr_any: TK_Word try { $$->token = *$1; };
1354 # Anything in a ExecValExpr that is not dynamically allocated. This includes
1355 # all special symbols caught in inline code except the semi.
1357 nonterm inline_expr_symbol uses token_type;
1359 inline_expr_symbol: ',' try { $$->token = *$1; };
1360 inline_expr_symbol: '(' try { $$->token = *$1; };
1361 inline_expr_symbol: ')' try { $$->token = *$1; };
1362 inline_expr_symbol: '*' try { $$->token = *$1; };
1363 inline_expr_symbol: TK_NameSep try { $$->token = *$1; };
1365 nonterm inline_expr_interpret uses inline_item;
1367 inline_expr_interpret:
1370 $$->inlineItem = new InlineItem( $1->loc, InlineItem::PChar );
1372 inline_expr_interpret:
1375 $$->inlineItem = new InlineItem( $1->loc, InlineItem::Char );
1377 inline_expr_interpret:
1380 $$->inlineItem = new InlineItem( $1->loc, InlineItem::Curs );
1382 inline_expr_interpret:
1385 $$->inlineItem = new InlineItem( $1->loc, InlineItem::Targs );
1387 inline_expr_interpret:
1388 KW_Entry '(' state_ref ')'
1390 $$->inlineItem = new InlineItem( $1->loc,
1391 new NameRef(nameRef), InlineItem::Entry );
1394 # A local state reference. Cannot have :: prefix.
1396 no_name_sep state_ref_names;
1398 # Clear the name ref structure.
1404 # A qualified state reference.
1405 state_ref: opt_name_sep state_ref_names;
1407 # Optional leading name separator.
1411 /* Insert an initial null pointer val to indicate the existence of the
1412 * initial name seperator. */
1420 # List of names separated by ::
1422 state_ref_names TK_NameSep TK_Word
1424 nameRef.append( $3->data );
1429 nameRef.append( $1->data );
1444 int Parser::parseLangEl( int type, const Token *token )
1447 return errCount == 0 ? 0 : -1;
1450 void Parser::tryMachineDef( InputLoc &loc, char *name,
1451 JoinOrLm *joinOrLm, bool isInstance )
1453 GraphDictEl *newEl = pd->graphDict.insert( name );
1455 /* New element in the dict, all good. */
1456 newEl->value = new VarDef( name, joinOrLm );
1457 newEl->isInstance = isInstance;
1459 newEl->value->isExport = exportContext[exportContext.length()-1];
1461 /* It it is an instance, put on the instance list. */
1463 pd->instanceList.append( newEl );
1466 // Recover by ignoring the duplicate.
1467 error(loc) << "fsm \"" << name << "\" previously defined" << endl;
1471 ostream &Parser::parse_error( int tokId, Token &token )
1473 /* Maintain the error count. */
1476 cerr << token.loc << ": ";
1477 cerr << "at token ";
1479 cerr << "\"" << Parser_lelNames[tokId] << "\"";
1481 cerr << Parser_lelNames[tokId];
1482 if ( token.data != 0 )
1483 cerr << " with data \"" << token.data << "\"";
1489 int Parser::token( InputLoc &loc, int tokId, char *tokstart, int toklen )
1492 token.data = tokstart;
1493 token.length = toklen;
1495 int res = parseLangEl( tokId, &token );
1497 parse_error(tokId, token) << "parse error" << endl;