tizen 2.3.1 release
[external/ragel.git] / ragel / rlparse.kl
index d6c2c02..36b8777 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  Copyright 2001-2007 Adrian Thurston <thurston@cs.queensu.ca>
+ *  Copyright 2001-2007 Adrian Thurston <thurston@complang.org>
  */
 
 /*  This file is part of Ragel.
@@ -29,8 +29,6 @@ using std::cout;
 using std::cerr;
 using std::endl;
 
-ParserDict parserDict;
-
 %%{
 
 parser Parser;
@@ -54,6 +52,44 @@ statement: getkey_spec commit;
 statement: access_spec commit;
 statement: variable_spec commit;
 statement: export_block commit;
+statement: pre_push_spec commit;
+statement: post_pop_spec commit;
+statement: length_spec commit;
+
+length_spec:
+       KW_Length TK_Word ';'
+       final {
+               LengthDef *lengthDef = new LengthDef( $2->data );
+               pd->lengthDefList.append( lengthDef );
+
+               /* Generic creation of machine for instantiation and assignment. */
+               MachineDef *machineDef = new MachineDef( lengthDef );
+               tryMachineDef( $2->loc, $2->data, machineDef, false );
+       };
+
+pre_push_spec:
+       KW_PrePush '{' inline_block '}' 
+       final {
+               if ( pd->prePushExpr != 0 ) {
+                       /* Recover by just ignoring the duplicate. */
+                       error($2->loc) << "pre_push code already defined" << endl;
+               }
+
+               pd->prePushExpr = $3->inlineList;
+       };
+
+
+post_pop_spec:
+       KW_PostPop '{' inline_block '}' 
+       final {
+               if ( pd->postPopExpr != 0 ) {
+                       /* Recover by just ignoring the duplicate. */
+                       error($2->loc) << "post_pop code already defined" << endl;
+               }
+
+               pd->postPopExpr = $3->inlineList;
+       };
+
 
 export_open: KW_Export 
        final {
@@ -84,20 +120,26 @@ assignment:
                }
 
                /* Generic creation of machine for instantiation and assignment. */
-               JoinOrLm *joinOrLm = new JoinOrLm( $4->join );
-               tryMachineDef( $2->token.loc, $2->token.data, joinOrLm, isInstance );
+               MachineDef *machineDef = new MachineDef( $4->join );
+               tryMachineDef( $2->token.loc, $2->token.data, machineDef, isInstance );
 
                if ( $1->isSet )
                        exportContext.remove( exportContext.length()-1 );
+
+               $4->join->loc = $3->loc;
        };
 
 instantiation: 
        opt_export machine_name TK_ColonEquals join_or_lm ';' final {
                /* Generic creation of machine for instantiation and assignment. */
-               tryMachineDef( $2->token.loc, $2->token.data, $4->joinOrLm, true );
+               tryMachineDef( $2->token.loc, $2->token.data, $4->machineDef, true );
 
                if ( $1->isSet )
                        exportContext.remove( exportContext.length()-1 );
+
+               /* Pass a location to join_or_lm */
+               if ( $4->machineDef->join != 0 )
+                       $4->machineDef->join->loc = $3->loc;
        };
 
 type token_type
@@ -147,7 +189,7 @@ action_spec:
 # semi-colon.
 alphtype_spec:
        KW_AlphType TK_Word TK_Word ';' final {
-               if ( ! pd->setAlphType( $2->data, $3->data ) ) {
+               if ( ! pd->setAlphType( $1->loc, $2->data, $3->data ) ) {
                        // Recover by ignoring the alphtype statement.
                        error($2->loc) << "\"" << $2->data << 
                                        " " << $3->data << "\" is not a valid alphabet type" << endl;
@@ -156,7 +198,7 @@ alphtype_spec:
 
 alphtype_spec:
        KW_AlphType TK_Word ';' final {
-               if ( ! pd->setAlphType( $2->data ) ) {
+               if ( ! pd->setAlphType( $1->loc, $2->data ) ) {
                        // Recover by ignoring the alphtype statement.
                        error($2->loc) << "\"" << $2->data << 
                                        "\" is not a valid alphabet type" << endl;
@@ -200,12 +242,12 @@ opt_whitespace: ;
 
 nonterm join_or_lm
 {
-       JoinOrLm *joinOrLm;
+       MachineDef *machineDef;
 };
 
 join_or_lm: 
        join final {
-               $$->joinOrLm = new JoinOrLm( $1->join );
+               $$->machineDef = new MachineDef( $1->join );
        };
 join_or_lm:
        TK_BarStar lm_part_list '*' '|' final {
@@ -215,7 +257,7 @@ join_or_lm:
                pd->lmList.append( lm );
                for ( LmPartList::Iter lmp = *($2->lmPartList); lmp.lte(); lmp++ )
                        lmp->longestMatch = lm;
-               $$->joinOrLm = new JoinOrLm( lm );
+               $$->machineDef = new MachineDef( lm );
        };
 
 nonterm lm_part_list
@@ -224,13 +266,15 @@ nonterm lm_part_list
 };
 
 lm_part_list:
-       lm_part_list longest_match_part final {
+       lm_part_list longest_match_part
+       final {
                if ( $2->lmPart != 0 ) 
                        $1->lmPartList->append( $2->lmPart );
                $$->lmPartList = $1->lmPartList;
        };
 lm_part_list:
-       longest_match_part final {
+       longest_match_part
+       final {
                /* Create a new list with the part. */
                $$->lmPartList = new LmPartList;
                if ( $1->lmPart != 0 )
@@ -254,6 +298,10 @@ longest_match_part:
                        action->isLmAction = true;
                $$->lmPart = new LongestMatchPart( $1->join, action, 
                                $3->loc, pd->nextLongestMatchId++ );
+
+               /* Provide a location to join. Unfortunately We don't
+                * have the start of the join as in other occurances. Use the end. */
+               $1->join->loc = $3->loc;
        };
 
 nonterm opt_lm_part_action
@@ -994,6 +1042,7 @@ factor:
        '(' join ')' final {
                /* Create a new factor going to a parenthesized join. */
                $$->factor = new Factor( $2->join );
+               $2->join->loc = $1->loc;
        };
 
 nonterm range_lit
@@ -1411,12 +1460,12 @@ int Parser::parseLangEl( int type, const Token *token )
 }
 
 void Parser::tryMachineDef( InputLoc &loc, char *name, 
-               JoinOrLm *joinOrLm, bool isInstance )
+               MachineDef *machineDef, bool isInstance )
 {
        GraphDictEl *newEl = pd->graphDict.insert( name );
        if ( newEl != 0 ) {
                /* New element in the dict, all good. */
-               newEl->value = new VarDef( name, joinOrLm );
+               newEl->value = new VarDef( name, machineDef );
                newEl->isInstance = isInstance;
                newEl->loc = loc;
                newEl->value->isExport = exportContext[exportContext.length()-1];
@@ -1436,7 +1485,7 @@ ostream &Parser::parse_error( int tokId, Token &token )
        /* Maintain the error count. */
        gblErrorCount += 1;
 
-       cerr << token.loc.fileName << ":" << token.loc.line << ":" << token.loc.col << ": ";
+       cerr << token.loc << ": ";
        cerr << "at token ";
        if ( tokId < 128 )
                cerr << "\"" << Parser_lelNames[tokId] << "\"";