summaryrefslogtreecommitdiff
path: root/parser.lisp
diff options
context:
space:
mode:
Diffstat (limited to 'parser.lisp')
-rw-r--r--parser.lisp55
1 files changed, 29 insertions, 26 deletions
diff --git a/parser.lisp b/parser.lisp
index 82f619e..ca13598 100644
--- a/parser.lisp
+++ b/parser.lisp
@@ -1,7 +1,13 @@
-(in-package #:parser)
+(in-package #:monparser)
-(defun run (p input)
- (let ((result (funcall p input)))
+(defun parse-string (p input)
+ (let ((result (funcall p (from-string input))))
+ (if (parsing-p result)
+ (parsing-tree result)
+ result)))
+
+(defun parse-file (p input)
+ (let ((result (funcall p (from-file input))))
(if (parsing-p result)
(parsing-tree result)
result)))
@@ -15,19 +21,17 @@
message)
(defmethod print-object ((obj failure) stream)
- (let ((file (input:file (failure-place obj))))
+ (let ((file (file (failure-place obj))))
(if file
- (multiple-value-bind (line column) (input:line-and-column (failure-place obj))
+ (multiple-value-bind (line column) (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)))))
+ (format stream "~a: ~a" (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))
- (function p f))
(lambda (input)
(let ((r (funcall p input)))
(if (parsing-p r)
@@ -36,8 +40,6 @@
r))))
(defun bind (p f)
- (declare (optimize (speed 3))
- (function p f))
(lambda (input)
(let ((r (funcall p input)))
(if (parsing-p r)
@@ -46,8 +48,6 @@
r))))
(defun discarding-bind (p f)
- (declare (optimize (speed 3))
- (function p f))
(lambda (input)
(let ((r (funcall p input)))
(if (parsing-p r)
@@ -103,19 +103,19 @@
(and (symbolp x)
(string-equal (symbol-name x) "IT"))) predicate))))
`(lambda (input)
- (if (input:has-data? input)
- (let ((it (input:peek input)))
+ (if (has-data? input)
+ (let ((it (peek input)))
(if ,predicate
- (make-parsing :tree it :left (input:advance input))
+ (make-parsing :tree it :left (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)))
+ (if (has-data? input)
+ (if (prefix? target input)
+ (make-parsing :tree target :left (advance input (length target)))
(make-failure :place input :message "Predicate not satisfied."))
(make-failure :place input :message "Reached end of input."))))
@@ -128,22 +128,25 @@
(defun many (p)
(comp ((x p)
(xs (optional (many p))))
- (cons x xs)))
+ (cons x xs)))
(defun repeat (p min &optional (max 0))
(if (> min 0)
(comp ((x p)
(xs (repeat p (1- min) (1- max))))
- (cons x xs))
+ (cons x xs))
(if (> max 0)
(comp ((x (optional p))
(xs (repeat p 0 (if x (1- max) 0))))
- (if x (cons x xs) x))
+ (if x (cons x xs) x))
nothing)))
+(defun whitespace? (x)
+ (some (lambda (y) (char= x y)) '(#\Space #\Newline #\Tab)))
+
(defparameter whitespace
- (comp ((_ (optional (many (unit char:whitespace?)))))
- nil))
+ (comp ((_ (optional (many (unit whitespace?)))))
+ nil))
(defun separated-list (p separator &key include-separator)
(comp ((v p)
@@ -151,6 +154,6 @@
(vn (if sep
(crit (separated-list p separator))
nothing)))
- (if include-separator
- (cons v (cons sep vn))
- (cons v vn))))
+ (if include-separator
+ (cons v (cons sep vn))
+ (cons v vn))))