(sigil peg)
(sigil peg) - Parsing Expression Grammar Library
A PEG (Parsing Expression Grammar) library representing patterns as S-expressions with a capture stack for structured output. Inspired by Janet's PEG module.
Basic Usage
(import (sigil peg))
;; Match a literal string
(peg/match '(seq "hello" " " "world") "hello world")
; => #<peg-match 0..11 ()>
;; Capture matched text
(peg-match-captures (peg/match '(<- (* (char-set char-alphabetic?))) "hello"))
; => ("hello")
;; Define a grammar with named rules
(define-grammar csv
(main (* (seq 'field (? (seq "," 'field)) 'newline))
(field (<- (+ (! ",") (! "\n") any-char)))
(newline (/ "\n" (input-end))))Pattern Language
Atomic: "literal", #\char, 'rule-ref, any-char, (char-range #\a #\z), (char-set char-alphabetic?)
Combinators: (seq p ...), (/ p ...), (* p), (+ p), (? p), (! p), (& p), (to p), (thru p), (between min max p)
Captures: (<- p), (<- p tag), (group p), (position), (constant val), (cmt p fn), (drop p), (backref tag), (replace p val)
Anchors: (line-start), (line-end), (input-start), (input-end)
Exports
peg-matchprocedureConstruct a peg-match struct.
peg-match?procedureTest if a value is a peg-match struct.
peg-match-startprocedureGet the start field of a peg-match struct.
peg-match-endprocedureGet the end field of a peg-match struct.
peg-match-capturesprocedureGet the captures field of a peg-match struct.
peg/matchprocedureMatch a PEG grammar against the input string.
Returns a peg-match on success, or #f on failure. The grammar can be a bare pattern or a list of named rules (entry point is 'main).
(peg/match "hello" "hello world")
; => #<peg-match 0..5 ()>
(peg/match '(<- (* (char-set char-alphabetic?))) "hello world")
; => #<peg-match 0..5 ("hello")>peg/findprocedureFind the first match of grammar anywhere in input.
Tries matching at each position from left to right, returning the first successful match, or #f if no match is found.
(peg/find '(<- "world") "hello world")
; => #<peg-match 6..11 ("world")>peg/find-allprocedureFind all non-overlapping matches of grammar in input.
Returns a list of peg-match objects.
(peg/find-all '(<- (+ (char-set char-alphabetic?))) "hello world foo")
; => (#<peg-match 0..5 ("hello")> #<peg-match 6..11 ("world")> ...)peg/replaceprocedureReplace the first match of grammar in input.
The replacement can be a string or a procedure. If a procedure, it receives the peg-match object and should return a string.
(peg/replace '(+ (char-set char-numeric?)) "X" "abc123def")
; => "abcXdef"peg/replace-allprocedureReplace all non-overlapping matches of grammar in input.
(peg/replace-all '(+ (char-set char-numeric?)) "N" "a1b23c456")
; => "aNbNcN"peg-match-textprocedureGet the matched text from a peg-match.
(let ((m (peg/find '(+ (char-set char-alphabetic?)) "hello world")))
(peg-match-text m "hello world"))
; => "hello"define-grammarvariable(No description)
peg/alphavariable(No description)
peg/digitvariable(No description)
peg/spacevariable(No description)
peg/alnumvariable(No description)
peg/newlinevariable(No description)
peg/anyvariable(No description)