If the condition embedding code runs out of available characters in the
[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, bool importMachines )
56         : 
57                 fileName(fileName), input(input), output(output),
58                 inclToParser(inclToParser),
59                 inclSectionTarg(inclSectionTarg),
60                 includeDepth(includeDepth),
61                 importMachines(importMachines),
62                 cur_token(0),
63                 line(1), column(1), lastnl(0), 
64                 parser(0), ignoreSection(false), 
65                 parserExistsError(false),
66                 whitespaceOn(true),
67                 lastToken(0)
68                 {}
69
70         bool recursiveInclude( char *inclFileName, char *inclSectionName );
71
72         char *prepareFileName( char *fileName, int len )
73         {
74                 bool caseInsensitive;
75                 Token tokenFnStr, tokenRes;
76                 tokenFnStr.data = fileName;
77                 tokenFnStr.length = len;
78                 tokenFnStr.prepareLitString( tokenRes, caseInsensitive );
79                 return tokenRes.data;
80         }
81
82         void init();
83         void token( int type, char *start, char *end );
84         void token( int type, char c );
85         void token( int type );
86         void processToken( int type, char *tokdata, int toklen );
87         void directToParser( Parser *toParser, char *tokFileName, int tokLine, 
88                 int tokColumn, int type, char *tokdata, int toklen );
89         void flushImport( );
90         void importToken( int type, char *start, char *end );
91         void pass( int token, char *start, char *end );
92         void pass();
93         void updateCol();
94         void startSection();
95         void endSection();
96         void do_scan();
97         bool active();
98         ostream &scan_error();
99
100         char *fileName;
101         istream &input;
102         ostream &output;
103         Parser *inclToParser;
104         char *inclSectionTarg;
105         int includeDepth;
106         bool importMachines;
107
108         /* For import parsing. */
109         int tok_cs, tok_act;
110         int *tok_tokstart, *tok_tokend;
111         int cur_token;
112         static const int max_tokens = 32;
113         int token_data[max_tokens];
114         char *token_strings[max_tokens];
115         int token_lens[max_tokens];
116
117         /* For section processing. */
118         int cs;
119         char *word, *lit;
120         int word_len, lit_len;
121
122         /* For character scanning. */
123         int line;
124         InputLoc sectionLoc;
125         char *tokstart, *tokend;
126         int column;
127         char *lastnl;
128
129         /* Set by machine statements, these persist from section to section
130          * allowing for unnamed sections. */
131         Parser *parser;
132         bool ignoreSection;
133         IncludeStack includeStack;
134
135         /* This is set if ragel has already emitted an error stating that
136          * no section name has been seen and thus no parser exists. */
137         bool parserExistsError;
138
139         /* This is for inline code. By default it is on. It goes off for
140          * statements and values in inline blocks which are parsed. */
141         bool whitespaceOn;
142
143         /* Keeps a record of the previous token sent to the section parser. */
144         int lastToken;
145 };
146
147 #endif /* _RLSCAN_H */