package code.loader.parser.txtParser; import java_cup.runtime.*; import java.util.*; import code.type.*; import code.symbols.*; import code.instr.*; parser code {: /** We can't use the usual setScanner/getScanner mechanism, because our lexer doesn't quite match the Lexer interface. */ Yylex jlexer; public TxtCupParser(Yylex jlexer) { super(); this.jlexer = jlexer; } public void syntax_error(java_cup.runtime.Symbol current) {} // override default message public void unrecovered_syntax_error(java_cup.runtime.Symbol current) throws ParseError { report_fatal_error("Error at line " + current.left + ": Parse Syntax error",current); } public void report_fatal_error(String message, java_cup.runtime.Symbol info) throws ParseError { done_parsing(); throw new ParseError(message); } :} action code {: // All these are data members describing the module being parsed. Vector importedv=new Vector(); Vector symbolv=new Vector(); Boolean compiled = null; String source = null; String name = null; Integer timestamp = null ; // a dummy variable to make things more meaningful // Order of the elements of 'a' is important. Object [] a = {importedv, symbolv, compiled, source,name,timestamp}; :} scan with {: Token tmp = jlexer.yylex(); // System.out.println(tmp.sym+" "+tmp.parse_state+" "+tmp.value); return tmp; :} terminal String UNQUAL_ID; terminal Integer INTEGER; terminal Float FLOAT; terminal NAME, COMPILED, TRUE, FALSE, TIME, IMPORT, TYPE, CONSTRUCTOR, OPERATION, PUBLIC, PRIVATE, LEFT, RIGHT, NONASSOC; terminal BRANCH, CHOICE, FAIL, LOAD, MAKEREF, MAKEANON, MAKECHAR, MAKEINT, MAKEFLOAT, MAKEPARTIAL, MAKETERM, MAKEVAR, NARROW, POP, PUSH, REPLACE, RESIDUATE, STOREVAR; terminal COLONCOLON, LPAREN, RPAREN, LCUBRA, RCUBRA, COMMA, DOT, ARROW; non terminal module; non terminal name; non terminal compiled; non terminal bool; non terminal timestamp; non terminal Vector [] imported_symbol; non terminal imported_or_symbol; non terminal String imported; non terminal code.symbols.Symbol symbol; non terminal TypeSymbol type; non terminal qualtype_name; non terminal String constructor_symbol_name; non terminal String operation_symbol_name; non terminal Integer visibility; non terminal ConstructorSymbol constructor; non terminal OperationSymbol operation; non terminal Integer staticNestingDepth; non terminal Integer arity; non terminal Integer kind; non terminal Object [] associativityPrecedence; non terminal TypeConstructorApplication type_constructorApplication; non terminal Vector type_args; non terminal Vector typeExpList; non terminal TypeExpression typeExp; non terminal FunctionType type_fun; non terminal Vector codeBlock; non terminal Vector instrs; non terminal Instruction instr; non terminal String sig_symbol_name; non terminal Vector intArgs; non terminal Vector codeBlocks; /* Precedence */ precedence right ARROW; module ::= name compiled timestamp imported_symbol {: RESULT = a; :} ; name ::= NAME UNQUAL_ID:module_name {: name = module_name ; a[4] = module_name; :} ; compiled ::= COMPILED bool ; bool ::= TRUE {: compiled = new Boolean(true); a[2] = compiled ; :} | FALSE {: compiled = new Boolean(false); a[2] = compiled ; :} ; timestamp ::= TIME INTEGER:ts {: timestamp = ts; a[5] = timestamp; :} ; imported_symbol ::= imported_symbol imported_or_symbol | imported_or_symbol ; imported_or_symbol ::= imported:i {: importedv.add(i); :} | symbol:s {: symbolv.add(s); :} ; imported ::= IMPORT UNQUAL_ID:module_name {: RESULT = module_name; :} ; symbol ::= type:t {: RESULT = t; :} | constructor:c {: RESULT = c; :} | operation:o {: RESULT = o; :} ; type ::= TYPE UNQUAL_ID:tsn INTEGER:type_arity visibility:v {: RESULT = new TypeSymbol(name,tsn,type_arity.intValue(),v.intValue()); :} ; visibility ::= {: RESULT = new Integer(code.symbols.Symbol.DefaultVisibility); :} | PUBLIC {: RESULT = new Integer(code.symbols.Symbol.Public); :} | PRIVATE {: RESULT = new Integer(code.symbols.Symbol.Private); :} ; constructor ::= CONSTRUCTOR constructor_symbol_name:csn staticNestingDepth:depth arity:ari kind:k associativityPrecedence:ap visibility:v COLONCOLON typeExp:te {: RESULT = new ConstructorSymbol(name,csn, depth.intValue(), ari.intValue(), k.intValue(), ((Integer)ap[0]).intValue(), ((Integer)ap[1]).intValue(), v.intValue(), te); :} ; operation ::= OPERATION operation_symbol_name:osn staticNestingDepth:depth arity:ari associativityPrecedence:ap visibility:v COLONCOLON typeExp:te codeBlock:c {: Object [] oa = c.toArray(); Instruction [] ia = new Instruction[oa.length]; for(int i=0;i