/*@bgen(jjtree) Generated By:JJTree: Do not edit this line. /home/hudson/jobs/ptII/workspace/ptolemy/data/expr/MatrixParser.jj */ /*@egen*//* -*- Mode: java -*- */ /* Input file to JJTree and JavaCC to generate Matrix Parser */ options { LOOKAHEAD=1; STATIC = true; } PARSER_BEGIN(MatrixParser) /* Parser for matrices written in matlab format. Copyright (c) 1998-2008 The Regents of the University of California. All rights reserved. Permission is hereby granted, without written agreement and without license or royalty fees, to use, copy, modify, and distribute this software and its documentation for any purpose, provided that the above copyright notice and the following two paragraphs appear in all copies of this software. IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. PT_COPYRIGHT_VERSION_2 COPYRIGHTENDKEY */ package ptolemy.data.expr; import ptolemy.kernel.util.IllegalActionException; import java.util.Vector; import java.io.ByteArrayInputStream; import java.io.FileReader; ////////////////////////////////////////////////////////////////////////// //// MatrixParser /** This Class provides a parser for read matrices in matlab format. @author Bart Kienhuis @version $Id: MatrixParser.jj 67136 2013-08-14 15:09:24Z hudson $ @since Ptolemy II 1.0 @Pt.ProposedRating Red @Pt.AcceptedRating Red (kienhuis) */ @SuppressWarnings("static-access") public class MatrixParser/*@bgen(jjtree)*/implements MatrixParserTreeConstants/*@egen*/ {/*@bgen(jjtree)*/ protected static JJTMatrixParserState jjtree = new JJTMatrixParserState(); /*@egen*/ /** Read a Matrix from File. @exception IllegalActionException If an error occurs during parsing. */ public Vector readMatrix() throws IllegalActionException { Vector m = null; try { m = matrix(); } catch (Exception e) { throw new IllegalActionException(e.getMessage()); } return m; } } PARSER_END(MatrixParser) SKIP : { " " | "\t" | "\n" | "\r" } TOKEN : /* numeric constants */ { < CONSTANT: | ( ["e","E"] ([ "-","+"])? )? > | < #FLOAT: | ( "." )? | "." > | < #INTEGER: ( )+ > | < #DIGIT: ["0" - "9"] > } TOKEN : { < NAME : ( ["a"-"z" , "A"-"Z" , "_"] )+ ( ["a"-"z" , "A"-"Z" , "_" , "'", "0"-"9"] )* > | < NATURAL : ( ["0"-"9"] )+ ( ["0"-"9"] )* > | < SIGN : ["-","+"] > } SPECIAL_TOKEN : { < COMMENT : "#" (~["\n","\r"])* ["\n","\r"] > } Vector matrix() : {/*@bgen(jjtree) matrix */ SimpleNode jjtn000 = new SimpleNode(null, JJTMATRIX); boolean jjtc000 = true; jjtree.openNodeScope(jjtn000); /*@egen*/ Vector m = new Vector();; Vector row = null; //Matrix m; //Matrix row = null; } {/*@bgen(jjtree) matrix */ try { /*@egen*/ ( "[" [row=row()] {m.add(row);} (";" row=row() {m.add(row);} )* "]" )/*@bgen(jjtree)*/ { jjtree.closeNodeScope(jjtn000, true); jjtc000 = false; } /*@egen*/ { return m; }/*@bgen(jjtree)*/ } catch (Throwable jjte000) { if (jjtc000) { jjtree.clearNodeScope(jjtn000); jjtc000 = false; } else { jjtree.popNode(); } if (jjte000 instanceof RuntimeException) { throw (RuntimeException)jjte000; } if (jjte000 instanceof ParseException) { throw (ParseException)jjte000; } throw (Error)jjte000; } finally { if (jjtc000) { jjtree.closeNodeScope(jjtn000, true); } } /*@egen*/ } Vector row() : {/*@bgen(jjtree) row */ SimpleNode jjtn000 = new SimpleNode(null, JJTROW); boolean jjtc000 = true; jjtree.openNodeScope(jjtn000); /*@egen*/ Vector row = new Vector(); double element; } {/*@bgen(jjtree) row */ try { /*@egen*/ ( element=element() {row.add(new Double(element));} ( "," element=element() {row.add(new Double(element));} )* )/*@bgen(jjtree)*/ { jjtree.closeNodeScope(jjtn000, true); jjtc000 = false; } /*@egen*/ { return row; }/*@bgen(jjtree)*/ } catch (Throwable jjte000) { if (jjtc000) { jjtree.clearNodeScope(jjtn000); jjtc000 = false; } else { jjtree.popNode(); } if (jjte000 instanceof RuntimeException) { throw (RuntimeException)jjte000; } if (jjte000 instanceof ParseException) { throw (ParseException)jjte000; } throw (Error)jjte000; } finally { if (jjtc000) { jjtree.closeNodeScope(jjtn000, true); } } /*@egen*/ } double element() : {/*@bgen(jjtree) element */ SimpleNode jjtn000 = new SimpleNode(null, JJTELEMENT); boolean jjtc000 = true; jjtree.openNodeScope(jjtn000); /*@egen*/ Token i; Token s; int sign = 1; double value; } {/*@bgen(jjtree) element */ try { /*@egen*/ ( [s= {if ( (s.image).equals("-") ) sign=-1*sign; }] i= {value = sign*(new Double(i.image)).doubleValue();} )/*@bgen(jjtree)*/ { jjtree.closeNodeScope(jjtn000, true); jjtc000 = false; } /*@egen*/ { return value; }/*@bgen(jjtree)*/ } finally { if (jjtc000) { jjtree.closeNodeScope(jjtn000, true); } } /*@egen*/ }