Mercurial > ~dholland > hg > ag > index.cgi
comparison anagram/agcore/ftpar.h @ 0:13d2b8934445
Import AnaGram (near-)release tree into Mercurial.
author | David A. Holland |
---|---|
date | Sat, 22 Dec 2007 17:52:45 -0500 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:13d2b8934445 |
---|---|
1 /* | |
2 * AnaGram, A System for Syntax Directed Programming | |
3 * Copyright 1993-2002 Parsifal Software. All Rights Reserved. | |
4 * See the file COPYING for license and usage terms. | |
5 * | |
6 * ftpar.h | |
7 */ | |
8 | |
9 #ifndef FTPAR_H | |
10 #define FTPAR_H | |
11 | |
12 class Token; // from token.h | |
13 struct tsd; // from tsd.h | |
14 #include "agarray.h" | |
15 #include "agsignal.h" | |
16 #include "agstack.h" | |
17 #include "cint.h" | |
18 | |
19 | |
20 extern AgArray<unsigned> traceCounts; | |
21 | |
22 int precedes(cint a, cint b); | |
23 | |
24 | |
25 class FtParser { | |
26 public: | |
27 enum ProcessState { | |
28 ready, | |
29 running, | |
30 finished, | |
31 syntaxError, | |
32 unexpectedEndOfFile, | |
33 selectionRequired, | |
34 selectionError | |
35 }; | |
36 | |
37 class State { | |
38 public: | |
39 friend class FtParser; | |
40 friend class FtParserDc; | |
41 friend class FtParserReductionDc; | |
42 friend class FileTraceWindow; | |
43 friend class GTView; | |
44 friend class GTWindow; | |
45 friend class TokenMenuDc; | |
46 | |
47 int number; | |
48 int line, column, charPos; | |
49 const unsigned char *pointer; | |
50 unsigned token; | |
51 | |
52 public: | |
53 State(char *p) | |
54 : number(0) | |
55 , line(0) | |
56 , column(0) | |
57 , charPos(0) | |
58 , pointer((const unsigned char*) p) | |
59 , token(0) | |
60 {} | |
61 State() | |
62 : number(0) | |
63 , line(0) | |
64 , column(0) | |
65 , charPos(0) | |
66 , pointer(0) | |
67 , token(0) | |
68 {} | |
69 State(unsigned sn, unsigned tn) | |
70 : number(sn) | |
71 , line(0) | |
72 , column(0) | |
73 , charPos(0) | |
74 , pointer(0) | |
75 , token(tn) | |
76 {} | |
77 | |
78 cint position() { return cint(charPos,line); } | |
79 int operator < (const State &s) const { return number < s.number; } | |
80 }; | |
81 | |
82 struct Transaction { | |
83 int count; | |
84 State state; | |
85 | |
86 Transaction() : count(0) {} | |
87 Transaction(int c, State s) : count(c), state(s) {} | |
88 int operator < (const Transaction &t) const { return count < t.count; } | |
89 }; | |
90 | |
91 struct StackChanged { | |
92 int newSize; | |
93 StackChanged() : newSize(0) {} | |
94 StackChanged(int n) : newSize(n) {} | |
95 }; | |
96 | |
97 AgString text; | |
98 State state; | |
99 State reductionState; | |
100 | |
101 tsd *initialStack; | |
102 | |
103 AgStack<State> stateStack; | |
104 AgStack<State> auxStack; | |
105 AgStack<Transaction> transStack; | |
106 const unsigned char *lookAhead; | |
107 const unsigned char *endPointer; | |
108 unsigned inputToken; | |
109 int nNullShifts; | |
110 ProcessState processState; | |
111 static const char *processStateText[]; | |
112 | |
113 unsigned actionParameter; | |
114 | |
115 int ruleLength; | |
116 int ruleToReduce; | |
117 int reductionIndex; | |
118 int reductionSelection; | |
119 | |
120 //DcRef<FtParserDc> displayControl; | |
121 struct BroadcastStackChanged : public Broadcaster<StackChanged> { | |
122 FtParser &parser; | |
123 BroadcastStackChanged(FtParser &p) : parser(p) {} | |
124 void operator () (unsigned n) const { | |
125 broadcast(FtParser::StackChanged(n)); | |
126 } | |
127 }; | |
128 | |
129 BroadcastStackChanged stackChanged; | |
130 //DcRef<rule_stack_dc> ruleControl; | |
131 //tsd *itemStack; | |
132 | |
133 int testingKeyword; | |
134 | |
135 void track(void); | |
136 void dispatchReductionToken(); | |
137 | |
138 void shiftTerminal(); | |
139 void shiftTerminalAndAccept(void); | |
140 void shiftTerminalAndReduce(); | |
141 void reduce(void); | |
142 void skip(void); | |
143 void shiftNull(void); | |
144 void error(); | |
145 void accept(); | |
146 | |
147 int shiftNonterminalAndReduce(); | |
148 int shiftNonterminal(); | |
149 int shiftNonterminalAndAccept(); | |
150 | |
151 static void (FtParser::*terminalAction[11])(); | |
152 static int (FtParser::*nonterminalAction[4])(); | |
153 | |
154 Token keyToken(void); | |
155 void getToken(); | |
156 unsigned inspectToken(); | |
157 void completeReduction(int reductionToken); | |
158 void completeReduction(); | |
159 int validToken(unsigned, unsigned); | |
160 int validSelection(unsigned, unsigned); | |
161 void requestSelection(int actionParameter); | |
162 | |
163 public: | |
164 FtParser &parseTo(unsigned char *target); | |
165 FtParser &parseTo(cint *target); | |
166 FtParser &parse(const char *fragment); | |
167 FtParser &prime(const char *fragment); | |
168 FtParser &parse(); | |
169 void parseAction(); | |
170 void stepToken(unsigned); | |
171 void parseToken(unsigned); | |
172 | |
173 FtParser &step(); | |
174 FtParser &step(char *fragment); | |
175 FtParser &reset(); | |
176 cint location() { return cint(state.charPos, state.line); } | |
177 cint displayLocation() { return cint(state.column, state.line); } | |
178 | |
179 FtParser &ignoreKeyword(int k) { | |
180 testingKeyword = k; | |
181 return *this; | |
182 } | |
183 | |
184 tsd *x1x_new(); | |
185 | |
186 FtParser(AgString t); | |
187 FtParser(); | |
188 FtParser(tsd *initialStack_); | |
189 ~FtParser(); | |
190 }; | |
191 | |
192 | |
193 #endif /* FTPAR_H */ |