001/* Generated By:JJTree&JavaCC: Do not edit this line. MatrixParser.java */
002/* Parser for matrices written in matlab format.
003
004 Copyright (c) 1998-2008 The Regents of the University of California.
005 All rights reserved.
006 Permission is hereby granted, without written agreement and without
007 license or royalty fees, to use, copy, modify, and distribute this
008 software and its documentation for any purpose, provided that the above
009 copyright notice and the following two paragraphs appear in all copies
010 of this software.
011
012 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
013 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
014 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
015 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
016 SUCH DAMAGE.
017
018 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
019 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
020 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
021 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
022 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
023 ENHANCEMENTS, OR MODIFICATIONS.
024
025                                        PT_COPYRIGHT_VERSION_2
026                                        COPYRIGHTENDKEY
027*/
028package ptolemy.data.expr;
029
030import java.util.Vector;
031
032import ptolemy.kernel.util.IllegalActionException;
033
034//////////////////////////////////////////////////////////////////////////
035//// MatrixParser
036/**
037This Class provides a parser for read matrices in matlab format.
038
039@author  Bart Kienhuis
040@version $Id$
041@since Ptolemy II 1.0
042@Pt.ProposedRating Red
043@Pt.AcceptedRating Red (kienhuis)
044*/
045@SuppressWarnings("static-access")
046public class MatrixParser/*@bgen(jjtree)*/ implements MatrixParserTreeConstants,
047        MatrixParserConstants {/*@bgen(jjtree)*/
048    protected JJTMatrixParserState jjtree = new JJTMatrixParserState();
049
050    /** Read a Matrix from File.
051     *  @return A Vector containing the contents of the file.
052     *  @exception IllegalActionException If an error occurs during parsing.
053     */
054    public Vector readMatrix() throws IllegalActionException {
055        Vector m = null;
056        try {
057            m = matrix();
058        } catch (Exception e) {
059            throw new IllegalActionException(e.getMessage());
060        }
061        return m;
062    }
063
064    final public Vector matrix() throws ParseException {
065        /*@bgen(jjtree) matrix */
066        SimpleNode jjtn000 = new SimpleNode(this, JJTMATRIX);
067        boolean jjtc000 = true;
068        jjtree.openNodeScope(jjtn000);
069        Vector m = new Vector();
070        ;
071        Vector row = null;
072        try {
073            jj_consume_token(13);
074            switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
075            case CONSTANT:
076            case SIGN:
077                row = row();
078                break;
079            default:
080                jj_la1[0] = jj_gen;
081                ;
082            }
083            m.add(row);
084            label_1: while (true) {
085                switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
086                case 14:
087                    ;
088                    break;
089                default:
090                    jj_la1[1] = jj_gen;
091                    break label_1;
092                }
093                jj_consume_token(14);
094                row = row();
095                m.add(row);
096            }
097            jj_consume_token(15);
098            jjtree.closeNodeScope(jjtn000, true);
099            jjtc000 = false;
100            {
101                if (true) {
102                    return m;
103                }
104            }
105        } catch (Throwable jjte000) {
106            if (jjtc000) {
107                jjtree.clearNodeScope(jjtn000);
108                jjtc000 = false;
109            } else {
110                jjtree.popNode();
111            }
112            if (jjte000 instanceof RuntimeException) {
113                {
114                    if (true) {
115                        throw (RuntimeException) jjte000;
116                    }
117                }
118            }
119            if (jjte000 instanceof ParseException) {
120                {
121                    if (true) {
122                        throw (ParseException) jjte000;
123                    }
124                }
125            }
126            {
127                if (true) {
128                    throw (Error) jjte000;
129                }
130            }
131        } finally {
132            if (jjtc000) {
133                jjtree.closeNodeScope(jjtn000, true);
134            }
135        }
136        throw new Error("Missing return statement in function");
137    }
138
139    final public Vector row() throws ParseException {
140        /*@bgen(jjtree) row */
141        SimpleNode jjtn000 = new SimpleNode(this, JJTROW);
142        boolean jjtc000 = true;
143        jjtree.openNodeScope(jjtn000);
144        Vector row = new Vector();
145        double element;
146        try {
147            element = element();
148            row.add(new Double(element));
149            label_2: while (true) {
150                switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
151                case 16:
152                    ;
153                    break;
154                default:
155                    jj_la1[2] = jj_gen;
156                    break label_2;
157                }
158                jj_consume_token(16);
159                element = element();
160                row.add(new Double(element));
161            }
162            jjtree.closeNodeScope(jjtn000, true);
163            jjtc000 = false;
164            {
165                if (true) {
166                    return row;
167                }
168            }
169        } catch (Throwable jjte000) {
170            if (jjtc000) {
171                jjtree.clearNodeScope(jjtn000);
172                jjtc000 = false;
173            } else {
174                jjtree.popNode();
175            }
176            if (jjte000 instanceof RuntimeException) {
177                {
178                    if (true) {
179                        throw (RuntimeException) jjte000;
180                    }
181                }
182            }
183            if (jjte000 instanceof ParseException) {
184                {
185                    if (true) {
186                        throw (ParseException) jjte000;
187                    }
188                }
189            }
190            {
191                if (true) {
192                    throw (Error) jjte000;
193                }
194            }
195        } finally {
196            if (jjtc000) {
197                jjtree.closeNodeScope(jjtn000, true);
198            }
199        }
200        throw new Error("Missing return statement in function");
201    }
202
203    final public double element() throws ParseException {
204        /*@bgen(jjtree) element */
205        SimpleNode jjtn000 = new SimpleNode(this, JJTELEMENT);
206        boolean jjtc000 = true;
207        jjtree.openNodeScope(jjtn000);
208        Token i;
209        Token s;
210        int sign = 1;
211        double value;
212        try {
213            switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
214            case SIGN:
215                s = jj_consume_token(SIGN);
216                if ((s.image).equals("-")) {
217                    sign = -1 * sign;
218                }
219                break;
220            default:
221                jj_la1[3] = jj_gen;
222                ;
223            }
224            i = jj_consume_token(CONSTANT);
225            value = sign * (new Double(i.image)).doubleValue();
226            jjtree.closeNodeScope(jjtn000, true);
227            jjtc000 = false;
228            {
229                if (true) {
230                    return value;
231                }
232            }
233        } finally {
234            if (jjtc000) {
235                jjtree.closeNodeScope(jjtn000, true);
236            }
237        }
238        throw new Error("Missing return statement in function");
239    }
240
241    /** Generated Token Manager. */
242    public MatrixParserTokenManager token_source;
243    SimpleCharStream jj_input_stream;
244    /** Current token. */
245    public Token token;
246    /** Next token. */
247    public Token jj_nt;
248    private int jj_ntk;
249    private int jj_gen;
250    final private int[] jj_la1 = new int[4];
251    static private int[] jj_la1_0;
252    static {
253        jj_la1_init_0();
254    }
255
256    private static void jj_la1_init_0() {
257        jj_la1_0 = new int[] { 0x820, 0x4000, 0x10000, 0x800, };
258    }
259
260    /** Constructor with InputStream. */
261    public MatrixParser(java.io.InputStream stream) {
262        this(stream, null);
263    }
264
265    /** Constructor with InputStream and supplied encoding */
266    public MatrixParser(java.io.InputStream stream, String encoding) {
267        try {
268            jj_input_stream = new SimpleCharStream(stream, encoding, 1, 1);
269        } catch (java.io.UnsupportedEncodingException e) {
270            throw new RuntimeException(e);
271        }
272        token_source = new MatrixParserTokenManager(jj_input_stream);
273        token = new Token();
274        jj_ntk = -1;
275        jj_gen = 0;
276        for (int i = 0; i < 4; i++) {
277            jj_la1[i] = -1;
278        }
279    }
280
281    /** Reinitialise. */
282    public void ReInit(java.io.InputStream stream) {
283        ReInit(stream, null);
284    }
285
286    /** Reinitialise. */
287    public void ReInit(java.io.InputStream stream, String encoding) {
288        try {
289            jj_input_stream.ReInit(stream, encoding, 1, 1);
290        } catch (java.io.UnsupportedEncodingException e) {
291            throw new RuntimeException(e);
292        }
293        token_source.ReInit(jj_input_stream);
294        token = new Token();
295        jj_ntk = -1;
296        jjtree.reset();
297        jj_gen = 0;
298        for (int i = 0; i < 4; i++) {
299            jj_la1[i] = -1;
300        }
301    }
302
303    /** Constructor. */
304    public MatrixParser(java.io.Reader stream) {
305        jj_input_stream = new SimpleCharStream(stream, 1, 1);
306        token_source = new MatrixParserTokenManager(jj_input_stream);
307        token = new Token();
308        jj_ntk = -1;
309        jj_gen = 0;
310        for (int i = 0; i < 4; i++) {
311            jj_la1[i] = -1;
312        }
313    }
314
315    /** Reinitialise. */
316    public void ReInit(java.io.Reader stream) {
317        jj_input_stream.ReInit(stream, 1, 1);
318        token_source.ReInit(jj_input_stream);
319        token = new Token();
320        jj_ntk = -1;
321        jjtree.reset();
322        jj_gen = 0;
323        for (int i = 0; i < 4; i++) {
324            jj_la1[i] = -1;
325        }
326    }
327
328    /** Constructor with generated Token Manager. */
329    public MatrixParser(MatrixParserTokenManager tm) {
330        token_source = tm;
331        token = new Token();
332        jj_ntk = -1;
333        jj_gen = 0;
334        for (int i = 0; i < 4; i++) {
335            jj_la1[i] = -1;
336        }
337    }
338
339    /** Reinitialise. */
340    public void ReInit(MatrixParserTokenManager tm) {
341        token_source = tm;
342        token = new Token();
343        jj_ntk = -1;
344        jjtree.reset();
345        jj_gen = 0;
346        for (int i = 0; i < 4; i++) {
347            jj_la1[i] = -1;
348        }
349    }
350
351    private Token jj_consume_token(int kind) throws ParseException {
352        Token oldToken;
353        if ((oldToken = token).next != null) {
354            token = token.next;
355        } else {
356            token = token.next = token_source.getNextToken();
357        }
358        jj_ntk = -1;
359        if (token.kind == kind) {
360            jj_gen++;
361            return token;
362        }
363        token = oldToken;
364        jj_kind = kind;
365        throw generateParseException();
366    }
367
368    /** Get the next Token. */
369    final public Token getNextToken() {
370        if (token.next != null) {
371            token = token.next;
372        } else {
373            token = token.next = token_source.getNextToken();
374        }
375        jj_ntk = -1;
376        jj_gen++;
377        return token;
378    }
379
380    /** Get the specific Token. */
381    final public Token getToken(int index) {
382        Token t = token;
383        for (int i = 0; i < index; i++) {
384            if (t.next != null) {
385                t = t.next;
386            } else {
387                t = t.next = token_source.getNextToken();
388            }
389        }
390        return t;
391    }
392
393    private int jj_ntk() {
394        if ((jj_nt = token.next) == null) {
395            return (jj_ntk = (token.next = token_source.getNextToken()).kind);
396        } else {
397            return (jj_ntk = jj_nt.kind);
398        }
399    }
400
401    private java.util.List<int[]> jj_expentries = new java.util.ArrayList<int[]>();
402    private int[] jj_expentry;
403    private int jj_kind = -1;
404
405    /** Generate ParseException. */
406    public ParseException generateParseException() {
407        jj_expentries.clear();
408        boolean[] la1tokens = new boolean[17];
409        if (jj_kind >= 0) {
410            la1tokens[jj_kind] = true;
411            jj_kind = -1;
412        }
413        for (int i = 0; i < 4; i++) {
414            if (jj_la1[i] == jj_gen) {
415                for (int j = 0; j < 32; j++) {
416                    if ((jj_la1_0[i] & (1 << j)) != 0) {
417                        la1tokens[j] = true;
418                    }
419                }
420            }
421        }
422        for (int i = 0; i < 17; i++) {
423            if (la1tokens[i]) {
424                jj_expentry = new int[1];
425                jj_expentry[0] = i;
426                jj_expentries.add(jj_expentry);
427            }
428        }
429        int[][] exptokseq = new int[jj_expentries.size()][];
430        for (int i = 0; i < jj_expentries.size(); i++) {
431            exptokseq[i] = jj_expentries.get(i);
432        }
433        return new ParseException(token, exptokseq, tokenImage);
434    }
435
436    /** Enable tracing. */
437    final public void enable_tracing() {
438    }
439
440    /** Disable tracing. */
441    final public void disable_tracing() {
442    }
443
444}