mirror of
https://git.savannah.gnu.org/git/guile.git
synced 2025-06-09 21:40:33 +02:00
Update String PEGs
* module/ice-9/peg/string-peg.scm: use new interface for extending PEG syntax * module/ice-9/peg.scm: remove peg-extended-compile
This commit is contained in:
parent
94e8517c16
commit
172ac609c4
2 changed files with 22 additions and 23 deletions
|
@ -22,7 +22,6 @@
|
||||||
define-nonterm
|
define-nonterm
|
||||||
; define-nonterm-f
|
; define-nonterm-f
|
||||||
peg-match)
|
peg-match)
|
||||||
; #:export-syntax (define-nonterm)
|
|
||||||
#:use-module (ice-9 peg codegen)
|
#:use-module (ice-9 peg codegen)
|
||||||
#:use-module (ice-9 peg string-peg)
|
#:use-module (ice-9 peg string-peg)
|
||||||
#:use-module (ice-9 peg simplify-tree)
|
#:use-module (ice-9 peg simplify-tree)
|
||||||
|
@ -30,7 +29,6 @@
|
||||||
#:re-export (peg-sexp-compile
|
#:re-export (peg-sexp-compile
|
||||||
define-grammar
|
define-grammar
|
||||||
define-grammar-f
|
define-grammar-f
|
||||||
; define-nonterm
|
|
||||||
keyword-flatten
|
keyword-flatten
|
||||||
context-flatten
|
context-flatten
|
||||||
peg:start
|
peg:start
|
||||||
|
@ -67,13 +65,6 @@ execute the STMTs and try again."
|
||||||
#f
|
#f
|
||||||
(make-prec 0 (car res) string (string-collapse (cadr res))))))
|
(make-prec 0 (car res) string (string-collapse (cadr res))))))
|
||||||
|
|
||||||
(define (peg-extended-compile pattern accum)
|
|
||||||
(syntax-case pattern (peg)
|
|
||||||
((peg str)
|
|
||||||
(string? (syntax->datum #'str))
|
|
||||||
(peg-string-compile #'str (if (eq? accum 'all) 'body accum)))
|
|
||||||
(else (peg-sexp-compile pattern accum))))
|
|
||||||
|
|
||||||
;; The results of parsing using a nonterminal are cached. Think of it like a
|
;; The results of parsing using a nonterminal are cached. Think of it like a
|
||||||
;; hash with no conflict resolution. Process for deciding on the cache size
|
;; hash with no conflict resolution. Process for deciding on the cache size
|
||||||
;; wasn't very scientific; just ran the benchmarks and stopped a little after
|
;; wasn't very scientific; just ran the benchmarks and stopped a little after
|
||||||
|
@ -85,7 +76,7 @@ execute the STMTs and try again."
|
||||||
(lambda (x)
|
(lambda (x)
|
||||||
(syntax-case x ()
|
(syntax-case x ()
|
||||||
((_ sym accum pat)
|
((_ sym accum pat)
|
||||||
(let ((matchf (peg-extended-compile #'pat (syntax->datum #'accum)))
|
(let ((matchf (peg-sexp-compile #'pat (syntax->datum #'accum)))
|
||||||
(accumsym (syntax->datum #'accum))
|
(accumsym (syntax->datum #'accum))
|
||||||
(c (datum->syntax x (gensym))));; the cache
|
(c (datum->syntax x (gensym))));; the cache
|
||||||
;; CODE is the code to parse the string if the result isn't cached.
|
;; CODE is the code to parse the string if the result isn't cached.
|
||||||
|
@ -103,6 +94,11 @@ execute the STMTs and try again."
|
||||||
(list str at fres))
|
(list str at fres))
|
||||||
fres)))))))))))
|
fres)))))))))))
|
||||||
|
|
||||||
|
(define (peg-like->peg pat)
|
||||||
|
(syntax-case pat ()
|
||||||
|
(str (string? (syntax->datum #'str)) #'(peg str))
|
||||||
|
(else pat)))
|
||||||
|
|
||||||
;; 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 peg-match
|
||||||
|
@ -110,9 +106,7 @@ execute the STMTs and try again."
|
||||||
(syntax-case x ()
|
(syntax-case x ()
|
||||||
((_ pattern string-uncopied)
|
((_ pattern string-uncopied)
|
||||||
(let ((pmsym (syntax->datum #'pattern)))
|
(let ((pmsym (syntax->datum #'pattern)))
|
||||||
(let ((matcher (if (string? (syntax->datum #'pattern))
|
(let ((matcher (peg-sexp-compile (peg-like->peg #'pattern) 'body)))
|
||||||
(peg-string-compile #'pattern 'body)
|
|
||||||
(peg-sexp-compile #'pattern 'body))))
|
|
||||||
;; We copy the string before using it because it might have been
|
;; We copy the string before using it because it might have been
|
||||||
;; modified in-place since the last time it was parsed, which would
|
;; modified in-place since the last time it was parsed, which would
|
||||||
;; invalidate the cache. Guile uses copy-on-write for strings, so
|
;; invalidate the cache. Guile uses copy-on-write for strings, so
|
||||||
|
|
|
@ -18,8 +18,7 @@
|
||||||
;;;;
|
;;;;
|
||||||
|
|
||||||
(define-module (ice-9 peg string-peg)
|
(define-module (ice-9 peg string-peg)
|
||||||
#:export (peg-string-compile
|
#:export (peg-as-peg
|
||||||
peg-as-peg
|
|
||||||
define-grammar
|
define-grammar
|
||||||
define-grammar-f
|
define-grammar-f
|
||||||
peg-grammar)
|
peg-grammar)
|
||||||
|
@ -248,11 +247,17 @@ RB < ']'
|
||||||
(compressor-core (syntax->datum syn))))
|
(compressor-core (syntax->datum syn))))
|
||||||
|
|
||||||
;; Builds a lambda-expressions for the pattern STR using accum.
|
;; Builds a lambda-expressions for the pattern STR using accum.
|
||||||
(define (peg-string-compile str-stx accum)
|
(define (peg-string-compile args accum)
|
||||||
(let ((string (syntax->datum str-stx)))
|
(syntax-case args ()
|
||||||
(peg-sexp-compile
|
((str-stx) (string? (syntax->datum #'str-stx))
|
||||||
(compressor
|
(let ((string (syntax->datum #'str-stx)))
|
||||||
(peg-pattern->defn
|
(peg-sexp-compile
|
||||||
(peg:tree (peg-parse peg-pattern string)) str-stx)
|
(compressor
|
||||||
str-stx)
|
(peg-pattern->defn
|
||||||
accum)))
|
(peg:tree (peg-parse peg-pattern string)) #'str-stx)
|
||||||
|
#'str-stx)
|
||||||
|
(if (eq? accum 'all) 'body accum))))
|
||||||
|
(else (error "Bad embedded PEG string" args))))
|
||||||
|
|
||||||
|
(add-peg-compiler! 'peg peg-string-compile)
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue