Skip to content

Commit 9c34c14

Browse files
committed
add tests for "zero-or-more" patterns; lots of very small tweaks
1 parent 91266fc commit 9c34c14

File tree

7 files changed

+166
-71
lines changed

7 files changed

+166
-71
lines changed

src/analysis.lisp

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,12 @@
9292
(defmethod match (pattern (state state) &key skipp)
9393
(match-parser-state pattern state :skipp skipp))
9494

95+
(defmethod match ((pattern null) (state state) &key skipp)
96+
"Nil should not match a parse-tree (use an empty vector as pattern to
97+
match an empty parse tree."
98+
(declare (ignore skipp))
99+
nil)
100+
95101
(defmethod match ((pattern symbol) (state state) &key skipp)
96102
(declare (ignore skipp))
97103
;; These should return nil because we're trying to match 1 symbol
@@ -151,24 +157,25 @@
151157
(unless (donep node-iterator)
152158
(breeze.pattern::make-binding pattern (copy-iterator node-iterator))))
153159

154-
(defmethod match ((pattern repetition) (node-iterator node-iterator) &key skipp)
155-
(when skipp (breeze.pattern::skip node-iterator skipp))
156-
(unless (donep node-iterator)
160+
(defmethod match ((pattern repetition) (iterator node-iterator) &key skipp)
161+
(when skipp (breeze.pattern::skip iterator skipp))
162+
(unless (donep iterator)
157163
(loop
158-
:with bindings := t ;; (make-binding-set)
164+
;; :with bindings := t ;; (make-binding-set)
159165
:with $pattern := (make-pattern-iterator
160166
(repetition-pattern pattern))
161167
;; TODO update node-iterator on match
162-
:with $input := (copy-iterator node-iterator)
168+
:with $input := (copy-iterator iterator)
163169
:for $prev-input := (copy-iterator $input)
164-
:then (copy-iterator $input $prev-input)
170+
:then (copy-iterator $input $prev-input)
165171
:for i :from 0 :below 100 ;; TODO removve infinite loop guard
166172

167173
:for new-bindings = (progn
168174
(reset $pattern)
169175
(match $pattern $input :skipp skipp))
176+
:when new-bindings
177+
:collect new-bindings :into bindings
170178
:do
171-
;;(break)
172179
;; No more input or, no match
173180
(when (or
174181
;; no more input
@@ -183,17 +190,22 @@
183190
;; (break "i: ~s new-bindings: ~s" i new-bindings)
184191
(if (and (zerop i) (not new-bindings))
185192
t
186-
(let (($start (copy-iterator node-iterator))
193+
(let (($start (copy-iterator iterator))
187194
($end
188195
;; if it was a match, include the current position,
189196
;; otherwise stop at the previous one.
190197
(if new-bindings $input $prev-input)))
191-
;; update node-iterator
192-
(copy-iterator $end node-iterator)
193-
#++ (return bindings)
198+
;; update iterator
199+
(copy-iterator $end iterator)
194200
;; TODO return an object (iterator-range? +
195201
;; binding-sets???)
196-
(cons $start $end))))))
202+
(make-binding
203+
pattern
204+
(list
205+
:bindings bindings
206+
:$start $start
207+
:$end $end
208+
:times i)))))))
197209
#++
198210
(when new-bindings
199211
;; collect all the bindings

src/command.lisp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -685,7 +685,7 @@ resulting string to the editor."
685685
(let ((doc (documentation function 'function)))
686686
(unless doc
687687
(error
688-
"Function ~s does not have a documentation string.~
688+
"Function ~s does not have a documentation string. ~
689689
Is it defined?"
690690
function))
691691
doc))

src/completion.lisp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616
;; "quickfix", and it shouldn't generate an interactive command when
1717
(define-command completions-at-point (&optional string)
1818
"completion-at-point"
19+
(declare (ignorable string))
1920
(let* (($node (node-iterator (current-buffer)))
2021
(node (breeze.iterator:value $node)))
22+
(declare (ignorable node))
2123
;; (break "~s" (breeze.lossless-reader:node-string $node))
2224
(return-value-from-command
2325
(list "prin1" "print")

src/iterator.lisp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ generalization of that first iteration (ha!).
6060
#:root-value)
6161
;; Functions for recursive-iterator
6262
(:export
63-
#:go-down)
63+
#:go-down
64+
#:go-up)
6465
;; Other utility functions
6566
(:export
6667
#:firstp
@@ -305,9 +306,9 @@ If APPLY-FILTER-TO-ITERATOR-P is non-nil, the predicate FILTER-IN will be applie
305306

306307
(defmethod print-object ((iterator nested-vector-iterator) stream)
307308
(print-unreadable-object
308-
(iterator stream :type nil :identity t)
309+
(iterator stream :type t :identity t)
309310
(with-slots (depth positions) iterator
310-
(format stream "nested-vector-iterator depth: ~s pos: ~s" depth positions))))
311+
(format stream "depth: ~s pos: ~s" depth positions))))
311312

312313
(defun make-nested-vector-iterator (vector)
313314
"Create a new depth-first iterator on VECTOR."
@@ -496,6 +497,11 @@ If APPLY-FILTER-TO-ITERATOR-P is non-nil, the predicate FILTER-IN will be applie
496497
value-to-dig-in))
497498
t))))
498499

500+
(defmethod go-up ((iterator recursive-iterator))
501+
(when (lastp iterator)
502+
(unless (zerop (slot-value iterator 'depth))
503+
(pop-vector iterator))))
504+
499505

500506

501507
(defclass pre-order-iterator (recursive-iterator) ())

src/pattern.lisp

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
#:repetition-max
1919
#:make-pattern-iterator)
2020
;; Working with match results
21-
(:export #:merge-sets-of-bindings
21+
(:export #:make-binding
22+
#:merge-sets-of-bindings
2223
#:find-binding
2324
#:binding
2425
#:from
@@ -120,6 +121,13 @@
120121
(mb (repetition-max a)))
121122
(or (eq ma mb) (and (numberp ma) (numberp mb)) (= ma mb)))))
122123

124+
(defmethod print-object ((repetition repetition) stream)
125+
(print-unreadable-object
126+
(repetition stream :type t :identity t)
127+
(format stream "[~s-~s]"
128+
(repetition-min repetition)
129+
(repetition-max repetition))))
130+
123131
(defun maybe (pattern)
124132
(repetition pattern 0 1))
125133

@@ -141,6 +149,12 @@
141149
(pattern= (alternation-pattern a)
142150
(alternation-pattern b))))
143151

152+
(defmethod print-object ((alternation alternation) stream)
153+
(print-unreadable-object
154+
(alternation stream :type t :identity t)
155+
;; (format stream "~s" (alternation-something alternation))
156+
))
157+
144158

145159
;;; Pattern comparison
146160

@@ -421,8 +435,8 @@ bindings and keeping only those that have not conflicting bindings."
421435
(make-binding pattern input)))
422436

423437
;; Recurse into a referenced pattern
424-
(defmethod match ((pattern ref) input &key)
425-
(match (ref-pattern pattern) input))
438+
(defmethod match ((pattern ref) input &key skipp)
439+
(match (ref-pattern pattern) input :skipp skipp))
426440

427441
;; Match a string literal
428442
(defmethod match ((pattern string) (input string) &key)
@@ -465,6 +479,8 @@ bindings and keeping only those that have not conflicting bindings."
465479
(funcall skipp $input))
466480
:do (next $input :dont-recurse-p t)))
467481

482+
;; TODO rename $input to iterator; :with $input = copy iterator, copy
483+
;; back if match is successful
468484
(defmethod match (($pattern pattern-iterator) ($input iterator) &key skipp)
469485
(loop
470486
:with bindings = t ;; (make-binding-set)
@@ -548,7 +564,6 @@ bindings and keeping only those that have not conflicting bindings."
548564
(reset $pattern)
549565
(match $pattern $input :skipp skipp))
550566
:do
551-
;;(break)
552567
;; No more input or, no match
553568
(when (or
554569
;; no more input

0 commit comments

Comments
 (0)