mini-parser小轮子

代码链接

demo如下,定义好rule后进行parse。

const rule = AndRule.of(
  [
    TextRule.of("hello", "HELLO_RULE"),
    OrRule.of(
      [TextRule.of(", "), TimesRule.of(3, TextRule.of(" ", "SPACE"), "TIMES")],
      "NO_NAME"
    ),
    TextRule.of("world", "WORLD"),
    OneOrMoreRule.of(TextRule.of("!"), "SAMPLE")
  ],
  "HELLO_WORLD"
);

console.log(JSON.stringify(rule.accept("hello   world!!!")));

结果为:

{
  "contain": true,
  "group": {
    "groups": [
      { "text": "hello", "name": "HELLO_RULE" },
      {
        "groups": [
          {
            "groups": [
              { "text": " ", "name": "SPACE" },
              { "text": " ", "name": "SPACE" },
              { "text": " ", "name": "SPACE" }
            ],
            "name": "TIMES"
          }
        ],
        "name": "NO_NAME"
      },
      { "text": "world", "name": "WORLD" },
      {
        "groups": [
          { "text": "!", "name": null },
          { "text": "!", "name": null },
          { "text": "!", "name": null }
        ],
        "name": "SAMPLE"
      }
    ],
    "name": "HELLO_WORLD"
  }
}
  

当然,这离真正可用还有很远:

红红姐的教导

This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License