- cleaned up expect-1* a bit

- fixed a bug in the expect and expect-action-clause macros
This commit is contained in:
frese 2004-08-31 10:16:54 +00:00
parent c608b585a5
commit 571e979360
1 changed files with 101 additions and 104 deletions

View File

@ -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) ;; start looking for input, handle it and throw away those
(string-length input))) ;; that got eof
(mend (- (match:end m 0) delta))) (let loop ((tasks-patterns-alist tasks-patterns-alist))
(monitor task (substring input 0 mend)) (if (null? tasks-patterns-alist)
(monitor task m) #f ;; all eof
;; Set the prematch buffer. (let ((ivec (list->vector (map (lambda (task-patterns)
(set-prematch task s m) (task:in (car task-patterns)))
((caddr pattern) m)) tasks-patterns-alist))))
(loop (cdr patterns))))) ;; what is this (time) thing in the original?
((eof) (loop (cdr patterns))) (if (not (in-select! ivec timeout))
((test) (handle-timeout monitor on-timeout)
(let ((v ((cadr pattern)))) (let iloop ((inports (vector->list ivec))
(if v (tasks-patterns-alist tasks-patterns-alist)
((caddr pattern) v) (remaining '()))
(loop (cdr patterns))))) (if (null? tasks-patterns-alist)
((else) (loop (reverse remaining))
((cadr pattern))) (if (car inports)
(else (error "undefined pattern type" (car pattern))) (let* ((task-patterns (car tasks-patterns-alist))
))))))) (task (car task-patterns))
(handle-eof (s (do-input task)))
(lambda (task patterns do-next) (if s
(set-task:pre-match! task (task:buf task)) (handle-input task (cdr task-patterns) s
(set-task:buf! task "") (lambda ()
(monitor task #f) (iloop (cdr inports)
(let loop ((patterns patterns)) (cdr tasks-patterns-alist)
(if (null? patterns) (cons task-patterns
(do-next) remaining)))
(let ((pattern (car patterns))) monitor)
(case (car pattern) (handle-eof task (cdr task-patterns)
((eof) ((cadr pattern))) (lambda ()
((match test else) (loop (cdr patterns))) (iloop (cdr inports)
(else (error "undefined pattern type" (car pattern))) (cdr tasks-patterns-alist)
)))))) remaining))
(handle-timeout monitor)))
(lambda () (iloop (cdr inports)
(monitor #f 'timeout) (cdr tasks-patterns-alist)
(on-timeout)))) (cons (car tasks-patterns-alist)
remaining))))))))))))
;; first check existing data in the buffers
(let loop ((f-tasks-patterns-alist tasks-patterns-alist))
(if (not (null? f-tasks-patterns-alist))
(let* ((task-patterns (car f-tasks-patterns-alist))
(task (car task-patterns))
(s (task:buf task)))
(if (not (zero? (string-length s)))
(handle-input task (cdr task-patterns) s
(lambda () (loop (cdr f-tasks-patterns-alist))))
(loop (cdr f-tasks-patterns-alist))))
;; start looking for input, handle it and throw away those
;; that got eof
(let loop ((tasks-patterns-alist tasks-patterns-alist))
(if (null? tasks-patterns-alist)
#f ;; all eof
(let ((ivec (list->vector (map (lambda (task-patterns)
(task:in (car task-patterns)))
tasks-patterns-alist))))
;; what is this (time) thing in the original?
(if (not (in-select! ivec timeout))
(handle-timeout)
(let iloop ((inports (vector->list ivec))
(tasks-patterns-alist tasks-patterns-alist)
(remaining '()))
(if (null? tasks-patterns-alist)
(loop (reverse remaining))
(if (car inports)
(let* ((task-patterns (car tasks-patterns-alist))
(task (car task-patterns))
(s (do-input task)))
(if s
(handle-input
task (cdr task-patterns) s
(lambda ()
(iloop (cdr inports)
(cdr tasks-patterns-alist)
(cons task-patterns
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)))))))