mirror of
https://git.savannah.gnu.org/git/guile.git
synced 2025-05-20 11:40:18 +02:00
PEG Renames
* doc/ref/api-peg.texi: rename 'peg-match' to 'search-for-pattern' * module/ice-9/peg.scm: same * module/ice-9/peg/using-parsers.scm: same * test-suite/tests/peg.test: same
This commit is contained in:
parent
ecaa261a20
commit
d7e2f5e3c2
4 changed files with 38 additions and 38 deletions
|
@ -23,10 +23,10 @@ can either be stored in variables at compile-time by the define macros
|
||||||
explicitly at runtime with the compile functions
|
explicitly at runtime with the compile functions
|
||||||
(@code{peg-sexp-compile} and @code{peg-string-compile}).
|
(@code{peg-sexp-compile} and @code{peg-string-compile}).
|
||||||
|
|
||||||
They can then be used for either parsing (@code{peg-parse}) or matching
|
They can then be used for either parsing (@code{peg-parse}) or searching
|
||||||
(@code{peg-match}). For convenience, @code{peg-match} also takes
|
(@code{search-for-pattern}). For convenience, @code{search-for-pattern}
|
||||||
pattern literals in case you want to inline a simple search (people
|
also takes pattern literals in case you want to inline a simple search
|
||||||
often use regular expressions this way).
|
(people often use regular expressions this way).
|
||||||
|
|
||||||
The rest of this documentation consists of a syntax reference, an API
|
The rest of this documentation consists of a syntax reference, an API
|
||||||
reference, and a tutorial.
|
reference, and a tutorial.
|
||||||
|
@ -201,7 +201,7 @@ The most straightforward way to define a PEG is by using one of the
|
||||||
define macros (both of these macroexpand into @code{define}
|
define macros (both of these macroexpand into @code{define}
|
||||||
expressions). These macros bind parsing functions to variables. These
|
expressions). These macros bind parsing functions to variables. These
|
||||||
parsing functions may be invoked by @code{peg-parse} or
|
parsing functions may be invoked by @code{peg-parse} or
|
||||||
@code{peg-match}, which return a PEG match record. Raw data can be
|
@code{search-for-pattern}, which return a PEG match record. Raw data can be
|
||||||
retrieved from this record with the PEG match deconstructor functions.
|
retrieved from this record with the PEG match deconstructor functions.
|
||||||
More complicated (and perhaps enlightening) examples can be found in the
|
More complicated (and perhaps enlightening) examples can be found in the
|
||||||
tutorial.
|
tutorial.
|
||||||
|
@ -320,7 +320,7 @@ For our purposes, ``parsing'' means parsing a string into a tree
|
||||||
starting from the first character, while ``matching'' means searching
|
starting from the first character, while ``matching'' means searching
|
||||||
through the string for a substring. In practice, the only difference
|
through the string for a substring. In practice, the only difference
|
||||||
between the two functions is that @code{peg-parse} gives up if it can't
|
between the two functions is that @code{peg-parse} gives up if it can't
|
||||||
find a valid substring starting at index 0 and @code{peg-match} keeps
|
find a valid substring starting at index 0 and @code{search-for-pattern} keeps
|
||||||
looking. They are both equally capable of ``parsing'' and ``matching''
|
looking. They are both equally capable of ``parsing'' and ``matching''
|
||||||
given those constraints.
|
given those constraints.
|
||||||
|
|
||||||
|
@ -360,79 +360,79 @@ nothing
|
||||||
@end lisp
|
@end lisp
|
||||||
@end deffn
|
@end deffn
|
||||||
|
|
||||||
@deffn {Scheme Macro} peg-match nonterm-or-peg string
|
@deffn {Scheme Macro} search-for-pattern nonterm-or-peg string
|
||||||
Searches through @var{string} looking for a matching subexpression.
|
Searches through @var{string} looking for a matching subexpression.
|
||||||
@var{nonterm-or-peg} can either be a nonterminal or a literal PEG
|
@var{nonterm-or-peg} can either be a nonterminal or a literal PEG
|
||||||
pattern. When a literal PEG pattern is provided, @code{peg-match} works
|
pattern. When a literal PEG pattern is provided, @code{search-for-pattern} works
|
||||||
very similarly to the regular expression searches many hackers are used
|
very similarly to the regular expression searches many hackers are used
|
||||||
to. If no match was found, @code{peg-match} returns false. If a match
|
to. If no match was found, @code{search-for-pattern} returns false. If a match
|
||||||
was found, a PEG match record is returned.
|
was found, a PEG match record is returned.
|
||||||
|
|
||||||
@lisp
|
@lisp
|
||||||
(define-nonterm as body (+ "a"))
|
(define-nonterm as body (+ "a"))
|
||||||
(peg-match as "aabbcc") @result{}
|
(search-for-pattern as "aabbcc") @result{}
|
||||||
#<peg start: 0 end: 2 string: aabbcc tree: aa>
|
#<peg start: 0 end: 2 string: aabbcc tree: aa>
|
||||||
(peg-match (+ "a") "aabbcc") @result{}
|
(search-for-pattern (+ "a") "aabbcc") @result{}
|
||||||
#<peg start: 0 end: 2 string: aabbcc tree: aa>
|
#<peg start: 0 end: 2 string: aabbcc tree: aa>
|
||||||
(peg-match "'a'+" "aabbcc") @result{}
|
(search-for-pattern "'a'+" "aabbcc") @result{}
|
||||||
#<peg start: 0 end: 2 string: aabbcc tree: aa>
|
#<peg start: 0 end: 2 string: aabbcc tree: aa>
|
||||||
|
|
||||||
(define-nonterm as all (+ "a"))
|
(define-nonterm as all (+ "a"))
|
||||||
(peg-match as "aabbcc") @result{}
|
(search-for-pattern as "aabbcc") @result{}
|
||||||
#<peg start: 0 end: 2 string: aabbcc tree: (as aa)>
|
#<peg start: 0 end: 2 string: aabbcc tree: (as aa)>
|
||||||
|
|
||||||
(define-nonterm bs body (+ "b"))
|
(define-nonterm bs body (+ "b"))
|
||||||
(peg-match bs "aabbcc") @result{}
|
(search-for-pattern bs "aabbcc") @result{}
|
||||||
#<peg start: 2 end: 4 string: aabbcc tree: bb>
|
#<peg start: 2 end: 4 string: aabbcc tree: bb>
|
||||||
(peg-match (+ "b") "aabbcc") @result{}
|
(search-for-pattern (+ "b") "aabbcc") @result{}
|
||||||
#<peg start: 2 end: 4 string: aabbcc tree: bb>
|
#<peg start: 2 end: 4 string: aabbcc tree: bb>
|
||||||
(peg-match "'b'+" "aabbcc") @result{}
|
(search-for-pattern "'b'+" "aabbcc") @result{}
|
||||||
#<peg start: 2 end: 4 string: aabbcc tree: bb>
|
#<peg start: 2 end: 4 string: aabbcc tree: bb>
|
||||||
|
|
||||||
(define-nonterm zs body (+ "z"))
|
(define-nonterm zs body (+ "z"))
|
||||||
(peg-match zs "aabbcc") @result{}
|
(search-for-pattern zs "aabbcc") @result{}
|
||||||
#f
|
#f
|
||||||
(peg-match (+ "z") "aabbcc") @result{}
|
(search-for-pattern (+ "z") "aabbcc") @result{}
|
||||||
#f
|
#f
|
||||||
(peg-match "'z'+" "aabbcc") @result{}
|
(search-for-pattern "'z'+" "aabbcc") @result{}
|
||||||
#f
|
#f
|
||||||
@end lisp
|
@end lisp
|
||||||
@end deffn
|
@end deffn
|
||||||
|
|
||||||
@subsubheading PEG Match Records
|
@subsubheading PEG Match Records
|
||||||
The @code{peg-parse} and @code{peg-match} functions both return PEG
|
The @code{peg-parse} and @code{search-for-pattern} functions both return PEG
|
||||||
match records. Actual information can be extracted from these with the
|
match records. Actual information can be extracted from these with the
|
||||||
following functions.
|
following functions.
|
||||||
|
|
||||||
@deffn {Scheme Procedure} peg:string peg-match
|
@deffn {Scheme Procedure} peg:string match-record
|
||||||
Returns the original string that was parsed in the creation of
|
Returns the original string that was parsed in the creation of
|
||||||
@code{peg-match}.
|
@code{match-record}.
|
||||||
@end deffn
|
@end deffn
|
||||||
|
|
||||||
@deffn {Scheme Procedure} peg:start peg-match
|
@deffn {Scheme Procedure} peg:start match-record
|
||||||
Returns the index of the first parsed character in the original string
|
Returns the index of the first parsed character in the original string
|
||||||
(from @code{peg:string}). If this is the same as @code{peg:end},
|
(from @code{peg:string}). If this is the same as @code{peg:end},
|
||||||
nothing was parsed.
|
nothing was parsed.
|
||||||
@end deffn
|
@end deffn
|
||||||
|
|
||||||
@deffn {Scheme Procedure} peg:end peg-match
|
@deffn {Scheme Procedure} peg:end match-record
|
||||||
Returns one more than the index of the last parsed character in the
|
Returns one more than the index of the last parsed character in the
|
||||||
original string (from @code{peg:string}). If this is the same as
|
original string (from @code{peg:string}). If this is the same as
|
||||||
@code{peg:start}, nothing was parsed.
|
@code{peg:start}, nothing was parsed.
|
||||||
@end deffn
|
@end deffn
|
||||||
|
|
||||||
@deffn {Scheme Procedure} peg:substring peg-match
|
@deffn {Scheme Procedure} peg:substring match-record
|
||||||
Returns the substring parsed by @code{peg-match}. This is equivalent to
|
Returns the substring parsed by @code{match-record}. This is equivalent to
|
||||||
@code{(substring (peg:string peg-match) (peg:start peg-match) (peg:end
|
@code{(substring (peg:string match-record) (peg:start match-record) (peg:end
|
||||||
peg-match))}.
|
match-record))}.
|
||||||
@end deffn
|
@end deffn
|
||||||
|
|
||||||
@deffn {Scheme Procedure} peg:tree peg-match
|
@deffn {Scheme Procedure} peg:tree match-record
|
||||||
Returns the tree parsed by @code{peg-match}.
|
Returns the tree parsed by @code{match-record}.
|
||||||
@end deffn
|
@end deffn
|
||||||
|
|
||||||
@deffn {Scheme Procedure} peg-record? peg-match
|
@deffn {Scheme Procedure} peg-record? match-record
|
||||||
Returns true if @code{peg-match} is a PEG match record, or false
|
Returns true if @code{match-record} is a PEG match record, or false
|
||||||
otherwise.
|
otherwise.
|
||||||
@end deffn
|
@end deffn
|
||||||
|
|
||||||
|
@ -440,10 +440,10 @@ Example:
|
||||||
@lisp
|
@lisp
|
||||||
(define-nonterm bs all (peg "'b'+"))
|
(define-nonterm bs all (peg "'b'+"))
|
||||||
|
|
||||||
(peg-match bs "aabbcc") @result{}
|
(search-for-pattern bs "aabbcc") @result{}
|
||||||
#<peg start: 2 end: 4 string: aabbcc tree: (bs bb)>
|
#<peg start: 2 end: 4 string: aabbcc tree: (bs bb)>
|
||||||
|
|
||||||
(let ((pm (peg-match bs "aabbcc")))
|
(let ((pm (search-for-pattern bs "aabbcc")))
|
||||||
`((string ,(peg:string pm))
|
`((string ,(peg:string pm))
|
||||||
(start ,(peg:start pm))
|
(start ,(peg:start pm))
|
||||||
(end ,(peg:end pm))
|
(end ,(peg:end pm))
|
||||||
|
|
|
@ -28,7 +28,7 @@
|
||||||
#:use-module (ice-9 peg cache)
|
#:use-module (ice-9 peg cache)
|
||||||
#:re-export (peg-parse
|
#:re-export (peg-parse
|
||||||
define-nonterm
|
define-nonterm
|
||||||
peg-match
|
search-for-pattern
|
||||||
peg-sexp-compile
|
peg-sexp-compile
|
||||||
define-grammar
|
define-grammar
|
||||||
define-grammar-f
|
define-grammar-f
|
||||||
|
|
|
@ -21,7 +21,7 @@
|
||||||
#:use-module (ice-9 peg simplify-tree)
|
#:use-module (ice-9 peg simplify-tree)
|
||||||
#:use-module (ice-9 peg codegen)
|
#:use-module (ice-9 peg codegen)
|
||||||
#:use-module (ice-9 peg cache)
|
#:use-module (ice-9 peg cache)
|
||||||
#:export (peg-parse define-nonterm peg-match
|
#:export (peg-parse define-nonterm search-for-pattern
|
||||||
prec make-prec peg:start peg:end peg:string
|
prec make-prec peg:start peg:end peg:string
|
||||||
peg:tree peg:substring peg-record?))
|
peg:tree peg:substring peg-record?))
|
||||||
|
|
||||||
|
@ -70,7 +70,7 @@ execute the STMTs and try again."
|
||||||
|
|
||||||
;; Searches through STRING for something that parses to PEG-MATCHER. Think
|
;; Searches through STRING for something that parses to PEG-MATCHER. Think
|
||||||
;; regexp search.
|
;; regexp search.
|
||||||
(define-syntax peg-match
|
(define-syntax search-for-pattern
|
||||||
(lambda (x)
|
(lambda (x)
|
||||||
(syntax-case x ()
|
(syntax-case x ()
|
||||||
((_ pattern string-uncopied)
|
((_ pattern string-uncopied)
|
||||||
|
|
|
@ -149,7 +149,7 @@ SLASH < '/'")
|
||||||
(pass-if
|
(pass-if
|
||||||
"basic parameter extraction"
|
"basic parameter extraction"
|
||||||
(equal?
|
(equal?
|
||||||
(let ((pm (peg-match bs "aabbcc")))
|
(let ((pm (search-for-pattern bs "aabbcc")))
|
||||||
`((string ,(peg:string pm))
|
`((string ,(peg:string pm))
|
||||||
(start ,(peg:start pm))
|
(start ,(peg:start pm))
|
||||||
(end ,(peg:end pm))
|
(end ,(peg:end pm))
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue