Created
April 6, 2016 17:11
-
-
Save kmizu/3fef75de17da1c4ffcf989b3087a044e to your computer and use it in GitHub Desktop.
Undefined Varible is Parse Error
This file contains 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
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