From a7570e6141bb0674d7dd9732ad289f736f6e6638 Mon Sep 17 00:00:00 2001 From: Utz-Uwe Haus Date: Fri, 3 Oct 2008 23:48:51 +0200 Subject: [PATCH] Fix PEG grammar and add action support. Add actions which are not part of the original PEG grammar. Signed-off-by: Utz-Uwe Haus --- opossum.peg | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/opossum.peg b/opossum.peg index d07760b..ce3ef23 100644 --- a/opossum.peg +++ b/opossum.peg @@ -21,7 +21,9 @@ ## ## Commentary: ## -## PEG syntax in PEG as in the original report +## PEG syntax in PEG as in the report from 2004 (`Parsing Expression Grammars: +## A Recognition-Based Syntactic Foundation'' by Bryan Ford) +## # # Process this file with an existing PEG parser to bootstrap. # The output needs to be adjusted by replacing the package OPOSSUM @@ -45,12 +47,13 @@ Definition <- Identifier LEFTARROW Expression { `(,(opossum::make-name (first data)) #'(lambda () ,(third data))) ;' } # -Expression <- Sequence (SLASH Sequence)* { (if (null (second data)) - (first data) - (let ((tail (second (cadr data)))) - (if (equal (first tail) 'seq) - `(seq ,(first data) ,@(rest tail)) - `(seq ,(first data) ,tail)))) ;' +Expression <- Sequence Action? (SLASH Sequence Action?)* { + (if (null (second data)) + (first data) + (let ((tail (second (cadr data)))) + (if (equal (first tail) 'seq) + `(seq ,(first data) ,@(rest tail)) + `(seq ,(first data) ,tail)))) ;' } # Sequence <- Prefix* { `(many ,(first data)) } @@ -59,11 +62,12 @@ Prefix <- (AND Suffix) { `(follow ,(second data)) } / (NOT Suffix) { `(negate ,(second data)) - } + } / Suffix { (second data) } # Suffix <- Primary QUESTION { `(optional ,(first data)) } / Primary STAR { `(many ,(first data)) - } / Primary PLUS { `(many1 ,(first data)) } + } / Primary PLUS { `(many1 ,(first data)) + } / Primary { (first data) } # Primary <- Identifier !LEFTARROW { (first data) } / OPEN Expression CLOSE { (second data) @@ -71,6 +75,18 @@ Primary <- Identifier !LEFTARROW { (first data) } / Class { (first data) } / DOT { `(match-any-char ,data) } # +Action <- "{" (!"}" .)* "}" Spacing { + (let* ((action-data (second data)) + (action-code (coerce + (opossum::fix-escape-sequences + (mapcar #'second action-data)) + 'string)) + (action-name (opossum::make-action-name))) + (opossum::store-action opossum:*context* + `(,action-name ,action-code)) + `(list ':action nil ',action-name)) + } +# # Lexical syntax Identifier <- [a-zA-Z_] [a-zA-Z0-9_]* Spacing { (concatenate 'string (first data) (second data)) } # -- 2.11.4.GIT