diff options
author | paltherr <paltherr@epfl.ch> | 2004-03-19 00:01:09 +0000 |
---|---|---|
committer | paltherr <paltherr@epfl.ch> | 2004-03-19 00:01:09 +0000 |
commit | c5ca08e53f6e6b6ba80f22b30cb6deb471c4ec69 (patch) | |
tree | d0f7b11d5261bbf859d01f4fbc7fce43c215f32a | |
parent | c94a990938f03b4075f976ce14958a20c4733124 (diff) | |
download | scala-c5ca08e53f6e6b6ba80f22b30cb6deb471c4ec69.tar.gz scala-c5ca08e53f6e6b6ba80f22b30cb6deb471c4ec69.tar.bz2 scala-c5ca08e53f6e6b6ba80f22b30cb6deb471c4ec69.zip |
- Moved methods precedence and isLeftAssoc from...
- Moved methods precedence and isLeftAssoc from Name to Parser
-rw-r--r-- | sources/scala/tools/scalac/ast/parser/Parser.scala | 38 | ||||
-rw-r--r-- | sources/scalac/util/Name.java | 42 |
2 files changed, 31 insertions, 49 deletions
diff --git a/sources/scala/tools/scalac/ast/parser/Parser.scala b/sources/scala/tools/scalac/ast/parser/Parser.scala index 3aefd6742f..29ee972e42 100644 --- a/sources/scala/tools/scalac/ast/parser/Parser.scala +++ b/sources/scala/tools/scalac/ast/parser/Parser.scala @@ -214,7 +214,7 @@ class Parser(unit: Unit) { */ def makeBinop(isExpr: boolean, pos: int, left: Tree, op: Name, right: Tree): Tree = if (isExpr) { - if (op.isLeftAssoc()) { + if (isLeftAssoc(op)) { make.Apply( pos, make.Select(pos, left, NameTransformer.encode(op)), @@ -432,6 +432,30 @@ class Parser(unit: Unit) { var operators = new Array[Name](8); var sp = 0; + def precedence(operator: Name): int = + if (operator eq Names.ERROR) -1 + else { + val first_ch = operator.charAt(0); + if (((first_ch >= 'A') && (first_ch <= 'Z')) || + ((first_ch >= 'a') && (first_ch <= 'z'))) + 1 + else + first_ch match { + case '|' => 2 + case '^' => 3 + case '&' => 4 + case '<' | '>' => 5 + case '=' | '!' => 6 + case ':' => 7 + case '+' | '-' => 8; + case '*' | '/' | '%' => 9; + case _ => 10; + } + } + + def isLeftAssoc(operator: Name): boolean = + operator.length() > 0 && operator.charAt(operator.length() - 1) != ':'; + def push(od: Tree, pos: int, op: Name): unit = { if (sp == operands.length) { val operands1 = new Array[Tree](sp * 2); @@ -453,16 +477,16 @@ class Parser(unit: Unit) { def reduceStack(isExpr: boolean, base: int, _top: Tree, prec: int, leftAssoc: boolean): Tree = { var top = _top; if (sp != base && - operators(sp-1).precedence() == prec && - operators(sp-1).isLeftAssoc() != leftAssoc) { + precedence(operators(sp-1)) == prec && + isLeftAssoc(operators(sp-1)) != leftAssoc) { syntaxError( positions(sp-1), "left- and right-associative operators with same precedence may not be mixed", false); } while (sp != base && - (prec < operators(sp-1).precedence() || - (leftAssoc && prec == operators(sp-1).precedence()))) { + (prec < precedence(operators(sp-1)) || + (leftAssoc && prec == precedence(operators(sp-1))))) { sp = sp - 1; top = makeBinop(isExpr, positions(sp), operands(sp), operators(sp), top); } @@ -903,7 +927,7 @@ class Parser(unit: Unit) { var top = prefixExpr(); while (s.token == IDENTIFIER) { top = reduceStack( - true, base, top, s.name.precedence(), s.name.isLeftAssoc()); + true, base, top, precedence(s.name), isLeftAssoc(s.name)); push(top, s.pos, s.name); ident(); if (isExprIntro()) { @@ -1242,7 +1266,7 @@ class Parser(unit: Unit) { while ((s.token == IDENTIFIER) && (s.name != BAR)) { val tokn = s.name; // for error message top = reduceStack( - false, base, top, s.name.precedence(), s.name.isLeftAssoc()); + false, base, top, precedence(s.name), isLeftAssoc(s.name)); push(top, s.pos, s.name); ident(); top = simplePattern(); diff --git a/sources/scalac/util/Name.java b/sources/scalac/util/Name.java index 6a8c8e2c6f..20f64e3a48 100644 --- a/sources/scalac/util/Name.java +++ b/sources/scalac/util/Name.java @@ -138,46 +138,4 @@ public final class Name { public static final Name ERROR = Name.fromString("<error>"); static { ERROR.type = ERROR; } -/** precedence of this name - */ - public int precedence() { - if (this == ERROR) - return -1; - char ch = string.charAt(0); - if (((ch >= 'A') && (ch <= 'Z')) || - ((ch >= 'a') && (ch <= 'z'))) - return 1; - switch (ch) { - case '|': - return 2; - case '^': - return 3; - case '&': - return 4; - case '<': - case '>': - return 5; - case '=': - case '!': - return 6; - case ':': - return 7; - case '+': - case '-': - return 8; - case '*': - case '/': - case '%': - return 9; - default: - return 10; - } - } - -/** is this operator left associative - */ - public boolean isLeftAssoc() { - int length = string.length(); - return length > 0 && string.charAt(length - 1) != ':'; - } } |