简体   繁体   English

如何在pegjs中匹配以“?”开头的令牌

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

I have to match tokens like this using pegjs: 我必须使用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? 这是因为str变量不是匹配标记的数组...任何想法该怎么做?

You can either group literals together: 您可以将文字组合在一起:

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

in which case str will be ["?",["a","b","c"]] for ?abc , or, if ? 在这种情况下["?",["a","b","c"]]对于?abcstr将为["?",["a","b","c"]] ,或者,如果为? 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"] . 那么您将获得一个普通数组["?","a","b","c"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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