0

I am trying out scala parser combinators with the following object:

object LogParser extends JavaTokenParsers with PackratParsers {

Some of the parsers are working. But the following one is getting tripped up:

 def time  = """([\d]{2}:[\d]{2}:[\d]{2}\.[\d]+)"""

Following is the input not working:

09:58:24.608891

On reaching that line we get:

[2.22] failure: `([\d]{2}:[\d]{2}:[\d]{2}\.[\d]+)' expected but `:' found

09:58:24.608891

Note: I did verify correct behavior of that regex within the scala repl on the same input pattern.

val r = """([\d]{2}):([\d]{2}):([\d]{2}\.[\d]+)""".r
val s = """09:58:24.608891"""

val r(t,t2,t3) = s
t: String = 09
t2: String = 58
t3: String = 24.608891

So.. AFA parser combinator: is there an issue with the ":" token itself - i.e. need to create my own custom Lexer and add ":" to lexical.delimiters?

Update an answer was provided to add ".r". I had already tried that- but in any case to be explicit: the following has the same behavior (does not work):

def time = """([\d]{2}:[\d]{2}:[\d]{2}.[\d]+)""".r

1 Answer 1

1

I think you're just missing an .r at the end here to actually have a Regex as opposed to a string literal.

 def time  = """([\d]{2}:[\d]{2}:[\d]{2}\.[\d]+)"""

it should be

 def time  = """([\d]{2}:[\d]{2}:[\d]{2}\.[\d]+)""".r

The first one expects the text to be exactly like the regex string literal (which obviously isn't present), the second one expects text that actually matches the regex. Both create a Parser[String], so it's not immediately obvious that something is missing.

  • There's an implicit conversion from java.lang.String to Parser[String], so that string literals can be used as parser combinators.
  • There's an implicit conversion from scala.util.matching.Regex to > Parser[String], so that regex expressions can be used as parser combinators.

http://www.scala-lang.org/files/archive/api/2.11.2/scala-parser-combinators/#scala.util.parsing.combinator.RegexParsers

Sign up to request clarification or add additional context in comments.

4 Comments

actually I had already tried that .. I will update the OP including the .r to demonstrate it.
I see. Do you get any different error when trying that? Here is the minimal example I used to check, that works fine in isolation (the test passes). Maybe there is something around it that's making problems, not that particular parser itself: gist.github.com/anonymous/3dbc9556044c6629b117
No, same behavior/error. I have also isolated that piece to a single parser expression: still no change.
I appreciate your effort / gist and have upvoted for it. Your gist demonstrates that the standalone piece works. Man these combinators are brittle. I will not award (yet..) in case someone were to have additional input leading to a complete solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.