0

I have to match tokens like this using pegjs:

?xxx ?yyy

I would have thought this would work :

variable 
   = str:?[a-z]+ {  console.log('---->>>',str); return str.join(""); } 

When I parse the source I get and error:

Object ? has no method 'join'

This is because the str variable is not an array of the matched tokens... Any idea how this should be done?

t.niese
  • 39,256
  • 9
  • 74
  • 101
Johan
  • 1,189
  • 3
  • 15
  • 28
  • Are you sure this is a real copy and past of the rule, because I would expect that the pegjs would already throw an error when parsing the grammar as `str:?[a-z]+` itself is not currect. – t.niese Apr 12 '14 at 20:35
  • Sorry. You are right. It was str[?][a-z]+ ... I copied from an older/wrong copy of the code ... – Johan Apr 17 '14 at 12:15

1 Answers1

1

You can either group literals together:

variable 
    = str:("?"[a-z]+)

in which case str will be ["?",["a","b","c"]] for ?abc, or, if ? is not necessarily the first char, just include it in the class:

variable 
    = str:[?a-z]+

then you'll get a normal array ["?","a","b","c"].

gog
  • 10,367
  • 2
  • 24
  • 38