options { STATIC = false; JAVA_UNICODE_ESCAPE = true; } PARSER_BEGIN(Parser) import java.util.*; import java.io.*; import edu.neu.ccs.demeter.*; public class Parser { // oit is uugly. Why isn't there a Character.valueOf(String)? static char unescapifyChar(String s) { char c = s.charAt(0); if (c == '\\') { switch (s.charAt(1)) { case 'n': c = '\n'; break; case 't': c = '\t'; break; case 'b': c = '\b'; break; case 'r': c = '\r'; break; case 'f': c = '\f'; break; case '\\': c = '\\'; break; case '\'': c = '\''; break; case '\"': c = '\"'; break; default: c = (char) Integer.parseInt(s.substring(1, s.length()), 8); break; } } return c; } // Even uglier... static String unescapify(String s) { char str[] = new char[s.length()]; int i = 0, o = 0; while (i < s.length()) { char c = s.charAt(i++); if (c == '\\') { int j = i + 1; while (j < s.length() && Character.digit(s.charAt(j), 8) != -1) { j++; } c = unescapifyChar(s.substring(i-1, j)); i = j; } str[o++] = c; } return String.valueOf(str, 0, o); } } PARSER_END(Parser) Main _Main() : { Main it = null; } { { it=new Main(); } { return it; } } ClassGraph _ClassGraph() : { ClassGraph it = null; ClassDef_DList _classes; } { { it=new ClassGraph(); } _classes=_ClassDef_DList() { it.set_classes(_classes); } { return it; } } ClassDef _ClassDef() : { ClassDef it = null; ClassName _classname; ClassParts _classparts; } { { it=new ClassDef(); } "(" _classname=_ClassName() { it.set_classname(_classname); } _classparts=_ClassParts() { it.set_classparts(_classparts); } ")" { return it; } } ClassParts _ClassParts() : { ClassParts it = null; } { ( it=_ConstOrAltClass() | it=_RepetitionClass() ) { return it; } } void common_ClassParts(ClassParts it) : { } { { } } ConstOrAltClass _ConstOrAltClass() : { ConstOrAltClass it = null; } { ( it=_ConstructionClass() | it=_AlternationClass() ) { return it; } } void common_ConstOrAltClass(ConstOrAltClass it) : { PartOrSyntax_List _parts; } { _parts=_PartOrSyntax_List() { it.set_parts(_parts); } common_ClassParts(it) { } } PartOrSyntax _PartOrSyntax() : { PartOrSyntax it = null; } { ( it=_Part() | it=_OptionalPart() | it=_Syntax() ) { return it; } } void common_PartOrSyntax(PartOrSyntax it) : { } { { } } Part _Part() : { Part it = null; PartName _partname; ClassSpec _classspec; } { { it=new Part(); } [ "(" _partname=_PartName() { it.set_partname(_partname); } ")" ] _classspec=_ClassSpec() { it.set_classspec(_classspec); } common_PartOrSyntax(it) { return it; } } OptionalPart _OptionalPart() : { OptionalPart it = null; Part_Sandwich _part; } { { it=new OptionalPart(); } "(opt" _part=_Part_Sandwich() { it.set_part(_part); } ")" common_PartOrSyntax(it) { return it; } } ConstructionClass _ConstructionClass() : { ConstructionClass it = null; } { { it=new ConstructionClass(); } "=" common_ConstOrAltClass(it) { return it; } } AlternationClass _AlternationClass() : { AlternationClass it = null; Subclass_Barlist _subclasses; Common _common; } { { it=new AlternationClass(); } ":" _subclasses=_Subclass_Barlist() { it.set_subclasses(_subclasses); } [ _common=_Common() { it.set_common(_common); } ] common_ConstOrAltClass(it) { return it; } } Subclass _Subclass() : { Subclass it = null; ClassSpec _classspec; } { { it=new Subclass(); } _classspec=_ClassSpec() { it.set_classspec(_classspec); } { return it; } } Common _Common() : { Common it = null; } { { it=new Common(); } "*" "common" "*" { return it; } } RepetitionClass _RepetitionClass() : { RepetitionClass it = null; RepeatedPart_Sandwich _sandwiched; } { { it=new RepetitionClass(); } "~" _sandwiched=_RepeatedPart_Sandwich() { it.set_sandwiched(_sandwiched); } common_ClassParts(it) { return it; } } RepeatedPart _RepeatedPart() : { RepeatedPart it = null; ClassSpec _nonempty; ClassSpec_Sandwich _repeated; } { { it=new RepeatedPart(); } [ _nonempty=_ClassSpec() { it.set_nonempty(_nonempty); } ] "(" _repeated=_ClassSpec_Sandwich() { it.set_repeated(_repeated); } ")" { return it; } } ClassSpec _ClassSpec() : { ClassSpec it = null; ClassName _classname; } { { it=new ClassSpec(); } _classname=_ClassName() { it.set_classname(_classname); } { return it; } } Syntax _Syntax() : { Syntax it = null; } { ( it=_PlainSyntax() ) { return it; } } void common_Syntax(Syntax it) : { } { common_PartOrSyntax(it) { } } PlainSyntax _PlainSyntax() : { PlainSyntax it = null; String _string; } { { it=new PlainSyntax(); } _string=_String() { it.set_string(_string); } common_Syntax(it) { return it; } } DirName _DirName() : { DirName it = null; Ident _name; } { { it=new DirName(); } _name=_Ident() { it.set_name(_name); } { return it; } } ClassName _ClassName() : { ClassName it = null; Ident _name; } { { it=new ClassName(); } _name=_Ident() { it.set_name(_name); } { return it; } } PartName _PartName() : { PartName it = null; Ident _name; } { { it=new PartName(); } _name=_Ident() { it.set_name(_name); } { return it; } } TraversalName _TraversalName() : { TraversalName it = null; Ident _name; } { { it=new TraversalName(); } _name=_Ident() { it.set_name(_name); } { return it; } } VisitorName _VisitorName() : { VisitorName it = null; Ident _name; } { { it=new VisitorName(); } _name=_Ident() { it.set_name(_name); } { return it; } } MethodName _MethodName() : { MethodName it = null; Ident _name; } { { it=new MethodName(); } _name=_Ident() { it.set_name(_name); } { return it; } } JavaCode _JavaCode() : { JavaCode it = null; String _code; } { { it=new JavaCode(); } "(@" _code=_String() { it.set_code(_code); } "@)" { return it; } } ClassDef_DList _ClassDef_DList() : { ClassDef_DList it = null; Nonempty_ClassDef_DList _first; } { { it=new ClassDef_DList(); } "(" _first=_Nonempty_ClassDef_DList() { it.set_first(_first); } ")" { return it; } } PartOrSyntax_List _PartOrSyntax_List() : { PartOrSyntax_List it = null; Nonempty_PartOrSyntax_List _first; } { { it=new PartOrSyntax_List(); } "(" [ _first=_Nonempty_PartOrSyntax_List() { it.set_first(_first); } ] ")" { return it; } } Part_Sandwich _Part_Sandwich() : { Part_Sandwich it = null; Syntax_List _first; Part _inner; Syntax_List _second; } { { it=new Part_Sandwich(); } _first=_Syntax_List() { it.set_first(_first); } _inner=_Part() { it.set_inner(_inner); } _second=_Syntax_List() { it.set_second(_second); } { return it; } } Subclass_Barlist _Subclass_Barlist() : { Subclass_Barlist it = null; Nonempty_Subclass_Barlist _first; } { { it=new Subclass_Barlist(); } "(" _first=_Nonempty_Subclass_Barlist() { it.set_first(_first); } ")" { return it; } } RepeatedPart_Sandwich _RepeatedPart_Sandwich() : { RepeatedPart_Sandwich it = null; Syntax_List _first; RepeatedPart _inner; Syntax_List _second; } { { it=new RepeatedPart_Sandwich(); } _first=_Syntax_List() { it.set_first(_first); } _inner=_RepeatedPart() { it.set_inner(_inner); } _second=_Syntax_List() { it.set_second(_second); } { return it; } } ClassSpec_Sandwich _ClassSpec_Sandwich() : { ClassSpec_Sandwich it = null; Syntax_List _first; ClassSpec _inner; Syntax_List _second; } { { it=new ClassSpec_Sandwich(); } _first=_Syntax_List() { it.set_first(_first); } _inner=_ClassSpec() { it.set_inner(_inner); } _second=_Syntax_List() { it.set_second(_second); } { return it; } } Syntax_List _Syntax_List() : { Syntax_List it = null; Nonempty_Syntax_List _first; } { { it=new Syntax_List(); } "(" [ _first=_Nonempty_Syntax_List() { it.set_first(_first); } ] ")" { return it; } } Nonempty_ClassDef_DList _Nonempty_ClassDef_DList() : { Nonempty_ClassDef_DList it = null; ClassDef _it; Nonempty_ClassDef_DList _next; } { { it=new Nonempty_ClassDef_DList(); } _it=_ClassDef() { it.set_it(_it); } [ _next=_Nonempty_ClassDef_DList() { it.set_next(_next); } ] { return it; } } Nonempty_PartOrSyntax_List _Nonempty_PartOrSyntax_List() : { Nonempty_PartOrSyntax_List it = null; PartOrSyntax _it; Nonempty_PartOrSyntax_List _next; } { { it=new Nonempty_PartOrSyntax_List(); } _it=_PartOrSyntax() { it.set_it(_it); } [ _next=_Nonempty_PartOrSyntax_List() { it.set_next(_next); } ] { return it; } } Nonempty_Subclass_Barlist _Nonempty_Subclass_Barlist() : { Nonempty_Subclass_Barlist it = null; Subclass _it; Nonempty_Subclass_Barlist _next; } { { it=new Nonempty_Subclass_Barlist(); } _it=_Subclass() { it.set_it(_it); } [ "|" _next=_Nonempty_Subclass_Barlist() { it.set_next(_next); } ] { return it; } } Nonempty_Syntax_List _Nonempty_Syntax_List() : { Nonempty_Syntax_List it = null; Syntax _it; Nonempty_Syntax_List _next; } { { it=new Nonempty_Syntax_List(); } _it=_Syntax() { it.set_it(_it); } [ _next=_Nonempty_Syntax_List() { it.set_next(_next); } ] { return it; } } boolean _boolean() : { Token t; }{ ( t= { return true; } | t= { return false; } ) } char _char() : { Token t; } { t= { String s = t.image; return unescapifyChar(s.substring(1, s.length()-1)); } } byte _byte() : { int i; } { i=_int() { return (byte) i; } } short _short() : { int i; } { i=_int() { return (short) i; } } int _int() : { Number num; } { num=_Number() { return num.intValue(); } } long _long() : { Number num; } { num=_Number() { return num.longValue(); } } float _float() : { Number num; } { num=_Number() { return num.floatValue(); } } double _double() : { Number num; } { num=_Number() { return num.doubleValue(); } } Boolean _Boolean() : { Token t; }{ ( t= { return Boolean.TRUE; } | t= { return Boolean.FALSE; } ) } Character _Character() : { char c; } { c=_char() { return new Character(c); } } Integer _Integer() : { int i; } { i = _int() { return new Integer(i); } } Long _Long() : { long l; } { l=_long() { return new Long(l); } } Float _Float() : { float f; } { f=_float() { return new Float(f); } } Double _Double() : { double d; } { d=_double() { return new Double(d); } } Number _Number() : { Token t; String s = null; int radix = 0; Number num = null; } { ( ( t= { s = t.image; radix = 10; } | t= { // Strip off the "0x". s = t.image.substring(2, t.image.length()); radix = 16; } | t= { s = t.image; radix = 8; } ) { switch (s.charAt(s.length()-1)) { case 'l': case 'L': s = s.substring(0, s.length()-1); num = new Long(new java.math.BigInteger(s, radix).longValue()); break; default: num = new Integer(new java.math.BigInteger(s, radix).intValue()); break; } } | t= { s = t.image; switch (s.charAt(s.length()-1)) { case 'd': case 'D': num = Double.valueOf(s.substring(0, s.length()-1)); break; case 'f': case 'F': num = Float.valueOf(s.substring(0, s.length()-1)); break; default: num = Float.valueOf(s); break; } } ) { return num; } } String _String() : { Token t; } { t= { String s = t.image; return unescapify(s.substring(1, s.length()-1)); } } StringBuffer _StringBuffer() : { String s; } { s=_String() { return new StringBuffer(s); } } Ident _Ident() : { Token t; } { t= { return new Ident(t.image); } } Text _Text() : { Token t; } { t= { String s = t.image; return new Text(s.substring(2, s.length()-2)); } } Line _Line() : { Token t; } { { token_source.SwitchTo(1); } t= { return new Line(t.image); } } Word _Word() : { Token t; } { { token_source.SwitchTo(2); } t= { return new Word(t.image); } } // Lexical specification (largely taken from Java.jack): SKIP : { " " | "\t" | "\n" | "\r" | <"//" (~["\n","\r"])* ("\n"|"\r\n")> | <"/*" (~["*"])* "*" (~["/"] (~["*"])* "*")* "/"> } TOKEN : { /* LITERALS */ < DECIMAL_LITERAL: ["1"-"9"] (["0"-"9"])* (["l","L"])? > | < HEX_LITERAL: "0" ["x","X"] (["0"-"9","a"-"f","A"-"F"])+ (["l","L"])? > | < OCTAL_LITERAL: "0" (["0"-"7"])* (["l","L"])? > | < FLOATING_POINT_LITERAL: (["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"] > | < #EXPONENT: ["e","E"] (["+","-"])? (["0"-"9"])+ > | < CHARACTER_LITERAL: "'" ( (~["\'","\\","\n","\r"]) | ("\\" ( ["n","t","b","r","f","\\","\'","\""] | ["0"-"7"] ( ["0"-"7"] )? | ["0"-"3"] ["0"-"7"] ["0"-"7"] ) ) ) "'" > | < STRING_LITERAL: "\"" ( (~["\"","\\","\n","\r"]) | ("\\" ( ["n","t","b","r","f","\\","\'","\""] | ["0"-"7"] ( ["0"-"7"] )? | ["0"-"3"] ["0"-"7"] ["0"-"7"] ) ) )* "\"" > | < TEXT_LITERAL: ( "(@" (~["@"])* ( "@" ~[")"] (~["@"])* )* "@)" ) | ( "{{" (~["}"])* ( "}" ~["}"] (~["}"])* )* "}}" ) > | < TRUE: "true" > | < FALSE: "false" > } TOKEN : { /* IDENTIFIERS */ < IDENTIFIER: (|)* > | < #LETTER: [ "\u0024", "\u0041"-"\u005a", "\u005f", "\u0061"-"\u007a", "\u00c0"-"\u00d6", "\u00d8"-"\u00f6", "\u00f8"-"\u00ff", "\u0100"-"\u1fff", "\u3040"-"\u318f", "\u3300"-"\u337f", "\u3400"-"\u3d2d", "\u4e00"-"\u9fff", "\uf900"-"\ufaff" ] > | < #DIGIT: [ "\u0030"-"\u0039", "\u0660"-"\u0669", "\u06f0"-"\u06f9", "\u0966"-"\u096f", "\u09e6"-"\u09ef", "\u0a66"-"\u0a6f", "\u0ae6"-"\u0aef", "\u0b66"-"\u0b6f", "\u0be7"-"\u0bef", "\u0c66"-"\u0c6f", "\u0ce6"-"\u0cef", "\u0d66"-"\u0d6f", "\u0e50"-"\u0e59", "\u0ed0"-"\u0ed9", "\u1040"-"\u1049" ] > } TOKEN : { < LINE: (~["\n","\r"])* > : DEFAULT } SKIP : { " " | "\t" | "\n" | "\r" } TOKEN : { < WORD: (~[" ","\t","\n","\r"])* > : DEFAULT }