3 ;;; Parsing property sets
5 ;;; (c) 2012 Straylight/Edgeware
8 ;;;----- Licensing notice ---------------------------------------------------
10 ;;; This file is part of the Sensble 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 (flet ((dispatch (name args &rest spec)
32 (acond ((find :invalid args :key #'car)
34 ((find-if (lambda (item)
35 (every (lambda (type arg)
40 (cons (car it) (apply (cadr it)
41 (mapcar #'cdr args))))
43 (cerror* "Type mismatch: operator `~A' applied to ~
44 types ~{~(~A~)~#[~; and ~;, ~]~}"
47 (cons :invalid nil)))))
48 (let ((add (binop "+" (x y 5)
49 (dispatch "+" (list x y) (list :int #'+ :int :int))))
50 (sub (binop "-" (x y 5)
51 (dispatch "-" (list x y) (list :int #'- :int :int))))
52 (mul (binop "*" (x y 7)
53 (dispatch "*" (list x y) (list :int #'* :int :int))))
54 (div (binop "/" (x y 7)
55 (dispatch "/" (list x y)
66 (dispatch "+" (list x) (list :int #'+ :int))))
68 (dispatch "-" (list x) (list :int #'- :int))))
72 (defun parse-expression (scanner)
73 "Parse and evaluate a simple expression.
75 The result is a pair (TYPE . VALUE). Currently, type types are `:id',
76 `:int', `:string', and `:char'. If an error prevented a sane value from
77 being produced, the type `:invalid' is returned.
79 The syntax of expressions is rather limited at the moment, but more may be
82 expression: term | expression `+' term | expression `-' term
83 term: factor | term `*' factor | term `/' factor
84 factor: primary | `+' factor | `-' factor
85 primary: int | id | string | `(' expression `)' | `{' fragment `}'
88 Only operators for dealing with integers are provided."
89 (with-parser-context (token-scanner-context :scanner scanner)
90 (parse (expr (:nestedp nestedp)
91 (lisp (flet ((prop (type value)
92 (scanner-step scanner)
93 (values (cons type value) t t)))
94 (case (token-type scanner)
95 ((:int :id :char :string)
96 (prop (token-type scanner)
97 (token-value scanner)))
99 (let* ((stream (make-scanner-stream scanner))
100 (sexp (read stream t)))
101 (scanner-step scanner)
102 (multiple-value-bind (type value)
103 (decode-property sexp)
104 (values (cons type value) t t))))
106 (values (cons :fragment
107 (parse-delimited-fragment scanner
111 (values (list :int :id :char :string #\?)
120 (when nestedp (seq (#\)) rp))))))))
122 ;;;--------------------------------------------------------------------------
123 ;;; Parsing property sets.
125 (defun parse-property (scanner pset)
126 "Parse a single property using the SCANNER; add it to the PSET."
127 ;; property ::= id `=' expression
128 (with-parser-context (token-scanner-context :scanner scanner)
129 (parse (seq ((name :id) #\= (result (parse-expression scanner)))
130 (let ((type (car result))
131 (value (cdr result)))
132 (unless (eq type :invalid)
133 (add-property pset name value
135 :location scanner)))))))
137 (export 'parse-property-set)
138 (defun parse-property-set (scanner)
139 "Parse an optional property set from the SCANNER and return it."
140 ;; property-set ::= [`[' property-list `]']
141 (with-parser-context (token-scanner-context :scanner scanner)
143 (pset (many (pset (make-property-set) pset)
145 (parse-property scanner pset)
146 (skip-until () #\, #\]))
151 ;;;----- That's all, folks --------------------------------------------------