The module works by compiling PEGs down to lambda expressions. These
can either be stored in variables at compile-time by the define macros
-(@code{define-peg-pattern} and @code{define-grammar}) or calculated
+(@code{define-peg-pattern} and @code{define-peg-string-patterns}) or calculated
explicitly at runtime with the compile functions
(@code{peg-sexp-compile} and @code{peg-string-compile}).
More complicated (and perhaps enlightening) examples can be found in the
tutorial.
-@deffn {Scheme Macro} define-grammar peg-string
+@deffn {Scheme Macro} define-peg-string-patterns peg-string
Defines all the nonterminals in the PEG @var{peg-string}. More
-precisely, @code{define-grammar} takes a superset of PEGs. A normal PEG
+precisely, @code{define-peg-string-patterns} takes a superset of PEGs. A normal PEG
has a @code{<-} between the nonterminal and the pattern.
-@code{define-grammar} uses this symbol to determine what information it
+@code{define-peg-string-patterns} uses this symbol to determine what information it
should propagate up the parse tree. The normal @code{<-} propagates the
matched text up the parse tree, @code{<--} propagates the matched text
up the parse tree tagged with the name of the nonterminal, and @code{<}
For example, if we:
@lisp
-(define-grammar
+(define-peg-string-patterns
"as <- 'a'+
bs <- 'b'+
as-or-bs <- as/bs")
-(define-grammar
+(define-peg-string-patterns
"as-tag <-- 'a'+
bs-tag <-- 'b'+
as-or-bs-tag <-- as-tag/bs-tag")
Doing this with string-based PEG syntax would look like this:
@lisp
-(define-grammar
+(define-peg-string-patterns
"passwd <- entry* !.
entry <-- (! NL .)* NL*
NL < '\n'")
information out of the passwd file:
@lisp
-(define-grammar
+(define-peg-string-patterns
"passwd <-- entry* !.
entry <-- login C pass C uid C gid C nameORcomment C homedir C shell NL*
login <-- text
We can parse simple mathematical expressions with the following PEG:
@lisp
-(define-grammar
+(define-peg-string-patterns
"expr <- sum
sum <-- (product ('+' / '-') sum) / product
product <-- (value ('*' / '/') product) / value
@lisp
(use-modules (srfi srfi-1))
-(define-grammar
+(define-peg-string-patterns
"expr <- sum
sum <-- (product ('+' / '-'))* product
product <-- (value ('*' / '/'))* value
parses (highly) simplified C functions:
@lisp
-(define-grammar
+(define-peg-string-patterns
"cfunc <-- cSP ctype cSP cname cSP cargs cLB cSP cbody cRB
ctype <-- cidentifier
cname <-- cidentifier
(with-test-prefix "PEG Grammar"
(pass-if
"defining PEGs with PEG"
- (and (eeval `(define-grammar ,(@@ (ice-9 peg) peg-as-peg))) #t))
+ (and (eeval `(define-peg-string-patterns ,(@@ (ice-9 peg) peg-as-peg))) #t))
(pass-if
"equivalence of definitions"
(equal?
")
;; A grammar for parsing /etc/passwd files.
-(define-grammar
+(define-peg-string-patterns
"passwd <-- entry* !.
entry <-- login CO pass CO uid CO gid CO nameORcomment CO homedir CO shell NL*
login <-- text
;; Tests some actual parsing using PEGs.
(with-test-prefix "Parsing"
- (eeval `(define-grammar ,comment-grammar))
+ (eeval `(define-peg-string-patterns ,comment-grammar))
(pass-if
;; Pascal-style comment parsing
"simple comment"
(record? #t)))))
;; PEG for parsing right-associative equations.
-(define-grammar
+(define-peg-string-patterns
"expr <- sum
sum <-- (product ('+' / '-') sum) / product
product <-- (value ('*' / '/') product) / value
'(+ 1 (+ (/ 1 (* 2 3)) (/ (+ 1 1) 2))))))
;; PEG for parsing left-associative equations (normal ones).
-(define-grammar
+(define-peg-string-patterns
"expr <- sum
sum <-- (product ('+' / '-'))* product
product <-- (value ('*' / '/'))* value