(in-package #:parser) (defun run (p input) (let ((result (funcall p input))) (if (parsing-p result) (parsing-tree result) result))) (defstruct parsing tree left) (defstruct failure place message) (defmethod print-object ((obj failure) stream) (let ((file (input:file (failure-place obj)))) (if file (multiple-value-bind (line column) (input:line-and-column (failure-place obj)) (format stream "~a:~a:~a: ~a" line column file (failure-message obj))) (format stream "~a: ~a" (input:cursor (failure-place obj)) (failure-message obj))))) (defun new (tree) (lambda (input) (make-parsing :tree tree :left input))) (defun bind-with-input (p f) (declare (optimize (speed 3))) (declare (type function p f)) (lambda (input) (let ((r (funcall p input))) (if (parsing-p r) (funcall (the function (funcall f (parsing-tree r) input)) (parsing-left r)) r)))) (defun bind (p f) (declare (optimize (speed 3))) (declare (type function p f)) (lambda (input) (let ((r (funcall p input))) (if (parsing-p r) (funcall (the function (funcall f (parsing-tree r))) (parsing-left r)) r)))) (defun discarding-bind (p f) (declare (optimize (speed 3))) (declare (type function p f)) (lambda (input) (let ((r (funcall p input))) (if (parsing-p r) (funcall (the function (funcall f)) (parsing-left r)) r)))) (defmacro comp (bindings &body body) (if (null bindings) `(new (progn ,@body)) (let ((v (first (car bindings))) (p (second (car bindings)))) (if (eq 'symbol (type-of v)) (if (string= (symbol-name v) "_") `(discarding-bind ,p (lambda () (comp ,(cdr bindings) ,@body))) `(bind ,p (lambda (,v) (comp ,(cdr bindings) ,@body)))) (if (and (eq 'cons (type-of v)) (eq 'symbol (type-of (car v))) (eq 'symbol (type-of (cdr v)))) `(bind-with-input ,p (lambda (,(car v) ,(cdr v)) (comp ,(cdr bindings) ,@body))) (error "Binding name/(name,input) must be either a symbol or a cons of symbols.")))))) (defun crit (p) (lambda (input) (let ((r (funcall p input))) (if (parsing-p r) r (error (format nil "~a" r)))))) (defun one-of (first-parser second-parser &rest other-parsers) (lambda (input) (labels ((one-of-rec (parsers failures) (if (car parsers) (let ((r (funcall (car parsers) input))) (cond ((failure-p r) (one-of-rec (cdr parsers) (cons r failures))) ((listp r) (one-of-rec (cdr parsers) (append r failures))) (t r))) failures))) (one-of-rec (cons first-parser (cons second-parser other-parsers)) nil)))) (defmacro unit (&optional predicate) (cond ((null predicate) (setf predicate '(characterp it))) ((symbolp predicate) (setf predicate `(,predicate it))) ((characterp predicate) (setf predicate `(char-equal ,predicate it))) (t (setf predicate (nsubst-if 'it (lambda (x) (and (symbolp x) (string-equal (symbol-name x) "IT"))) predicate)))) `(lambda (input) (if (input:has-data? input) (let ((it (input:peek input))) (if ,predicate (make-parsing :tree it :left (input:advance input)) (make-failure :place input :message (format nil "Expected: ~a, Got: ~a" ',predicate it)))) (make-failure :place input :message "Reached end of input.")))) (defun literal (target) (lambda (input) (if (input:has-data? input) (if (input:prefix? target input) (make-parsing :tree target :left (input:advance input (length target))) (make-failure :place input :message "Predicate not satisfied.")) (make-failure :place input :message "Reached end of input.")))) (defparameter nothing (new nil)) (defun optional (p) (one-of p nothing)) (defun many (p) (comp ((x p) (xs (optional (many p)))) (cons x xs))) (defparameter whitespace (comp ((_ (optional (many (unit char:whitespace?))))) nil)) (defun separated-list (p separator &key include-separator) (comp ((v p) (sep (optional separator)) (vn (if sep (crit (separated-list p separator)) nothing))) (if include-separator (cons v (cons sep vn)) (cons v vn))))