// ** This class was generated with DemFGen (vers:09/27/2009) options{ STATIC = false; LOOKAHEAD = 2; } PARSER_BEGIN(TheParser) package gen; import edu.neu.ccs.demeterf.lib.*; import edu.neu.ccs.demeterf.lib.*; import edu.neu.ccs.demeterf.*; import edu.neu.ccs.demeterf.lib.*; import edu.neu.ccs.demeterf.control.Fields; import edu.neu.ccs.demeterf.lib.ident; import edu.neu.ccs.demeterf.lib.verbatim; class TheParser{ public static String unescape(String str){ String retval = ""; int index = 0; char ch, ch1; int ordinal = 0; while (index < str.length()) { if(str.charAt(index) != '\\') { retval += str.charAt(index++); continue; } ch = str.charAt(++index); if(ch == 'b') { retval += '\b'; index++; continue; } if(ch == 't') { retval += '\t'; index++; continue; } if(ch == 'n') { retval += '\n'; index++; continue; } if(ch == 'f') { retval += '\f'; index++; continue; } if(ch == 'r') { retval += '\r'; index++; continue; } if(ch == '"') { retval += '\"'; index++; continue; } if(ch == '\'') { retval += '\''; index++; continue; } if(ch == '\\') { retval += '\\'; index++; continue; } if(ch >= '0' && ch <= '7'){ ordinal = ((int)ch) - ((int)'0'); index++; ch1 = str.charAt(index); if(ch1 >= '0' && ch1 <= '7'){ ordinal = ordinal*8 + ((int)ch1) - ((int)'0'); index++; ch1 = str.charAt(index); if(ch <= '3' && ch1 >= '0' && ch1 <= '7'){ ordinal = ordinal*8 + ((int)ch1) - ((int)'0'); index++; } } retval += (char)ordinal; continue; } if(ch == 'u'){ ordinal = 0; for(int i = 0; i < 4; i++){ index++; ch = str.charAt(index); ordinal = ordinal*16+hexval(ch); } index++; retval += (char)ordinal; continue; } } return retval; } static int hexval(char c){ int r = "0123456789ABCDEF".indexOf(Character.toUpperCase(c)); if(r >= 0)return r; throw new RuntimeException(" ** Bad Escaped Character"); } } PARSER_END(TheParser) byte parse_byte():{ int i; }{ i = parse_int() { return (byte)i; } } Byte parse_Byte():{ byte b; }{ b = parse_byte() { return b; } } short parse_short():{ int i; }{ i = parse_int() { return (short)i; } } Short parse_Short():{ short s; }{ s = parse_short() { return s; } } int parse_int():{ Token t; }{ t = { if(t.image.length() > 1 && Character.toLowerCase(t.image.charAt(1)) == 'x') return Integer.parseInt(t.image.substring(2), 16); return Integer.parseInt(t.image); } } Integer parse_Integer():{ int i; }{ i = parse_int() { return i; } } long parse_long():{ Token t; }{ t = { if(t.image.length() > 1 && Character.toLowerCase(t.image.charAt(1)) == 'x') return Long.parseLong(t.image.substring(2), 16); return Long.parseLong(t.image); } } Long parse_Long():{ long l; }{ l = parse_long() { return l; } } double parse_double():{ Token t; }{ t = { return Double.parseDouble(t.image); } } Double parse_Double():{ double d; }{ d = parse_double() { return d; } } float parse_float():{ Token t; }{ t = { return Float.parseFloat(t.image); } } Float parse_Float():{ float f; }{ f = parse_float() { return f; } } String parse_String():{ Token t; }{ t = { return unescape(t.image.substring(1,t.image.length()-1)); } } boolean parse_boolean():{ Token t; }{ t = { return true; } | t = { return false; } } Boolean parse_Boolean():{ boolean b; }{ b = parse_boolean() { return b; } } char parse_char():{ Token t; }{ t = { return unescape(t.image.substring(1,t.image.length()-1)).charAt(0); } } Character parse_Character():{ char c; }{ c = parse_char() { return c; } } ident parse_ident():{ Token t; }{ t = { return new ident(t.image); } } verbatim parse_verbatim():{ Token t; }{ t = { return new verbatim(t.image.substring(2,t.image.length()-2)); } } public Exp parse_Exp():{ Exp sup = null; }{ ( sup = parse_Ifz() { return sup; } | sup = parse_Def() { return sup; } | sup = parse_Bin() { return sup; } | sup = parse_Var() { return sup; } | sup = parse_Num() { return sup; } ) } public Ifz parse_Ifz():{ Exp cnd; Exp thn; Exp els; }{ "ifz" cnd = parse_Exp() "then" thn = parse_Exp() "else" els = parse_Exp() { return new Ifz(cnd,thn,els); } } public Def parse_Def():{ ident id; Exp e; Exp body; }{ id = parse_ident() "=" e = parse_Exp() ";" body = parse_Exp() { return new Def(id,e,body); } } public Bin parse_Bin():{ Oper op; Exp left; Exp right; }{ "(" op = parse_Oper() left = parse_Exp() right = parse_Exp() ")" { return new Bin(op,left,right); } } public Var parse_Var():{ ident id; }{ id = parse_ident() { return new Var(id); } } public Num parse_Num():{ int val; }{ val = parse_int() { return new Num(val); } } public Oper parse_Oper():{ Oper sup = null; }{ ( sup = parse_Sub() { return sup; } ) } public Sub parse_Sub():{ }{ "-" { return new Sub(); } } public Env parse_Env():{ Env sup = null; }{ ( sup = parse_ExtEnv() { return sup; } | sup = parse_EmptyEnv() { return sup; } ) } public ExtEnv parse_ExtEnv():{ ident id; int v; Env rest; }{ id = parse_ident() "=" v = parse_int() ";" rest = parse_Env() { return new ExtEnv(id,v,rest); } } public EmptyEnv parse_EmptyEnv():{ }{ { return new EmptyEnv(); } } public error parse_error():{ }{ { return new error(); } } public Op parse_Op():{ Op sup = null; }{ ( sup = parse_MathOp() { return sup; } | sup = parse_StkOp() { return sup; } | sup = parse_CtrlOp() { return sup; } ) } public MathOp parse_MathOp():{ MathOp sup = null; }{ ( sup = parse_Minus() { return sup; } ) } public StkOp parse_StkOp():{ StkOp sup = null; }{ ( sup = parse_Push() { return sup; } | sup = parse_Pop() { return sup; } | sup = parse_Define() { return sup; } | sup = parse_Undef() { return sup; } | sup = parse_Load() { return sup; } ) } public CtrlOp parse_CtrlOp():{ CtrlOp sup = null; }{ ( sup = parse_Label() { return sup; } | sup = parse_Jmp() { return sup; } | sup = parse_IfNZ() { return sup; } ) } public Minus parse_Minus():{ }{ "minus" { return new Minus(); } } public Push parse_Push():{ int i; }{ "push" i = parse_int() { return new Push(i); } } public Pop parse_Pop():{ }{ "pop" { return new Pop(); } } public Define parse_Define():{ }{ "def" { return new Define(); } } public Undef parse_Undef():{ }{ "undef" { return new Undef(); } } public Load parse_Load():{ int i; }{ "load" i = parse_int() { return new Load(i); } } public Label parse_Label():{ ident id; }{ "label" id = parse_ident() { return new Label(id); } } public Jmp parse_Jmp():{ ident id; }{ "jump" id = parse_ident() { return new Jmp(id); } } public IfNZ parse_IfNZ():{ ident id; }{ "ifnzero" id = parse_ident() { return new IfNZ(id); } } public OpList parse_OpList():{ OpList sup = null; }{ ( sup = parse_OpCons() { return sup; } | sup = parse_OpEmpty() { return sup; } ) } public OpCons parse_OpCons():{ Op first; OpList rest; }{ first = parse_Op() rest = parse_OpList() { return new OpCons(first,rest); } } public OpEmpty parse_OpEmpty():{ }{ { return new OpEmpty(); } } public ExecStack parse_ExecStack():{ }{ { return new ExecStack(); } } public CodeEntry parse_CodeEntry():{ String label; OpList pc; }{ label = parse_String() pc = parse_OpList() { return new CodeEntry(label,pc); } } SKIP : { " " | "\t" | "\n" | "\r" | "\r\n" } SKIP : { < "//" (~["\n","\r"])* ("\n"|"\r\n") > | < "/*" (~["*"])* "*" (~["/"] (~["*"])* "*")* "/" > } TOKEN: { < TRUE : "true" > | < FALSE : "false" > } TOKEN: { < INT : ("+" | "-")? ( (["0"-"9"])+ | ("0" ["x","X"]) (["0"-"9","a"-"f","A"-"F"])+ ) > | < DOUBLE : ("-")?(["0"-"9"])+ "." (["0"-"9"])+ ()? | "." (["0"-"9"])+ ()? > | < #EXPON: ["e","E"] (["+","-"])? (["0"-"9"])+ > } TOKEN: { < CHAR: "\'" ( (~["\'","\\","\n","\r"]) | ("\\" ( ["n","t","b","r","f","\\","\'","\""] | ["0"-"7"] ( ["0"-"7"] )? | ["0"-"3"] ["0"-"7"] ["0"-"7"] ) ) ) "\'" > | < STRING : "\"" ( (~["\"","\\","\n","\r"]) | ("\\" ( ["n","t","b","r","f","\\","'","\""] | ["0"-"7"] ( ["0"-"7"] )? | ["0"-"3"] ["0"-"7"] ["0"-"7"] ) ) )* "\"" > | < TEXT : ( "{{" (~["}"])* ( "}" ~["}"] (~["}"])* )* "}}" ) > | < IDENT : ["a"-"z","A"-"Z","$","_"] (["a"-"z","A"-"Z","0"-"9","_","$"])* > }