Line directives need to use the fileName stored in the InputLoc stuctures from
[external/ragel.git] / ragel / parsedata.cpp
index 46ecf2f..8ce89db 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  Copyright 2001-2006 Adrian Thurston <thurston@complang.org>
+ *  Copyright 2001-2008 Adrian Thurston <thurston@complang.org>
  */
 
 /*  This file is part of Ragel.
@@ -32,7 +32,6 @@
 #include "mergesort.h"
 #include "xmlcodegen.h"
 #include "version.h"
-#include "xmlparse.h"
 #include "inputdata.h"
 
 using namespace std;
@@ -116,7 +115,7 @@ Key makeFsmKeyHex( char *str, const InputLoc &loc, ParseData *pd )
        }
 
        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 );
 }
@@ -454,7 +453,8 @@ ParseData::ParseData( const 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
@@ -829,8 +829,8 @@ 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 );
 }
@@ -971,7 +971,7 @@ 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 );
@@ -1432,8 +1432,10 @@ void ParseData::generateReduced( InputData &inputData )
 {
        beginProcessing();
 
+       cgd = makeCodeGen( inputData.inputFileName, sectionName, *inputData.outStream );
+
        /* Make the generator. */
-       BackendGen backendGen( sectionName, this, sectionGraph, inputData );
+       BackendGen backendGen( sectionName, this, sectionGraph, cgd );
 
        /* Write out with it. */
        backendGen.makeBackend();
@@ -1445,38 +1447,12 @@ void ParseData::generateReduced( InputData &inputData )
        }
 }
 
-/* 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, Parser_tk_eof, 0, 0 );
-}
-
-void writeLanguage( std::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;
-               case HostLang::CSharp: out << "C#"; break;
-       }
-       out << "\"";
-}
-
-void ParseData::generateXML( ostream &out, InputData &inputData )
+void ParseData::generateXML( ostream &out )
 {
        beginProcessing();
 
        /* Make the generator. */
-       XMLCodeGen codeGen( sectionName, this, sectionGraph, out, inputData );
+       XMLCodeGen codeGen( sectionName, this, sectionGraph, out );
 
        /* Write out with it. */
        codeGen.writeXML();
@@ -1488,169 +1464,3 @@ void ParseData::generateXML( ostream &out, InputData &inputData )
        }
 }
 
-void writeMachines( std::ostream &out, std::string hostData, 
-               const char *inputFileName, InputData &inputData )
-{
-       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 );
-               }
-
-               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, inputData );
-                       }
-                       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;
-                               }
-                       }
-               }
-
-               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, inputData );
-                               out << hostData;
-                               out << "</ragel>\n";
-                       }
-               }
-       }
-}
-
-void generateSpecificReduced( InputData &inputData )
-{
-       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;
-                               }
-                       }
-               }
-
-               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 )
-                               parseData->generateReduced( inputData );
-               }
-       }
-
-
-       for ( InputItemList::Iter ii = inputItems; ii.lte(); ii++ ) {
-               if ( ii->type == InputItem::Write ) {
-                       CodeGenMapEl *mapEl = inputData.codeGenMap.find( (char*)ii->name.c_str() );
-                       inputData.cgd = mapEl->value;
-                       ::keyOps = &inputData.cgd->thisKeyOps;
-
-                       inputData.cgd->writeStatement( ii->loc, ii->writeArgs.length()-1, ii->writeArgs.data );
-               }
-               else 
-                       inputData.cgd->out << ii->data.str();
-       }
-}
-
-void InputData::openOutput()
-{
-       if ( generateDot )
-               outStream = dotOpenOutput( sourceFileName );
-       else if ( hostLang->lang == HostLang::C )
-               outStream = cdOpenOutput( sourceFileName );
-       else if ( hostLang->lang == HostLang::D )
-               outStream = cdOpenOutput( sourceFileName );
-       else if ( hostLang->lang == HostLang::Java )
-               outStream = javaOpenOutput( sourceFileName );
-       else if ( hostLang->lang == HostLang::Ruby )
-               outStream = rubyOpenOutput( sourceFileName );
-       else if ( hostLang->lang == HostLang::CSharp )
-               outStream = csharpOpenOutput( sourceFileName );
-       else {
-               assert( false );
-       }
-}
-
-void generateReduced( const char *sourceFileName, const char *xmlFileName, 
-               bool outputActive, bool wantComplete )
-{
-       /* 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 );
-       }
-
-       InputData inputData( sourceFileName, xmlFileName, outputActive, wantComplete );
-       inputData.openOutput();
-
-       if ( gblErrorCount > 0 )
-               return;
-
-       for ( ParserDict::Iter parser = parserDict; parser.lte(); parser++ ) {
-               ParseData *pd = parser->value->pd;
-               if ( pd->instanceList.length() > 0 )
-                       pd->generateReduced( inputData );
-       }
-
-       for ( InputItemList::Iter ii = inputItems; ii.lte(); ii++ ) {
-               if ( ii->type == InputItem::Write ) {
-                       CodeGenMapEl *mapEl = inputData.codeGenMap.find( (char*)ii->name.c_str() );
-                       inputData.cgd = mapEl->value;
-                       ::keyOps = &inputData.cgd->thisKeyOps;
-
-                       inputData.cgd->writeStatement( ii->loc, ii->writeArgs.length()-1, ii->writeArgs.data );
-               }
-               else 
-                       inputData.cgd->out << ii->data.str();
-       }
-}