File tree 3 files changed +68
-1
lines changed
main/scala/scala/util/parsing/combinator
test/scala/scala/util/parsing/combinator
3 files changed +68
-1
lines changed Original file line number Diff line number Diff line change @@ -138,6 +138,22 @@ trait RegexParsers extends Parsers {
138
138
}
139
139
}
140
140
141
+ // we might want to make it public/protected in a future version
142
+ private def ws [T ](p : Parser [T ]): Parser [T ] = new Parser [T ] {
143
+ def apply (in : Input ) = {
144
+ val offset = in.offset
145
+ val start = handleWhiteSpace(in.source, offset)
146
+ p(in.drop (start - offset))
147
+ }
148
+ }
149
+
150
+ /**
151
+ * @inheritdoc
152
+ *
153
+ * This parser additionally skips whitespace if `skipWhitespace` returns true.
154
+ */
155
+ override def err (msg : String ) = ws(super .err(msg))
156
+
141
157
/**
142
158
* A parser generator delimiting whole phrases (i.e. programs).
143
159
*
Original file line number Diff line number Diff line change 1
1
package scala .util .parsing .combinator
2
2
3
3
import org .junit .Test
4
- import org .junit .Assert .assertEquals
4
+ import org .junit .Assert .{ assertEquals , assertTrue }
5
5
6
6
class RegexParsersTest {
7
7
@ Test
@@ -100,4 +100,19 @@ class RegexParsersTest {
100
100
val success = parseAll(twoWords, " first second" ).asInstanceOf [Success [(String , String )]]
101
101
assertEquals((" second" , " first" ), success.get)
102
102
}
103
+
104
+ @ Test
105
+ def errorConsumesWhitespace : Unit = {
106
+ object parser extends RegexParsers {
107
+ def num = " \\ d+" .r
108
+
109
+ def twoNums = num ~ (num | err(" error!" ))
110
+ }
111
+ import parser ._
112
+
113
+ // this used to return a Failure (for the second num)
114
+ val error = parseAll(twoNums, " 458 bar" )
115
+ assertTrue(s " expected an Error but got: ${error.getClass.getName}" , error.isInstanceOf [Error ])
116
+ assertEquals(" error!" , error.asInstanceOf [Error ].msg)
117
+ }
103
118
}
Original file line number Diff line number Diff line change
1
+ package scala .util .parsing .combinator
2
+
3
+ import org .junit .Test
4
+ import org .junit .Assert .assertEquals
5
+
6
+ class gh29 {
7
+ object Foo extends JavaTokenParsers {
8
+ def word (x : String ) = s " \\ b $x\\ b " .r
9
+
10
+ lazy val expr = aSentence | something
11
+
12
+ lazy val aSentence = noun ~ verb ~ obj
13
+
14
+ lazy val noun = word(" noun" )
15
+ lazy val verb = word(" verb" ) | err(" not a verb!" )
16
+ lazy val obj = word(" object" )
17
+
18
+ lazy val something = word(" FOO" )
19
+ }
20
+
21
+ val expected =
22
+ """ [1.6] error: not a verb!
23
+
24
+ noun vedsfasdf
25
+ ^""" .stripMargin
26
+
27
+ @ Test
28
+ def test (): Unit = {
29
+ val f = Foo .parseAll(Foo .expr, " noun verb object" )
30
+
31
+ assertEquals(" [1.17] parsed: ((noun~verb)~object)" , f.toString)
32
+
33
+ val g = Foo .parseAll(Foo .expr, " noun vedsfasdf" )
34
+ assertEquals(expected, g.toString)
35
+ }
36
+ }
You can’t perform that action at this time.
0 commit comments