summaryrefslogtreecommitdiff
path: root/test.lisp
blob: b0ffdc213bc571c905cea3eba4aa7f6bece831cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
(in-package #:monparser)

(defmacro deftest (title &body tests)
  (let ((result '(progn)))
    (push `(format t "~a:~&" ,title) result)
    (dolist (test tests)
      (let ((parser (first test))
            (input (second test)))
        (push `(format t "~a~&" (parse ,parser ,input)) result)))
    (push '(format t "~%") result)
    (reverse result)))

(defun test ()
  (deftest "unit"
           ((unit) "hello")
           ((unit #\h) "hello"))
  (deftest "many"
           ((many (unit #\h)) "hello")
           ((many (unit #\h)) "hhhhhhhello")
           ((many (unit #\h)) "ello"))
  (deftest "optional many"
           ((optional (many (unit #\h))) "hello")
           ((optional (many (unit #\h))) "hhhhhhhello")
           ((optional (many (unit #\h))) "ello"))
  (deftest "many optional"
           ((many (optional (unit #\h))) "hello")
           ((many (optional (unit #\h))) "hhhhhhhello")
           ((many (optional (unit #\h))) "ello"))
  (deftest "until literal"
           ((comp ((prefix (optional (literal "zy")))
                   (match (if prefix
                            (fail "Reached prefix")
                            (unit))))
              match)
            "zy")
           ((comp (((prefix) (optional (literal "zy")))
                   ((match) (if (parsing-tree prefix)
                              (fail "Reached prefix")
                              (progn
                                (format t "prefix start: ~a, end: ~a~&"
                                        (parsing-start prefix)
                                        (parsing-end prefix))
                                (unit)))))
              (format t "match start: ~a, end: ~a~&"
                      (parsing-start match)
                      (parsing-end match))
              match)
            "ezy")
           ((many (comp ((prefix (optional (literal "zy")))
                         (match (if prefix
                                  (fail "Reached prefix")
                                  (unit))))
                    match))
            "the quick brown fox jumps over the lazy dog.")
           ((optional
              (many
                (comp ((prefix (optional (literal "zy")))
                       (match (if prefix
                                (fail "Reached prefix")
                                (unit))))
                  match)))
            "the quick brown fox jumps over the lazy dog.")))