Skip to content

moajo/simple-peg-parser-combinator

Repository files navigation

simple-peg-parser-combinator

peg 対応のパーサコンビネータ・パーサジェネレータ。
pegjs互換
.pegjsファイルからパーサを生成できる。

// .pegjs形式のファイルをパースしてpeg astを作成
const js_syntax_definition = fs.readFileSync("./sample_files/peg/javascript.pegjs")
const ast = Grammar.parse(pc, js_syntax_definition)!.value

// peg astからパーサオブジェクトを生成
const js_parser = compile(ast)

// パーサオブジェクトでjavascriptをパースしてjavascript astを得る
expect(js_parser.parse("1+1")!.value).toStrictEqual({
  type: "Program",
  body: [
    {
      type: "ExpressionStatement",
      expression: {
        type: "BinaryExpression",
        operator: "+",
        left: {
          type: "Literal",
          value: 1
        },
        right: {
          type: "Literal",
          value: 1
        }
      }
    }
  ]
})

setup

npm i

build

npm run build

test

see: /test/*

npm t

examples

see: /src/examples/* npm run exec -- src/example/javascript/parse_javascript.ts

reference

https://github.com/sap/chevrotain/blob/gh-pages/performance/samples/1K_json.js

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published