- cleaned up expect-1* a bit
- fixed a bug in the expect and expect-action-clause macros
This commit is contained in:
parent
c608b585a5
commit
571e979360
|
@ -229,107 +229,105 @@
|
||||||
(set-task:buf! task
|
(set-task:buf! task
|
||||||
(substring buffer (match:end m) (string-length buffer))))
|
(substring buffer (match:end m) (string-length buffer))))
|
||||||
|
|
||||||
|
(define (handle-input task patterns input do-next monitor)
|
||||||
|
(let ((s (task:buf task)))
|
||||||
|
(let loop ((patterns patterns))
|
||||||
|
(if (null? patterns)
|
||||||
|
(do-next)
|
||||||
|
(let ((pattern (car patterns)))
|
||||||
|
(case (car pattern)
|
||||||
|
((match)
|
||||||
|
(let ((m (string-match ((cadr pattern)) s)))
|
||||||
|
(if m
|
||||||
|
;; Log all new data up to the match.
|
||||||
|
(let* ((delta (- (string-length s)
|
||||||
|
(string-length input)))
|
||||||
|
(mend (- (match:end m 0) delta)))
|
||||||
|
(monitor task (substring input 0 mend))
|
||||||
|
(monitor task m)
|
||||||
|
;; Set the prematch buffer.
|
||||||
|
(set-prematch task s m)
|
||||||
|
((caddr pattern) m))
|
||||||
|
(loop (cdr patterns)))))
|
||||||
|
((eof) (loop (cdr patterns)))
|
||||||
|
((test)
|
||||||
|
(let ((v ((cadr pattern))))
|
||||||
|
(if v
|
||||||
|
((caddr pattern) v)
|
||||||
|
(loop (cdr patterns)))))
|
||||||
|
((else)
|
||||||
|
((cadr pattern)))
|
||||||
|
(else (error "undefined pattern type" (car pattern)))))))))
|
||||||
|
|
||||||
|
(define (handle-eof task patterns do-next monitor)
|
||||||
|
(set-task:pre-match! task (task:buf task))
|
||||||
|
(set-task:buf! task "")
|
||||||
|
(monitor task #f)
|
||||||
|
(let loop ((patterns patterns))
|
||||||
|
(if (null? patterns)
|
||||||
|
(do-next)
|
||||||
|
(let ((pattern (car patterns)))
|
||||||
|
(case (car pattern)
|
||||||
|
((eof) ((cadr pattern)))
|
||||||
|
((match test else) (loop (cdr patterns)))
|
||||||
|
(else (error "undefined pattern type" (car pattern))))))))
|
||||||
|
|
||||||
|
(define (handle-timeout monitor on-timeout)
|
||||||
|
(monitor #f 'timeout)
|
||||||
|
(on-timeout))
|
||||||
|
|
||||||
(define (expect-1* on-timeout monitor timeout echo max-size
|
(define (expect-1* on-timeout monitor timeout echo max-size
|
||||||
tasks-patterns-alist)
|
tasks-patterns-alist)
|
||||||
(let ((handle-input
|
;; first check existing data in the buffers
|
||||||
(lambda (task patterns input do-next)
|
(let loop ((f-tasks-patterns-alist tasks-patterns-alist))
|
||||||
(let ((s (task:buf task)))
|
(if (not (null? f-tasks-patterns-alist))
|
||||||
(let loop ((patterns patterns))
|
(let* ((task-patterns (car f-tasks-patterns-alist))
|
||||||
(if (null? patterns)
|
(task (car task-patterns))
|
||||||
(do-next)
|
(s (task:buf task)))
|
||||||
(let ((pattern (car patterns)))
|
(if (not (zero? (string-length s)))
|
||||||
(case (car pattern)
|
(handle-input task (cdr task-patterns) s
|
||||||
((match)
|
(lambda () (loop (cdr f-tasks-patterns-alist)))
|
||||||
(let ((m (string-match ((cadr pattern)) s)))
|
monitor)
|
||||||
(if m
|
(loop (cdr f-tasks-patterns-alist))))
|
||||||
;; Log all new data up to the match.
|
|
||||||
(let* ((delta (- (string-length s)
|
|
||||||
(string-length input)))
|
|
||||||
(mend (- (match:end m 0) delta)))
|
|
||||||
(monitor task (substring input 0 mend))
|
|
||||||
(monitor task m)
|
|
||||||
;; Set the prematch buffer.
|
|
||||||
(set-prematch task s m)
|
|
||||||
((caddr pattern) m))
|
|
||||||
(loop (cdr patterns)))))
|
|
||||||
((eof) (loop (cdr patterns)))
|
|
||||||
((test)
|
|
||||||
(let ((v ((cadr pattern))))
|
|
||||||
(if v
|
|
||||||
((caddr pattern) v)
|
|
||||||
(loop (cdr patterns)))))
|
|
||||||
((else)
|
|
||||||
((cadr pattern)))
|
|
||||||
(else (error "undefined pattern type" (car pattern)))
|
|
||||||
)))))))
|
|
||||||
(handle-eof
|
|
||||||
(lambda (task patterns do-next)
|
|
||||||
(set-task:pre-match! task (task:buf task))
|
|
||||||
(set-task:buf! task "")
|
|
||||||
(monitor task #f)
|
|
||||||
(let loop ((patterns patterns))
|
|
||||||
(if (null? patterns)
|
|
||||||
(do-next)
|
|
||||||
(let ((pattern (car patterns)))
|
|
||||||
(case (car pattern)
|
|
||||||
((eof) ((cadr pattern)))
|
|
||||||
((match test else) (loop (cdr patterns)))
|
|
||||||
(else (error "undefined pattern type" (car pattern)))
|
|
||||||
))))))
|
|
||||||
(handle-timeout
|
|
||||||
(lambda ()
|
|
||||||
(monitor #f 'timeout)
|
|
||||||
(on-timeout))))
|
|
||||||
|
|
||||||
;; first check existing data in the buffers
|
;; start looking for input, handle it and throw away those
|
||||||
(let loop ((f-tasks-patterns-alist tasks-patterns-alist))
|
;; that got eof
|
||||||
(if (not (null? f-tasks-patterns-alist))
|
(let loop ((tasks-patterns-alist tasks-patterns-alist))
|
||||||
(let* ((task-patterns (car f-tasks-patterns-alist))
|
(if (null? tasks-patterns-alist)
|
||||||
(task (car task-patterns))
|
#f ;; all eof
|
||||||
(s (task:buf task)))
|
(let ((ivec (list->vector (map (lambda (task-patterns)
|
||||||
(if (not (zero? (string-length s)))
|
(task:in (car task-patterns)))
|
||||||
(handle-input task (cdr task-patterns) s
|
tasks-patterns-alist))))
|
||||||
(lambda () (loop (cdr f-tasks-patterns-alist))))
|
;; what is this (time) thing in the original?
|
||||||
(loop (cdr f-tasks-patterns-alist))))
|
(if (not (in-select! ivec timeout))
|
||||||
|
(handle-timeout monitor on-timeout)
|
||||||
;; start looking for input, handle it and throw away those
|
(let iloop ((inports (vector->list ivec))
|
||||||
;; that got eof
|
(tasks-patterns-alist tasks-patterns-alist)
|
||||||
(let loop ((tasks-patterns-alist tasks-patterns-alist))
|
(remaining '()))
|
||||||
(if (null? tasks-patterns-alist)
|
(if (null? tasks-patterns-alist)
|
||||||
#f ;; all eof
|
(loop (reverse remaining))
|
||||||
(let ((ivec (list->vector (map (lambda (task-patterns)
|
(if (car inports)
|
||||||
(task:in (car task-patterns)))
|
(let* ((task-patterns (car tasks-patterns-alist))
|
||||||
tasks-patterns-alist))))
|
(task (car task-patterns))
|
||||||
;; what is this (time) thing in the original?
|
(s (do-input task)))
|
||||||
(if (not (in-select! ivec timeout))
|
(if s
|
||||||
(handle-timeout)
|
(handle-input task (cdr task-patterns) s
|
||||||
(let iloop ((inports (vector->list ivec))
|
(lambda ()
|
||||||
(tasks-patterns-alist tasks-patterns-alist)
|
(iloop (cdr inports)
|
||||||
(remaining '()))
|
(cdr tasks-patterns-alist)
|
||||||
(if (null? tasks-patterns-alist)
|
(cons task-patterns
|
||||||
(loop (reverse remaining))
|
remaining)))
|
||||||
(if (car inports)
|
monitor)
|
||||||
(let* ((task-patterns (car tasks-patterns-alist))
|
(handle-eof task (cdr task-patterns)
|
||||||
(task (car task-patterns))
|
(lambda ()
|
||||||
(s (do-input task)))
|
(iloop (cdr inports)
|
||||||
(if s
|
(cdr tasks-patterns-alist)
|
||||||
(handle-input
|
remaining))
|
||||||
task (cdr task-patterns) s
|
monitor)))
|
||||||
(lambda ()
|
(iloop (cdr inports)
|
||||||
(iloop (cdr inports)
|
(cdr tasks-patterns-alist)
|
||||||
(cdr tasks-patterns-alist)
|
(cons (car tasks-patterns-alist)
|
||||||
(cons task-patterns
|
remaining))))))))))))
|
||||||
remaining))))
|
|
||||||
(handle-eof
|
|
||||||
task (cdr task-patterns)
|
|
||||||
(lambda ()
|
|
||||||
(iloop (cdr inports)
|
|
||||||
(cdr tasks-patterns-alist)
|
|
||||||
remaining)))))
|
|
||||||
(iloop (cdr inports)
|
|
||||||
(cdr tasks-patterns-alist)
|
|
||||||
(cons (car tasks-patterns-alist)
|
|
||||||
remaining)))))))))))))
|
|
||||||
|
|
||||||
;; Syntax based on procedural interface
|
;; Syntax based on procedural interface
|
||||||
|
|
||||||
|
@ -377,11 +375,10 @@
|
||||||
(expect-action-clauses clause ...)))
|
(expect-action-clauses clause ...)))
|
||||||
((expect-action-clauses (pattern () body ...) clause ...)
|
((expect-action-clauses (pattern () body ...) clause ...)
|
||||||
(expect-action-clauses (pattern (ignore) body ...) clause ...))
|
(expect-action-clauses (pattern (ignore) body ...) clause ...))
|
||||||
((expect-action-clauses (pattern (match mvars ...) body ...) clause ...)
|
((expect-action-clauses (pattern (m mvars ...) body ...) clause ...)
|
||||||
(cons (list 'match (lambda () pattern)
|
(cons (list 'match (lambda () pattern)
|
||||||
(lambda (m)
|
(lambda (m)
|
||||||
(let ((match m))
|
(let-match m (mvars ...) body ...)))
|
||||||
(let-match m (mvars ...) body ...))))
|
|
||||||
(expect-action-clauses clause ...)))
|
(expect-action-clauses clause ...)))
|
||||||
((expect-action-clauses (else body ...) clause ...)
|
((expect-action-clauses (else body ...) clause ...)
|
||||||
(cons (list else (lambda () body ...))
|
(cons (list else (lambda () body ...))
|
||||||
|
@ -405,8 +402,8 @@
|
||||||
(syntax-rules ()
|
(syntax-rules ()
|
||||||
((expect (x ...) eclause ...)
|
((expect (x ...) eclause ...)
|
||||||
(let ((r (expect-clauses (x ...) eclause ...)))
|
(let ((r (expect-clauses (x ...) eclause ...)))
|
||||||
(expect* (car r) (cdr r))))
|
(apply expect* (car r) (cdr r))))
|
||||||
((expect name (var-inits ...) eclause ...)
|
((expect name (var-inits ...) eclause ...)
|
||||||
(let name (var-inits ...)
|
(let name (var-inits ...)
|
||||||
(let ((r (expect-clauses eclause ...)))
|
(let ((r (expect-clauses eclause ...)))
|
||||||
(expect* (car r) (cdr r)))))))
|
(apply expect* (car r) (cdr r)))))))
|
||||||
|
|
Loading…
Reference in New Issue