X-Git-Url: https://git.distorted.org.uk/~mdw/sod/blobdiff_plain/c91b90c3bfd3d3e68cc3d3ff3f431d1e73920061..ba8bae5fe1f190d4a61a4ab63b52725bc0e07c9b:/src/module-parse.lisp diff --git a/src/module-parse.lisp b/src/module-parse.lisp index 6fb6be8..9522085 100644 --- a/src/module-parse.lisp +++ b/src/module-parse.lisp @@ -7,7 +7,7 @@ ;;;----- Licensing notice --------------------------------------------------- ;;; -;;; This file is part of the Sensble Object Design, an object system for C. +;;; This file is part of the Sensible Object Design, an object system for C. ;;; ;;; SOD is free software; you can redistribute it and/or modify ;;; it under the terms of the GNU General Public License as published by @@ -28,13 +28,11 @@ ;;;-------------------------------------------------------------------------- ;;; Toplevel syntax. -(export 'module) - ;;; Type names. -(define-pluggable-parser module typename (scanner) - ;; `typename' ID ( `,' ID )* `;' - +(define-pluggable-parser module typename (scanner pset) + ;; `typename' id ( `,' id )* `;' + (declare (ignore pset)) (with-parser-context (token-scanner-context :scanner scanner) (parse (and "typename" (skip-many (:min 1) @@ -49,30 +47,44 @@ ;;; Fragments. -(define-pluggable-parser module code (scanner) - ;; `code' ID `:' ID [ CONSTRAINTS ] `{' C-FRAGMENT `}' - +(define-pluggable-parser module code (scanner pset) + ;; `code' id `:' item-name [constraints] `{' c-fragment `}' + ;; + ;; constrains ::= `[' constraint-list `]' + ;; constraint ::= item-name+ + ;; item-name ::= id | `(' id+ `)' + (declare (ignore pset)) (with-parser-context (token-scanner-context :scanner scanner) - (parse (seq ("code" - (reason :id) - #\: - (name :id) - (constraints (? (seq (#\[ - (constraints (list (:min 1) - (list (:min 1) :id) - #\,)) - #\]) - constraints))) - (fragment (parse-delimited-fragment scanner #\{ #\}))) - (add-to-module *module* (make-instance 'code-fragment-item - :fragment fragment - :constraints constraints - :reason reason - :name name)))))) + (labels ((kw () + (parse (seq ((kw :id)) + (intern (frob-identifier kw) 'keyword)))) + (item () + (parse (or (kw) + (seq (#\( (names (list (:min 1) (kw))) #\)) + names))))) + (parse (seq ("code" + (reason (kw)) + #\: + (name (item)) + (constraints (? (seq (#\[ + (constraints (list (:min 1) + (list (:min 1) + (item)) + #\,)) + #\]) + constraints))) + (fragment (parse-delimited-fragment scanner #\{ #\}))) + (add-to-module *module* + (make-instance 'code-fragment-item + :fragment fragment + :constraints constraints + :reason reason + :name name))))))) ;;; External files. -(defun read-module (pathname &key (truename (truename pathname)) location) +(export 'read-module) +(defun read-module (pathname &key (truename nil truep) location) "Parse the file at PATHNAME as a module, returning it. This is the main entry point for parsing module files. You may well know @@ -83,6 +95,9 @@ `file-location' object, though it might be anything other than `t' which can be printed in the event of circular imports." + (setf pathname (merge-pathnames pathname + (make-pathname :type "SOD" :case :common))) + (unless truep (setf truename (truename pathname))) (define-module (pathname :location location :truename truename) (with-open-file (f-stream pathname :direction :input) (let* ((*readtable* (copy-readtable)) @@ -92,19 +107,23 @@ :char-scanner char-scanner))) (with-default-error-location (scanner) (with-parser-context (token-scanner-context :scanner scanner) - (parse (skip-many () (plug module scanner))))))))) - -(define-pluggable-parser module test (scanner) - ;; `demo' STRING `;' - + (parse (skip-many () + (seq ((pset (parse-property-set scanner)) + (nil (error () + (plug module scanner pset)))) + (check-unused-properties pset)))))))))) + +(define-pluggable-parser module test (scanner pset) + ;; `demo' string `;' + (declare (ignore pset)) (with-parser-context (token-scanner-context :scanner scanner) (parse (seq ("demo" (string :string) #\;) (format t ";; DEMO ~S~%" string))))) -(define-pluggable-parser module file (scanner) - ;; `import' STRING `;' - ;; `load' STRING `;' - +(define-pluggable-parser module file (scanner pset) + ;; `import' string `;' + ;; `load' string `;' + (declare (ignore pset)) (flet ((common (name type what thunk) (find-file scanner (merge-pathnames name @@ -136,11 +155,34 @@ (cerror* "Error loading Lisp file ~S: ~A" path error))))))))))) +;;; Setting properties. + +(define-pluggable-parser module set (scanner pset) + ;; `set' property-list `;' + (with-parser-context (token-scanner-context :scanner scanner) + (parse (and "set" + (lisp (let ((module-pset (module-pset *module*))) + (when pset + (pset-map (lambda (prop) + (add-property module-pset + (p-name prop) + (p-value prop) + :type (p-type prop) + :location (p-location prop)) + (setf (p-seenp prop) t)) + pset)) + (parse (skip-many (:min 0) + (error (:ignore-unconsumed t) + (parse-property scanner module-pset) + (skip-until (:keep-end t) #\, #\;)) + #\,)))) + #\;)))) + ;;; Lisp escape. -(define-pluggable-parser module lisp (scanner) +(define-pluggable-parser module lisp (scanner pset) ;; `lisp' s-expression `;' - + (declare (ignore pset)) (with-parser-context (token-scanner-context :scanner scanner) (parse (seq ((sexp (if (and (eql (token-type scanner) :id) (string= (token-value scanner) "lisp")) @@ -155,11 +197,15 @@ ;;;-------------------------------------------------------------------------- ;;; Class declarations. -(defun parse-class-body (scaner pset name supers) +(export 'class-item) + +(defun parse-class-body (scanner pset name supers) ;; class-body ::= `{' class-item* `}' + ;; + ;; class-item ::= property-set raw-class-item (with-parser-context (token-scanner-context :scanner scanner) (make-class-type name) - (let* ((class (make-sod-class name (mapcat #'find-sod-class supers) + (let* ((class (make-sod-class name (mapcar #'find-sod-class supers) pset scanner)) (nick (sod-class-nickname class))) @@ -174,20 +220,25 @@ ;; names. (parse-declarator scanner base-type - :centre (parser () + :kernel (parser () (seq ((name-a :id) (name-b (? (seq (#\. (id :id)) id)))) (if name-b (cons name-a name-b) name-a))))) - ;; class-item ::= [property-set] raw-class-item - ;; - (parse-message-item (sub-pset type name) ;; message-item ::= ;; declspec+ declarator -!- (method-body | `;') - (make-sod-message class name type sub-pset scanner) - (parse (or #\; (parse-method-item nil type nick name)))) + ;; + ;; Don't allow a method-body here if the message takes a + ;; varargs list, because we don't have a name for the + ;; `va_list' parameter. + (let ((message (make-sod-message class name type + sub-pset scanner))) + (if (varargs-message-p message) + (parse #\;) + (parse (or #\; (parse-method-item sub-pset + type nick name)))))) (parse-method-item (sub-pset type sub-nick name) ;; method-item ::= @@ -205,13 +256,46 @@ ;; ;; Return (VALUE-KIND . VALUE-FORM), ready for passing to a ;; `sod-initializer' constructor. - (parse (or (peek (seq (#\= (frag (parse-delimited-fragment - scanner #\{ #\}))) - (cons :compound frag))) - (seq ((frag (parse-delimited-fragment - scanner #\= '(#\; #\,) - :keep-end t))) - (cons :simple frag))))) + + ;; This is kind of tricky because we have to juggle both + ;; layers of the parsing machinery. The character scanner + ;; will already have consumed the lookahead token (which, if + ;; we're going to do anything, is `='). + (let ((char-scanner (token-scanner-char-scanner scanner))) + + ;; First, skip the character-scanner past any whitespace. + ;; We don't record this consumption, which is a bit + ;; naughty, but nobody will actually mind. + (loop + (when (or (scanner-at-eof-p char-scanner) + (not (whitespace-char-p + (scanner-current-char char-scanner)))) + (return)) + (scanner-step char-scanner)) + + ;; Now maybe read an initializer. + (cond ((not (eql (token-type scanner) #\=)) + ;; It's not an `=' after all. There's no + ;; initializer. + (values '(#\=) nil nil)) + + ((and (not (scanner-at-eof-p char-scanner)) + (char= (scanner-current-char char-scanner) + #\{)) + ;; There's a brace after the `=', so we should + ;; consume the `=' here, and read a compound + ;; initializer enclosed in braces. + (parse (seq (#\= (frag (parse-delimited-fragment + scanner #\{ #\}))) + (cons :compound frag)))) + + (t + ;; No brace, so read from the `=' up to, but not + ;; including, the trailing `,' or `;' delimiter. + (parse (seq ((frag (parse-delimited-fragment + scanner #\= '(#\; #\,) + :keep-end t))) + (cons :simple frag))))))) (parse-slot-item (sub-pset base-type type name) ;; slot-item ::= @@ -226,7 +310,7 @@ (when init (make-sod-instance-initializer class nick name (car init) (cdr init) - nil scanner))) + sub-pset scanner))) (skip-many () (seq (#\, (ds (parse-declarator scanner @@ -238,7 +322,7 @@ (make-sod-instance-initializer class nick (cdr ds) (car init) (cdr init) - nil scanner)))) + sub-pset scanner)))) #\;))) (parse-initializer-item (sub-pset constructor) @@ -289,14 +373,14 @@ ;; Most of the above begin with declspecs and a declarator ;; (which might be dotted). So we parse that here and ;; dispatch based on what we find. - (parse (or (peek + (parse (or (plug class-item scanner class sub-pset) + (peek (seq ((ds (parse-c-type scanner)) (dc (parse-maybe-dotted-declarator ds)) - (result (class-item-dispatch sub-pset - ds - (car dc) - (cdr dc)))) - result)) + (nil (class-item-dispatch sub-pset + ds + (car dc) + (cdr dc)))))) (and "class" (parse-initializer-item sub-pset @@ -305,16 +389,18 @@ sub-pset #'make-sod-instance-initializer))))) - (parse (and #\{ - (skip-many () - (seq ((sub-pset (? (parse-property-set))) - (nil (parse-raw-class-item sub-pset))))) - #\})))))) + (parse (seq (#\{ + (nil (skip-many () + (seq ((sub-pset (parse-property-set scanner)) + (nil (parse-raw-class-item sub-pset))) + (check-unused-properties sub-pset)))) + (nil (error () #\}))) + (finalize-sod-class class) + (add-to-module *module* class))))))) -(define-pluggable-parser module class (scanner) +(define-pluggable-parser module class (scanner pset) ;; `class' id [`:' id-list] class-body ;; `class' id `;' - (with-parser-context (token-scanner-context :scanner scanner) (parse (seq ("class" (name :id) @@ -326,14 +412,4 @@ scanner pset name supers))))))))))) - - - - (parse (seq ("class" - (name :id) - (supers (? (seq (#\: (supers (list (:min 1) :id #\,))) - supers))) - #\{ - - ;;;----- That's all, folks --------------------------------------------------