;;; -*-lisp-*- ;;; ;;; Parsing property sets ;;; ;;; (c) 2012 Straylight/Edgeware ;;; ;;;----- Licensing notice --------------------------------------------------- ;;; ;;; 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 ;;; the Free Software Foundation; either version 2 of the License, or ;;; (at your option) any later version. ;;; ;;; SOD is distributed in the hope that it will be useful, ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;;; GNU General Public License for more details. ;;; ;;; You should have received a copy of the GNU General Public License ;;; along with SOD; if not, write to the Free Software Foundation, ;;; Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. (in-package #:sod) ;;;-------------------------------------------------------------------------- ;;; The expression parser. (defun parse-expression (scanner) "Parse and evaluate a simple expression. The result is a pair (TYPE . VALUE). Currently, type types are `:id', `:int', `:string', `:char', `:fragment', `:type'. If an error prevented a sane value from being produced, the type `:invalid' is returned. The syntax of expressions is rather limited at the moment, but more may be added later. expression: term | expression `+' term | expression `-' term term: factor | term `*' factor | term `/' factor factor: primary | `+' factor | `-' factor primary: int | id | string | `(' expression `)' | `{' fragment `}' | `<' declspec+ declarator[empty] `>' | `?' lisp-expression Only operators for dealing with integers are provided." ;; The expression parser works in two stages. First, the parser proper ;; builds a thunk as its `value'. If this is successful, then the thunk is ;; invoked to return a property type and value. Primitive expressions ;; produce thunks which just return their values; operators combine their ;; argument thunks together, evaluating them (or not) on demand. (macrolet ((oplambda (&body body) ;; Like `lambda', but (a) always produces a function with no ;; arguments, and (b) captures the current location so that ;; errors are attributed correctly. (with-gensyms (floc) `(let ((,floc (file-location scanner))) (lambda () (with-default-error-location (,floc) ,@body)))))) (labels ((want (type thunk) ;; Evaluate THUNK and convert its result to the given TYPE. (multiple-value-bind (ty val) (funcall thunk) (coerce-property-value val ty type))) (int-unop (intop x) ;; Evaluate X to an integer, and apply INTOP to the result, ;; giving another integer. (oplambda (values :int (funcall intop (want :int x))))) (int-binop (intop x y) ;; Evaluate X and Y to integers, and apply INTOP to the ;; results, giving another integer. (oplambda (values :int (funcall intop (want :int x) (want :int y))))) (compareop (intop strop x y) ;; Evaluate X and Y. If they're integers, then apply INTOP to ;; them; if they're strings, apply STROP. The result is a ;; boolean. (oplambda (multiple-value-bind (xty xval) (funcall x) (case xty (:int (values :boolean (funcall intop xval (want :int y)))) ((:id :string :symbol) (values :boolean (funcall strop (coerce-property-value xval xty :id) (want :id y)))) (t (error "Can't compare objects of type ~(~A~)" xty))))))) (with-parser-context (token-scanner-context :scanner scanner) (when-parse () ;; Parse the expression, producing a thunk. (expr (:nestedp nestedp) (lisp (case (token-type scanner) ((:int :id :char :string) ;; A simple literal. (let ((type (token-type scanner)) (value (token-value scanner))) (scanner-step scanner) (values (lambda () (values type value)) t t))) (#\? ;; A Lisp s-expression. Catch and report reader- ;; errors (though the main parser will probably ;; end up /very/ confused); delay evaluation for ;; later. (handler-case (let* ((stream (make-scanner-stream scanner)) (sexp (read stream t))) (scanner-step scanner) (values (oplambda (decode-property (eval sexp))) t t)) (error (cond) (scanner-step scanner) (cerror*-with-location scanner "Lisp `read' error: ~A" cond) (values #'continue t t)))) (#\{ ;; A code fragment. (let ((fragment (parse-delimited-fragment scanner #\{ #\}))) (values (lambda () (values :fragment fragment)) t t))) (#\< ;; A C type. (parse (seq (#\< (ds (parse-c-type scanner)) (dc (parse-declarator scanner ds :kernel (lambda () (values nil t nil)) :abstractp t)) #\>) (values (lambda () (values :type (car dc))) t t)))) (t ;; Anything else is an error. (values (list :int :id :char :string #\? #\{ #\<) nil nil)))) ;; Binary operators. ((:op #\* binop "*" (x y 70) (int-binop #'* x y)) (:op #\/ binop "/" (x y 70) (oplambda (let ((x (want :int x)) (y (want :int y))) (when (zerop y) (error "Division by zero")) (values :int (floor x y))))) (:op #\+ binop "+" (x y 60) (int-binop #'+ x y)) (:op #\- binop "-" (x y 60) (int-binop #'- x y)) (:op :shl binop "<<" (x y 50) (int-binop #'ash x y)) (:op :shr binop ">>" (x y 50) (int-binop (lambda (x y) (ash x (- y))) x y)) (:op #\< binop "<" (x y 45) (compareop #'< #'string< x y)) (:op :le binop "<=" (x y 45) (compareop #'<= #'string<= x y)) (:op :ge binop ">=" (x y 45) (compareop #'>= #'string>= x y)) (:op #\> binop ">" (x y 45) (compareop #'> #'string> x y)) (:op :eq binop "==" (x y 40) (compareop #'= #'string= x y)) (:op :ne binop "!=" (x y 40) (compareop #'/= #'string/= x y)) (:op #\& binop "&" (x y 37) (int-binop #'logand x y)) (:op #\^ binop "^" (x y 35) (int-binop #'logxor x y)) (:op #\| binop "|" (x y 32) (int-binop #'logior x y)) (:op :and binop "&&" (x y 27) (oplambda (if (want :boolean x) (funcall y) (values :boolean nil)))) (:op :or binop "||" (x y 22) (oplambda (multiple-value-bind (xty xval) (funcall x) (if (coerce-property-value xval xty :boolean) (values xty xval) (funcall y)))))) ;; Prefix operators. ((:op #\~ preop "~" (x 90) (int-unop #'lognot x)) (:op #\! preop "!" (x 90) (oplambda (values :boolean (not (want :boolean (funcall x)))))) (:op #\+ preop "+" (x 90) (int-unop #'identity x)) (:op #\- preop "-" (x 90) (int-unop #'- x)) (:op #\( lparen #\))) ;; Postfix operators. ((:op (when nestedp #\)) rparen #\)))) ;; Do the delayed evaluation. Establish a restart so that we can ;; continue if evaluation fails for some reason. (The value thunk ;; is expected to report the correct error locations, if it signals ;; conditions.) (restart-case (multiple-value-bind (type value) (funcall it) (values (cons type value) t t)) (continue () (values (cons :invalid nil) t t)))))))) ;;;-------------------------------------------------------------------------- ;;; Parsing property sets. (export 'parse-property) (defun parse-property (scanner pset) "Parse a single property using the SCANNER; add it to the PSET." ;; property ::= id `=' expression (with-parser-context (token-scanner-context :scanner scanner) (parse (seq ((name :id) #\= (result (parse-expression scanner))) (let ((type (car result)) (value (cdr result))) (unless (eq type :invalid) (add-property pset name value :type type :location scanner))))))) (export 'parse-property-set) (defun parse-property-set (scanner) "Parse an optional property set from the SCANNER and return it." ;; property-set ::= [`[' property-list `]'] (with-parser-context (token-scanner-context :scanner scanner) (parse (? (seq (#\[ (pset (many (pset (make-property-set) pset) (error () (parse-property scanner pset) (skip-until () #\, #\])) #\,)) #\]) pset))))) ;;;----- That's all, folks --------------------------------------------------