首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >PEG语法的PEG解析器

PEG语法的PEG解析器
EN

Stack Overflow用户
提问于 2015-11-20 22:58:02
回答 1查看 201关注 0票数 0

我正在寻找一个库,它提供了PEG语法的输入,例如:

代码语言:javascript
运行
复制
Expression
  = A B

A 
  = [0-9]

B
  = [a-z]

将输出机器可读的版本,例如:

代码语言:javascript
运行
复制
{  
  "expression":{  
    "components":[  
      {  
        "type": "subexpression",
        "subexpressionName": "A"
      },
      {  
        "type": "subexpression",
        "subexpressionName": "B"
      }
    ]
  },
  "subexpressions":[  
    {  
      "name": "A",
      "components":[  
        {  
          "type": "regex",
          "regexString": "[0-9]"
        }
      ]
    },
    {  
      "name": "B",
      "components":[  
        {  
          "type": "regex",
          "regexString": "[a-z]"
        }
      ]
    }
  ]
}

有人知道这样的图书馆吗?Javascript库首选,但任何一个都会有所帮助。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-11-21 08:16:08

多亏了Ira上面的评论,我做了更深入的研究,找到了我真正需要的东西。

我正在使用的库是pegjs,它使得将解析的语法作为JSON很容易得到。

代码语言:javascript
运行
复制
var pegjs = require('pegjs');

var exampleGrammar = ''+
'Expression  \n'+
'  = A B     \n'+
'            \n'+
'A           \n'+
'  = [0-9]   \n'+
'            \n'+
'B           \n'+
'  = [a-z]   ';

var parsed = pegjs.parser.parse(exampleGrammar);
console.log(JSON.stringify(parsed));

如果有人感兴趣的话,它提供的输出:

代码语言:javascript
运行
复制
{  
  "type":"grammar",
  "initializer":null,
  "rules":[  
    {  
      "type":"rule",
      "name":"Expression",
      "expression":{  
        "type":"sequence",
        "elements":[  
          {  
            "type":"rule_ref",
            "name":"A",
            "location":{  
              "start":{  
                "offset":16,
                "line":2,
                "column":5
              },
              "end":{  
                "offset":17,
                "line":2,
                "column":6
              }
            }
          },
          {  
            "type":"rule_ref",
            "name":"B",
            "location":{  
              "start":{  
                "offset":18,
                "line":2,
                "column":7
              },
              "end":{  
                "offset":19,
                "line":2,
                "column":8
              }
            }
          }
        ],
        "location":{  
          "start":{  
            "offset":16,
            "line":2,
            "column":5
          },
          "end":{  
            "offset":19,
            "line":2,
            "column":8
          }
        }
      },
      "location":{  
        "start":{  
          "offset":0,
          "line":1,
          "column":1
        },
        "end":{  
          "offset":25,
          "line":3,
          "column":1
        }
      }
    },
    {  
      "type":"rule",
      "name":"A",
      "expression":{  
        "type":"class",
        "parts":[  
          [  
            "0",
            "9"
          ]
        ],
        "inverted":false,
        "ignoreCase":false,
        "rawText":"[0-9]",
        "location":{  
          "start":{  
            "offset":55,
            "line":5,
            "column":5
          },
          "end":{  
            "offset":60,
            "line":5,
            "column":10
          }
        }
      },
      "location":{  
        "start":{  
          "offset":38,
          "line":4,
          "column":1
        },
        "end":{  
          "offset":64,
          "line":6,
          "column":1
        }
      }
    },
    {  
      "type":"rule",
      "name":"B",
      "expression":{  
        "type":"class",
        "parts":[  
          [  
            "a",
            "z"
          ]
        ],
        "inverted":false,
        "ignoreCase":false,
        "rawText":"[a-z]",
        "location":{  
          "start":{  
            "offset":94,
            "line":8,
            "column":5
          },
          "end":{  
            "offset":99,
            "line":8,
            "column":10
          }
        }
      },
      "location":{  
        "start":{  
          "offset":77,
          "line":7,
          "column":1
        },
        "end":{  
          "offset":103,
          "line":9,
          "column":1
        }
      }
    }
  ],
  "location":{  
    "start":{  
      "offset":0,
      "line":1,
      "column":1
    },
    "end":{  
      "offset":103,
      "line":9,
      "column":1
    }
  }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33837455

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档