orangeduck/mpc

parsing decimal numbers

TheFausap opened this issue · 5 comments

Hello,

I'm trying to parse decimal numbers using this regexp:

mpca_lang(MPCA_LANG_DEFAULT,
"
number : /-?[0-9]+(.[0-9]*)?/ ;
"
this works but I have to specify always a decimal dot.
lispy> list 1.0 2.0 3.0 4.0

regex
expr|symbol|string:1:1 'list'
expr|number|regex:1:6 '1.0'
expr|number|regex:1:10 '2.0'
expr|number|regex:1:14 '3.0'
expr|number|regex:1:18 '4.0'
regex
{1.000000 2.000000 3.000000 4.000000}

but if I try
lispy> list 1 2 3 4

regex
expr|symbol|string:1:1 'list'
expr|number|regex:1:6 '1 2'
expr|number|regex:1:10 '3 4'
regex
{1.000000 3.000000}

so it seems the number is not parsed correctly.
I'm sure it's my fault, but I cannot understand the error in my regexp.

thanks
Fausto

Hey, you need to escape the period character . because it has the special meaning of matching any character in regular expressions (in this case it is matching a single space character).

You can try "number : /-?[0-9]+(\\.[0-9]*)?/ ; "

Thanks for bringing it up. I'll make sure to mention this in the book because a few people have had trouble with it.

Thanks! I tried to escape it, but with only one , I didn't try with a double \
Btw, your parser is very very nice software!

Yeah you need it twice because it is inside a C string, confusing!

Thanks :)

No... thanks to you.
I sent an email to contact@theorangeduck.com about "cons" :-)
When you have time if you can read it... thanks again!