From f450a3f29645da8e88213d865b9796567a381b9e Mon Sep 17 00:00:00 2001 Message-Id: From: Mark Wooding Date: Wed, 16 Dec 2015 05:57:20 +0000 Subject: [PATCH] src/c-types-parse.lisp (parse-declarator): Refactor argument list parsing. Organization: Straylight/Edgeware From: Mark Wooding The `argument-list' parser is now less monolithic: the work of parsing individual arguments is given to new functions `arg-decl' and `argument'. Also, the interface between `argument-list' and its parent `postfix-lparen' is changed. Instead of simply returning its argument list, `argument-list' now returns a function which can be applied to a base type to produce the appropriate function type. None of this changes any externally observable behaviour. --- src/c-types-parse.lisp | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/src/c-types-parse.lisp b/src/c-types-parse.lisp index c797010..e1a7dcd 100644 --- a/src/c-types-parse.lisp +++ b/src/c-types-parse.lisp @@ -393,8 +393,24 @@ (defun parse-declarator (scanner base-type &key kernel abstractp) (parse (seq ((name (funcall kernel-parser))) (cons #'identity name)))) + (arg-decl (abstractp) + (parse (seq ((base-type (parse-c-type scanner)) + (dtor (parse-declarator scanner base-type + :abstractp abstractp))) + dtor))) + + (argument () + ;; argument ::= type abstract-declspec + + (parse (seq ((dtor (arg-decl t))) + (make-argument (cdr dtor) (car dtor))))) + (argument-list () - ;; [argument [`,' argument]* [`,' `...']] | `...' + ;; argument-list ::= + ;; [argument [`,' argument]* [`,' argument-tail]] + ;; | argument-tail + ;; + ;; argument-tail ::= `...' ;; ;; The possibility of a trailing `,' `...' means that we ;; can't use the standard `list' parser. Note that, unlike @@ -408,11 +424,7 @@ (defun parse-declarator (scanner base-type &key kernel abstractp) (scanner-step scanner) (return)) (multiple-value-bind (arg winp consumedp) - (parse (seq ((base-type (parse-c-type scanner)) - (dtor (parse-declarator scanner - base-type - :abstractp t))) - (make-argument (cdr dtor) (car dtor)))) + (argument) (unless winp (if (or consumedp args) (return-from argument-list (values arg nil t)) @@ -421,16 +433,20 @@ (defun parse-declarator (scanner base-type &key kernel abstractp) (unless (eq (token-type scanner) #\,) (return)) (scanner-step scanner)) - (values (nreverse args) t args))) + (values (let ((rargs (nreverse args))) + (lambda (ret) + (make-function-type ret rargs))) + t + args))) (postfix-lparen () ;; Postfix: `(' argument-list `)' - (parse (seq (#\( (args (argument-list)) #\)) + (parse (seq (#\( (make (argument-list)) #\)) (postop "()" (state 10) (cons (lambda (type) (funcall (car state) - (make-function-type type args))) + (funcall make type))) (cdr state)))))) (dimension () -- [mdw]