From 9289f299cffe96f87483064e71c60fb7da0cb0ad Mon Sep 17 00:00:00 2001 From: colin Date: Sat, 29 Jul 2023 10:22:44 -0700 Subject: Add better grammar parsing for subexpressions --- argot.lisp | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) (limited to 'argot.lisp') diff --git a/argot.lisp b/argot.lisp index 4c1dfdc..c2b3556 100644 --- a/argot.lisp +++ b/argot.lisp @@ -50,6 +50,9 @@ expression. Such expressions look like (OP . ARGS) where OP is one of (:@ (and (var? (first more)) (not (third more)) (pattern? (second more)))) + (:{} (and (car more) + (symbolp (car more)) + (endp (cdr more)))) ((:item :eof) (endp more))))))) (defun var-pattern? (pat) @@ -101,7 +104,6 @@ and it returns VAR in that case." (symbolp (second s)) (endp (cddr s))))) - (defmacro deflanguage (name (&key (documentation "")) &body ruledefs) (let ((bindings-var (gensym "BINDINGS"))) (labels ((collect-let-bindings (lhs vars) @@ -269,6 +271,24 @@ any pattern fails the whole parse fails." (succeed nil) (fail))) +(defun find-grammar (name) + (and (symbolp name) + (boundp name) + (let ((val (symbol-value name))) + (and (typep val 'grammar) + val)))) + +(defun parse-grammar-pattern (language) + (if-parse (subtokens) (parse-item) + (if (listp subtokens) + (if-let (grammar (find-grammar language)) + (let ((*grammar* grammar) + (*position* 0) + (*tokens* (coerce subtokens 'vector))) + (parse-rule (start-rule grammar))) + (error "No grammar called ~s" language)) + (fail)))) + (defun parse-pattern (pattern) (if (nonterminal? pattern) (parse-rule pattern) @@ -286,6 +306,7 @@ any pattern fails the whole parse fails." (:?= (parse-optional-literal (first args))) (:seq= (parse-literal-sequence args)) (:@ (parse-binding-pattern (first args) (second args))) + (:{} (parse-grammar-pattern (first args))) (:item (parse-item)) (:eof (parse-eof)))))) -- cgit v1.2.3