Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite3de0aa

Browse files
committed
New interface for specifying precedence that doesn't allow mixed associativity on a level
1 parent01ed840 commite3de0aa

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

‎shared/src/main/scala/scala/util/parsing/combinator/Parsers.scala

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,24 +1056,34 @@ trait Parsers {
10561056
* from the perspective of binary operators). May include
10571057
* unary operators or parentheses.
10581058
*@parambinop a parser that matches binary operators.
1059-
*@paramprecedence afunction from operators to their precedence levels.
1060-
*Operators with higher precedence values bind more
1061-
*tightly than those with lower values.
1062-
*@paramassociativity a function from operators to their associativity.
1059+
*@paramprec_table alist of tuples, each of which encodes a level of
1060+
*precedence. Precedence is encoded highest to lowest.
1061+
*Each precedence level contains an Associativity value
1062+
* and a list of operators.
10631063
*@parammakeBinop a function that combines two operands and an operator
10641064
* into a new expression. The result must have the same type
10651065
* as the operands because intermediate results become
10661066
* operands to other operators.
10671067
*/
10681068
classPrecedenceParser[Exp,Op,E<:Exp](primary:Parser[E],
10691069
binop:Parser[Op],
1070-
precedence:Op=>Int,
1071-
associativity:Op=>Associativity,
1070+
prec_table:List[(Associativity,List[Op])],
10721071
makeBinop: (Exp,Op,Exp)=>Exp)extendsParser[Exp] {
1072+
privatedefdecodePrecedence: (Map[Op,Int],Map[Op,Associativity])= {
1073+
varprecedence=Map.empty[Op,Int]
1074+
varassociativity=Map.empty[Op,Associativity]
1075+
varlevel= prec_table.length
1076+
for ((assoc, ops)<- prec_table) {
1077+
precedence= precedence++ (for (op<- ops)yield (op, level))
1078+
associativity= associativity++ (for (op<- ops)yield (op, assoc))
1079+
level-=1
1080+
}
1081+
(precedence, associativity)
1082+
}
1083+
val (precedence, associativity)= decodePrecedence
10731084
privateclassExpandLeftParser(lhs:Exp,minLevel:Int)extendsParser[Exp] {
1074-
valopPrimary= binop~ primary;
10751085
defapply(input:Input):ParseResult[Exp]= {
1076-
opPrimary(input)match {
1086+
(binop~ primary)(input)match {
10771087
caseSuccess(op~ rhs, next)if precedence(op)>= minLevel=> {
10781088
newExpandRightParser(rhs, precedence(op), minLevel)(next)match {
10791089
caseSuccess(r, nextInput)=>newExpandLeftParser(makeBinop(lhs, op, r), minLevel)(nextInput);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp