简体   繁体   中英

How do I match tokens starting with “?” in pegjs

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?

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"] .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM