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

Commit5a3b475

Browse files
committed
added docs and improved names
1 parent8dfb87b commit5a3b475

File tree

1 file changed

+36
-15
lines changed

1 file changed

+36
-15
lines changed

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

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ import scala.language.implicitConversions
2020

2121
// TODO: better error handling (labelling like parsec's <?>)
2222

23+
/** An enumeration of operator associativity values: `Left`, `Right`, and
24+
* `Non`.
25+
*/
2326
objectAssociativityextendsEnumeration {
2427
typeAssociativity=Value
2528

@@ -1025,18 +1028,34 @@ trait Parsers {
10251028

10261029
importAssociativity._
10271030

1028-
classPrecedenceParser[Exp,Op](primary:Parser[Exp],
1029-
binop:Parser[Op],
1030-
precedence:Op=>Int,
1031-
associativity:Op=>Associativity,
1032-
makeBinop: (Exp,Op,Exp)=>Exp)extendsParser[Exp] {
1033-
classPrecedenceSuffixParser(lhs:Exp,minLevel:Int)extendsParser[Exp] {
1031+
/** A parser that respects operator the precedence and associativity
1032+
* conventions specified in its constructor.
1033+
*
1034+
*@paramprimary a parser that matches atomic expressions (the atomicity is
1035+
* from the perspective of binary operators). May include
1036+
* unary operators or parentheses.
1037+
*@parambinop a parser that matches binary operators.
1038+
*@paramprecedence a function from operators to their precedence levels.
1039+
* Operators with higher precedence values bind more
1040+
* tightly than those with lower values.
1041+
*@paramassociativity a function from operators to their associativity.
1042+
*@parammakeBinop a function that combines two operands and an operator
1043+
* into a new expression. The result must have the same type
1044+
* as the operands because intermediate results become
1045+
* operands to other operators.
1046+
*/
1047+
classPrecedenceParser[Exp,Op,E<:Exp](primary:Parser[E],
1048+
binop:Parser[Op],
1049+
precedence:Op=>Int,
1050+
associativity:Op=>Associativity,
1051+
makeBinop: (Exp,Op,Exp)=>Exp)extendsParser[Exp] {
1052+
privateclassExpandLeftParser(lhs:Exp,minLevel:Int)extendsParser[Exp] {
10341053
valopPrimary= binop~ primary;
1035-
defparse(input:Input):ParseResult[Exp]= {
1054+
defapply(input:Input):ParseResult[Exp]= {
10361055
opPrimary(input)match {
10371056
caseSuccess(op~ rhs, next)if precedence(op)>= minLevel=> {
1038-
newPrecedenceRhsSuffixParser(rhs, precedence(op), minLevel)(next)match {
1039-
caseSuccess(r, nextInput)=>newPrecedenceSuffixParser(makeBinop(lhs, op, r), minLevel)(nextInput);
1057+
newExpandRightParser(rhs, precedence(op), minLevel)(next)match {
1058+
caseSuccess(r, nextInput)=>newExpandLeftParser(makeBinop(lhs, op, r), minLevel)(nextInput);
10401059
case ns=> ns// dead code
10411060
}
10421061
}
@@ -1047,7 +1066,7 @@ trait Parsers {
10471066
}
10481067
}
10491068

1050-
classPrecedenceRhsSuffixParser(rhs:Exp,currentLevel:Int,minLevel:Int)extendsParser[Exp] {
1069+
privateclassExpandRightParser(rhs:Exp,currentLevel:Int,minLevel:Int)extendsParser[Exp] {
10511070
privatedefnextLevel(nextBinop:Op):Option[Int]= {
10521071
if (precedence(nextBinop)> currentLevel) {
10531072
Some(minLevel+1)
@@ -1057,14 +1076,14 @@ trait Parsers {
10571076
None
10581077
}
10591078
}
1060-
defparse(input:Input):ParseResult[Exp]= {
1079+
defapply(input:Input):ParseResult[Exp]= {
10611080
defdone:ParseResult[Exp]=Success(rhs, input)
10621081
binop(input)match {
10631082
caseSuccess(nextBinop,_)=> {
10641083
nextLevel(nextBinop)match {
10651084
caseSome(level)=> {
1066-
newPrecedenceSuffixParser(rhs, level)(input)match {
1067-
caseSuccess(r, next)=>newPrecedenceRhsSuffixParser(r, currentLevel, minLevel)(next)
1085+
newExpandLeftParser(rhs, level)(input)match {
1086+
caseSuccess(r, next)=>newExpandRightParser(r, currentLevel, minLevel)(next)
10681087
case ns=> ns// dead code
10691088
}
10701089
}
@@ -1076,10 +1095,12 @@ trait Parsers {
10761095
}
10771096
}
10781097

1079-
defparse(input:Input):ParseResult[Exp]= {
1098+
/** Parse an expression.
1099+
*/
1100+
defapply(input:Input):ParseResult[Exp]= {
10801101
primary(input)match {
10811102
caseSuccess(lhs, next)=> {
1082-
newPrecedenceSuffixParser(lhs,0)(next)
1103+
newExpandLeftParser(lhs,0)(next)
10831104
}
10841105
case noSuccess=> noSuccess
10851106
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp