sigildocs

(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-matchprocedure

Construct a peg-match struct.

peg-match?procedure

Test if a value is a peg-match struct.

Get the start field of a peg-match struct.

peg-match-endprocedure

Get the end field of a peg-match struct.

Get the captures field of a peg-match struct.

peg/matchprocedure

Match 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/findprocedure

Find 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-allprocedure

Find 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/replaceprocedure

Replace 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"

Replace all non-overlapping matches of grammar in input.

(peg/replace-all '(+ (char-set char-numeric?)) "N" "a1b23c456")
; => "aNbNcN"

Get the matched text from a peg-match.

(let ((m (peg/find '(+ (char-set char-alphabetic?)) "hello world")))
  (peg-match-text m "hello world"))
; => "hello"

(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)