001/* Generated By:JavaCC: Do not edit this line. GMLParser.java */
002/*
003 * Copyright 2006 - 2012
004 *     Julien Baudry    <julien.baudry@graphstream-project.org>
005 *     Antoine Dutot    <antoine.dutot@graphstream-project.org>
006 *     Yoann Pigné              <yoann.pigne@graphstream-project.org>
007 *     Guilhelm Savin   <guilhelm.savin@graphstream-project.org>
008 * 
009 * This file is part of GraphStream <http://graphstream-project.org>.
010 * 
011 * GraphStream is a library whose purpose is to handle static or dynamic
012 * graph, create them from scratch, file or any source and display them.
013 * 
014 * This program is free software distributed under the terms of two licenses, the
015 * CeCILL-C license that fits European law, and the GNU Lesser General Public
016 * License. You can  use, modify and/ or redistribute the software under the terms
017 * of the CeCILL-C license as circulated by CEA, CNRS and INRIA at the following
018 * URL <http://www.cecill.info> or under the terms of the GNU LGPL as published by
019 * the Free Software Foundation, either version 3 of the License, or (at your
020 * option) any later version.
021 * 
022 * This program is distributed in the hope that it will be useful, but WITHOUT ANY
023 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
024 * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
025 * 
026 * You should have received a copy of the GNU Lesser General Public License
027 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
028 * 
029 * The fact that you are presently reading this means that you have had
030 * knowledge of the CeCILL-C and LGPL licenses and that you accept their terms.
031 */
032package org.graphstream.stream.file.gml;
033
034import java.io.InputStream;
035import java.io.IOException;
036import java.io.Reader;
037
038import org.graphstream.stream.file.FileSourceGML;
039
040import org.graphstream.util.parser.ParseException;
041import org.graphstream.util.parser.Parser;
042import org.graphstream.util.parser.SimpleCharStream;
043import org.graphstream.util.parser.Token;
044import org.graphstream.util.parser.TokenMgrError;
045
046@SuppressWarnings("unused")
047public class GMLParser implements Parser, GMLParserConstants {
048        boolean inGraph = false;
049        GMLContext ctx;
050        boolean step;
051
052        public GMLParser(FileSourceGML gml, InputStream stream) {
053                this(stream);
054                this.ctx = new GMLContext(gml);
055        }
056
057        public GMLParser(FileSourceGML gml, Reader stream) {
058                this(stream);
059                this.ctx = new GMLContext(gml);
060        }
061
062        public boolean isInGraph() {
063                return inGraph;
064        }
065
066        public void open() throws IOException, ParseException {
067        }
068
069        public boolean next() throws IOException, ParseException {
070                KeyValues kv = null;
071                kv = nextEvents();
072                ctx.handleKeyValues(kv);
073
074                return (kv != null);
075        }
076
077        public boolean step() throws IOException, ParseException {
078                KeyValues kv = null;
079                step = false;
080
081                while ((kv = nextEvents()) != null && !step)
082                        ctx.handleKeyValues(kv);
083
084                if (kv != null)
085                        ctx.setNextStep(kv);
086
087                return (kv != null);
088        }
089
090        /**
091         * Closes the parser, closing the opened stream.
092         */
093        public void close() throws IOException {
094                jj_input_stream.close();
095        }
096
097        /*****************************************************************/
098        /* The parser. */
099        /*****************************************************************/
100
101        /** Unused rule, call it to slurp in the whole file. */
102        final public void start() throws ParseException {
103                list();
104        }
105
106        final public void all() throws ParseException, IOException {
107                KeyValues values = new KeyValues();
108                String key;
109                switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
110                case GRAPH:
111                        graphStart();
112                        ctx.setIsInGraph(true);
113                        ctx.setDirected(false);
114                        break;
115                case DIGRAPH:
116                        diGraphStart();
117                        ctx.setIsInGraph(true);
118                        ctx.setDirected(true);
119                        break;
120                default:
121                        jj_la1[0] = jj_gen;
122                        jj_consume_token(-1);
123                        throw new ParseException();
124                }
125                label_1: while (true) {
126                        switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
127                        case STRING:
128                        case KEY:
129                        case COMMENT:
130                                ;
131                                break;
132                        default:
133                                jj_la1[1] = jj_gen;
134                                break label_1;
135                        }
136                        key = keyValue(values);
137                        values.key = key;
138                        ctx.handleKeyValues(values);
139                        values.clear();
140                }
141                graphEnd();
142                values.key = null;
143                inGraph = false;
144                jj_consume_token(0);
145        }
146
147        final public void graphStart() throws ParseException {
148                jj_consume_token(GRAPH);
149                jj_consume_token(LSQBR);
150        }
151
152        final public void diGraphStart() throws ParseException {
153                jj_consume_token(DIGRAPH);
154                jj_consume_token(LSQBR);
155        }
156
157        final public void graphEnd() throws ParseException {
158                jj_consume_token(RSQBR);
159        }
160
161        /**
162         * The top-level method to be called by the file source. Returns a set of
163         * top-level key values or null if the end of the file was reached.
164         * 
165         * Top-level key values are nodes and edges as well as all key-values
166         * defined before and after the graph.
167         */
168        final public KeyValues nextEvents() throws ParseException {
169                KeyValues values = new KeyValues();
170                String key;
171                switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
172                case GRAPH:
173                        graphStart();
174                        values.key = null;
175                        ctx.setIsInGraph(true);
176                        ctx.setDirected(false);
177                        break;
178                case DIGRAPH:
179                        diGraphStart();
180                        values.key = null;
181                        ctx.setIsInGraph(true);
182                        ctx.setDirected(true);
183                        break;
184                case RSQBR:
185                        graphEnd();
186                        values.key = null;
187                        ctx.setIsInGraph(false);
188                        break;
189                case STRING:
190                case KEY:
191                case COMMENT:
192                        key = keyValue(values);
193                        values.key = key;
194                        break;
195                case 0:
196                        jj_consume_token(0);
197                        values = null;
198                        break;
199                default:
200                        jj_la1[2] = jj_gen;
201                        jj_consume_token(-1);
202                        throw new ParseException();
203                }
204                {
205                        if (true)
206                                return values;
207                }
208                throw new Error("Missing return statement in function");
209        }
210
211        /**
212         * A list of key values, all values are stored in a KeyValues object.
213         */
214        final public KeyValues list() throws ParseException {
215                KeyValues values = new KeyValues();
216                label_2: while (true) {
217                        switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
218                        case STRING:
219                        case KEY:
220                        case COMMENT:
221                                ;
222                                break;
223                        default:
224                                jj_la1[3] = jj_gen;
225                                break label_2;
226                        }
227                        keyValue(values);
228                }
229                {
230                        if (true)
231                                return values;
232                }
233                throw new Error("Missing return statement in function");
234        }
235
236        /**
237         * A set of key and value, the value can recursively be a list of
238         * key-values. Only the key-value list "graph [ ... ]" is not parsed by this
239         * rule, and parsed by another rules, so that the nextEvent() rule can be
240         * called repeatedly.
241         */
242        final public String keyValue(KeyValues values) throws ParseException {
243                Token k;
244                String key;
245                Object v;
246                boolean isGraph = false;
247                label_3: while (true) {
248                        switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
249                        case COMMENT:
250                                ;
251                                break;
252                        default:
253                                jj_la1[4] = jj_gen;
254                                break label_3;
255                        }
256                        jj_consume_token(COMMENT);
257                }
258                switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
259                case KEY:
260                        k = jj_consume_token(KEY);
261                        key = k.image;
262                        if (key.equalsIgnoreCase("step"))
263                                step = true;
264                        break;
265                case STRING:
266                        k = jj_consume_token(STRING);
267                        key = k.image.substring(1, k.image.length() - 2);
268                        break;
269                default:
270                        jj_la1[5] = jj_gen;
271                        jj_consume_token(-1);
272                        throw new ParseException();
273                }
274                v = value(key);
275                values.put(key, v);
276                values.line = k.beginLine;
277                values.column = k.beginColumn;
278                {
279                        if (true)
280                                return key;
281                }
282                throw new Error("Missing return statement in function");
283        }
284
285        /**
286         * A value for a key, either a number, a string or a recursive list of
287         * key-values.
288         */
289        final public Object value(String key) throws ParseException {
290                Token t;
291                Object val;
292                KeyValues kv;
293                switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
294                case REAL:
295                        t = jj_consume_token(REAL);
296                        if (t.image.indexOf('.') < 0)
297                                val = Integer.valueOf(t.image);
298                        else
299                                val = Double.valueOf(t.image);
300                        break;
301                case STRING:
302                        t = jj_consume_token(STRING);
303                        val = t.image.substring(1, t.image.length() - 1);
304                        break;
305                case KEY:
306                        t = jj_consume_token(KEY);
307                        val = t.image;
308                        break;
309                case LSQBR:
310                        jj_consume_token(LSQBR);
311                        kv = list();
312                        val = kv;
313                        jj_consume_token(RSQBR);
314                        break;
315                default:
316                        jj_la1[6] = jj_gen;
317                        jj_consume_token(-1);
318                        throw new ParseException();
319                }
320                {
321                        if (true)
322                                return val;
323                }
324                throw new Error("Missing return statement in function");
325        }
326
327        /** Generated Token Manager. */
328        public GMLParserTokenManager token_source;
329        SimpleCharStream jj_input_stream;
330        /** Current token. */
331        public Token token;
332        /** Next token. */
333        public Token jj_nt;
334        private int jj_ntk;
335        private int jj_gen;
336        final private int[] jj_la1 = new int[7];
337        static private int[] jj_la1_0;
338        static {
339                jj_la1_init_0();
340        }
341
342        private static void jj_la1_init_0() {
343                jj_la1_0 = new int[] { 0x3000, 0xc800, 0xfa01, 0xc800, 0x8000, 0x4800,
344                                0x4d00, };
345        }
346
347        /** Constructor with InputStream. */
348        public GMLParser(java.io.InputStream stream) {
349                this(stream, null);
350        }
351
352        /** Constructor with InputStream and supplied encoding */
353        public GMLParser(java.io.InputStream stream, String encoding) {
354                try {
355                        jj_input_stream = new SimpleCharStream(stream, encoding, 1, 1);
356                } catch (java.io.UnsupportedEncodingException e) {
357                        throw new RuntimeException(e);
358                }
359                token_source = new GMLParserTokenManager(jj_input_stream);
360                token = new Token();
361                jj_ntk = -1;
362                jj_gen = 0;
363                for (int i = 0; i < 7; i++)
364                        jj_la1[i] = -1;
365        }
366
367        /** Reinitialise. */
368        public void ReInit(java.io.InputStream stream) {
369                ReInit(stream, null);
370        }
371
372        /** Reinitialise. */
373        public void ReInit(java.io.InputStream stream, String encoding) {
374                try {
375                        jj_input_stream.ReInit(stream, encoding, 1, 1);
376                } catch (java.io.UnsupportedEncodingException e) {
377                        throw new RuntimeException(e);
378                }
379                token_source.ReInit(jj_input_stream);
380                token = new Token();
381                jj_ntk = -1;
382                jj_gen = 0;
383                for (int i = 0; i < 7; i++)
384                        jj_la1[i] = -1;
385        }
386
387        /** Constructor. */
388        public GMLParser(java.io.Reader stream) {
389                jj_input_stream = new SimpleCharStream(stream, 1, 1);
390                token_source = new GMLParserTokenManager(jj_input_stream);
391                token = new Token();
392                jj_ntk = -1;
393                jj_gen = 0;
394                for (int i = 0; i < 7; i++)
395                        jj_la1[i] = -1;
396        }
397
398        /** Reinitialise. */
399        public void ReInit(java.io.Reader stream) {
400                jj_input_stream.ReInit(stream, 1, 1);
401                token_source.ReInit(jj_input_stream);
402                token = new Token();
403                jj_ntk = -1;
404                jj_gen = 0;
405                for (int i = 0; i < 7; i++)
406                        jj_la1[i] = -1;
407        }
408
409        /** Constructor with generated Token Manager. */
410        public GMLParser(GMLParserTokenManager tm) {
411                token_source = tm;
412                token = new Token();
413                jj_ntk = -1;
414                jj_gen = 0;
415                for (int i = 0; i < 7; i++)
416                        jj_la1[i] = -1;
417        }
418
419        /** Reinitialise. */
420        public void ReInit(GMLParserTokenManager tm) {
421                token_source = tm;
422                token = new Token();
423                jj_ntk = -1;
424                jj_gen = 0;
425                for (int i = 0; i < 7; i++)
426                        jj_la1[i] = -1;
427        }
428
429        private Token jj_consume_token(int kind) throws ParseException {
430                Token oldToken;
431                if ((oldToken = token).next != null)
432                        token = token.next;
433                else
434                        token = token.next = token_source.getNextToken();
435                jj_ntk = -1;
436                if (token.kind == kind) {
437                        jj_gen++;
438                        return token;
439                }
440                token = oldToken;
441                jj_kind = kind;
442                throw generateParseException();
443        }
444
445        /** Get the next Token. */
446        final public Token getNextToken() {
447                if (token.next != null)
448                        token = token.next;
449                else
450                        token = token.next = token_source.getNextToken();
451                jj_ntk = -1;
452                jj_gen++;
453                return token;
454        }
455
456        /** Get the specific Token. */
457        final public Token getToken(int index) {
458                Token t = token;
459                for (int i = 0; i < index; i++) {
460                        if (t.next != null)
461                                t = t.next;
462                        else
463                                t = t.next = token_source.getNextToken();
464                }
465                return t;
466        }
467
468        private int jj_ntk() {
469                if ((jj_nt = token.next) == null)
470                        return (jj_ntk = (token.next = token_source.getNextToken()).kind);
471                else
472                        return (jj_ntk = jj_nt.kind);
473        }
474
475        private java.util.List<int[]> jj_expentries = new java.util.ArrayList<int[]>();
476        private int[] jj_expentry;
477        private int jj_kind = -1;
478
479        /** Generate ParseException. */
480        public ParseException generateParseException() {
481                jj_expentries.clear();
482                boolean[] la1tokens = new boolean[16];
483                if (jj_kind >= 0) {
484                        la1tokens[jj_kind] = true;
485                        jj_kind = -1;
486                }
487                for (int i = 0; i < 7; i++) {
488                        if (jj_la1[i] == jj_gen) {
489                                for (int j = 0; j < 32; j++) {
490                                        if ((jj_la1_0[i] & (1 << j)) != 0) {
491                                                la1tokens[j] = true;
492                                        }
493                                }
494                        }
495                }
496                for (int i = 0; i < 16; i++) {
497                        if (la1tokens[i]) {
498                                jj_expentry = new int[1];
499                                jj_expentry[0] = i;
500                                jj_expentries.add(jj_expentry);
501                        }
502                }
503                int[][] exptokseq = new int[jj_expentries.size()][];
504                for (int i = 0; i < jj_expentries.size(); i++) {
505                        exptokseq[i] = jj_expentries.get(i);
506                }
507                return new ParseException(token, exptokseq, tokenImage);
508        }
509
510        /** Enable tracing. */
511        final public void enable_tracing() {
512        }
513
514        /** Disable tracing. */
515        final public void disable_tracing() {
516        }
517
518}