簡體   English   中英

如何使用 PEG.js 創建一個簡單的解析器

[英]How to create a simple parser using PEG.js

我想解析的語法如下:

# This is a comment

# This is a block. It starts with \begin{} and ends with \end{}
\begin{document}

# Within the document block other kinds of blocks can exist, and within them yet other kinds.
# Comments can exist anywhere in the code.

This is another block within the block. It is a paragraph, no formal \begin{} and \end{} are needed.
The parser infers its type as a ParagraphBlock. The block ends with the newline.

\end{document}

我正在學習如何使用 PEG,這是我迄今為止為當前語法開發的內容:

Start
  = (Newline / Comment / DocumentBlock)*
  
Comment
  = '#' value: (!Newline .)* Newline? {
    return {
      type: "comment",
      value: value.map(y => y[1]).join('').trim()
    }
  } 
  
Newline
  = [\n\r\t]
  
DocumentBlock
  = "\\begin\{document\}"
  
    (!"\\end\{document\}" DocumentChildren)*
    
    "\\end\{document\}"
    
DocumentChildren
  = NewlineBlock / ParagraphBlock
    
NewlineBlock
  = value: Newline*
  {
    return {
      type: "newline",
      value: value.length
    }
  }
    
ParagraphBlock
  = (!Newline .)* Newline

我在無限循環方面遇到了一些問題。 當前代碼產生此錯誤:

Line 19, column 5: Possible infinite loop when parsing (repetition used with an expression that may not consume any input).

上述簡單語法的正確實現是什么?

我認為這是由於NewlineBlock規則在 Newline 上使用了Newline星。

DocumentBlock你有一個重復的DocumentChildren NewlineBlock中有一個重復的Newline ,這意味着它總是可以返回'' ,即 null 字符串,這將導致無限循環。

NewlineBlock中的*更改為+可以解決問題。 這樣它就不再有返回 null 字符串的選項。

NewlineBlock
  = value: Newline+
  {
    return {
      type: "newline",
      value: value.length
    }
  }

暫無
暫無

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

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