/* -*- Mode: java -*- */ /* Input file to JJTree to generate Ptolemy II Units Parser */ options { LOOKAHEAD=1; STATIC = false; //DEBUG_PARSER=true; //DEBUG_TOKEN_MANAGER=true; //DEBUG_LOOKAHEAD=true; } PARSER_BEGIN(UParser) /* 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_3 COPYRIGHTENDKEY */ package ptolemy.moml.unit; import ptolemy.kernel.util.*; import java.util.Vector; import java.io.*; ////////////////////////////////////////////////////////////////////// //// UParser.jj /** This file implements a Unit parser for Ptolemy II using the JavaCC parser generator. @author Rowland R Johnson @version UParser.jj,v 1.9 2004/04/14 14:18:38 rowland Exp @since Ptolemy II 4.0 @Pt.ProposedRating Red (rowland) @Pt.AcceptedRating Red (rowland) */ @SuppressWarnings("unused") public class UParser { boolean debug = false; public UParser() { this(new StringReader("")); } /** Parse a unit expression. * @param expression * @exception ParseException If parsing error is encountered. */ public UnitExpr parseUnitExpr(String expression) throws ParseException { if (expression.equals("")) { return null; } Reader reader = new StringReader(expression); this.ReInit(reader); // Parse the expression to obtain the parse tree UnitExpr unitExpr = uExpr(); return unitExpr; } /** Parse a set of equations separated by semicolons. * @param expression * @exception ParseException If parsing error is encountered. */ public Vector parseEquations(String expression) throws ParseException { if (expression.equals("")) { return null; } Reader reader = new StringReader(expression); this.ReInit(reader); // Parse the expression to obtain the parse tree Vector _equations = Equations(); return _equations; } } PARSER_END(UParser) /* Now come to proper tokens */ SKIP : { " " | "\r" | "\t" | "\n" } TOKEN : /* Arithmetic operators */ { < PLUS: "+" > | < MINUS: "-" > | < MULTIPLY: "*" > | < DIVIDE: "/" > | < POWER: "^" > | < EQUALS: "="> | < LT: "<"> | < DOLLAR : "$"> | < SEMICOLON : ";"> } TOKEN : /* Numeric literals */ { < INTEGER: ["1"-"9"] (["0"-"9"])* > | < #EXPONENT: ["e","E"] (["+","-"])? (["0"-"9"])+ > | < DOUBLE: (["0"-"9"])+ "." (["0"-"9"])+ ()? (["f","F","d","D"])? | "." (["0"-"9"])+ ()? (["f","F","d","D"])? | (["0"-"9"])+ (["f","F","d","D"])? | (["0"-"9"])+ ()? ["f","F","d","D"] > } TOKEN : /* Function names */ { < UNITLABEL: ( )+ (["0"-"9"] | )* > | < #LETTER: ["a"-"z", "A"-"Z", "_"] > | < PORT: ( )+ (["0"-"9"] | )* > } TOKEN : { < LPAREN : "(" > | } UnitExpr uExpr() : { UnitTerm unitTerm = null; UnitTerm firstUnitTerm = null; UnitExpr unitExpr = new UnitExpr(); } { firstUnitTerm = uTerm() { unitExpr.addUnitTerm(firstUnitTerm); } ( unitTerm = uTerm() { unitExpr.addUnitTerm(unitTerm.invert()); } | unitTerm = uTerm() { unitExpr.addUnitTerm(unitTerm); } | unitTerm = uTerm() { unitExpr.addUnitTerm(unitTerm); } )* { return unitExpr; } } UnitTerm uTerm() : { UnitTerm unitTerm = new UnitTerm(); UnitExpr unitExpr; Token U; int exponent; double scale; } { (LOOKAHEAD(2) U = unit() exponent = exponent() { String unitLabel = U.image; if (U.kind == UParserConstants.PORT) { unitTerm.setVariable(unitLabel.substring(1)); } else if (U.kind == UParserConstants.UNITLABEL) { Unit unit = UnitLibrary.getUnitByName(unitLabel); if (unit != null) { unitTerm.setUnit(unit); } else { throw new ParseException( unitLabel + " is a not variable and is not grounded in the Units Library"); } } else { throw new ParseException( unitLabel + " is a not variable and is not a Unit"); } unitTerm.setExponent(exponent); } | U = unit() { String unitLabel = U.image; if (U.kind == UParserConstants.PORT) { unitTerm.setVariable(unitLabel.substring(1)); } else if (U.kind == UParserConstants.UNITLABEL) { Unit unit = UnitLibrary.getUnitByName(unitLabel); if (unit != null) { unitTerm.setUnit(unit); } else { throw new ParseException( unitLabel + " is a not variable and is not grounded in the Units Library"); } } else { throw new ParseException( unitLabel + " is a not variable and is not a Unit"); } } | scale = number() { Unit unit = new Unit(); unit.setScale(scale); unitTerm.setUnit(unit); } | unitExpr = uExpr() { unitTerm.setUnitExpr(unitExpr);; }) { return unitTerm; } } Token unit() : { Token retv; } { ( retv = | retv = ) { return retv; } } Vector Equations() : { Vector l = null; UnitEquation node; } { node = Equation() ( LOOKAHEAD(2) l = Equations() )* { if (l == null) { l = new Vector(); } l.add(node); return l; } } UnitEquation Equation() : { UnitEquation uEquation; Token r; UnitExpr lhs, rhs; } { lhs = uExpr() (r = | r = ) rhs = uExpr() { uEquation = new UnitEquation(lhs, rhs); return uEquation; } } int exponent() : { Token x; int retv; } { x = { retv = Integer.parseInt(x.image); return retv; } | x = { retv = - Integer.parseInt(x.image); return retv; } } double number() : { int len; String tidied, x; } { ( { Double value; try { x = token.image.toLowerCase(); len = x.length(); if ( x.endsWith("d") || x.endsWith("f") ) { // all floating point numbers are double value = new Double(x.substring(0, len-1 )); } else { value = new Double(x); } } catch (NumberFormatException ee) { throw new ParseException( "Unable to convert token " + token.image + " to an float or double"); } return value.doubleValue(); } ) | ( { try { x = token.image.toLowerCase(); len = x.length(); int radix; boolean mustBeLong = x.endsWith("l"); int prefixLength; int suffixLength; if(mustBeLong) { suffixLength = 1; } else { suffixLength = 0; } if (x.startsWith("0x") ) { // Input is a hex number. radix = 16; prefixLength = 2; } else if(x.startsWith("0")) { // Input is an octal number. radix = 8; prefixLength = 0; } else { // Input is a decimal number. radix = 10; prefixLength = 0; } // Strip off the radix prefix and the length suffix. x = x.substring(prefixLength, len - suffixLength); if (mustBeLong) { // If the size was specified as long, then create a long. return (Long.parseLong(x, radix)); } else { // Try to infer the size. Inferred sizes are at least // integer. try { return Integer.parseInt(x, radix); } catch (NumberFormatException nfe) { return Double.NaN; } } } catch (NumberFormatException ee) { throw new ParseException( "Unable to convert token " + token.image + " to an integer or long"); } } ) }