X-Git-Url: https://git.distorted.org.uk/~mdw/sod/blobdiff_plain/db2abd9da3507b4329c06cced193ab7bedb49cea..00d59354c311fb28730b7c9b117b0d91aac092cc:/src/module-parse.lisp diff --git a/src/module-parse.lisp b/src/module-parse.lisp index 9cad3d4..eff4af7 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 @@ -31,48 +31,71 @@ ;;; Type names. (define-pluggable-parser module typename (scanner pset) - ;; `typename' id ( `,' id )* `;' + ;; `typename' list[id] `;' (declare (ignore pset)) (with-parser-context (token-scanner-context :scanner scanner) (parse (and "typename" - (skip-many (:min 1) - (seq ((id :id)) - (if (gethash id *module-type-map*) - (cerror* "Type `~A' already defined" id) - (add-to-module *module* - (make-instance 'type-item - :name id)))) + (skip-many () + (error () + (seq ((id :id)) + (if (or (gethash id *module-type-map*) + (find-simple-c-type id)) + (cerror* "Type `~A' already defined" id) + (add-to-module *module* + (make-instance 'type-item + :name id)))) + (skip-until () #\, #\;)) #\,) - #\;)))) + (must #\;))))) ;;; Fragments. (define-pluggable-parser module code (scanner pset) - ;; `code' id `:' id [constraints] `{' c-fragment `}' + ;; `code' id `:' item-name [constraints] `{' c-fragment `}' + ;; `code' id `:' constraints `;' ;; - ;; constrains ::= `[' constraint-list `]' - ;; constraint ::= id+ + ;; constraints ::= `[' list[constraint] `]' + ;; constraint ::= item-name+ + ;; item-name ::= id | `(' id+ `)' (declare (ignore pset)) (with-parser-context (token-scanner-context :scanner scanner) - (flet ((kw () - (parse (seq ((kw :id)) (intern (string-upcase kw) 'keyword))))) + (labels ((kw () + (parse (seq ((kw :id)) + (intern (frob-identifier kw) 'keyword)))) + (item () + (parse (or (kw) + (seq (#\( (names (list (:min 1) (kw))) #\)) + names)))) + (constraints () + (parse (seq (#\[ + (constraints + (list () + (list (:min 1) + (error (:ignore-unconsumed t) (item) + (skip-until () :id #\( #\, #\]))) + #\,)) + #\]) + constraints))) + (fragment () + (parse-delimited-fragment scanner #\{ #\}))) (parse (seq ("code" - (reason (kw)) - #\: - (name (kw)) - (constraints (? (seq (#\[ - (constraints (list (:min 1) - (list (:min 1) (kw)) - #\,)) - #\]) - constraints))) - (fragment (parse-delimited-fragment scanner #\{ #\}))) - (add-to-module *module* - (make-instance 'code-fragment-item - :fragment fragment - :constraints constraints - :reason reason - :name name))))))) + (reason (must (kw))) + (nil (must #\:)) + (item (or (seq ((constraints (constraints)) + (nil (must #\;))) + (make-instance 'code-fragment-item + :reason reason + :constraints constraints)) + (seq ((name (must (item))) + (constraints (? (constraints))) + (fragment (fragment))) + (and name + (make-instance 'code-fragment-item + :reason reason + :constraints constraints + :name name + :fragment fragment)))))) + (when item (add-to-module *module* item))))))) ;;; External files. @@ -93,39 +116,38 @@ (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)) - (char-scanner (make-instance 'charbuf-scanner - :stream f-stream)) + (let* ((char-scanner (make-instance 'charbuf-scanner + :stream f-stream + :filename (namestring pathname))) (scanner (make-instance 'sod-token-scanner :char-scanner char-scanner))) (with-default-error-location (scanner) (with-parser-context (token-scanner-context :scanner scanner) - (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))))) + (multiple-value-bind (result winp consumedp) + (parse (skip-many () + (seq ((pset (parse-property-set scanner)) + (nil (error () + (plug module scanner pset) + (skip-until (:keep-end nil) + #\; #\})))) + (check-unused-properties pset)))) + (declare (ignore consumedp)) + (unless winp (syntax-error scanner result))))))))) (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 - (make-pathname :type type - :case :common)) - what - thunk))) + (when name + (find-file (pathname (scanner-filename scanner)) + (merge-pathnames name + (make-pathname :type type + :case :common)) + what + thunk)))) (with-parser-context (token-scanner-context :scanner scanner) - (parse (or (seq ("import" (name :string) #\;) + (parse (or (seq ("import" (name (must :string)) (nil (must #\;))) (common name "SOD" "module" (lambda (path true) (handler-case @@ -133,21 +155,51 @@ :truename true))) (when module (module-import module) + (pushnew path (module-files *module*)) (pushnew module (module-dependencies *module*)))) (file-error (error) (cerror* "Error reading module ~S: ~A" + path error)) + (error (error) + (cerror* "Unexpected error reading ~ + module ~S: ~A" path error)))))) - (seq ("load" (name :string) #\;) + (seq ("load" (name (must :string)) (nil (must #\;))) (common name "LISP" "Lisp file" (lambda (path true) (handler-case - (load true :verbose nil :print nil) + (progn + (pushnew path (module-files *module*)) + (load true :verbose nil :print nil)) (error (error) (cerror* "Error loading Lisp file ~S: ~A" path error))))))))))) +;;; Setting properties. + +(define-pluggable-parser module set (scanner pset) + ;; `set' list[property] `;' + (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 (if pset 0 1)) + (error (:ignore-unconsumed t) + (parse-property scanner module-pset) + (skip-until () #\, #\;)) + #\,)))) + #\;)))) + ;;; Lisp escape. (define-pluggable-parser module lisp (scanner pset) @@ -161,38 +213,157 @@ (scanner-step scanner) (values sexp t t)) (values '((:id "lisp")) nil nil))) - #\;) + (nil (must #\;))) (eval sexp))))) ;;;-------------------------------------------------------------------------- +;;; Static instances. + +(define-pluggable-parser module instance (scanner pset) + ;; `instance' id id list[slot-initializer] `;' + (with-parser-context (token-scanner-context :scanner scanner) + (let ((duff nil) + (floc nil) + (empty-pset (make-property-set))) + (parse (seq ("instance" + (class (seq ((class-name (must :id))) + (setf floc (file-location scanner)) + (restart-case (find-sod-class class-name) + (continue () + (setf duff t) + nil)))) + (name (must :id)) + (inits (? (seq (#\: + (inits (list (:min 0) + (seq ((nick (must :id)) + #\. + (name (must :id)) + (value + (parse-delimited-fragment + scanner #\= '(#\, #\;) + :keep-end t))) + (make-sod-instance-initializer + class nick name value + empty-pset + :add-to-class nil + :location scanner)) + #\,))) + inits))) + #\;) + (unless duff + (acond ((find-if (lambda (item) + (and (typep item 'static-instance) + (string= (static-instance-name item) + name))) + (module-items *module*)) + (cerror*-with-location floc + "Instance with name `~A' ~ + already defined." + name) + (info-with-location (file-location it) + "Previous definition was ~ + here.")) + (t + (add-to-module *module* + (make-static-instance class name + inits + pset + floc)))))))))) + +;;;-------------------------------------------------------------------------- ;;; Class declarations. +(export 'class-item) + +(define-pluggable-parser class-item initfrags (scanner class pset) + ;; raw-class-item ::= frag-keyword `{' c-fragment `}' + ;; frag-keyword ::= `init' | `teardown' + (with-parser-context (token-scanner-context :scanner scanner) + (parse (seq ((make (or (seq ("init") #'make-sod-class-initfrag) + (seq ("teardown") #'make-sod-class-tearfrag))) + (frag (parse-delimited-fragment scanner #\{ #\}))) + (funcall make class frag pset :location scanner))))) + +(define-pluggable-parser class-item initargs (scanner class pset) + ;; initarg-item ::= `initarg' declspec+ list[init-declarator] + ;; init-declarator ::= declarator [`=' initializer] + (with-parser-context (token-scanner-context :scanner scanner) + (parse (seq ("initarg" + (base-type (parse-c-type scanner)) + (nil (skip-many (:min 1) + (seq ((declarator (parse-declarator scanner + base-type)) + (init (? (parse-delimited-fragment + scanner #\= (list #\; #\,) + :keep-end t)))) + (make-sod-user-initarg class + (cdr declarator) + (car declarator) + pset + :default init + :location scanner)) + #\,)) + (nil (must #\;))))))) + +(defun synthetic-name () + "Return an obviously bogus synthetic not-identifier." + (let ((ix *temporary-index*)) + (incf *temporary-index*) + (make-instance 'temporary-variable :tag (format nil "%%#~A" ix)))) + (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 (mapcar #'find-sod-class supers) - pset scanner)) + (when name (make-class-type name)) + (let* ((duff (null name)) + (superclasses + (let ((superclasses (restart-case + (mapcar #'find-sod-class + (or supers (list "SodObject"))) + (continue () + (setf duff t) + (list (find-sod-class "SodObject")))))) + (find-duplicates (lambda (first second) + (declare (ignore second)) + (setf duff t) + (cerror* "Class `~A' has duplicate ~ + direct superclass `~A'" + name first)) + superclasses) + (delete-duplicates superclasses))) + (synthetic-name (or name + (let ((var (synthetic-name))) + (unless pset + (setf pset (make-property-set))) + (unless (pset-get pset "nick") + (add-property pset "nick" var :type :id)) + var))) + (class (make-sod-class synthetic-name superclasses pset + :location scanner)) (nick (sod-class-nickname class))) - (labels ((parse-maybe-dotted-declarator (base-type) - ;; Parse a declarator or dotted-declarator, i.e., one whose - ;; centre is - ;; - ;; maybe-dotted-identifier ::= [id `.'] id + (labels ((must-id () + (parse (must :id (progn (setf duff t) (synthetic-name))))) + + (parse-maybe-dotted-name () + ;; maybe-dotted-name ::= [id `.'] id ;; ;; A plain identifier is returned as a string, as usual; a ;; dotted identifier is returned as a cons cell of the two ;; names. - (parse-declarator - scanner base-type - :kernel (parser () - (seq ((name-a :id) - (name-b (? (seq (#\. (id :id)) id)))) - (if name-b (cons name-a name-b) - name-a))))) + (parse (seq ((name-a (must-id)) + (name-b (? (seq (#\. (id (must-id))) id)))) + (if name-b (cons name-a name-b) + name-a)))) + + (parse-maybe-dotted-declarator (base-type) + ;; Parse a declarator or dotted-declarator, i.e., one whose + ;; centre is maybe-dotted-name above. + (parse-declarator scanner base-type + :keywordp t + :kernel #'parse-maybe-dotted-name)) (parse-message-item (sub-pset type name) ;; message-item ::= @@ -201,8 +372,8 @@ ;; 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))) + (let ((message (make-sod-message class name type sub-pset + :location scanner))) (if (varargs-message-p message) (parse #\;) (parse (or #\; (parse-method-item sub-pset @@ -216,97 +387,71 @@ (parse (seq ((body (or (seq ("extern" #\;) nil) (parse-delimited-fragment scanner #\{ #\})))) - (make-sod-method class sub-nick name type - body sub-pset scanner)))) + (restart-case + (make-sod-method class sub-nick name type + body sub-pset + :location scanner) + (continue () :report "Continue"))))) (parse-initializer () - ;; initializer ::= `=' c-fragment | `=' `{' c-fragment `}' + ;; initializer ::= `=' c-fragment ;; - ;; Return (VALUE-KIND . VALUE-FORM), ready for passing to a - ;; `sod-initializer' constructor. - - ;; 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))))))) + ;; Return a VALUE, ready for passing to a `sod-initializer' + ;; constructor. + (parse-delimited-fragment scanner #\= '(#\, #\;) + :keep-end t)) (parse-slot-item (sub-pset base-type type name) ;; slot-item ::= ;; declspec+ declarator -!- [initializer] - ;; [`,' init-declarator-list] `;' + ;; [`,' list[init-declarator]] `;' ;; - ;; init-declarator-list ::= - ;; declarator [initializer] [`,' init-declarator-list] - (parse (and (seq ((init (? (parse-initializer)))) - (make-sod-slot class name type - sub-pset scanner) - (when init - (make-sod-instance-initializer - class nick name (car init) (cdr init) - sub-pset scanner))) - (skip-many () - (seq (#\, - (ds (parse-declarator scanner - base-type)) - (init (? (parse-initializer)))) - (make-sod-slot class (cdr ds) (car ds) - sub-pset scanner) - (when init - (make-sod-instance-initializer - class nick (cdr ds) - (car init) (cdr init) - sub-pset scanner)))) - #\;))) - - (parse-initializer-item (sub-pset constructor) + ;; init-declarator ::= declarator [initializer] + (flet ((make-it (name type init) + (restart-case + (progn + (make-sod-slot class name type sub-pset + :location scanner) + (when init + (make-sod-instance-initializer + class nick name init sub-pset + :location scanner))) + (continue () :report "Continue")))) + (parse (and (error () + (seq ((init (? (parse-initializer)))) + (make-it name type init)) + (skip-until () #\, #\;)) + (skip-many () + (error (:ignore-unconsumed t) + (seq (#\, + (ds (parse-declarator scanner + base-type)) + (init (? (parse-initializer)))) + (make-it (cdr ds) (car ds) init)) + (skip-until () #\, #\;))) + (must #\;))))) + + (parse-initializer-item (sub-pset must-init-p constructor) ;; initializer-item ::= - ;; [`class'] -!- slot-initializer-list `;' + ;; [`class'] -!- list[slot-initializer] `;' ;; - ;; slot-initializer ::= id `.' id initializer - (parse (and (skip-many () - (seq ((name-a :id) #\. (name-b :id) - (init (parse-initializer))) - (funcall constructor class - name-a name-b - (car init) (cdr init) - sub-pset scanner)) - #\,) - #\;))) + ;; slot-initializer ::= id `.' id [initializer] + (let ((parse-init (if must-init-p #'parse-initializer + (parser () (? (parse-initializer)))))) + (parse (and (skip-many () + (error (:ignore-unconsumed t) + (seq ((name-a :id) #\. + (name-b (must-id)) + (init (funcall parse-init))) + (restart-case + (funcall constructor class + name-a name-b init + sub-pset + :location scanner) + (continue () :report "Continue"))) + (skip-until () #\, #\;)) + #\,) + (must #\;))))) (class-item-dispatch (sub-pset base-type type name) ;; Logically part of `parse-raw-class-item', but the @@ -320,9 +465,8 @@ ;; definition; otherwise it might be a message or slot. (cond ((not (typep type 'c-function-type)) (when (consp name) - (cerror*-with-location - scanner - "Method declarations must have function type.") + (cerror* + "Method declarations must have function type") (setf name (cdr name))) (parse-slot-item sub-pset base-type type name)) ((consp name) @@ -337,6 +481,7 @@ ;; | method-item ;; | slot-item ;; | initializer-item + ;; | initfrag-item ;; ;; Most of the above begin with declspecs and a declarator ;; (which might be dotted). So we parse that here and @@ -345,37 +490,39 @@ (peek (seq ((ds (parse-c-type scanner)) (dc (parse-maybe-dotted-declarator ds)) + (nil (commit)) (nil (class-item-dispatch sub-pset ds (car dc) (cdr dc)))))) (and "class" - (parse-initializer-item - sub-pset + (parse-initializer-item sub-pset t #'make-sod-class-initializer)) - (parse-initializer-item - sub-pset + (parse-initializer-item sub-pset nil #'make-sod-instance-initializer))))) - (parse (seq (#\{ + (parse (seq ((nil (must #\{)) (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))))))) + (nil (must #\}))) + (unless (finalize-sod-class class) + (setf duff t)) + (unless duff + (add-to-module *module* class)))))))) (define-pluggable-parser module class (scanner pset) - ;; `class' id [`:' id-list] class-body + ;; `class' id `:' list[id] class-body ;; `class' id `;' (with-parser-context (token-scanner-context :scanner scanner) (parse (seq ("class" - (name :id) + (name (must :id)) (nil (or (seq (#\;) - (make-class-type name)) - (seq ((supers (? (seq (#\: (ids (list () :id #\,))) - ids))) + (when name (make-class-type name))) + (seq ((supers (must (seq (#\: + (ids (list () :id #\,))) + ids))) (nil (parse-class-body scanner pset name supers)))))))))))