Skip to content

Instantly share code, notes, and snippets.

@kmizu
Created April 6, 2016 17:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kmizu/3fef75de17da1c4ffcf989b3087a044e to your computer and use it in GitHub Desktop.
Save kmizu/3fef75de17da1c4ffcf989b3087a044e to your computer and use it in GitHub Desktop.
Undefined Varible is Parse Error
object L {
lazy val Spacing: P[Any] = ("\r" | "\t" | " " | "\r" | "\n").*
def OPEN: P[Any] = "(" ~ Spacing
def CLOSE: P[Any] = ")" ~ Spacing
def EQ: P[Any] = "=" ~ Spacing
def SEMI_COLON: P[Any] = string(";") ~ Spacing
def VAL: P[Any] = "val" ~ Spacing
lazy val S: P[Any] = Statements(!"") ~ !any
def Statements(table: P[Any]): P[Any] = VAL ~ Identifier.evalCC{i => EQ ~ Expression(table) ~ SEMI_COLON ~ refer(Statements(table | i)).? } / Expression(table) ~ SEMI_COLON ~ refer(Statements(table)).?
def Expression(table: P[Any]): P[Any] = refer(Primary(table)) ~ (range(Seq('+','-','*','/')) ~ Spacing ~ refer(Primary(table))).*
def Primary(table: P[Any]): P[Any] = table.and ~ Identifier | IntegerLiteral | (OPEN ~ refer(Expression(table)) ~ CLOSE)
lazy val Identifier: P[Any] = range('a'to'z','A'to'Z',Seq('_')) ~ range('a'to'z','A'to'Z','0'to'9',Seq('_')).* ~ Spacing
lazy val IntegerLiteral: P[Any] = range('0'to'9').+ ~ Spacing
}
val S = L.S
assert(S("(s * 1);").drop == ParseFailure("", "s * 1);"))
assert(S("val s = 1; (s * 1);").drop == ParseSuccess(None, ""))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment