1

I am trying to get my thick head around the following problem:

class LineParser extends JavaTokenParsers {
   def lines: Parser[Any] = rep(line)
   def line: Parser[String] = """^.+$""".r

}

object LineParserTest extends LineParser {
   def main(args: Array[String]) {
       val reader = new FileReader(args(0))
       println("input : "+ args(0))
       println(parseAll(lines, reader))
   }
}

Input file:

one
two

When I run the program, it gives me this error:

[1.1] failure: string matching regex `^.+$' expected but `o' found

one

^

Apparently, I did something stupid, but couldn't figure out why. Please advise.

The above is a simplified version of the real goal: to parse a cisco-like configuration file which include commands and sub-commands and build an AST. There are several commands that I don't care and would like to ignore them using the pattern """^.+$""" above.

1 Answer 1

2

You don't need ^ and $ in the regex. By default . doesn't match end of line, so the following will make it work:

def line: Parser[String] = """.+""".r

In Java, if you want ^ and $ to match the line terminator, you have to set the Pattern.MULTILINE flag. So the following definition will work as well:

def line: Parser[String] = """(?m)^.+$""".r
Sign up to request clarification or add additional context in comments.

Comments

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.