Started on a scanner for importing definitions.
[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 struct ImportScanner
37 {
38         ImportScanner( char *fileName, istream &input, ostream &output, Parser *parser )
39         : 
40                 fileName(fileName), input(input), output(output), 
41                 parser(parser), 
42                 cur_token(0),
43                 line(1), column(1)
44         {}
45
46         void token( int type, char *start, char *end );
47         void updateCol();
48         void startSection();
49         void endSection();
50         void do_scan();
51         ostream &scan_error();
52
53         char *fileName;
54         istream &input;
55         ostream &output;
56         Parser *parser;
57
58         /* For scanning the tokens. */
59         int tok_cs, tok_act;
60         int *tok_tokstart, *tok_tokend;
61         int cur_token;
62         static const int max_tokens = 8;
63         int token_data[max_tokens];
64
65         /* For scanning the characters. */
66         int line;
67         char *chr_tokstart, *chr_tokend;
68         int column;
69 };
70
71
72 extern char *Parser_lelNames[];
73
74 /* This is used for tracking the current stack of include file/machine pairs. It is
75  * is used to detect and recursive include structure. */
76 struct IncludeStackItem
77 {
78         IncludeStackItem( char *fileName, char *sectionName )
79                 : fileName(fileName), sectionName(sectionName) {}
80
81         char *fileName;
82         char *sectionName;
83 };
84
85 typedef Vector<IncludeStackItem> IncludeStack;
86
87 struct Scanner
88 {
89         Scanner( char *fileName, istream &input, ostream &output,
90                         Parser *inclToParser, char *inclSectionTarg,
91                         int includeDepth )
92         : 
93                 fileName(fileName), input(input), output(output),
94                 inclToParser(inclToParser),
95                 inclSectionTarg(inclSectionTarg),
96                 includeDepth(includeDepth),
97                 line(1), column(1), lastnl(0), 
98                 parser(0), ignoreSection(false), 
99                 parserExistsError(false),
100                 whitespaceOn(true),
101                 lastToken(0)
102                 {}
103
104         bool recursiveInclude( char *inclFileName, char *inclSectionName );
105
106         char *prepareFileName( char *fileName, int len )
107         {
108                 bool caseInsensitive;
109                 Token tokenFnStr, tokenRes;
110                 tokenFnStr.data = fileName;
111                 tokenFnStr.length = len;
112                 tokenFnStr.prepareLitString( tokenRes, caseInsensitive );
113                 return tokenRes.data;
114         }
115
116         void init();
117         void token( int type, char *start, char *end );
118         void token( int type, char c );
119         void token( int type );
120         void updateCol();
121         void startSection();
122         void endSection();
123         void do_scan();
124         bool active();
125         ostream &scan_error();
126
127         char *fileName;
128         istream &input;
129         ostream &output;
130         Parser *inclToParser;
131         char *inclSectionTarg;
132         int includeDepth;
133
134         /* For section processing. */
135         int cs;
136         char *word, *lit;
137         int word_len, lit_len;
138
139         /* For character scanning. */
140         int line;
141         InputLoc sectionLoc;
142         char *tokstart, *tokend;
143         int column;
144         char *lastnl;
145
146         /* Set by machine statements, these persist from section to section
147          * allowing for unnamed sections. */
148         Parser *parser;
149         bool ignoreSection;
150         IncludeStack includeStack;
151
152         /* This is set if ragel has already emitted an error stating that
153          * no section name has been seen and thus no parser exists. */
154         bool parserExistsError;
155
156         /* This is for inline code. By default it is on. It goes off for
157          * statements and values in inline blocks which are parsed. */
158         bool whitespaceOn;
159
160         /* Keeps a record of the previous token sent to the section parser. */
161         int lastToken;
162 };
163
164 #endif /* _RLSCAN_H */