aboutsummaryrefslogtreecommitdiff
path: root/tests/parse.scm
blob: d8d359433412bb58b1445e5c1fe0dcab71c414b9 (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
63
64
65
66
;;; kolam --- GraphQL implementation
;;; Copyright © 2022 Arun Isaac <arunisaac@systemreboot.net>
;;;
;;; This file is part of kolam.
;;;
;;; kolam is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU Affero General Public License as
;;; published by the Free Software Foundation; either version 3 of the
;;; License, or (at your option) any later version.
;;;
;;; kolam 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
;;; Affero General Public License for more details.
;;;
;;; You should have received a copy of the GNU Affero General Public
;;; License along with kolam.  If not, see
;;; <http://www.gnu.org/licenses/>.

(import (srfi srfi-64))

(define token-pattern
  (@@ (kolam parse) token-pattern))

(define token-value-pattern
  (@@ (kolam parse) token-value-pattern))

(define optional?
  (@@ (kolam parse) optional?))

(eval-when (expand load eval)
  (define (macro-ref module symbol)
    (macro-transformer
     (module-ref (resolve-module module) symbol))))

(define-syntax pattern-many
  (macro-ref '(kolam parse) 'pattern-many))

(define-syntax pattern-optional
  (macro-ref '(kolam parse) 'pattern-optional))

(define-syntax pattern-and
  (macro-ref '(kolam parse) 'pattern-and))

(define-syntax pattern-or
  (macro-ref '(kolam parse) 'pattern-or))

(test-begin "parse")

(test-assert "pattern-and should not fail when matching an optional pattern on an empty list of tokens"
  ((pattern-and identity
                (pattern-optional (token-pattern 'foo)))
   (list)))

(test-assert "pattern-or should not fail when matching an optional pattern on an empty list of tokens"
  ((pattern-or identity
               (pattern-optional (token-pattern 'foo)))
   (list)))

(test-equal "pattern-many should return an empty list when there are no matches"
  (list)
  ((pattern-many identity
                 (token-pattern 'foo))
   (list (cons 'bar 1))))

(test-end "parse")