Cleanup and removal of globals.
authorthurston <thurston@052ea7fc-9027-0410-9066-f65837a77df0>
Fri, 26 Jan 2007 01:42:43 +0000 (01:42 +0000)
committerthurston <thurston@052ea7fc-9027-0410-9066-f65837a77df0>
Fri, 26 Jan 2007 01:42:43 +0000 (01:42 +0000)
git-svn-id: http://svn.complang.org/ragel/trunk@14 052ea7fc-9027-0410-9066-f65837a77df0

ragel/main.cpp
ragel/ragel.h
ragel/rlparse.kh
ragel/rlparse.kl
ragel/rlscan.rl

index 419ce83..7a124f9 100644 (file)
@@ -46,11 +46,6 @@ using std::cout;
 using std::cerr;
 using std::endl;
 
-/* Io globals. */
-istream *inStream = 0;
-ostream *outStream = 0;
-char *outputFileName = 0;
-
 /* Controls minimization. */
 MinimizeLevel minimizeLevel = MinimizePartition2;
 MinimizeOpt minimizeOpt = MinimizeMostOps;
@@ -96,6 +91,15 @@ void version()
 /* Total error count. */
 int gblErrorCount = 0;
 
+/* Print the opening to a warning in the input, then return the error ostream. */
+ostream &warning( const InputLoc &loc )
+{
+       assert( loc.fileName != 0 );
+       cerr << loc.fileName << ":" << loc.line << ":" << 
+                       loc.col << ": warning: ";
+       return cerr;
+}
+
 /* Print the opening to a program error, then return the error stream. */
 ostream &error()
 {
@@ -104,12 +108,11 @@ ostream &error()
        return cerr;
 }
 
-
-/* Print the opening to a warning in the input, then return the error ostream. */
-ostream &warning( const InputLoc &loc )
+ostream &error( const InputLoc &loc )
 {
-       cerr << loc.fileName << ":" << loc.line << ":" << 
-                       loc.col << ": warning: ";
+       gblErrorCount += 1;
+       assert( loc.fileName != 0 );
+       cerr << loc.fileName << ":" << loc.line << ": ";
        return cerr;
 }
 
@@ -128,6 +131,7 @@ int main(int argc, char **argv)
 {
        ParamCheck pc("o:nmleabjkS:M:CDJvHh?-:s", argc, argv);
        char *inputFileName = 0;
+       char *outputFileName = 0;
 
        while ( pc.check() ) {
                switch ( pc.state ) {
@@ -266,6 +270,7 @@ int main(int argc, char **argv)
        }
 
        /* Open the input file for reading. */
+       istream *inStream;
        if ( inputFileName != 0 ) {
                /* Open the input file for reading. */
                ifstream *inFile = new ifstream( inputFileName );
@@ -284,12 +289,11 @@ int main(int argc, char **argv)
                exit(1);
 
        std::ostringstream outputBuffer;
-       outStream = &outputBuffer;
 
        if ( machineSpec == 0 && machineName == 0 )
-               *outStream << "<host line=\"1\" col=\"1\">";
+               outputBuffer << "<host line=\"1\" col=\"1\">";
 
-       scan( inputFileName, *inStream );
+       scan( inputFileName, *inStream, outputBuffer );
 
        /* Finished, final check for errors.. */
        if ( gblErrorCount > 0 )
@@ -303,7 +307,7 @@ int main(int argc, char **argv)
                return 1;
 
        if ( machineSpec == 0 && machineName == 0 )
-               *outStream << "</host>\n";
+               outputBuffer << "</host>\n";
 
        checkMachines();
 
index 8f539af..81d3f9a 100644 (file)
@@ -45,12 +45,6 @@ enum MinimizeOpt {
        MinimizeEveryOp
 };
 
-
-/* IO filenames and stream. */
-extern char *outputFileName;
-extern std::istream *inStream;
-extern std::ostream *outStream;
-
 /* Options. */
 extern MinimizeLevel minimizeLevel;
 extern MinimizeOpt minimizeOpt;
@@ -73,7 +67,7 @@ std::ostream &error();
 std::ostream &error( const InputLoc &loc ); 
 std::ostream &warning( const InputLoc &loc ); 
 
-void scan( char *fileName, std::istream &input );
+void scan( char *fileName, std::istream &input, std::ostream &output );
 void terminateAllParsers( );
 void checkMachines( );
 void writeMachines( std::ostream &out, std::string hostData, char *inputFileName );
index 287b560..ac8cb47 100644 (file)
 #include "avltree.h"
 #include "parsedata.h"
 
-extern char *Parser_lelNames[];
-
-struct LangEl;
-
 struct Parser
 {
-       %%{
+%%{
        parser Parser;
 
        # These must be declared first and in this order. Ragel currently cannot
@@ -93,7 +89,7 @@ struct Parser
        # semi is sent as a token in the code block rather than as a generic
        # symbol.
        token TK_Semi;
-       }%%
+}%%
 
        %% write instance_data;
 
@@ -111,7 +107,7 @@ struct Parser
                JoinOrLm *joinOrLm, bool isInstance );
 
        /* Report an error encountered by the parser. */
-       ostream &parser_error( int tokId, Token &token );
+       ostream &parse_error( int tokId, Token &token );
 
        ParseData *pd;
 
index dd29446..daf1fd7 100644 (file)
@@ -1347,7 +1347,6 @@ state_ref_names:
        write data;
 }%%
 
-
 void Parser::init()
 {
        %% write init;
@@ -1379,16 +1378,9 @@ void Parser::tryMachineDef( InputLoc &loc, char *name,
        }
 }
 
-ostream &error( const InputLoc &loc )
-{
-       gblErrorCount += 1;
-       assert( loc.fileName != 0 );
-       cerr << loc.fileName << ":" << loc.line << ": ";
-       return cerr;
-}
-
-ostream &Parser::parser_error( int tokId, Token &token )
+ostream &Parser::parse_error( int tokId, Token &token )
 {
+       /* Maintain the error count. */
        gblErrorCount += 1;
 
        cerr << token.loc.fileName << ":" << token.loc.line << ":" << token.loc.col << ": ";
@@ -1412,7 +1404,7 @@ int Parser::token( InputLoc &loc, int tokId, char *tokstart, int toklen )
        token.loc = loc;
        int res = parseLangEl( tokId, &token );
        if ( res < 0 ) {
-               parser_error(tokId, token) << "parse error" << endl;
+               parse_error(tokId, token) << "parse error" << endl;
                exit(1);
        }
        return res;
index e8154a1..db2c7ef 100644 (file)
@@ -49,7 +49,6 @@ struct IncludeStackItem
 };
 
 typedef Vector<IncludeStackItem> IncludeStack;
-IncludeStack includeStack;
 
 enum InlineBlockType
 {
@@ -59,22 +58,21 @@ enum InlineBlockType
 
 struct Scanner
 {
-       Scanner( char *fileName, istream &input,
+       Scanner( char *fileName, istream &input, ostream &output,
                        Parser *inclToParser, char *inclSectionTarg,
-                       int include_depth )
+                       int includeDepth )
        : 
-               fileName(fileName), input(input), 
+               fileName(fileName), input(input), output(output),
                inclToParser(inclToParser),
                inclSectionTarg(inclSectionTarg),
-               include_depth(include_depth),
+               includeDepth(includeDepth),
                line(1), column(1), lastnl(0), 
                parser(0), active(false), 
                parserExistsError(false), ragelDefOpen(false),
                whitespaceOn(true)
                {}
 
-       bool recursiveInclude( IncludeStack &includeStack, 
-                       char *inclFileName, char *inclSectionName );
+       bool recursiveInclude( char *inclFileName, char *inclSectionName );
 
        char *prepareFileName( char *fileName, int len )
        {
@@ -96,13 +94,14 @@ struct Scanner
        void openRagelDef();
        void do_scan();
        bool parserExists();
-       ostream &error();
+       ostream &scan_error();
 
        char *fileName;
        istream &input;
+       ostream &output;
        Parser *inclToParser;
        char *inclSectionTarg;
-       int include_depth;
+       int includeDepth;
 
        int cs;
        int line;
@@ -117,6 +116,7 @@ struct Scanner
         * allowing for unnamed sections. */
        Parser *parser;
        bool active;
+       IncludeStack includeStack;
 
        /* This is set if ragel has already emitted an error stating that
         * no section name has been seen and thus no parser exists. */
@@ -145,23 +145,21 @@ bool Scanner::parserExists()
                return true;
 
        if ( ! parserExistsError ) {
-               error() << "include: there is no previous specification name" << endl;
+               scan_error() << "include: there is no previous specification name" << endl;
                parserExistsError = true;
        }
        return false;
 }
 
-ostream &Scanner::error()
+ostream &Scanner::scan_error()
 {
        /* Maintain the error count. */
        gblErrorCount += 1;
-
        cerr << fileName << ":" << line << ":" << column << ": ";
        return cerr;
 }
 
-bool Scanner::recursiveInclude( IncludeStack &includeStack, 
-                       char *inclFileName, char *inclSectionName )
+bool Scanner::recursiveInclude( char *inclFileName, char *inclSectionName )
 {
        for ( IncludeStack::Iter si = includeStack; si.lte(); si++ ) {
                if ( strcmp( si->fileName, inclFileName ) == 0 &&
@@ -208,9 +206,9 @@ void Scanner::token( int type )
        action store_word { word = tokdata; word_len = toklen; }
        action store_lit { lit = tokdata; lit_len = toklen; }
 
-       action mach_err { error() << "bad machine statement" << endl; }
-       action incl_err { error() << "bad include statement" << endl; }
-       action write_err { error() << "bad write statement" << endl; }
+       action mach_err { scan_error() << "bad machine statement" << endl; }
+       action incl_err { scan_error() << "bad include statement" << endl; }
+       action write_err { scan_error() << "bad write statement" << endl; }
 
        action handle_machine
        {
@@ -265,18 +263,18 @@ void Scanner::token( int type )
                         * name then check if what we are including is already in the stack. */
                        includeStack.append( IncludeStackItem( fileName, parser->sectionName ) );
 
-                       if ( recursiveInclude( includeStack, inclFileName, inclSectionName ) )
-                               error() << "include: this is a recursive include operation" << endl;
+                       if ( recursiveInclude( inclFileName, inclSectionName ) )
+                               scan_error() << "include: this is a recursive include operation" << endl;
                        else {
                                /* Open the input file for reading. */
                                ifstream *inFile = new ifstream( inclFileName );
                                if ( ! inFile->is_open() ) {
-                                       error() << "include: could not open " << 
+                                       scan_error() << "include: could not open " << 
                                                        inclFileName << " for reading" << endl;
                                }
 
-                               Scanner scanner( inclFileName, *inFile, parser,
-                                               inclSectionName, include_depth+1 );
+                               Scanner scanner( inclFileName, *inFile, output, parser,
+                                               inclSectionName, includeDepth+1 );
                                scanner.init();
                                scanner.do_scan( );
                                delete inFile;
@@ -305,21 +303,21 @@ void Scanner::token( int type )
                                        strcmp( tokdata, "exec" ) != 0 &&
                                        strcmp( tokdata, "eof" ) != 0 )
                        {
-                               error() << "unknown write command" << endl;
+                               scan_error() << "unknown write command" << endl;
                        }
-                       *outStream << "  <write what=\"" << tokdata << "\">";
+                       output << "  <write what=\"" << tokdata << "\">";
                }
        }
 
        action write_option
        {
                if ( active )
-                       *outStream << "<option>" << tokdata << "</option>";
+                       output << "<option>" << tokdata << "</option>";
        }
        action write_close
        {
                if ( active )
-                       *outStream << "</write>\n";
+                       output << "</write>\n";
        }
 
        write_stmt =
@@ -387,9 +385,9 @@ void Scanner::startSection( )
 {
        parserExistsError = false;
 
-       if ( include_depth == 0 ) {
+       if ( includeDepth == 0 ) {
                if ( machineSpec == 0 && machineName == 0 )
-                       *outStream << "</host>\n";
+                       output << "</host>\n";
                ragelDefOpen = false;
        }
 
@@ -402,7 +400,7 @@ void Scanner::openRagelDef()
 {
        if ( ! ragelDefOpen ) {
                ragelDefOpen = true;
-               *outStream << "<ragel_def name=\"" << parser->sectionName << "\">\n";
+               output << "<ragel_def name=\"" << parser->sectionName << "\">\n";
        }
 }
 
@@ -424,16 +422,16 @@ void Scanner::endSection( )
                parser->token( loc, TK_EndSection, 0, 0 );
        }
 
-       if ( include_depth == 0 ) {
+       if ( includeDepth == 0 ) {
                if ( ragelDefOpen ) {
-                       *outStream << "</ragel_def>\n";
+                       output << "</ragel_def>\n";
                        ragelDefOpen = false;
                }
 
                if ( machineSpec == 0 && machineName == 0 ) {
                        /* The end section may include a newline on the end, so
                         * we use the last line, which will count the newline. */
-                       *outStream << "<host line=\"" << line << "\">";
+                       output << "<host line=\"" << line << "\">";
                }
        }
 }
@@ -567,7 +565,7 @@ void Scanner::endSection( )
                };
 
                EOF => {
-                       error() << "unterminated code block" << endl;
+                       scan_error() << "unterminated code block" << endl;
                };
 
                # Send every other character as a symbol.
@@ -594,7 +592,7 @@ void Scanner::endSection( )
                ']'     => { token( RE_SqClose ); fret; };
 
                EOF => {
-                       error() << "unterminated OR literal" << endl;
+                       scan_error() << "unterminated OR literal" << endl;
                };
 
                # Characters in an OR expression.
@@ -629,7 +627,7 @@ void Scanner::endSection( )
                '[^' => { token( RE_SqOpenNeg ); fcall or_literal; };
 
                EOF => {
-                       error() << "unterminated regular expression" << endl;
+                       scan_error() << "unterminated regular expression" << endl;
                };
 
                # Characters in an OR expression.
@@ -643,7 +641,7 @@ void Scanner::endSection( )
                ';' => { token( ';' ); fgoto parser_def; };
 
                EOF => {
-                       error() << "unterminated write statement" << endl;
+                       scan_error() << "unterminated write statement" << endl;
                };
        *|;
 
@@ -790,7 +788,7 @@ void Scanner::endSection( )
                };
 
                EOF => {
-                       error() << "unterminated ragel section" << endl;
+                       scan_error() << "unterminated ragel section" << endl;
                };
 
                any => { token( *tokstart ); } ;
@@ -801,8 +799,8 @@ void Scanner::endSection( )
 
                /* If no errors and we are at the bottom of the include stack (the
                 * source file listed on the command line) then write out the data. */
-               if ( include_depth == 0 && machineSpec == 0 && machineName == 0 )
-                       xmlEscapeHost( *outStream, tokstart, tokend-tokstart );
+               if ( includeDepth == 0 && machineSpec == 0 && machineName == 0 )
+                       xmlEscapeHost( output, tokstart, tokend-tokstart );
        }
 
        # Outside code scanner. These tokens get passed through.
@@ -886,7 +884,7 @@ void Scanner::do_scan()
                if ( cs == rlscan_error ) {
                        /* Machine failed before finding a token. I'm not yet sure if this
                         * is reachable. */
-                       error() << "scanner error" << endl;
+                       scan_error() << "scanner error" << endl;
                        exit(1);
                }
 
@@ -912,9 +910,9 @@ void Scanner::do_scan()
        delete[] buf;
 }
 
-void scan( char *fileName, istream &input )
+void scan( char *fileName, istream &input, ostream &output )
 {
-       Scanner scanner( fileName, input, 0, 0, 0 );
+       Scanner scanner( fileName, input, output, 0, 0, 0 );
        scanner.init();
        scanner.do_scan();