/* * Copyrights : CNRS * Author : Oleg Lodygensky * Acknowledgment : XtremWeb-HEP is based on XtremWeb 1.8.0 by inria : http://www.xtremweb.net/ * Web : http://www.xtremweb-hep.org * * This file is part of XtremWeb-HEP. * * XtremWeb-HEP is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * XtremWeb-HEP is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with XtremWeb-HEP. If not, see . * */ header { package xtremweb.worker; import java.util.Vector; import java.text.ParseException; } /** * * (Don't move those comments on very first lines * since header part must start on line 1) * * Created : May 14th, 2003
* * @author : Oleg Lodygensky (lodygens@lal.in2p3.fr) * * Purpose : ANTLR grammar definition to decode java procedure calls * Grammar : * aJar.jar:aPackage.aClass.aMethod(paramsList) * where * - aJar.jar is optionnal but if present must be followed by ':' * - aPackage is optionnal (may be formatted as pkg.subpkg. etc) * - aClass is required (inner class not allowed) * - aMethod is required with its open and close parenthesis * - paramList is optionnal and depends on the method definition * - paramList is formatted as param0[, param1, ... paramN] * - paramN is formatted as paramType paramName paramValue * - paramType is scalarType or vectorType or matriceType * - scalarType is one of the following: * - boolean * - char * - byte * - short * - int * - long * - float * - double * - String * - vectorType is scalarType[] * - matriceType is vectorType[] (i.e scalarType[][]) * - paramName must be a valid java identifier * - paramValue must be a valid java value (depending of paramType) * * Note : I don't really understand where (SPACE)* is necessary * But don't remove those found in this file or it won't work :( * */ class MethodParser extends Parser; options { /* * set lookahead depth to 3 to remove grammar nondeterminisms * (it must be : k > 0) * * If you try k = 1 or k = 2 nondeterminisms appear at rules : * - methodCall * - className * */ k = 3; } /** * This is a test method */ startRule { CallDef n; } : n = methodCall {System.out.println("call def found = " +n.toString ());} ; /** * This parses a method call definition * @return a CallDef object containing the method call */ methodCall returns [CallDef calldef] { calldef = new CallDef(); String jarfilename = null; String[] methodnames = null; Vector params = null; } : (jarfilename = jarFileName COLON)? methodnames = methodName LPAREN params = paramsList RPAREN { calldef.setJarFileName (jarfilename); if (methodnames [0] != null) calldef.setClassName (methodnames [0]); if (methodnames [1] != null) calldef.setMethodName (methodnames [1]); calldef.setParams (params); } ; /** * This parses the class name * @return a string containing the class name */ private className returns [String classname] { classname = null; } : n1:IDENT { if (n1.getText () == null) return classname; if (classname == null) classname = n1.getText (); else classname = classname.concat ("." + n1.getText()); } ( DOT n2:IDENT { if (n2.getText () == null) return classname; if (classname == null) classname = n2.getText (); else classname = classname.concat ("." + n2.getText()); } )* ; /** * This parses the method name * @return a string containing the method name */ private methodName returns [String[] names] { names = new String [2]; names[0] = null; names[1] = null; String classname = null; } : classname = className DOT methodname:IDENT { names[0] = classname; names[1] = methodname.getText (); } ; /** * This parses the jar file name * @return a string containing the jar file name */ private jarFileName returns [String jarfilename] { jarfilename = null; } : jarname:IDENT dotJar { jarfilename = jarname.getText (); } ; /** * This parses the jar file extension ".jar" */ private dotJar : DOT "jar" ; /** * This parses a method parameter; its type, name and value * @return a ParamDef object containing the parameter */ private param returns [ParamDef param] { param = new ParamDef (); Class t = null; Object v = null; } : t = type (SPACE)* n:IDENT (SPACE)* v = value [t] { param.setType (t); param.setName (n.getText ()); param.setValue (v); } ; /** * This parses a method parameter list; it calls param as many time as needed * @return a Vector containing the method parameters */ private paramsList returns [Vector params] { params = new Vector (); ParamDef p1 = null; ParamDef p2 = null; } : ( p1 = param { params.add (p1); } ( (SPACE)* COMMA (SPACE)* p2 = param { params.add (p2); } )* )? ; /** * This parses a method parameter type which can be a scalar, vector or matrice * @return a Class containing a method parameters type */ private type returns [Class t] { t = null; } : t = scalarType ( LBRACKET RBRACKET ( LBRACKET RBRACKET )? )? ; /** * This parses a scalar method parameter type (int, boolean etc. or String) * @return a Class containing a method parameters type */ private scalarType returns [Class t] { // System.out.println ("scalarType"); t = null; } : t = basicType | t = stringType ; /** * This parses a scalar method parameter type (int, boolean...) * @return a Class containing a method parameters type */ private basicType returns [Class t] { // System.out.println ("basicType"); t = null; } : "boolean" { t = Boolean.TYPE; } | "char" { t = Character.TYPE; } | "byte" { t = Byte.TYPE; } | "short" { t = Short.TYPE; } | "int" { t = Integer.TYPE; } | "long" { t = Long.TYPE; } | "float" { t = Float.TYPE; } | "double" { t = Double.TYPE; } ; /** * This parses a scalar method parameter String type * @return a Class containing a method parameters type */ private stringType returns [Class t] { // System.out.println ("stringType"); t = null; } : "String" { t = String.class; } ; /** * This parses parameter value, which can be a scalar, vector or matrice * @param type is the parameter type as read * @return an object containing the value in the expected type */ private value [Class type] returns [Object v] { v = null; } : v = scalarValue [type] { } | v = vectorValues [type] { } | v = matriceValues [type] { } ; exception catch [ParseException e] { System.out.println (e.toString ()); } /** * This parses a matrice parameter value * @param type is the parameter type as read * @return an object containing the value in the expected type */ private matriceValues [Class type] returns [Object m] { m = new Vector (); Object m1 = null; Object m2 = null; } : LCURL m1 = vectorValues [type] { ((Vector)m).add (m1); } ( (SPACE)* COMMA (SPACE)* m2 = vectorValues [type] { ((Vector)m).add (m2); } )* RCURL ; /** * This parses a matrice parameter value. * It catches ParseException that can be thrown by scalarValue * @param type is the parameter type as read * @return an object containing the value in the expected type */ private vectorValues [Class type] returns [Object v] { v = new Vector (); Object v1 = null; Object v2 = null; } : LCURL v1 = scalarValue [type] { ((Vector)v).add (v1); } ( (SPACE)* COMMA (SPACE)* v2 = scalarValue [type] { ((Vector)v).add (v2); } )* RCURL ; exception catch [ParseException e] { System.out.println (e.toString ()); } /** * This parses parameter value * @param type is the parameter type as read * @return an object containing the value in the expected type * @exception ParseException is thrown if value is not in the expected type * */ private scalarValue [Class type] returns [Object s] throws ParseException { s = null; } : "true" { if (type != Boolean.TYPE) throw new ParseException ("boolean value not expected", 0); s = new Boolean(true); } | "false" { if (type != Boolean.TYPE) throw new ParseException ("boolean value not expected", 0); s = new Boolean(false); } | valD:DOUBLE { if (type == Double.TYPE) s = new Double (Double.parseDouble (valD.getText())); else if (type == Float.TYPE) s = new Float (Float.parseFloat (valD.getText())); else throw new ParseException ("double/float value not expected", 0); } | valI:INTEGER { if (type != Integer.TYPE) throw new ParseException ("integer value not expected", 0); s = new Integer(Integer.parseInt (valI.getText())); } | valString:STRING_LITERAL { if (type != String.class) throw new ParseException ("string value not expected", 0); String val = valString.getText(); s = new String(val.substring(1, val.length() - 1)); } ; class MethodLexer extends Lexer; SPACE : ' ' | '\t' ; IDENT : ('a'..'z' | 'A'..'Z' | '_' ) ( 'a'..'z' | 'A'..'Z' | '_' | '0'..'9')* ; NEWLINE : '\r' '\n' // DOS | '\n' // UNIX ; NUM : (DIGIT)+ { // System.out.println ("num integer"); $setType(INTEGER); } ('.' (DIGIT)+ { // System.out.println ("num double"); $setType(DOUBLE); } )?; STRING_LITERAL : '"' (ESC|~('"'|'\\'))* '"' ; DOT : '.' ; COLON : ':' ; COMMA : ',' ; LPAREN options { paraphrase = "("; } : '(' ; RPAREN options { paraphrase = ")"; } : ')' ; LCURL options { paraphrase = "{"; } : '{' ; RCURL options { paraphrase = "}"; } : '}' ; LBRACKET options { paraphrase = "["; } : '[' ; RBRACKET options { paraphrase = "]"; } : ']' ; protected ESC : '\\' ( 'n' | 'r' | 't' | 'b' | 'f' | '"' | '\'' | '\\' ) ; protected DIGIT : '0'..'9' ;