I'm beginning PEG's with PEG.js
.
There's something I can't get my head around...I'm sure it's simple but it's giving me a headache trying to understand the concept...
Consider this two-rule grammar:
name
= name:.* {return name.join("")}
put
= "put " a:name " into " b:name "." {put(a,b)}
I want to be able to pass this parser "put foo into bar." and cause put("foo","bar")
to evaluate.
But PEG.js gives me the error Expected " into " or any character but end of input found.
I think I could fix this problem if the regex for the name
rule where more specific than .*
but why does it have to be? Can't the parser be smart enough to look ahead and see that " into " is coming up, as well as the "." at the end?
How can I achieve what I'm looking for? Is this perhaps the difference between a "bottom-up" and "top-down" grammar?
Edit:
The regex /put (.*) into (.*)/g
works like I want -- if I pass it "put foo into bar"
, it gives me $1="foo"
and $2="bar"
. I'm just asking if I can get this same functionality (taking the whole string into account before deciding where the token boundaries are) using PEGjs or Jison.