0
|
1 |
/* OGDL, Ordered Graph Data Language
|
|
2 |
* (c) R.Veen, 2002-2006.
|
|
3 |
* License: see http://ogdl.org/ (similar to zlib)
|
|
4 |
*/
|
|
5 |
|
|
6 |
package ogdl;
|
|
7 |
|
|
8 |
|
|
9 |
import java.io.IOException;
|
|
10 |
import java.io.Reader;
|
|
11 |
|
|
12 |
import ogdl.parser.IParserHandler;
|
|
13 |
|
|
14 |
|
|
15 |
/** Parser for the OGDL text format
|
|
16 |
|
|
17 |
Non recursive parser. Doesn't need a special Reader.
|
|
18 |
|
|
19 |
<ul>
|
|
20 |
<li>All functions return boolean if the item exists, false if not. If false, they don't modify anything.
|
|
21 |
<li>Relevant functions send an event thru the ParserEvent object.
|
|
22 |
<li>Uses a special ParserReader, not a java.io.PushBackReader.
|
|
23 |
<li>Stores locally one leaf/string to process escape sequences. The minimum content element returned
|
|
24 |
is one leaf (block or not).
|
|
25 |
<li>Leaf type is not preserved: simple, quoted or blocks are all returned as
|
|
26 |
</ul>
|
|
27 |
|
|
28 |
date: Nov 2002.
|
|
29 |
|
|
30 |
$Id: OgdlParser.java,v 1.8 2005/06/06 16:10:53 tbryan Exp $
|
|
31 |
*/
|
|
32 |
|
|
33 |
public final class OgdlParser
|
|
34 |
{
|
|
35 |
public final static int CONTENT = 1; /* primary events, content related */
|
|
36 |
public final static int FORMAT = 2; /* secondary events, format related */
|
|
37 |
|
|
38 |
protected Reader r;
|
|
39 |
int line=1, level=-1, groups[], lineInd[], lineLevel, groupLevel, groupIndex=0;
|
|
40 |
|
|
41 |
int savedSpaces = 0;
|
|
42 |
boolean savedNewline = false;
|
|
43 |
|
12
|
44 |
boolean continueParsing = true;
|
|
45 |
|
0
|
46 |
private IParserHandler event;
|
|
47 |
|
|
48 |
/* leaf buffer, for first level string processing: escape sequences */
|
|
49 |
StringBuffer sb;
|
|
50 |
|
|
51 |
public OgdlParser (Reader r, IParserHandler event) throws Exception
|
|
52 |
{
|
|
53 |
this.r = r;
|
|
54 |
this.event = event;
|
|
55 |
sb = new StringBuffer();
|
|
56 |
groups = new int[64];
|
|
57 |
lineInd = new int[64];
|
|
58 |
}
|
|
59 |
|
|
60 |
/* XXX: here an efficient string reader ... */
|
|
61 |
|
|
62 |
/** space = char_space+
|
|
63 |
|
|
64 |
Returns the number of spaces. Mixing of tabs and spaces is not
|
|
65 |
allowed as indentation, but not checked yet.
|
|
66 |
*/
|
|
67 |
|
|
68 |
public int space() throws IOException
|
|
69 |
{
|
|
70 |
int i;
|
|
71 |
|
|
72 |
if ( savedSpaces != 0 ) {
|
|
73 |
i = savedSpaces;
|
|
74 |
savedSpaces = 0;
|
|
75 |
return i;
|
|
76 |
}
|
|
77 |
|
|
78 |
i = 0;
|
|
79 |
while ( Characters.is( read(), Characters.SPACE ) )
|
|
80 |
i++;
|
|
81 |
|
|
82 |
unread();
|
|
83 |
return i;
|
|
84 |
}
|
|
85 |
|
|
86 |
/** newline ::= ( CR LF) | CR | LF */
|
|
87 |
|
|
88 |
public boolean newline() throws IOException
|
|
89 |
{
|
|
90 |
line++;
|
|
91 |
|
|
92 |
if ( savedNewline ) {
|
|
93 |
savedNewline = false;
|
|
94 |
return true;
|
|
95 |
}
|
|
96 |
|
|
97 |
int c = read();
|
|
98 |
|
|
99 |
if ( c == '\r' ) {
|
|
100 |
c = read();
|
|
101 |
if ( c != '\n' )
|
|
102 |
unread();
|
|
103 |
return true;
|
|
104 |
}
|
|
105 |
else if ( c == '\n' )
|
|
106 |
return true;
|
|
107 |
|
|
108 |
line--;
|
|
109 |
unread();
|
|
110 |
return false;
|
|
111 |
}
|
|
112 |
|
|
113 |
|
|
114 |
/** eos ::= end of stream (no more chars) */
|
|
115 |
|
|
116 |
public boolean eos() throws IOException
|
|
117 |
{
|
|
118 |
int c = read();
|
|
119 |
unread();
|
|
120 |
|
|
121 |
return c == -1 || c == '\f' ? true: false;
|
|
122 |
}
|
|
123 |
|
|
124 |
/** word
|
|
125 |
*/
|
|
126 |
|
|
127 |
public boolean word() throws IOException
|
|
128 |
{
|
|
129 |
int i = 0, c;
|
|
130 |
|
|
131 |
sb.setLength(0);
|
|
132 |
|
|
133 |
while ( Characters.is( c=read(), Characters.WORD ) ) {
|
|
134 |
sb.append((char) c);
|
|
135 |
i++;
|
|
136 |
}
|
|
137 |
|
|
138 |
unread();
|
|
139 |
return i>0 ? true:false;
|
|
140 |
}
|
|
141 |
|
|
142 |
public boolean comment() throws IOException
|
|
143 |
{
|
|
144 |
int i = 0, c;
|
|
145 |
|
|
146 |
sb.setLength(0);
|
|
147 |
|
|
148 |
if ( (c=read()) == '#' ) {
|
|
149 |
sb.append((char) c);
|
|
150 |
i++;
|
|
151 |
while ( ! Characters.is( c=read(), Characters.BREAK ) ) {
|
|
152 |
sb.append((char) c);
|
|
153 |
i++;
|
|
154 |
}
|
|
155 |
}
|
|
156 |
|
|
157 |
unread();
|
|
158 |
return i>0 ? true:false;
|
|
159 |
}
|
|
160 |
|
|
161 |
/** separator
|
|
162 |
*/
|
|
163 |
|
|
164 |
public boolean separator() throws IOException
|
|
165 |
{
|
|
166 |
int i = 0, c;
|
|
167 |
|
|
168 |
sb.setLength(0);
|
|
169 |
|
|
170 |
if ( Characters.isSeparator( (char) (c=read()) ) ) {
|
|
171 |
sb.append((char) c);
|
|
172 |
i++;
|
|
173 |
}
|
|
174 |
else
|
|
175 |
unread();
|
|
176 |
|
|
177 |
return i>0 ? true:false;
|
|
178 |
}
|
|
179 |
|
|
180 |
public boolean quoted() throws IOException
|
|
181 |
{
|
|
182 |
int q = read();
|
|
183 |
int c, cc=0;
|
|
184 |
int skip=0;
|
|
185 |
|
|
186 |
if ( q != '"' && q != '\'' ) {
|
|
187 |
unread();
|
|
188 |
return false;
|
|
189 |
}
|
|
190 |
|
|
191 |
sb.setLength(0);
|
|
192 |
|
|
193 |
while ( true ) {
|
|
194 |
c = read();
|
|
195 |
if ( c == -1 || ( c == q && cc != '\\' ) )
|
|
196 |
break;
|
|
197 |
|
|
198 |
if (skip > 0) {
|
|
199 |
if ( Characters.isSpace((char) c) ) {
|
|
200 |
skip--;
|
|
201 |
cc = c;
|
|
202 |
continue;
|
|
203 |
}
|
|
204 |
else
|
|
205 |
skip = 0;
|
|
206 |
}
|
|
207 |
|
|
208 |
sb.append((char) c);
|
|
209 |
cc = c;
|
|
210 |
if ( c == '\n' )
|
|
211 |
skip = lineInd[level] + 1;
|
|
212 |
}
|
|
213 |
return true;
|
|
214 |
}
|
|
215 |
|
|
216 |
public boolean block() throws IOException
|
|
217 |
{
|
|
218 |
int c = read();
|
|
219 |
int m,i;
|
|
220 |
int ind=-1;
|
|
221 |
|
|
222 |
if ( c != '\\' ) {
|
|
223 |
unread();
|
|
224 |
return false;
|
|
225 |
}
|
|
226 |
|
|
227 |
if ( !newline() )
|
|
228 |
return false; // loosing one char !
|
|
229 |
|
|
230 |
sb.setLength(0);
|
|
231 |
|
|
232 |
while (true) {
|
|
233 |
m = space();
|
|
234 |
if (m <= lineInd[lineLevel]) {
|
|
235 |
if (newline())
|
|
236 |
sb.append('\n');
|
|
237 |
else {
|
|
238 |
savedSpaces = m;
|
|
239 |
break;
|
|
240 |
}
|
|
241 |
}
|
|
242 |
else {
|
|
243 |
if (ind<0) ind = m;
|
|
244 |
for (i=ind; i<m; i++)
|
|
245 |
sb.append(' ');
|
|
246 |
while (true) {
|
|
247 |
c = read();
|
|
248 |
if ( c == -1 || c == '\n' ) {
|
|
249 |
sb.append('\n');
|
|
250 |
break;
|
|
251 |
}
|
|
252 |
sb.append((char)c);
|
|
253 |
}
|
|
254 |
if (c == -1)
|
|
255 |
break;
|
|
256 |
}
|
|
257 |
}
|
|
258 |
return true;
|
|
259 |
}
|
|
260 |
|
|
261 |
public int node() throws IOException
|
|
262 |
{
|
|
263 |
if (block()) {
|
12
|
264 |
continueParsing = event.event(CONTENT,level, sb.toString());
|
0
|
265 |
return -1;
|
|
266 |
}
|
|
267 |
|
|
268 |
if (!quoted() && !comment() && !word() && !separator())
|
|
269 |
return 0;
|
|
270 |
|
|
271 |
int len = sb.length();
|
|
272 |
char c = len>0? sb.charAt(0): 0;
|
|
273 |
|
|
274 |
if ( c == '(' ) {
|
|
275 |
groups[groupIndex++] = level;
|
|
276 |
event.event(FORMAT,level, "(");
|
|
277 |
return 1;
|
|
278 |
}
|
|
279 |
else if ( c == ')' ) {
|
|
280 |
groups[--groupIndex] = level;
|
|
281 |
event.event(FORMAT,level, ")");
|
|
282 |
return 1;
|
|
283 |
}
|
|
284 |
else if (c == ',') {
|
|
285 |
// reset level to the first node of this line
|
|
286 |
if (groupIndex==0)
|
|
287 |
level=lineLevel;
|
|
288 |
else
|
|
289 |
level = groups[groupIndex-1];
|
|
290 |
return 1;
|
|
291 |
}
|
|
292 |
else if (c == '#')
|
|
293 |
return 1;
|
|
294 |
|
|
295 |
if (len != 0)
|
12
|
296 |
continueParsing = event.event(CONTENT, level, sb.toString());
|
0
|
297 |
|
|
298 |
level++;
|
|
299 |
|
|
300 |
return 1;
|
|
301 |
}
|
|
302 |
|
|
303 |
|
|
304 |
/** line() : space? ( node ( space node )* )? space? newline
|
|
305 |
|
|
306 |
returns:
|
|
307 |
0 : EOS
|
|
308 |
1 : more
|
|
309 |
*/
|
|
310 |
|
|
311 |
public boolean line() throws Exception
|
|
312 |
{
|
|
313 |
int i = space();
|
|
314 |
if ( newline() ) return true;
|
|
315 |
if ( eos () ) return false;
|
|
316 |
|
|
317 |
if ( level < 0 ) {
|
|
318 |
lineInd[0]=i;
|
|
319 |
lineLevel=0;
|
|
320 |
}
|
|
321 |
else if ( i > lineInd[lineLevel] ) {
|
|
322 |
lineInd[++lineLevel] = i;
|
|
323 |
}
|
|
324 |
else if ( i < lineInd[lineLevel] ) {
|
|
325 |
while (lineLevel != 0) {
|
|
326 |
if ( i >= lineInd[lineLevel] )
|
|
327 |
break;
|
|
328 |
lineLevel--;
|
|
329 |
}
|
|
330 |
}
|
|
331 |
|
|
332 |
level = lineLevel;
|
|
333 |
|
|
334 |
while ( (i=node()) > 0)
|
|
335 |
space();
|
12
|
336 |
if (!continueParsing)
|
|
337 |
return false;
|
0
|
338 |
|
|
339 |
if (i > 0) { /* after a block don't eat spaces */
|
|
340 |
space();
|
|
341 |
newline();
|
|
342 |
}
|
|
343 |
|
|
344 |
if (eos())
|
|
345 |
return false;
|
|
346 |
return true;
|
|
347 |
}
|
|
348 |
|
|
349 |
/** parse ::= line* */
|
|
350 |
|
|
351 |
public void parse() throws Exception
|
|
352 |
{
|
|
353 |
try {
|
|
354 |
while ( line() ) ;
|
|
355 |
}
|
|
356 |
catch (SyntaxException e)
|
|
357 |
{
|
|
358 |
event.error(e,e.line);
|
|
359 |
}
|
|
360 |
}
|
|
361 |
|
|
362 |
|
|
363 |
|
|
364 |
/* -----------------------------------------------------
|
|
365 |
Small isolation layer.
|
|
366 |
*/
|
|
367 |
|
|
368 |
boolean unGetFlag = false;
|
|
369 |
int unChar;
|
|
370 |
|
|
371 |
public void unread(int c)
|
|
372 |
{
|
|
373 |
unGetFlag = true;
|
|
374 |
unChar = c;
|
|
375 |
}
|
|
376 |
|
|
377 |
private int read() throws IOException
|
|
378 |
{
|
|
379 |
if (unGetFlag) {
|
|
380 |
unGetFlag = false;
|
|
381 |
return unChar;
|
|
382 |
}
|
|
383 |
return unChar = r.read();
|
|
384 |
}
|
|
385 |
|
|
386 |
private void unread()
|
|
387 |
{
|
|
388 |
unGetFlag = true;
|
|
389 |
}
|
|
390 |
|
|
391 |
}
|
|
392 |
|
|
393 |
|