Skip to content

Correcting grammer for nicer errors #955

Answered by Quan1umMango
Quan1umMango asked this question in Q&A
Discussion options

You must be logged in to vote

I got this solution, but if theres a better way, please let me know:

WHITESPACE = _{" "|"\r\n" | "\n"}
semi = _{";"}
let_keyword = _{"let"}
ident = {ASCII_ALPHA ~ (ASCII_DIGIT | ASCII_ALPHA|"_")*}
assign = _{"="}


add = {"+"}
sub = {"-"}
mul = {"*"}
div = {"/"}

bin_op = _{ add | sub | mul | div }
int_lit =  {ASCII_DIGIT+}

expr = { ((string_lit) ~ (add~string_lit)* ~ semi )| (int_expr ~semi)  }

string_lit= { "\"" ~ (!("\"" | "\n" | "\r") ~ ANY)* ~ "\"" }

int_expr = {int_lit ~ (bin_op ~ int_expr)*}
let_stmt = { let_keyword ~ ident ~ assign ~ expr}
program = _{SOI ~ let_stmt+ ~ EOI}

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by Quan1umMango
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant