File: grap_tokenizer.cc

package info (click to toggle)
grap 1.23-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 672 kB
  • ctags: 563
  • sloc: cpp: 2,907; lex: 1,034; yacc: 1,027; sh: 162; makefile: 157; awk: 5
file content (303 lines) | stat: -rw-r--r-- 9,686 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/* This code is (c) 1998-2001 Ted Faber see COPYRIGHT
   for the full copyright and limitations of liabilities. */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifdef STDC_HEADERS
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#endif
#if defined(STDC_HEADERS) | defined(HAVE_STDLIB_H)
#include <stdlib.h>
#endif
#include <iostream>
#include <sys/param.h>
#include <set> 
#include "grap.h"
#include "grap_data.h"
#include "grap_draw.h" 
#include "y.tab.h"

// Keywords recognized in the initial GRAP state (AT, SPRINTF and all
// the string modifiers are there for implicit PLOT statements).
string gk[] = {
    "copy", "next", "draw", "new", "line", "define", "arrow", "circle",
    "plot", "at", "frame", "graph", "coord", "for", "if", "print", "sprintf",
    "ticks", "tick", "label", "grid", "pic", "sh", "bar", "ljust", "rjust",
    "above", "below", "aligned", "unaligned", "size", "undefine"
};

// These are the keywords recognized by any keyword that takes a line
// descriptor:
string lk[] = {
    "invis", "solid", "dotted", "dashed", "fill", "fillcolor", "color"
};

// Keywords recognized for sting modifiers 
string sk[] = {
    "ljust", "rjust", "above", "below", "aligned", "unaligned", "size"
};

// Keywords recognized by ticks and graph
string tk[] = {
    "left", "right", "up", "down", "bottom", "bot", "top", "in", "out",
    "from", "to", "by", "at", "off", "on", "auto", "sprintf"
};


// Vector versions of these
vector<string> grap_keys(gk, gk+sizeof(gk)/sizeof(string));
vector<string> linedesc_keys(lk, lk+sizeof(lk)/sizeof(string));
vector<string> strmod_keys(sk, sk+sizeof(sk)/sizeof(string));
vector<string> tick_keys(tk, tk+sizeof(tk)/sizeof(string));

// The data structure encapsulating the keyword handling algorithms
keywordDictionary keywords;

// This builds a large hash table encapsulating the actions to take
// when a keyword is parsed.  This is one big static data structure,
// and should just be statically initialized, but is complex enough
// that building it is easier to maintain.  It's split into
// subfunctions primarily because the gcc optimizer has a terrible
// time with th eone big function, and partially to make it easier to
// maintain.

// All these can appear multiple times in a command
void init_multiples() {
    vector<string> empty;

    keywords["above"] = keyword(empty, empty, false, ABOVE);
    keywords["top"] = keyword(empty, empty, false, TOP);
    keywords["bot"] = keywords["bottom"] =
	keyword(empty, empty, false, BOTTOM);
    keywords["left"] = keyword(empty, empty, false, LEFT);
    keywords["right"] = keyword(empty, empty, false, RIGHT);
    keywords["up"] = keyword(empty, empty, false, UP);
    keywords["down"] = keyword(empty, empty, false, DOWN);
    keywords["ljust"] = keyword(empty, empty, false, LJUST);
    keywords["rjust"] = keyword(empty, empty, false, RJUST);
    keywords["below"] = keyword(empty, empty, false, BELOW);
    keywords["aligned"] = keyword(empty, empty, false, ALIGNED);
    keywords["unaligned"] = keyword(empty, empty, false, UNALIGNED);
    keywords["size"] = keyword(empty, empty, false, SIZE);
    keywords["in"] = keyword(empty, empty, false, IN);
    keywords["out"] = keyword(empty, empty, false, OUT);
    keywords["off"] = keyword(empty, empty, false, OFF);
    keywords["on"] = keywords["auto"] = keyword(empty, empty, false, ON);
    keywords["fill"] = keyword(empty, empty, false, FILL);
    keywords["invis"] = keyword(empty, empty, false, INVIS);
    keywords["solid"] = keyword(empty, empty, false, SOLID);
    keywords["dotted"] = keyword(empty, empty, false, DOTTED);
    keywords["dashed"] = keyword(empty, empty, false, DASHED);
    keywords["fillcolor"] = keyword(empty, empty, false, FILLCOLOR);
    keywords["color"] = keyword(empty, empty, false, COLOR);

}

// These are all one time command modifiers
void init_only_once() {
    vector<string> empty;
    vector<string> temp;

    // temp.clear();
    temp.push_back("radius");
    temp.push_back("rad");
    keywords["radius"] = keywords["rad"] = keyword(empty, temp, false, RADIUS);

    temp.clear();
    temp.push_back("from");
    keywords["from"] = keyword(empty, temp, false, FROM);

    temp.clear();
    temp.push_back("thru");
    temp.push_back("through");
    keywords["thru"] = keywords["through"] = keyword(empty, temp, false, THRU);

    temp.clear();
    temp.push_back("to");
    keywords["to"] = keyword(empty, temp, false, TO);

    temp.clear();
    temp.push_back("by");
    keywords["by"] = keyword(empty, temp, false, BY);

    temp.clear();
    temp.push_back("until");
    keywords["until"] = keyword(empty, temp, false, UNTIL);

    temp.clear();
    temp.push_back("do");
    keywords["do"] = keyword(empty, temp, false, DO);

    temp.clear();
    temp.push_back("x");
    keywords["x"] = keyword(empty, temp, false, XDIM);

    temp.clear();
    temp.push_back("y");
    keywords["y"] = keyword(empty, temp, false, YDIM);

    temp.clear();
    temp.push_back("base");
    keywords["base"] = keyword(empty, temp, false, BASE);

    temp.clear();
    temp.push_back("ht");
    keywords["ht"] = keyword(empty, temp, false, HT);

    temp.clear();
    temp.push_back("wid");
    keywords["wid"] = keyword(empty, temp, false, WID);

    temp.clear();
    temp.push_back("at");
    keywords["plot"] = keyword(temp, empty, true, PLOT);
}

// These all signal the start of a command
void init_line_starters() {
    vector<string> empty;
    vector<string> temp;

    temp.clear();
    temp.push_back("thru");
    temp.push_back("through");
    temp.push_back("until");
    keywords["copy"] = keyword(temp, empty, true, COPY);

    temp = linedesc_keys;
    temp.push_back("at");
    temp.push_back("sprintf");
    keywords["next"] = keyword(temp, empty, true, NEXT);

    temp = linedesc_keys;
    temp.insert(temp.end(), strmod_keys.begin(), strmod_keys.end());
    temp.push_back("sprintf");
    keywords["draw"] = keywords["new"] = keyword(temp, empty, true, DRAW);
    
    temp = linedesc_keys;
    temp.push_back("from");
    temp.push_back("to");
    keywords["line"] = keyword(temp, empty, true, LINE);

    temp = linedesc_keys;
    temp.push_back("from");
    temp.push_back("to");
    keywords["arrow"] = keyword(temp, empty, true, ARROW);
    
    temp = linedesc_keys;
    temp.push_back("at");
    temp.push_back("radius");
    temp.push_back("rad");
    keywords["circle"] = keyword(temp, empty, true, CIRCLE);

    temp = linedesc_keys;
    temp.push_back("top");
    temp.push_back("bottom");
    temp.push_back("bot");
    temp.push_back("left");
    temp.push_back("right");
    temp.push_back("ht");
    temp.push_back("wid");
    keywords["frame"] = keyword(temp, empty, true, FRAME);

    temp.clear();
    temp.push_back("x");
    temp.push_back("y");
    temp.push_back("log");
    keywords["coord"] = keyword(temp, empty, true, COORD);

    temp.clear();
    temp.push_back("from");
    temp.push_back("to");
    temp.push_back("by");
    temp.push_back("do");
    keywords["for"] = keyword(temp, empty, true, FOR);

    temp.clear();
    temp.push_back("then");
    keywords["if"] = keyword(temp, empty, true, IF);

    temp.clear();
    temp.push_back("else");
    keywords["then"] = keyword(temp, empty, true, THEN);

    temp = tick_keys;
    temp.insert(temp.end(), strmod_keys.begin(), strmod_keys.end());
    keywords["tick"] = keywords["ticks"] = keyword(temp, empty, true, TICKS);

    temp = strmod_keys;
    temp.push_back("left");
    temp.push_back("right");
    temp.push_back("up");
    temp.push_back("down");
    temp.push_back("bottom");
    temp.push_back("bot");
    temp.push_back("top");
    temp.push_back("sprintf");
    keywords["label"] = keyword(temp, empty, true, LABEL);

    temp = linedesc_keys;
    temp.insert(temp.end(), tick_keys.begin(), tick_keys.end());
    temp.insert(temp.end(), strmod_keys.begin(), strmod_keys.end());
    temp.push_back("ticks");
    temp.push_back("tick");
    keywords["grid"] = keyword(temp, empty, true, GRID);

    temp = linedesc_keys;
    temp.push_back("ht");
    temp.push_back("wid");
    temp.push_back("up");
    temp.push_back("right");
    temp.push_back("base");
    keywords["bar"] = keyword(temp, empty, true, BAR);

}

// These have special parsing rules.  They clear the active commands.
void init_wipers() {
    vector<string> empty;
    keywords["define"] = keyword(empty, empty, true, DEFINE);
    keywords["undefine"] = keyword(empty, empty, true, UNDEFINE);
    keywords["graph"] = keyword(empty, empty, true, GRAPH);
    keywords["else"] = keyword(empty, empty, true, ELSE);
    keywords["print"] = keyword(empty, empty, true, PRINT);
    keywords["pic"] = keyword(empty, empty, true, PIC);
    keywords["sh"] = keyword(empty, empty, true, SH);
}

// This are used in parsing multi-element matches for macros.

bool id_letter[256];

// set up the table to scan for embedded macros
void init_id() {
    int i;	// Scratch
    for (i = 0; i < 256; i++ ) id_letter[i] = false;

    // assumes letters are contiugous.  Is there still any character set
    // for this which doesn't hold?
    
    for ( i = 'a'; i <= 'z' ; i++ ) id_letter[i] = true;
    for ( i = 'A'; i <= 'Z' ; i++ ) id_letter[i] = true;
    for ( i = '0'; i <= '9' ; i++ ) id_letter[i] = true;
    id_letter['_'] = true;
}

// Initialize the table of keywords.
void init_keywords() {
    vector<string> empty;

    init_multiples();
    init_only_once();
    init_line_starters();
    init_wipers();

    // Sprintf is unusual in that it doesn't alter the parse state at
    // all.
    keywords["sprintf"] = keyword(empty, empty, false, SPRINTF);

    // init the table
    init_id();
}