Skip to content

Instantly share code, notes, and snippets.

@HerringtonDarkholme
Last active August 29, 2015 14:06
Show Gist options
  • Save HerringtonDarkholme/64494283e44d2eebe4ae to your computer and use it in GitHub Desktop.
Save HerringtonDarkholme/64494283e44d2eebe4ae to your computer and use it in GitHub Desktop.
Parser Combinator requires every regex is initialized before usage
import scala.util.parsing.combinator._
import scala.util.matching.Regex.Match
import scala.util.matching.Regex
import scala.annotation.{switch, tailrec}
object T extends RegexParsers {
abstract class AbstractSelector[S <: AbstractSelector[S]]
class ComplexSelector(val combinator: Char, val x: CompoundSelector, val xs: ComplexSelector)
extends AbstractSelector[ComplexSelector]() {
override def toString = xs match {
case _: ComplexSelector => xs + combinator.toString + x
case _ => x.toString
}
}
case class CompoundSelector(tpe: String, simpleSelectors: List[SimpleSelector])
extends AbstractSelector[CompoundSelector] {
override def toString = tpe + simpleSelectors.mkString("")
}
class SimpleSelector extends AbstractSelector[SimpleSelector]
override def skipWhitespace = false
private final val Combinator = """\s*[+>~]\s*""".r ^^ {
case cmb => (cpx: ComplexSelector, cpd: CompoundSelector) =>
new ComplexSelector(cmb.trim.head, cpd, cpx)
}
private final val Whitespace = """\s+""".r ^^ {
case cmb => (cpx: ComplexSelector, cpd: CompoundSelector) =>
new ComplexSelector(' ', cpd, cpx)
}
private final val ComplexZero = CompoundParser ^^ {
case cpd =>
new ComplexSelector('\u0000', cpd, null)
}
def ComplexParser: Parser[ComplexSelector] =
chainl1(ComplexZero, CompoundParser, (Combinator|Whitespace))
def CompoundParser: Parser[CompoundSelector] = (ident|"*") ^^ {
case tpe =>
CompoundSelector(tpe, List())
}
private final val ident = "[0-9A-Za-z_-]+".r
def main(args: Array[String]): Unit =
println(ident.toString)
println(parseAll(ComplexParser, "div").get)
}
@HerringtonDarkholme
Copy link
Author

L53 will incurs a NullPointerException

@HerringtonDarkholme
Copy link
Author

I'm using scala 2.11 to write a CSS parser.
It gives me a weird NullPointerException where happens in Parser Combinators' library.

After several debugging, I found the problem is that my regular expression declaration is behind my Parser. The gist is here.
Placing the declaration to the top will fix this.

I'm confused why Parsers trait will override the execution order in my code. In the line 55 in the gist, a NullPointerException is thrown before the println above it.

val ident = """[A-Za-z0-9-_]""".r
def main(args: Array[String]): Unit =
  println(ident.toString)
  println(parseAll(ComplexParser, "div").get)
}

After browsing scala-parser-combinator library, I don't think it's a trait initialization problem though, and all parse related codes are method. So what's the problem here? I'm a newbie to Scala so do I misunderstand initialization order?

Scala Parser Combinator: parseAll executed before field initialization

@HerringtonDarkholme
Copy link
Author

I got it.

https://github.com/scala/scala-parser-combinators/blob/master/src/main/scala/scala/util/parsing/combinator/Parsers.scala#L248

By implicit conversion, ident is a Parser[String], which has plenty of call by name parameter and ... say, null value filled val get stuffed into those method's apply closure (yes, tons of closure). So the val should be lifted up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment