options { STATIC = false; JAVA_UNICODE_ESCAPE = true; } PARSER_BEGIN(Parser) 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; String _s; } { { it=new Main(); } _s=_String() { it.set_s(_s); } { return it; } } LogicAJ _LogicAJ() : { LogicAJ it = null; PCD_List _pcd_list; } { { it=new LogicAJ(); } _pcd_list=_PCD_List() { it.set_pcd_list(_pcd_list); } { return it; } } PCD _PCD() : { PCD it = null; } { ( it=_Sim() | it=_Com() ) { return it; } } void common_PCD(PCD it) : { } { { } } Sim _Sim() : { Sim it = null; } { ( it=_Within() | it=_Target() | it=_This() | it=_Call() | it=_Execution() | it=_Withincode() ) { return it; } } void common_Sim(Sim it) : { } { common_PCD(it) { } } Within _Within() : { Within it = null; TypePat _typepat; } { { it=new Within(); } "within" "(" _typepat=_TypePat() { it.set_typepat(_typepat); } ")" common_Sim(it) { return it; } } Target _Target() : { Target it = null; TypePat _typepat; } { { it=new Target(); } "target" "(" _typepat=_TypePat() { it.set_typepat(_typepat); } ")" common_Sim(it) { return it; } } This _This() : { This it = null; TypePat _typepat; } { { it=new This(); } "this" "(" _typepat=_TypePat() { it.set_typepat(_typepat); } ")" common_Sim(it) { return it; } } Call _Call() : { Call it = null; MethodPat _methodpat; } { { it=new Call(); } "call" "(" _methodpat=_MethodPat() { it.set_methodpat(_methodpat); } ")" common_Sim(it) { return it; } } Execution _Execution() : { Execution it = null; MethodPat _methodpat; } { { it=new Execution(); } "execution" "(" _methodpat=_MethodPat() { it.set_methodpat(_methodpat); } ")" common_Sim(it) { return it; } } Withincode _Withincode() : { Withincode it = null; MethodPat _methodpat; } { { it=new Withincode(); } "withincode" "(" _methodpat=_MethodPat() { it.set_methodpat(_methodpat); } ")" common_Sim(it) { return it; } } Com _Com() : { Com it = null; O _o; PCD_List _args; } { { it=new Com(); } "(" _o=_O() { it.set_o(_o); } _args=_PCD_List() { it.set_args(_args); } ")" common_PCD(it) { return it; } } O _O() : { O it = null; } { ( it=_Inter() | it=_Uni() | it=_Compl() ) { return it; } } void common_O(O it) : { } { { } } Inter _Inter() : { Inter it = null; } { { it=new Inter(); } "&&" common_O(it) { return it; } } Uni _Uni() : { Uni it = null; } { { it=new Uni(); } "||" common_O(it) { return it; } } Compl _Compl() : { Compl it = null; } { { it=new Compl(); } "!" common_O(it) { return it; } } LMV _LMV() : { LMV it = null; Ident _ident; } { { it=new LMV(); } "?" _ident=_Ident() { it.set_ident(_ident); } common_ModifierPart(it) { return it; } } LMV1 _LMV1() : { LMV1 it = null; LMV _lmv; } { { it=new LMV1(); } _lmv=_LMV() { it.set_lmv(_lmv); } common_MethodName(it) { return it; } } LMV2 _LMV2() : { LMV2 it = null; LMV _lmv; } { { it=new LMV2(); } _lmv=_LMV() { it.set_lmv(_lmv); } common_NPat(it) { return it; } } LMV3 _LMV3() : { LMV3 it = null; LMV _lmv; } { { it=new LMV3(); } _lmv=_LMV() { it.set_lmv(_lmv); } { return it; } } LLMV _LLMV() : { LLMV it = null; Ident _ident; } { { it=new LLMV(); } "??" _ident=_Ident() { it.set_ident(_ident); } common_Arguments(it) { return it; } } MethodPat _MethodPat() : { MethodPat it = null; ModifierPart _modifierpart; TypePat _ret; TypePat _typ; MethodName _methodname; Arguments _arguments; } { { it=new MethodPat(); } _modifierpart=_ModifierPart() { it.set_modifierpart(_modifierpart); } _ret=_TypePat() { it.set_ret(_ret); } _typ=_TypePat() { it.set_typ(_typ); } "." _methodname=_MethodName() { it.set_methodname(_methodname); } "(" _arguments=_Arguments() { it.set_arguments(_arguments); } ")" { return it; } } ModifierPart _ModifierPart() : { ModifierPart it = null; } { ( it=_ModifierPat() | it=_LMV() ) { return it; } } void common_ModifierPart(ModifierPart it) : { } { { } } ModifierPat _ModifierPat() : { ModifierPat it = null; } { { it=new ModifierPat(); } "public" common_ModifierPart(it) { return it; } } MethodName _MethodName() : { MethodName it = null; } { ( it=_NamePat() | it=_LMV1() ) { return it; } } void common_MethodName(MethodName it) : { } { { } } Arguments _Arguments() : { Arguments it = null; } { ( it=_TypePat() | it=_LLMV() | it=_ListWildCard() ) { return it; } } void common_Arguments(Arguments it) : { } { { } } ListWildCard _ListWildCard() : { ListWildCard it = null; } { { it=new ListWildCard(); } ".." common_Arguments(it) { return it; } } TypePat _TypePat() : { TypePat it = null; } { ( it=_Simple() | it=_Compound() ) { return it; } } void common_TypePat(TypePat it) : { } { common_Arguments(it) { } } Simple _Simple() : { Simple it = null; NPat _npat; SubTypes _subtypes; } { { it=new Simple(); } _npat=_NPat() { it.set_npat(_npat); } [ _subtypes=_SubTypes() { it.set_subtypes(_subtypes); } ] common_TypePat(it) { return it; } } SubTypes _SubTypes() : { SubTypes it = null; } { { it=new SubTypes(); } "+" { return it; } } NPat _NPat() : { NPat it = null; } { ( it=_LMV2() | it=_NamePat2() ) { return it; } } void common_NPat(NPat it) : { } { { } } NamePat _NamePat() : { NamePat it = null; } { ( it=_WildCard() | it=_Name() ) { return it; } } void common_NamePat(NamePat it) : { } { common_MethodName(it) { } } NamePat2 _NamePat2() : { NamePat2 it = null; NamePat _namepat; } { { it=new NamePat2(); } _namepat=_NamePat() { it.set_namepat(_namepat); } common_NPat(it) { return it; } } WildCard _WildCard() : { WildCard it = null; } { { it=new WildCard(); } "*" common_NamePat(it) { return it; } } Name _Name() : { Name it = null; Ident _ident; } { { it=new Name(); } _ident=_Ident() { it.set_ident(_ident); } common_NamePat(it) { return it; } } Compound _Compound() : { Compound it = null; Op _op; TypePat_List _args; } { { it=new Compound(); } "(" _op=_Op() { it.set_op(_op); } _args=_TypePat_List() { it.set_args(_args); } ")" common_TypePat(it) { return it; } } Op _Op() : { Op it = null; } { ( it=_Intersection() | it=_Union() | it=_Complement() ) { return it; } } void common_Op(Op it) : { } { { } } Intersection _Intersection() : { Intersection it = null; } { { it=new Intersection(); } "&" common_Op(it) { return it; } } Union _Union() : { Union it = null; } { { it=new Union(); } "|" common_Op(it) { return it; } } Complement _Complement() : { Complement it = null; } { { it=new Complement(); } "!" common_Op(it) { return it; } } PCD_List _PCD_List() : { PCD_List it = null; Nonempty_PCD_List _first; } { { it=new PCD_List(); } [ _first=_Nonempty_PCD_List() { it.set_first(_first); } ] { return it; } } TypePat_List _TypePat_List() : { TypePat_List it = null; Nonempty_TypePat_List _first; } { { it=new TypePat_List(); } [ _first=_Nonempty_TypePat_List() { it.set_first(_first); } ] { return it; } } Nonempty_PCD_List _Nonempty_PCD_List() : { Nonempty_PCD_List it = null; PCD _it; Nonempty_PCD_List _next; } { { it=new Nonempty_PCD_List(); } _it=_PCD() { it.set_it(_it); } [ _next=_Nonempty_PCD_List() { it.set_next(_next); } ] { return it; } } Nonempty_TypePat_List _Nonempty_TypePat_List() : { Nonempty_TypePat_List it = null; TypePat _it; Nonempty_TypePat_List _next; } { { it=new Nonempty_TypePat_List(); } _it=_TypePat() { it.set_it(_it); } [ _next=_Nonempty_TypePat_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 }