branch: externals/peg commit cb46f6f76b080c314826a4ea510336efff5b2810 Author: Helmut Eller <eller.hel...@gmail.com> Commit: Helmut Eller <eller.hel...@gmail.com>
Introduce an operator to match string literals. * peg.el (translate =): Add = operator. (peg-ex-split): Example use. --- ChangeLog | 140 +++++++++++++++++++++++++++----------------------------------- peg.el | 47 ++++++++++++++++++++- 2 files changed, 106 insertions(+), 81 deletions(-) diff --git a/ChangeLog b/ChangeLog index 577288c..7abe671 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,122 +1,104 @@ -Author: Helmut Eller <eller.hel...@gmail.com> -Date: Mon Dec 22 09:13:05 2008 +0100 +2009-03-06 Helmut Eller <eller.hel...@gmail.com> - Fix typo. + Introduce an operator to match string literals. -Author: Helmut Eller <eller.hel...@gmail.com> -Date: Sun Dec 21 09:38:06 2008 +0100 + * peg.el (translate =): Add = operator. + (peg-ex-split): Example use. - Version 0.5 for savannah. +2008-12-22 Helmut Eller <eller.hel...@gmail.com> -Author: Helmut Eller <eller.hel...@gmail.com> -Date: Thu Dec 18 10:19:18 2008 +0100 + Fix typo. - Remove *list op. +2008-12-21 Helmut Eller <eller.hel...@gmail.com> -Author: Helmut Eller <eller.hel...@gmail.com> -Date: Wed Dec 17 18:59:23 2008 +0100 + Version 0.5 for savannah. - Bump version number to 0.4. +2008-12-18 Helmut Eller <eller.hel...@gmail.com> -Author: Helmut Eller <eller.hel...@gmail.com> -Date: Wed Dec 17 18:52:55 2008 +0100 + Remove *list op. - Add some more operators for actions. - list: Factored out of *list. Collects all items on the stack. - region: New. Pushes start and end. - replaces: Replaces match. +2008-12-17 Helmut Eller <eller.hel...@gmail.com> -Author: Helmut Eller <eller.hel...@gmail.com> -Date: Wed Dec 17 18:08:28 2008 +0100 + Bump version number to 0.4. - Fix normalizer for \`: Push values in left-to-right order. - peg-postprocess: turn positions in to markers to - support replacement ops in the actions. +2008-12-17 Helmut Eller <eller.hel...@gmail.com> -Author: Helmut Eller <eller.hel...@gmail.com> -Date: Mon Dec 1 19:10:21 2008 +0100 + Add some more operators for actions. + list: Factored out of *list. Collects all items on the stack. + region: New. Pushes start and end. + replaces: Replaces match. - (peg-ex-lisp): Minor change to allow comments in more places. +2008-12-17 Helmut Eller <eller.hel...@gmail.com> -Author: Helmut Eller <eller.hel...@gmail.com> -Date: Mon Dec 1 15:19:31 2008 +0100 + Fix normalizer for \`: Push values in left-to-right order. + peg-postprocess: turn positions in to markers to + support replacement ops in the actions. - (peg-ex-uri): Fix some stack actions so that the file: example works. +2008-12-01 Helmut Eller <eller.hel...@gmail.com> -Author: Helmut Eller <eller.hel...@gmail.com> -Date: Fri Nov 28 15:21:35 2008 +0100 + (peg-ex-lisp): Minor change to allow comments in more places. - Remove dups from char sets to avoid clashes with [:CLASS:] syntax. - There's still no way to include syntax classes in charsets. There - really should be. +2008-12-01 Helmut Eller <eller.hel...@gmail.com> -Author: Helmut Eller <eller.hel...@gmail.com> -Date: Fri Nov 28 12:09:31 2008 +0100 + (peg-ex-uri): Fix some stack actions so that the file: example works. - Handle ^ in charsets. +2008-11-28 Helmut Eller <eller.hel...@gmail.com> -Author: Helmut Eller <eller.hel...@gmail.com> -Date: Fri Nov 28 10:12:24 2008 +0100 + Remove dups from char sets to avoid clashes with [:CLASS:] syntax. + There's still no way to include syntax classes in charsets. There + really should be. - Use (declare (indent )) again. Emacs 21 compatibility is to hard - due to broken ,@ or something. +2008-11-28 Helmut Eller <eller.hel...@gmail.com> -Author: Helmut Eller <eller.hel...@gmail.com> -Date: Fri Nov 28 08:50:10 2008 +0100 + Handle ^ in charsets. - Don't use (declare (indent 3)) because that doesn' work in Emacs 21. +2008-11-28 Helmut Eller <eller.hel...@gmail.com> -Author: Helmut Eller <eller.hel...@gmail.com> -Date: Fri Nov 28 08:44:03 2008 +0100 + Use (declare (indent )) again. Emacs 21 compatibility is to hard + due to broken ,@ or something. - Fix minor spelling error. +2008-11-28 Helmut Eller <eller.hel...@gmail.com> -Author: Helmut Eller <eller.hel...@gmail.com> -Date: Fri Nov 28 08:35:16 2008 +0100 + Don't use (declare (indent 3)) because that doesn' work in Emacs 21. - Fix range translator: we need to check for eob. - Thanks to Hugo Schmitt. +2008-11-28 Helmut Eller <eller.hel...@gmail.com> -Author: Helmut Eller <eller.hel...@gmail.com> -Date: Thu Nov 27 23:54:18 2008 +0100 + Fix minor spelling error. - *** empty log message *** +2008-11-28 Helmut Eller <eller.hel...@gmail.com> -Author: Helmut Eller <eller.hel...@gmail.com> -Date: Thu Nov 27 23:52:25 2008 +0100 + Fix range translator: we need to check for eob. + Thanks to Hugo Schmitt. - Make file byte-compileable in Emacs 22. +2008-11-28 Helmut Eller <eller.hel...@gmail.com> -Author: Helmut Eller <eller.hel...@gmail.com> -Date: Thu Nov 27 10:07:40 2008 +0100 + Make file byte-compileable in Emacs 22. - Add mini test suite - Restore subtring operator. - Add translate method for <fail>. +2008-11-27 Helmut Eller <eller.hel...@gmail.com> -Author: Helmut Eller <eller.hel...@gmail.com> -Date: Thu Nov 27 09:26:33 2008 +0100 + Add mini test suite + Restore subtring operator. + Add translate method for <fail>. - Add *list operator. +2008-11-27 Helmut Eller <eller.hel...@gmail.com> -Author: Helmut Eller <eller.hel...@gmail.com> -Date: Sat Nov 8 14:37:40 2008 +0100 + Add *list operator. - More friendly syntax for character sets. - For example (set A-Z 0-9 "_!-" ?%). - [A-Z] is now the same as (set A-Z) which should look familiar. +2008-11-08 Helmut Eller <eller.hel...@gmail.com> - Fix bug in *-loops. We must backtrack. + More friendly syntax for character sets. + For example (set A-Z 0-9 "_!-" ?%). + [A-Z] is now the same as (set A-Z) which should look familiar. - Add somewhat bigger examples to parse URIs and Lisp-style Sexps. + Fix bug in *-loops. We must backtrack. -Author: Helmut Eller <eller.hel...@gmail.com> -Date: Thu Nov 6 12:18:26 2008 +0100 + Add somewhat bigger examples to parse URIs and Lisp-style Sexps. - Detect infinite loops. - Use a single peg-add-method macro for all gfs. +2008-11-06 Helmut Eller <eller.hel...@gmail.com> -Author: Helmut Eller <eller.hel...@gmail.com> -Date: Wed Nov 5 23:12:05 2008 +0100 + Detect infinite loops. + Use a single peg-add-method macro for all gfs. - Add peg.el. +2008-11-06 Helmut Eller <eller.hel...@gmail.com> + + Add peg.el. diff --git a/peg.el b/peg.el index 8494d2f..d51fa2d 100644 --- a/peg.el +++ b/peg.el @@ -81,7 +81,7 @@ ;; (substring E) ; match E and push the substring for the matched region ;; (region E) ; match E and push the corresponding start and end positions ;; (replace E RPL); match E and replace the matched region with RPL. -;; (list E) ; match E and push a list of all items that E produces. +;; (list E) ; match E and push a list out of the items that E produces. ;; ;; Regexp equivalents: ;; @@ -225,7 +225,7 @@ Note: a PE can't \"call\" rules by name." (error "Invalid parsing expression: %S" exp)))) (defvar peg-leaf-types '(null fail any call action char range str set - bob eob bol eol bow eow bos eos syntax-class)) + bob eob bol eol bow eow bos eos syntax-class =)) (dolist (type peg-leaf-types) (puthash type `(lambda (&rest args) (cons ',type args)) @@ -421,6 +421,12 @@ Note: a PE can't \"call\" rules by name." (t (error "Invalid syntax class: %S\nMust be one of: %s" class (mapcar #'car peg-syntax-classes)))))) +(peg-add-method translate = (string) + `(let ((str ,string)) + (when (zerop (length str)) + (error "Empty strings not allowed for =")) + (search-forward str (+ (point) (length str)) t))) + (peg-add-method translate * (e) (let ((cp (peg-make-choicepoint))) `(progn (while (,@(peg-save-choicepoint cp) @@ -584,6 +590,7 @@ input. PATH is the list of rules that we have visited so far." (peg-add-method detect-cycles eow (path) t) (peg-add-method detect-cycles bos (path) t) (peg-add-method detect-cycles eos (path) t) +(peg-add-method detect-cycles = (path s) nil) (peg-add-method detect-cycles syntax-class (p n) nil) (peg-add-method detect-cycles action (path form) t) @@ -653,6 +660,9 @@ input. PATH is the list of rules that we have visited so far." (assert (not (peg-parse-string ((s (bos))) " x"))) (assert (peg-parse-string ((s "x" (eos))) "x")) (assert (peg-parse-string ((s (syntax-class whitespace))) " ")) + (assert (peg-parse-string ((s (= "foo"))) "foo")) + (assert (let ((f "foo")) (peg-parse-string ((s (= f))) "foo"))) + (assert (not (peg-parse-string ((s (= "foo"))) "xfoo"))) (assert (equal (peg-parse-string ((s `(-- 1 2))) "") '(t (2 1)))) (assert (equal (peg-parse-string ((s `(-- 1 2) `(a b -- a b))) "") '(t (2 1)))) @@ -821,6 +831,39 @@ input. PATH is the list of rules that we have visited so far." ;; (peg-ex-uri)file:/bar/baz.html?foo=df#x ;; (peg-ex-uri)http://lu...@www.foo.com:8080/bar/baz.html?x=1#foo +;; Split STRING where SEPARATOR occurs. +(defun peg-ex-split (string separator) + (peg-parse-string ((s (list (* (* sep) elt))) + (elt (substring (+ (not sep) (any)))) + (sep (= separator))) + string)) + +;; (peg-ex-split "-abc-cd-" "-") + +;; Find the last digit in a string. +(defun peg-ex-last-digit (string) + (peg-parse-string ((s (or (and (any) s) + (substring [0-9])))) + string)) + +;; (peg-ex-last-digit "ab0cd1ef2gh") +;; (peg-ex-last-digit (make-string 50 ?-)) +;; (peg-ex-last-digit (make-string 1000 ?-)) + +;; Find the last digit without recursion. +(defun peg-ex-last-digit2 (string) + (peg-parse-string ((s `(-- nil) + (+ (* (not digit) (any)) + (substring digit) + `(d1 d2 -- d2))) + (digit [0-9])) + string)) + +;; (peg-ex-last-digit2 "ab0cd1ef2gh") +;; (peg-ex-last-digit2 (concat (make-string 500000 ?-) "8a9b")) +;; (peg-ex-last-digit2 (make-string 500000 ?-)) +;; (peg-ex-last-digit2 (make-string 500000 ?5)) + ;; Parse a lisp style Sexp. ;; [To keep the example short, ' and . are handled as ordinary symbol.] (defun peg-ex-lisp ()