Line directives need to use the fileName stored in the InputLoc stuctures from
[external/ragel.git] / ragel / parsedata.cpp
index 6abe995..8ce89db 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  Copyright 2001-2006 Adrian Thurston <thurston@cs.queensu.ca>
+ *  Copyright 2001-2008 Adrian Thurston <thurston@complang.org>
  */
 
 /*  This file is part of Ragel.
 #include "mergesort.h"
 #include "xmlcodegen.h"
 #include "version.h"
+#include "inputdata.h"
 
 using namespace std;
 
 char mainMachine[] = "main";
 
-void Token::set( char *str, int len )
+void Token::set( const char *str, int len )
 {
        length = len;
        data = new char[len+1];
@@ -61,7 +62,7 @@ void Token::append( const Token &other )
 void afterOpMinimize( FsmAp *fsm, bool lastInSeq )
 {
        /* Switch on the prefered minimization algorithm. */
-       if ( minimizeOpt == MinimizeEveryOp || minimizeOpt == MinimizeMostOps && lastInSeq ) {
+       if ( minimizeOpt == MinimizeEveryOp || ( minimizeOpt == MinimizeMostOps && lastInSeq ) ) {
                /* First clean up the graph. FsmAp operations may leave these
                 * lying around. There should be no dead end states. The subtract
                 * intersection operators are the only places where they may be
@@ -108,13 +109,13 @@ Key makeFsmKeyHex( char *str, const InputLoc &loc, ParseData *pd )
 
        unsigned long ul = strtoul( str, 0, 16 );
 
-       if ( errno == ERANGE || unusedBits && ul >> (size * 8) ) {
+       if ( errno == ERANGE || ( unusedBits && ul >> (size * 8) ) ) {
                error(loc) << "literal " << str << " overflows the alphabet type" << endl;
                ul = 1 << (size * 8);
        }
 
        if ( unusedBits && keyOps->alphType->isSigned && ul >> (size * 8 - 1) )
-               ul |= (0xffffffff >> (size*8 ) ) << (size*8);
+               ul |= ( -1L >> (size*8) ) << (size*8);
 
        return Key( (long)ul );
 }
@@ -130,12 +131,12 @@ Key makeFsmKeyDec( char *str, const InputLoc &loc, ParseData *pd )
        long long ll = strtoll( str, 0, 10 );
 
        /* Check for underflow. */
-       if ( errno == ERANGE && ll < 0 || ll < minVal) {
+       if ( ( errno == ERANGE && ll < 0 ) || ll < minVal) {
                error(loc) << "literal " << str << " underflows the alphabet type" << endl;
                ll = minVal;
        }
        /* Check for overflow. */
-       else if ( errno == ERANGE && ll > 0 || ll > maxVal ) {
+       else if ( ( errno == ERANGE && ll > 0 ) || ll > maxVal ) {
                error(loc) << "literal " << str << " overflows the alphabet type" << endl;
                ll = maxVal;
        }
@@ -416,7 +417,7 @@ bool NameInst::anyRefsRec()
 
 /* Initialize the structure that will collect info during the parse of a
  * machine. */
-ParseData::ParseData( char *fileName, char *sectionName, 
+ParseData::ParseData( const char *fileName, char *sectionName, 
                const InputLoc &sectionLoc )
 :      
        sectionGraph(0),
@@ -429,8 +430,11 @@ ParseData::ParseData( char *fileName, char *sectionName,
        alphTypeSet(false),
        getKeyExpr(0),
        accessExpr(0),
+       prePushExpr(0),
+       postPopExpr(0),
        pExpr(0),
        peExpr(0),
+       eofExpr(0),
        csExpr(0),
        topExpr(0),
        stackExpr(0),
@@ -449,7 +453,8 @@ ParseData::ParseData( char *fileName, char *sectionName,
        exportsRootName(0),
        nextEpsilonResolvedLink(0),
        nextLongestMatchId(1),
-       lmRequiresErrorState(false)
+       lmRequiresErrorState(false),
+       cgd(0)
 {
        /* Initialize the dictionary of graphs. This is our symbol table. The
         * initialization needs to be done on construction which happens at the
@@ -468,7 +473,7 @@ ParseData::~ParseData()
 
 /* Make a name id in the current name instantiation scope if it is not
  * already there. */
-NameInst *ParseData::addNameInst( const InputLoc &loc, char *data, bool isLabel )
+NameInst *ParseData::addNameInst( const InputLoc &loc, const char *data, bool isLabel )
 {
        /* Create the name instantitaion object and insert it. */
        NameInst *newNameInst = new NameInst( loc, curNameInst, data, nextNameId++, isLabel );
@@ -553,7 +558,7 @@ void ParseData::unsetObsoleteEntries( FsmAp *graph )
        }
 }
 
-NameSet ParseData::resolvePart( NameInst *refFrom, char *data, bool recLabelsOnly )
+NameSet ParseData::resolvePart( NameInst *refFrom, const char *data, bool recLabelsOnly )
 {
        /* Queue needed for breadth-first search, load it with the start node. */
        NameInstList nameQueue;
@@ -732,21 +737,25 @@ void ParseData::resolveNameRefs( InlineList *inlineList, Action *action )
                                /* Resolve, pass action for local search. */
                                NameInst *target = resolveStateRef( *item->nameRef, item->loc, action );
 
-                               /* Check if the target goes into a longest match. */
-                               NameInst *search = target->parent;
-                               while ( search != 0 ) {
-                                       if ( search->isLongestMatch ) {
-                                               error(item->loc) << "cannot enter inside a longest "
-                                                               "match construction as an entry point" << endl;
-                                               break;
+                               /* Name lookup error reporting is handled by resolveStateRef. */
+                               if ( target != 0 ) {
+                                       /* Check if the target goes into a longest match. */
+                                       NameInst *search = target->parent;
+                                       while ( search != 0 ) {
+                                               if ( search->isLongestMatch ) {
+                                                       error(item->loc) << "cannot enter inside a longest "
+                                                                       "match construction as an entry point" << endl;
+                                                       break;
+                                               }
+                                               search = search->parent;
                                        }
-                                       search = search->parent;
-                               }
 
-                               /* Note the reference in the name. This will cause the entry
-                                * point to survive to the end of the graph generating walk. */
-                               if ( target != 0 )
+                                       /* Record the reference in the name. This will cause the
+                                        * entry point to survive to the end of the graph
+                                        * generating walk. */
                                        target->numRefs += 1;
+                               }
+
                                item->nameTarg = target;
                                break;
                        }
@@ -816,12 +825,12 @@ void ParseData::makeNameTree( GraphDictEl *dictEl )
 }
 
 
-void ParseData::createBuiltin( char *name, BuiltinMachine builtin )
+void ParseData::createBuiltin( const char *name, BuiltinMachine builtin )
 {
        Expression *expression = new Expression( builtin );
        Join *join = new Join( expression );
-       JoinOrLm *joinOrLm = new JoinOrLm( join );
-       VarDef *varDef = new VarDef( name, joinOrLm );
+       MachineDef *machineDef = new MachineDef( join );
+       VarDef *varDef = new VarDef( name, machineDef );
        GraphDictEl *graphDictEl = new GraphDictEl( name, varDef );
        graphDict.insert( graphDictEl );
 }
@@ -874,6 +883,8 @@ bool ParseData::setVariable( char *var, InlineList *inlineList )
                pExpr = inlineList;
        else if ( strcmp( var, "pe" ) == 0 )
                peExpr = inlineList;
+       else if ( strcmp( var, "eof" ) == 0 )
+               eofExpr = inlineList;
        else if ( strcmp( var, "cs" ) == 0 )
                csExpr = inlineList;
        else if ( strcmp( var, "data" ) == 0 )
@@ -884,9 +895,9 @@ bool ParseData::setVariable( char *var, InlineList *inlineList )
                stackExpr = inlineList;
        else if ( strcmp( var, "act" ) == 0 )
                actExpr = inlineList;
-       else if ( strcmp( var, "tokstart" ) == 0 )
+       else if ( strcmp( var, "ts" ) == 0 )
                tokstartExpr = inlineList;
-       else if ( strcmp( var, "tokend" ) == 0 )
+       else if ( strcmp( var, "te" ) == 0 )
                tokendExpr = inlineList;
        else
                set = false;
@@ -955,12 +966,12 @@ void ParseData::removeActionDups( FsmAp *graph )
        }
 }
 
-Action *ParseData::newAction( char *name, InlineList *inlineList )
+Action *ParseData::newAction( const char *name, InlineList *inlineList )
 {
        InputLoc loc;
        loc.line = 1;
        loc.col = 1;
-       loc.fileName = "<NONE>";
+       loc.fileName = "NONE";
 
        Action *action = new Action( loc, name, inlineList, nextCondId++ );
        action->actionRefs.append( rootName );
@@ -986,13 +997,13 @@ void ParseData::initLongestMatchData()
                /* The setTokStart action sets tokstart. */
                InlineList *il5 = new InlineList;
                il5->append( new InlineItem( InputLoc(), InlineItem::LmSetTokStart ) );
-               setTokStart = newAction( "tokstart", il5 );
+               setTokStart = newAction( "ts", il5 );
                setTokStart->isLmAction = true;
 
                /* The setTokEnd action sets tokend. */
                InlineList *il3 = new InlineList;
                il3->append( new InlineItem( InputLoc(), InlineItem::LmSetTokEnd ) );
-               setTokEnd = newAction( "tokend", il3 );
+               setTokEnd = newAction( "te", il3 );
                setTokEnd->isLmAction = true;
 
                /* The action will also need an ordering: ahead of all user action
@@ -1059,11 +1070,16 @@ FsmAp *ParseData::makeInstance( GraphDictEl *gdNode )
         * All state construction is now complete.
         */
 
+       /* Transfer actions from the out action tables to eof action tables. */
+       for ( StateSet::Iter state = graph->finStateSet; state.lte(); state++ )
+               graph->transferOutActions( *state );
+
        /* Transfer global error actions. */
        for ( StateList::Iter state = graph->stateList; state.lte(); state++ )
                graph->transferErrorActions( state, 0 );
        
-       removeActionDups( graph );
+       if ( ::wantDupsRemoved )
+               removeActionDups( graph );
 
        /* Remove unreachable states. There should be no dead end states. The
         * subtract and intersection operators are the only places where they may
@@ -1112,7 +1128,7 @@ void ParseData::printNameTree()
        /* Show that the name index is correct. */
        for ( int ni = 0; ni < nextNameId; ni++ ) {
                cerr << ni << ": ";
-               char *name = nameIndex[ni]->name;
+               const char *name = nameIndex[ni]->name;
                cerr << ( name != 0 ? name : "<ANON>" ) << endl;
        }
 }
@@ -1230,31 +1246,9 @@ void ParseData::checkInlineList( Action *act, InlineList *inlineList )
                /* EOF checks. */
                if ( act->numEofRefs > 0 ) {
                        switch ( item->type ) {
-                       case InlineItem::PChar: 
-                               error(item->loc) << "pointer to current element does not exist in "
-                                               "EOF action code" << endl;
-                               break;
-                       case InlineItem::Char: 
-                               error(item->loc) << "current element does not exist in "
-                                               "EOF action code" << endl;
-                               break;
-                       case InlineItem::Hold:
-                               error(item->loc) << "changing the current element not possible in "
-                                               "EOF action code" << endl;
-                               break;
-                       case InlineItem::Exec:
-                               error(item->loc) << "changing the current element not possible in "
-                                               "EOF action code" << endl;
-                               break;
-                       case InlineItem::Goto: case InlineItem::Call: 
-                       case InlineItem::Next: case InlineItem::GotoExpr: 
-                       case InlineItem::CallExpr: case InlineItem::NextExpr:
-                       case InlineItem::Ret:
-                               error(item->loc) << "changing the current state not possible in "
-                                               "EOF action code" << endl;
-                               break;
-                       default:
-                               break;
+                               /* Currently no checks. */
+                               default:
+                                       break;
                        }
                }
 
@@ -1434,15 +1428,17 @@ void ParseData::prepareMachineGenTBWrapped( GraphDictEl *graphDictEl )
        sectionGraph->setStateNumbers( 0 );
 }
 
-void ParseData::generateXML( ostream &out )
+void ParseData::generateReduced( InputData &inputData )
 {
        beginProcessing();
 
+       cgd = makeCodeGen( inputData.inputFileName, sectionName, *inputData.outStream );
+
        /* Make the generator. */
-       XMLCodeGen codeGen( sectionName, this, sectionGraph, out );
+       BackendGen backendGen( sectionName, this, sectionGraph, cgd );
 
        /* Write out with it. */
-       codeGen.writeXML();
+       backendGen.makeBackend();
 
        if ( printStatistics ) {
                cerr << "fsm name  : " << sectionName << endl;
@@ -1451,91 +1447,20 @@ void ParseData::generateXML( ostream &out )
        }
 }
 
-/* Send eof to all parsers. */
-void terminateAllParsers( )
-{
-       /* FIXME: a proper token is needed here. Suppose we should use the
-        * location of EOF in the last file that the parser was referenced in. */
-       InputLoc loc;
-       loc.fileName = "<EOF>";
-       loc.line = 0;
-       loc.col = 0;
-       for ( ParserDict::Iter pdel = parserDict; pdel.lte(); pdel++ )
-               pdel->value->token( loc, _eof, 0, 0 );
-}
-
-void writeLanguage( std::ostream &out )
+void ParseData::generateXML( ostream &out )
 {
-       out << " lang=\"";
-       switch ( hostLang->lang ) {
-               case HostLang::C:    out << "C"; break;
-               case HostLang::D:    out << "D"; break;
-               case HostLang::Java: out << "Java"; break;
-               case HostLang::Ruby: out << "Ruby"; break;
-       }
-       out << "\"";
-       
-}
+       beginProcessing();
 
-void writeMachines( std::ostream &out, std::string hostData, char *inputFileName )
-{
-       if ( machineSpec == 0 && machineName == 0 ) {
-               /* No machine spec or machine name given. Generate everything. */
-               for ( ParserDict::Iter parser = parserDict; parser.lte(); parser++ ) {
-                       ParseData *pd = parser->value->pd;
-                       if ( pd->instanceList.length() > 0 )
-                               pd->prepareMachineGen( 0 );
-               }
+       /* Make the generator. */
+       XMLCodeGen codeGen( sectionName, this, sectionGraph, out );
 
-               if ( gblErrorCount == 0 ) {
-                       out << "<ragel version=\"" VERSION "\" filename=\"" << inputFileName << "\"";
-                       writeLanguage( out );
-                       out << ">\n";
-                       for ( ParserDict::Iter parser = parserDict; parser.lte(); parser++ ) {
-                               ParseData *pd = parser->value->pd;
-                               if ( pd->instanceList.length() > 0 )
-                                       pd->generateXML( out );
-                       }
-                       out << hostData;
-                       out << "</ragel>\n";
-               }
-       }
-       else if ( parserDict.length() > 0 ) {
-               /* There is either a machine spec or machine name given. */
-               ParseData *parseData = 0;
-               GraphDictEl *graphDictEl = 0;
-
-               /* Traverse the sections, break out when we find a section/machine
-                * that matches the one specified. */
-               for ( ParserDict::Iter parser = parserDict; parser.lte(); parser++ ) {
-                       ParseData *checkPd = parser->value->pd;
-                       if ( machineSpec == 0 || strcmp( checkPd->sectionName, machineSpec ) == 0 ) {
-                               GraphDictEl *checkGdEl = 0;
-                               if ( machineName == 0 || (checkGdEl = 
-                                               checkPd->graphDict.find( machineName )) != 0 )
-                               {
-                                       /* Have a machine spec and/or machine name that matches
-                                        * the -M/-S options. */
-                                       parseData = checkPd;
-                                       graphDictEl = checkGdEl;
-                                       break;
-                               }
-                       }
-               }
+       /* Write out with it. */
+       codeGen.writeXML();
 
-               if ( parseData == 0 )
-                       error() << "could not locate machine specified with -S and/or -M" << endl;
-               else {
-                       /* Section/Machine to emit was found. Prepare and emit it. */
-                       parseData->prepareMachineGen( graphDictEl );
-                       if ( gblErrorCount == 0 ) {
-                               out << "<ragel version=\"" VERSION "\" filename=\"" << inputFileName << "\"";
-                               writeLanguage( out );
-                               out << ">\n";
-                               parseData->generateXML( out );
-                               out << hostData;
-                               out << "</ragel>\n";
-                       }
-               }
+       if ( printStatistics ) {
+               cerr << "fsm name  : " << sectionName << endl;
+               cerr << "num states: " << sectionGraph->stateList.length() << endl;
+               cerr << endl;
        }
 }
+