diff --git a/sources/zweic/Parser.scala b/sources/zweic/Parser.scala index 7e6476c..887985e 100755 --- a/sources/zweic/Parser.scala +++ b/sources/zweic/Parser.scala @@ -108,7 +108,7 @@ classes = classDecl() :: classes; } val main = expression(); - return Program(classes.reverse, main); + return Program(classes.reverse, main).setPos(pos); } /** @@ -128,7 +128,7 @@ while ( !check(RACCOLADE) ) { members = member() :: members; } - return ClassDef (name, extend, members.reverse); + return ClassDef (name, extend, members.reverse).setPos(pos); } /** @@ -142,18 +142,17 @@ return FieldDecl(f); } else { accept(LPAREN); - //TODO: is this neccessary? var par:List[Formal] = Nil; if (token == IDENT || token == INT || token == NULLTYPE) { - par = formal() :: par; - while (token != RPAREN) { - accept (PERIOD); - par = formal() :: par; - } - } + par = formal() :: par; + while (token != RPAREN) { + accept (PERIOD); + par = formal() :: par; + } + } accept (RPAREN); val b:Block = block(); - return MethodDef (f.name, par.reverse, f.typ, b); + return MethodDef (f.name, par.reverse, f.typ, b).setPos(pos); } } @@ -164,7 +163,7 @@ val typ = type1(); val name = Name(chars); accept(IDENT); - return Formal(name, typ); + return Formal(name, typ).setPos(pos); } /** @@ -173,7 +172,7 @@ private def block(): Block = { accept (LACCOLADE); var s:List[Stat] = Nil; - var e:Expr = NullLit(); //CHAOS + var e:Expr = NullLit().setPos(pos); while (token != RETURN && token != RACCOLADE) { s = statement() :: s; } @@ -181,16 +180,17 @@ e = expression(); } accept(RACCOLADE); - return Block(s.reverse, e); + return Block(s.reverse, e).setPos(pos); } /** * Type1 = "Int" | "Null" | ident */ private def type1(): TypeTree = token match { - case INT => nextToken; return IntType(); - case NULLTYPE => nextToken; return NullType(); - case IDENT => val name=Name(chars); nextToken; return ClassType(name); + case INT => nextToken; return IntType().setPos(pos); + case NULLTYPE => nextToken; return NullType().setPos(pos); + case IDENT => val name=Name(chars); nextToken; + return ClassType(name).setPos(pos); case _ => error("'Int', 'Null' or identifier token"); return null; } @@ -205,71 +205,73 @@ private def statement(): Stat = { if (token == IDENT) { peekAhead match { - case IDENT => - //type definition and affection - val v = formal(); - accept(EQUALS); - val e = expression(); - accept(SEMICOLON); - return Var(v.name, v.typ, e); + case IDENT => + //type definition and affection + val v = formal(); + accept(EQUALS); + val e = expression(); + accept(SEMICOLON); + return Var(v.name, v.typ, e).setPos(pos); - case EQUALS => - //affection - val name = Name(chars); - accept(IDENT); - accept(EQUALS); - val e = expression(); - accept(SEMICOLON); - return Set(name, e); - - case _ => - //expression - val e = expression(); - accept(SEMICOLON); - return Do(e); - } + case EQUALS => + //affection + val name = Name(chars); + accept(IDENT); + accept(EQUALS); + val e = expression(); + accept(SEMICOLON); + return Set(name, e).setPos(pos); + + case _ => + //expression + val e = expression(); + accept(SEMICOLON); + return Do(e).setPos(pos); + } } else { - if (check(WHILE)) { - //while - accept(LPAREN); - val cond = expression(); - accept(RPAREN); - accept(LACCOLADE); - var stats:List[Stat] = Nil; - while ( !check(RACCOLADE) ) { - stats = statement() :: stats; - } - return While(cond, stats.reverse); - - } else if (token == NULLTYPE || token == INT) { - //type definition and affection - val v = formal(); - accept(EQUALS); - val e = expression(); - accept(SEMICOLON); - return Var(v.name, v.typ, e); - } else if (check(PRINTINT)) { - //printInt - accept(LPAREN); - val e = expression(); - accept(RPAREN); - accept(SEMICOLON); - return PrintInt(e); + if (check(WHILE)) { + //while + accept(LPAREN); + val cond = expression(); + accept(RPAREN); + accept(LACCOLADE); + var stats:List[Stat] = Nil; + while ( !check(RACCOLADE) ) { + stats = statement() :: stats; + } + return While(cond, stats.reverse).setPos(pos); + } else if (token == NULLTYPE || token == INT) { + //type definition and affection + val v = formal(); + accept(EQUALS); + val e = expression(); + accept(SEMICOLON); + return Var(v.name, v.typ, e).setPos(pos); + + } else if (check(PRINTINT)) { + //printInt + accept(LPAREN); + val e = expression(); + accept(RPAREN); + accept(SEMICOLON); + return PrintInt(e).setPos(pos); + } else if (check(PRINTCHAR)) { - //printChar - accept(LPAREN); - val e = expression(); - accept(RPAREN); - accept(SEMICOLON); - return PrintChar(e); + //printChar + accept(LPAREN); + val e = expression(); + accept(RPAREN); + accept(SEMICOLON); + return PrintChar(e).setPos(pos); + } else { - //expression - val e = expression(); - accept(SEMICOLON); - return Do(e); + //expression + val e = expression(); + accept(SEMICOLON); + return Do(e).setPos(pos); } } } @@ -287,7 +289,7 @@ if ( check(ELSE) ) { statb = expression(); } - return If(cond, stata, statb); + return If(cond, stata, statb).setPos(pos); } else { return cmpExpression(); } @@ -302,7 +304,7 @@ || token == GT || token == LE || token == GE ) { var op = compOp(); var right = sumExpression(); - left = Binop(op, left, right); + left = Binop(op, left, right).setPos(pos); } return left; } @@ -319,11 +321,11 @@ var right = term(); left.brackets = true; right.brackets = true; - var bo = Binop(Operators.AND, Unop(Operators.NOT, left), Unop(Operators.NOT, right)); + var bo = Binop(Operators.AND, Unop(Operators.NOT, left).setPos(pos), Unop(Operators.NOT, right).setPos(pos)).setPos(pos); bo.brackets = true; - left = Unop(Operators.NOT, bo); + left = Unop(Operators.NOT, bo).setPos(pos); } else { - left = Binop(sumOp(), left, term()); + left = Binop(sumOp(), left, term()).setPos(pos); } } @@ -339,7 +341,7 @@ while ( token == MUL || token == DIV || token == MOD || token == AND ) { var op = prodOp(); - left = Binop(op, left, _negFactor()); + left = Binop(op, left, _negFactor()).setPos(pos); } return left; @@ -351,17 +353,16 @@ nextToken; val e = factor(); e.brackets = true; - return Unop(op, e); + return Unop(op, e).setPos(pos); } else { return factor(); } private def _string(str: String): New = { - // TODO: make this less dirty if ( str.length() > 1 ) { - return New(Name("Cons"), IntLit(str.charAt(0).asInstanceOf[Int])::_string(str.substring(1))::Nil); + return New(Name("Cons"), IntLit(str.charAt(0).asInstanceOf[Int]).setPos(pos)::_string(str.substring(1))::Nil).setPos(pos); } else { - return New(Name("Cons"), New(Name("Nil"), Nil)::Nil); + return New(Name("Cons"), New(Name("Nil"), Nil).setPos(pos)::Nil).setPos(pos); } } @@ -372,30 +373,30 @@ var name = chars; var rval:Expr = null; rval = token match { - case IDENT => nextToken; Ident(Name(name)); - case NUMBER => nextToken; IntLit(Integer.parseInt(name)); + case IDENT => nextToken; Ident(Name(name)).setPos(pos); + case NUMBER => nextToken; IntLit(Integer.parseInt(name)).setPos(pos); case STRING => nextToken; _string(name); - case TRUE => nextToken; IntLit(1); - case FALSE => nextToken; IntLit(0); - case THIS => nextToken; Ident(Name("this")); - case NULLFACTOR => nextToken; NullLit(); - case READINT => nextToken; ReadInt(); - case READCHAR => nextToken; ReadChar(); + case TRUE => nextToken; IntLit(1).setPos(pos); + case FALSE => nextToken; IntLit(0).setPos(pos); + case THIS => nextToken; Ident(Name("this")).setPos(pos); + case NULLFACTOR => nextToken; NullLit().setPos(pos); + case READINT => nextToken; ReadInt().setPos(pos); + case READCHAR => nextToken; ReadChar().setPos(pos); case LPAREN => nextToken; var t = expression(); t.brackets = true; accept(RPAREN); t; case LACCOLADE => block(); case NEW => nextToken; accept(IDENT); - New(Name(chars), params()); + New(Name(chars), params()).setPos(pos); case _ => error("Identifier, Number, String, 'true', 'false', 'this', 'null', 'readInt', 'readChar', '(Expression)', '{Block}' or 'new'"); null; } while ( check(DOT) ) { accept(IDENT); if ( token == LPAREN ) { - rval = Call(rval, Name(chars), params()); + rval = Call(rval, Name(chars), params()).setPos(pos); } else { - rval = Select(rval, Name(chars)); + rval = Select(rval, Name(chars)).setPos(pos); } name = chars; }