簡體   English   中英

用PEG.js忽略空格

[英]Ignore whitespace with PEG.js

我想用我的語法忽略空格新行 ,因此它們在PEG.js輸出中缺失。 此外,括號內的文字應該在新數組中返回。

語法

start
  = 'a'? sep+ ('cat'/'dog') sep* '(' sep* stmt_list sep* ')'

stmt_list
  = exp: [a-zA-Z]+ { return new Array(exp.join('')) }

sep
  = [' '\t\r\n]

測試用例

a dog( Harry )

產量

[
   "a",
   [
      " "
   ],
   "dog",
   [],
   "(",
   [
      " "
   ],
   [
       "Harry"
   ],
   [
      " "
   ],
   ")"
]

我想要的輸出

[
   "a",
   "dog",
   [
      "Harry"
   ]
]

你必須更多地使用更多的“非終端”分解語法(不確定這是否是你在PEG中所稱的那些):

start
  = article animal stmt_list

article
  = article:'a'? __ { return article; }

animal
  = animal:('cat'/'dog') _ { return animal; }

stmt_list
  = '(' _ exp:[a-zA-Z]+ _ ')' { return [ exp.join('') ]; }

// optional whitespace
_  = [ \t\r\n]*

// mandatory whitespace
__ = [ \t\r\n]+

謝謝你提出這個問題!

編輯:為了提高可讀性,有兩個產品: ___

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM