返回 Skill 列表
extension
分类: 开发与工程无需 API Key

peggy

用于使用Peggy(以前称为PEG.js)编写PEG语法时 - 解析表达式语法、解析器生成和语法定义

person作者: jakexiaohubgithub

Peggy Parser Generator

Quick Start

// grammar.peggy
Expression = head:Term tail:(_ ("+" / "-") _ Term)* {
  return tail.reduce((result, [, op, , term]) => {
    return op === "+" ? result + term : result - term;
  }, head);
}

Term = Integer / "(" _ expr:Expression _ ")" { return expr; }

Integer = digits:[0-9]+ { return parseInt(digits.join(""), 10); }

_ = [ \t\n\r]*
import * as peggy from 'peggy';
import fs from 'fs';

const grammar = fs.readFileSync('grammar.peggy', 'utf-8');
const parser = peggy.generate(grammar);
const result = parser.parse('2 + 3 * 4'); // 14

Core Syntax

| Pattern | Meaning | |---------|---------| | "literal" | Match exact string | | [a-z] | Character class | | rule1 / rule2 | Ordered choice (try rule1 first) | | rule* | Zero or more | | rule+ | One or more | | rule? | Optional | | &rule | Positive lookahead | | !rule | Negative lookahead | | label:rule | Capture as variable | | { code } | Action (return value) |

Rule Definitions

// Named rule
Identifier = [a-zA-Z_][a-zA-Z0-9_]* { return text(); }

// With semantic action
Number = digits:[0-9]+ { return parseInt(digits.join(""), 10); }

// Choice with labels
BinaryOp = left:Term op:("+" / "-") right:Term {
  return { type: "binary", op, left, right };
}

Built-in Functions

  • text() - Matched text as string
  • location() - Start/end positions
  • expected(desc) - Throw expected error
  • error(message) - Throw custom error

CLI Usage

npx peggy grammar.peggy -o parser.js
npx peggy --format es grammar.peggy  # ES module output

Tips

  • Order choices from most to least specific
  • Use !. for "not end of input"
  • Whitespace is significant; define _ rule for optional whitespace
  • Use @ prefix for pluck: @value:Rule returns just the labeled value