3 ;;; Parsing property sets
5 ;;; (c) 2012 Straylight/Edgeware
8 ;;;----- Licensing notice ---------------------------------------------------
10 ;;; This file is part of the Sensible Object Design, an object system for C.
12 ;;; SOD is free software; you can redistribute it and/or modify
13 ;;; it under the terms of the GNU General Public License as published by
14 ;;; the Free Software Foundation; either version 2 of the License, or
15 ;;; (at your option) any later version.
17 ;;; SOD is distributed in the hope that it will be useful,
18 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;;; GNU General Public License for more details.
22 ;;; You should have received a copy of the GNU General Public License
23 ;;; along with SOD; if not, write to the Free Software Foundation,
24 ;;; Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28 ;;;--------------------------------------------------------------------------
29 ;;; The expression parser.
31 (defun parse-expression (scanner)
32 "Parse and evaluate a simple expression.
34 The result is a pair (TYPE . VALUE). Currently, type types are `:id',
35 `:int', `:string', `:char', `:fragment', `:type'. If an error prevented a
36 sane value from being produced, the type `:invalid' is returned.
38 The syntax of expressions is rather limited at the moment, but more may be
41 expression: term | expression `+' term | expression `-' term
42 term: factor | term `*' factor | term `/' factor
43 factor: primary | `+' factor | `-' factor
44 primary: int | id | string | `(' expression `)' | `{' fragment `}'
45 | `<' declspec+ declarator[empty] `>' | `?' lisp-expression
47 Only operators for dealing with integers are provided."
49 ;; The expression parser works in two stages. First, the parser proper
50 ;; builds a thunk as its `value'. If this is successful, then the thunk is
51 ;; invoked to return a property type and value. Primitive expressions
52 ;; produce thunks which just return their values; operators combine their
53 ;; argument thunks together, evaluating them (or not) on demand.
55 (macrolet ((oplambda (&body body)
56 ;; Like `lambda', but (a) always produces a function with no
57 ;; arguments, and (b) captures the current location so that
58 ;; errors are attributed correctly.
61 `(let ((,floc (file-location scanner)))
63 (with-default-error-location (,floc)
66 (flet ((dispatch (name args &rest spec)
67 ;; Evaluate the ARGS to determine their types and values. Find
68 ;; the first SPEC, of the form (RETTY OP ARGTY*), where the
69 ;; ARGTYs match the argument types, in order, and apply OP to
70 ;; the argument values, return this as a result of type RETTY.
71 ;; If no SPEC matches, then report an error.
74 (let ((args (mapcar (compose #'funcall #'cons) args)))
75 (aif (find-if (lambda (item)
76 (every (lambda (type arg)
80 (values (car it) (apply (cadr it) (mapcar #'cdr args)))
81 (error "Type mismatch: operator `~A' applied to ~
82 types ~{~(~A~)~#[~; and ~;, ~]~}"
83 name (mapcar #'car args)))))))
85 (with-parser-context (token-scanner-context :scanner scanner)
88 ;; Parse the expression, producing a thunk.
89 (expr (:nestedp nestedp)
91 (lisp (case (token-type scanner)
93 ((:int :id :char :string)
95 (let ((type (token-type scanner))
96 (value (token-value scanner)))
97 (scanner-step scanner)
98 (values (lambda () (values type value)) t t)))
101 ;; A Lisp s-expression. Catch and report reader-
102 ;; errors (though the main parser will probably
103 ;; end up /very/ confused); delay evaluation for
106 (let* ((stream (make-scanner-stream scanner))
107 (sexp (read stream t)))
108 (scanner-step scanner)
109 (values (oplambda (decode-property (eval sexp)))
112 (scanner-step scanner)
113 (cerror*-with-location scanner
114 "Lisp `read' error: ~A"
116 (values #'continue t t))))
120 (let ((fragment (parse-delimited-fragment scanner
122 (values (lambda () (values :fragment fragment))
128 (ds (parse-c-type scanner))
129 (dc (parse-declarator
135 (values (lambda () (values :type (car dc)))
139 ;; Anything else is an error.
140 (values (list :int :id :char :string #\? #\{ #\<)
144 ((:op #\* binop "*" (x y 7)
145 (dispatch "*" (list x y) (list :int #'* :int :int)))
146 (:op #\/ binop "/" (x y 7)
147 (dispatch "/" (list x y)
157 (:op #\+ binop "+" (x y 5)
158 (dispatch "+" (list x y) (list :int #'+ :int :int)))
159 (:op #\- binop "-" (x y 5)
160 (dispatch "-" (list x y) (list :int #'- :int :int))))
163 ((:op #\+ preop "+" (x 9)
164 (dispatch "+" (list x) (list :int #'+ :int)))
165 (:op #\- preop "-" (x 9)
166 (dispatch "-" (list x) (list :int #'- :int)))
167 (:op #\( lparen #\)))
169 ;; Postfix operators.
170 ((:op (when nestedp #\)) rparen #\))))
172 ;; Do the delayed evaluation. Establish a restart so that we can
173 ;; continue if evaluation fails for some reason. (The value thunk
174 ;; is expected to report the correct error locations, if it signals
176 (restart-case (multiple-value-bind (type value) (funcall it)
177 (values (cons type value) t t))
178 (continue () (values (cons :invalid nil) t t))))))))
180 ;;;--------------------------------------------------------------------------
181 ;;; Parsing property sets.
183 (export 'parse-property)
184 (defun parse-property (scanner pset)
185 "Parse a single property using the SCANNER; add it to the PSET."
186 ;; property ::= id `=' expression
187 (with-parser-context (token-scanner-context :scanner scanner)
188 (parse (seq ((name :id) #\= (result (parse-expression scanner)))
189 (let ((type (car result))
190 (value (cdr result)))
191 (unless (eq type :invalid)
192 (add-property pset name value
194 :location scanner)))))))
196 (export 'parse-property-set)
197 (defun parse-property-set (scanner)
198 "Parse an optional property set from the SCANNER and return it."
199 ;; property-set ::= [`[' property-list `]']
200 (with-parser-context (token-scanner-context :scanner scanner)
202 (pset (many (pset (make-property-set) pset)
204 (parse-property scanner pset)
205 (skip-until () #\, #\]))
210 ;;;----- That's all, folks --------------------------------------------------