Last active
August 29, 2015 14:06
-
-
Save HerringtonDarkholme/64494283e44d2eebe4ae to your computer and use it in GitHub Desktop.
Parser Combinator requires every regex is initialized before usage
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
I got it.
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
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, aNullPointerException
is thrown before theprintln
above it.After browsing
scala-parser-combinator
library, I don't think it's a trait initialization problem though, and allparse
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