Ignore whitespace with PEG.js

前端 未结 1 1598
别跟我提以往
别跟我提以往 2021-02-12 14:19

I want to ignore whitespaces and new lines with my grammar so they are missing in the PEG.js output. Also, a literal within brackets should be returned in a ne

相关标签:
1条回答
  • 2021-02-12 15:05

    You have to break up the grammar more, using more "non-terminals" (not sure if that's what you call them in a 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]+
    

    Thanks for asking this question!

    Edit: To increase readability, have two productions: _ and __

    0 讨论(0)
提交回复
热议问题