AtlatestRepositorysigil-peg
1# sigil-peg
2
3PEG (Parsing Expression Grammar) library for [Sigil](https://codeberg.org/sigil/sigil). Represent patterns as S-expressions with a capture stack for structured output. Inspired by Janet's PEG module.
4
5## Usage
6
7```scheme
8(import (sigil peg))
9
10;; Match a literal string
11(peg/match '(seq "hello" " " "world") "hello world")
12; => #<peg-match 0..11 ()>
14;; Capture matched text
15(peg-match-captures (peg/match '(<- (* (char-set char-alphabetic?))) "hello"))
16; => ("hello")
18;; Define a grammar with named rules
19(define-grammar csv
20 (main (* (seq 'field (? (seq "," 'field)) 'newline))
21 (field (<- (+ (! ",") (! "\n") any-char)))
22 (newline (/ "\n" (input-end))))
23```
25## Pattern Language
27- **Atomic:** `"literal"`, `#\char`, `'rule-ref`, `any-char`, `(char-range #\a #\z)`, `(char-set char-alphabetic?)`
28- **Combinators:** `(seq p ...)`, `(/ p ...)`, `(* p)`, `(+ p)`, `(? p)`, `(! p)`, `(& p)`, `(to p)`, `(thru p)`, `(between min max p)`
29- **Captures:** `(<- p)`, `(<- p tag)`, `(group p)`, `(position)`, `(constant val)`, `(cmt p fn)`, `(drop p)`, `(backref tag)`, `(replace p val)`
30- **Anchors:** `(line-start)`, `(line-end)`, `(input-start)`, `(input-end)`
32## Dependencies
34- sigil-stdlib
36## Build
38```sh
39sigil build
40sigil test
41```
43## License
45BSD-3-Clause