From 0c7b0d917d8da0c5e56b359974e7a0e0068617c7 Mon Sep 17 00:00:00 2001 From: Utz-Uwe Haus Date: Tue, 7 Oct 2008 10:11:05 +0200 Subject: [PATCH] Code cleanup againt SBCL, CMUCL and ECL. This uncovered a CMUCL bug when (safety 3) (debug 3) is set on pegutils.lisp. Signed-off-by: Utz-Uwe Haus --- .gitignore | 6 ++++- README | 3 +++ entrypoints.lisp | 12 ++++++--- opossum.asd | 2 +- package.lisp | 2 +- pegutils.lisp | 82 +++++++++++++++++++++++++++++++------------------------- 6 files changed, 64 insertions(+), 43 deletions(-) diff --git a/.gitignore b/.gitignore index 19bfbae..615cd17 100644 --- a/.gitignore +++ b/.gitignore @@ -1,8 +1,12 @@ *.elc -*~ *.fasla16 +*.fasl +*.x86f +*.x86f.BAK +*~ /metapeg /opossum.asd~ /peg-mode.elc /pegparser.lisp /pegutils.lisp~ +/web diff --git a/README b/README index e7399cc..ebc6314 100644 --- a/README +++ b/README @@ -17,5 +17,8 @@ in your own lisp code. Documentation is automatically generated from the docstrings in html format using David Lichteblau's atdoc package, with minor modifications. +Developement is done on Allegro Common Lisp, but the code is also tested against +SBCL, CMUCL and ECL. + $Id$ diff --git a/entrypoints.lisp b/entrypoints.lisp index a612595..4aae43c 100644 --- a/entrypoints.lisp +++ b/entrypoints.lisp @@ -58,11 +58,15 @@ (get-string-parser (apply #'opossum:generate-parser-package grammarfile args))) -(defun make-file-parser (grammarfile head &key (dst-package (make-default-dst-package grammarfile))) +(defun make-file-parser (grammarfile start-rule &key (dst-package (make-default-dst-package grammarfile))) "Return a function of 1 argument, a file, that parses according to GRAMMARFILE, starting at rule named HEAD. The parser is instantiated in package DST-PACKAGE." - (get-file-parser (opossum:generate-parser-package grammarfile dst-package head))) + (get-file-parser (opossum:generate-parser-package grammarfile + :dst-package dst-package + :start-rule start-rule))) -(defun make-stream-parser (grammarfile head &key (dst-package (make-default-dst-package grammarfile))) +(defun make-stream-parser (grammarfile start-rule &key (dst-package (make-default-dst-package grammarfile))) "Return a function of 1 argument, a stream, that parses according to GRAMMARFILE, starting at rule named HEAD. The parser is instantiated in package DST-PACKAGE." - (get-stream-parser (opossum:generate-parser-package grammarfile dst-package head))) + (get-stream-parser (opossum:generate-parser-package grammarfile + :dst-package dst-package + :start-rule start-rule))) diff --git a/opossum.asd b/opossum.asd index 1cb1044..9c3dd54 100644 --- a/opossum.asd +++ b/opossum.asd @@ -40,7 +40,7 @@ ;; bootstrapped-file. (defclass bootstrapped-file (cl-source-file) () - (:documentation)) + (:documentation "")) ;; and use it to (defsystem #:opossum diff --git a/package.lisp b/package.lisp index 044af6f..d191522 100644 --- a/package.lisp +++ b/package.lisp @@ -36,7 +36,7 @@ ;; types (:export #:context) ;; special variables - (:export #:*context*) + (:export #:*context* #:*trace*) ;; low-level user interface (:export #:generate-parser-file #:generate-parser-package #:get-string-parser #:get-file-parser #:get-stream-parser diff --git a/pegutils.lisp b/pegutils.lisp index 9ff4272..2dfff5a 100644 --- a/pegutils.lisp +++ b/pegutils.lisp @@ -30,7 +30,10 @@ (eval-when (:load-toplevel :compile-toplevel :execute) - (declaim (optimize (speed 0) (safety 3) (debug 3)))) + (declaim (optimize (speed 0) + #+cmu (safety 2) + #-cmu (safety 3) + (debug 3)))) (in-package #:opossum) @@ -40,20 +43,20 @@ (defclass context () (;; these slots are copied when cloning a context for recursion (input :accessor input :initarg :input :initform nil - :type 'string + :type string :documentation "The input string being parsed.") (dst-package :accessor dst-package :initarg :dst-package :initform nil - :type 'package + :type package :documentation "The package into which symbols generated during the parse are interned.") (memotab :accessor memotab :initarg :memotab :initform (make-hash-table :test #'equal) - :type 'hash-table + :type hash-table :documentation "Hash-table used to memoize parsing result. Keyed on (fun . offset) pairs.") ;; these slots are shared by all cloned copies of a context -- use only STORE-ACTION to guarantee consistency (actions :accessor actions :initarg :actions :initform (make-list 1 :initial-element NIL) - :type 'list + :type list :documentation "The list of actions accumulated during the parse.") (action-counter :accessor action-counter :initarg :action-counter :initform '(0) - :type '(cons (integer 0) null) + :type (cons (integer 0) null) :documentation "The counter of actions.") ;; these slots are what make a context unique (parent :accessor parent :initarg :parent :initform nil @@ -407,8 +410,10 @@ returns the result of that function, or a failure context if none succeeded." (read-stream f))))) (defun make-default-dst-package (grammarfile) - (make-package (gensym "opossum-parser") - :documentation (format T "Opossum parser for grammar ~A" grammarfile))) + (let ((pkg (make-package (gensym "opossum-parser")))) + (setf (documentation pkg 'cl:package) + (format T "Opossum parser for grammar ~A" grammarfile)) + pkg)) (defun get-iso-time () "Return a string in ISO format for the current time" @@ -438,10 +443,10 @@ returns the result of that function, or a failure context if none succeeded." ,res) (T ,res))))) -(defun generate-parser-file (grammarfile dst-package dst-file &key start-rule (parse-file-fun #'opossum:parse-file)) +(defun generate-parser-file (grammarfile dst-package dst-file &key start-rule (parse-file-fun (symbol-function 'opossum:parse-file))) "Create lisp code in DST-FILE that can be loaded to yield functions to parse using GRAMMARFILE in DST-PACKAGE. DST-PACKAGE will contain the 3 functions PARSE-STRING, PARSE-FILE and PARSE-STREAM as exported entrypoints." - (let* ((*package* dst-package) + (let* ((*package* (find-package dst-package)) (result (checking-parse grammarfile parse-file-fun)) ;; FIXME: check for complete parse (*context* result) ;; routines in pegutils.lisp expect *context* to be bound properly @@ -474,22 +479,22 @@ DST-PACKAGE will contain the 3 functions PARSE-STRING, PARSE-FILE and PARSE-STRE (format *trace-output* "Inserting definitions for parser entry points through ~A~%" entryrule)) (terpri s) - (prin1 `(defun parse-string (,(intern :s dst-package) dst-package) + (prin1 `(defun parse-string (,(intern "s" dst-package) dst-package) ,(format nil "Parse S using grammar ~A starting at ~A" grammarfile entryrule) (let ((*context* (make-instance 'opossum:context :dst-package dst-package - :input ,(intern :s dst-package)))) + :input ,(intern "s" dst-package)))) (funcall (,entryrule) 0))) s) (terpri s) - (prin1 `(defun parse-file (,(intern :f dst-package) dst-package) + (prin1 `(defun parse-file (,(intern "f" dst-package) dst-package) ,(format nil "Parse file F using grammar ~A starting at ~A" grammarfile entryrule) - (parse-string (opossum:read-file ,(intern :f dst-package)) dst-package)) + (parse-string (opossum:read-file ,(intern "f" dst-package)) dst-package)) s) (terpri s) - (prin1 `(defun parse-stream (,(intern :stream dst-package) dst-package) + (prin1 `(defun parse-stream (,(intern "stream" dst-package) dst-package) ,(format nil "Parse stream F using grammar ~A starting at ~A" grammarfile entryrule) - (parse-string (opossum:read-stream ,(intern :stream dst-package)) dst-package)) + (parse-string (opossum:read-stream ,(intern "stream" dst-package)) dst-package)) s) (fresh-line s)))) (loop :for aform :in forms @@ -512,7 +517,7 @@ DST-PACKAGE will contain the 3 functions PARSE-STRING, PARSE-FILE and PARSE-STRE sym (cleanup-action-code code)))))))) (defun generate-parser-package (grammarfile &key (dst-package (make-package (gensym "opossum-parser-"))) - start-rule (parse-file-fun #'opossum:parse-file)) + start-rule (parse-file-fun (symbol-function 'opossum:parse-file))) "Create functions to parse using GRAMMARFILE in DST-PACKAGE, starting ar rule named HEAD. DST-PACKAGE will contain the 3 functions PARSE-STRING, PARSE-FILE and PARSE-STREAM as exported entrypoints." (let* ((*package* dst-package) @@ -532,27 +537,30 @@ DST-PACKAGE will contain the 3 functions PARSE-STRING, PARSE-FILE and PARSE-STRE (when *trace* (format *trace-output* "Inserting definitions for parser entry points through ~A~%" entryrule)) - (intern (compile 'parse-string - `(lambda (,(intern :s dst-package)) - ,(format nil "Parse S using grammar ~A starting at ~A" grammarfile entryrule) - (let ((*context* (make-instance 'opossum:context - :dst-package ,dst-package - :input ,(intern :s dst-package)))) - (funcall (,entryrule) 0)))) + (intern (symbol-name + (compile 'parse-string + `(lambda (,(intern "s" dst-package)) + ,(format nil "Parse S using grammar ~A starting at ~A" grammarfile entryrule) + (let ((*context* (make-instance 'opossum:context + :dst-package ,dst-package + :input ,(intern "s" dst-package)))) + (funcall (,entryrule) 0))))) dst-package) - (intern (compile 'parse-file - `(lambda (,(intern :f dst-package)) - ,(format nil "Parse file F using grammar ~A starting at ~A" grammarfile entryrule) - (parse-string (opossum:read-file ,(intern :f dst-package))))) + (intern (symbol-name + (compile 'parse-file + `(lambda (,(intern "f" dst-package)) + ,(format nil "Parse file F using grammar ~A starting at ~A" grammarfile entryrule) + (parse-string (opossum:read-file ,(intern "f" dst-package)))))) dst-package) - (intern (compile 'parse-stream - `(lambda (,(intern :stream dst-package)) - ,(format nil "Parse stream F using grammar ~A starting at ~A" grammarfile entryrule) - (parse-string (opossum:read-stream ,(intern :stream dst-package))))) + (intern (symbol-name + (compile 'parse-stream + `(lambda (,(intern "stream" dst-package)) + ,(format nil "Parse stream F using grammar ~A starting at ~A" grammarfile entryrule) + (parse-string (opossum:read-stream ,(intern "stream" dst-package)))))) dst-package)))) - (intern '*trace* dst-package) - (setf (documentation '*trace* 'cl:variable) + (intern "*TRACE*" dst-package) + (setf (documentation (find-symbol "*TRACE*" dst-package) 'cl:variable) "When non-nil, the generated parser function log to cl:*trace-output*.") (export '(:parse-string :parse-file :parse-stream :*trace*) dst-package) @@ -562,11 +570,13 @@ DST-PACKAGE will contain the 3 functions PARSE-STRING, PARSE-FILE and PARSE-STRE :do (destructuring-bind (defun-sym name args &rest body) aform (declare (ignore defun-sym)) - (intern (compile name `(lambda ,args ,@body)) dst-package))) + (intern (symbol-name + (compile name `(lambda ,args ,@body))) dst-package))) (loop :for (sym code) :in actions :when sym :do (when *trace* (format *trace-output* "Injecting definition for ~A~%" sym)) - :and :do (intern (compile sym `(lambda (data) (declare (ignorable data)) ,code)) dst-package))))) + :and :do (intern (symbol-name + (compile sym `(lambda (data) (declare (ignorable data)) ,code))) dst-package))))) -- 2.11.4.GIT