Moved scanner class into a new header rlscan.h.
[external/ragel.git] / ragel / rlscan.h
1 /*
2  *  Copyright 2007 Adrian Thurston <thurston@cs.queensu.ca>
3  */
4
5 /*  This file is part of Ragel.
6  *
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.
11  * 
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.
16  * 
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 
20  */
21
22 #ifndef _RLSCAN_H
23 #define _RLSCAN_H
24
25 #include <iostream>
26 #include "rlscan.h"
27 #include "vector.h"
28 #include "rlparse.h"
29 #include "parsedata.h"
30 #include "avltree.h"
31 #include "vector.h"
32
33 using std::istream;
34 using std::ostream;
35
36 extern char *Parser_lelNames[];
37
38 /* This is used for tracking the current stack of include file/machine pairs. It is
39  * is used to detect and recursive include structure. */
40 struct IncludeStackItem
41 {
42         IncludeStackItem( char *fileName, char *sectionName )
43                 : fileName(fileName), sectionName(sectionName) {}
44
45         char *fileName;
46         char *sectionName;
47 };
48
49 typedef Vector<IncludeStackItem> IncludeStack;
50
51 struct Scanner
52 {
53         Scanner( char *fileName, istream &input, ostream &output,
54                         Parser *inclToParser, char *inclSectionTarg,
55                         int includeDepth )
56         : 
57                 fileName(fileName), input(input), output(output),
58                 inclToParser(inclToParser),
59                 inclSectionTarg(inclSectionTarg),
60                 includeDepth(includeDepth),
61                 line(1), column(1), lastnl(0), 
62                 parser(0), ignoreSection(false), 
63                 parserExistsError(false),
64                 whitespaceOn(true),
65                 lastToken(0)
66                 {}
67
68         bool recursiveInclude( char *inclFileName, char *inclSectionName );
69
70         char *prepareFileName( char *fileName, int len )
71         {
72                 bool caseInsensitive;
73                 Token tokenFnStr, tokenRes;
74                 tokenFnStr.data = fileName;
75                 tokenFnStr.length = len;
76                 tokenFnStr.prepareLitString( tokenRes, caseInsensitive );
77                 return tokenRes.data;
78         }
79
80         void init();
81         void token( int type, char *start, char *end );
82         void token( int type, char c );
83         void token( int type );
84         void updateCol();
85         void startSection();
86         void endSection();
87         void do_scan();
88         bool active();
89         ostream &scan_error();
90
91         char *fileName;
92         istream &input;
93         ostream &output;
94         Parser *inclToParser;
95         char *inclSectionTarg;
96         int includeDepth;
97
98         int cs;
99         int line;
100         char *word, *lit;
101         int word_len, lit_len;
102         InputLoc sectionLoc;
103         char *tokstart, *tokend;
104         int column;
105         char *lastnl;
106
107         /* Set by machine statements, these persist from section to section
108          * allowing for unnamed sections. */
109         Parser *parser;
110         bool ignoreSection;
111         IncludeStack includeStack;
112
113         /* This is set if ragel has already emitted an error stating that
114          * no section name has been seen and thus no parser exists. */
115         bool parserExistsError;
116
117         /* This is for inline code. By default it is on. It goes off for
118          * statements and values in inline blocks which are parsed. */
119         bool whitespaceOn;
120
121         /* Keeps a record of the previous token sent to the section parser. */
122         int lastToken;
123 };
124
125 #endif /* _RLSCAN_H */