orangeduck/mpc

Regex difficulty with adding decimal support

judepayne opened this issue · 2 comments

Hi,
first of all thanks for your wonderful book!
I am having difficulty adding support for doubles and wondered if you could offer any tips?
I've added a parser for doubles and all the rest of the plumbing as per number but can't seem to get past the first stage, defining the reggae for the parser:
mpca_lang(MPCA_LANG_DEFAULT,
"
number : /-?[0-9]+/ ;
doub : /-?[0-9]+.[0-9]+/ ;
symbol : "list" | "head" | "tail"
| "join" | "eval" | "len"
...
I also get the following error message when typing in say '1.0' into my interpreter.
:1:2: error: expected one of '0123456789', whitespace, '-', one or more of one of '0123456789', "list", "head", "tail", "join", "eval", "len", "cons", "init", '+', '*', '/', '(', '{' or end of input at '.'
thanks in advance,
Jude

Hi Jude,

Probably all you need to do is escape the . character twice (because it is inside a C string) so it looks like \\.. Also if you are using a new rule such as doub make sure you include it as a possible expr - you should also make sure you put the most specific clause first if there is some overlap:

https://github.com/orangeduck/mpc#backtracking-isnt-working

There are a couple of other people who had the same issue that you can look at for reference:

#12
#10

Thanks,

Dan

Thank you. silly error. I had omitted to include in the expr list. adding .. solved it.
expr : | | |
| ; \