|
|
@ -11,27 +11,35 @@
|
|
|
|
make-lex-buf
|
|
|
|
make-lex-buf
|
|
|
|
get-position position-offset position-line position-col position?
|
|
|
|
get-position position-offset position-line position-col position?
|
|
|
|
define-tokens define-empty-tokens)
|
|
|
|
define-tokens define-empty-tokens)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define-syntax lexer
|
|
|
|
(define-syntax lexer
|
|
|
|
(let ((code
|
|
|
|
(let ((code
|
|
|
|
`(letrec ((match
|
|
|
|
`(letrec ((match
|
|
|
|
(lambda (lb first-pos end-pos longest-match-length longest-match-action length)
|
|
|
|
(lambda (lb first-pos end-pos longest-match-length longest-match-action length)
|
|
|
|
(let ((match
|
|
|
|
(let ((match
|
|
|
|
(push-back lb (- length longest-match-length))))
|
|
|
|
(push-back lb (- length longest-match-length))))
|
|
|
|
(if (not longest-match-action)
|
|
|
|
(if (not longest-match-action)
|
|
|
|
(error 'lexer (format "No match found in input starting with: ~a"
|
|
|
|
(raise (make-exn:read
|
|
|
|
(list->string (lex-buffer-from lb)))))
|
|
|
|
(format "lexer: No match found in input starting with: ~a"
|
|
|
|
(longest-match-action
|
|
|
|
(list->string (lex-buffer-from lb)))
|
|
|
|
(lambda ()
|
|
|
|
(current-continuation-marks)
|
|
|
|
first-pos)
|
|
|
|
(lex-buffer-ip lb)
|
|
|
|
(lambda ()
|
|
|
|
#f
|
|
|
|
end-pos)
|
|
|
|
(position-line first-pos)
|
|
|
|
(lambda ()
|
|
|
|
(position-col first-pos)
|
|
|
|
(if (char? (car match))
|
|
|
|
(position-offset first-pos)
|
|
|
|
(list->string (reverse match))
|
|
|
|
(- (position-offset end-pos) (position-offset first-pos)))))
|
|
|
|
(list->string (reverse (cdr match)))))
|
|
|
|
(longest-match-action
|
|
|
|
lb)))))
|
|
|
|
(lambda ()
|
|
|
|
|
|
|
|
first-pos)
|
|
|
|
|
|
|
|
(lambda ()
|
|
|
|
|
|
|
|
end-pos)
|
|
|
|
|
|
|
|
(lambda ()
|
|
|
|
|
|
|
|
(if (char? (car match))
|
|
|
|
|
|
|
|
(list->string (reverse match))
|
|
|
|
|
|
|
|
(list->string (reverse (cdr match)))))
|
|
|
|
|
|
|
|
lb)))))
|
|
|
|
(lambda (lb)
|
|
|
|
(lambda (lb)
|
|
|
|
(unless (lex-buffer? lb)
|
|
|
|
(unless (lex-buffer? lb)
|
|
|
|
(raise-type-error
|
|
|
|
(raise-type-error
|
|
|
@ -142,7 +150,7 @@
|
|
|
|
;; c = char | eof
|
|
|
|
;; c = char | eof
|
|
|
|
;; lex-buf =
|
|
|
|
;; lex-buf =
|
|
|
|
;; (make-lex-buffer input-port (c list) (c list) int int int (int list))
|
|
|
|
;; (make-lex-buffer input-port (c list) (c list) int int int (int list))
|
|
|
|
(define-struct lex-buffer (ip from to offset line col line-lengths))
|
|
|
|
(define-struct lex-buffer (ip from to offset line col line-lengths tab-skips))
|
|
|
|
|
|
|
|
|
|
|
|
;; make-lex-buf: input-port -> lex-buf
|
|
|
|
;; make-lex-buf: input-port -> lex-buf
|
|
|
|
(define make-lex-buf
|
|
|
|
(define make-lex-buf
|
|
|
@ -152,7 +160,7 @@
|
|
|
|
((not (input-port? ip))
|
|
|
|
((not (input-port? ip))
|
|
|
|
(raise-type-error 'make-lex-buf "input-port" 0 ip))
|
|
|
|
(raise-type-error 'make-lex-buf "input-port" 0 ip))
|
|
|
|
(else
|
|
|
|
(else
|
|
|
|
(make-lex-buffer ip null null 1 1 1 null))))
|
|
|
|
(make-lex-buffer ip null null 1 1 1 null null))))
|
|
|
|
((ip offsets)
|
|
|
|
((ip offsets)
|
|
|
|
(cond
|
|
|
|
(cond
|
|
|
|
((not (input-port? ip))
|
|
|
|
((not (input-port? ip))
|
|
|
@ -163,13 +171,13 @@
|
|
|
|
(not (andmap (lambda (x) (>= x 0)) offsets)))
|
|
|
|
(not (andmap (lambda (x) (>= x 0)) offsets)))
|
|
|
|
(raise-type-error 'make-lex-buf "list of 3 non-negative exact integers" 1 ip offsets))
|
|
|
|
(raise-type-error 'make-lex-buf "list of 3 non-negative exact integers" 1 ip offsets))
|
|
|
|
(else
|
|
|
|
(else
|
|
|
|
(make-lex-buffer ip null null (add1 (caddr offsets)) (add1 (car offsets)) (add1 (cadr offsets)) null))))))
|
|
|
|
(make-lex-buffer ip null null (add1 (caddr offsets)) (add1 (car offsets)) (add1 (cadr offsets)) null null))))))
|
|
|
|
|
|
|
|
|
|
|
|
;; next-char: lex-buf -> c
|
|
|
|
;; next-char: lex-buf -> c
|
|
|
|
;; gets the next character from the buffer
|
|
|
|
;; gets the next character from the buffer
|
|
|
|
(define (next-char lb)
|
|
|
|
(define (next-char lb)
|
|
|
|
(let ((get-next
|
|
|
|
(let ((get-next
|
|
|
|
(lambda ()
|
|
|
|
(lambda ()
|
|
|
|
(cond
|
|
|
|
(cond
|
|
|
|
((null? (lex-buffer-from lb))
|
|
|
|
((null? (lex-buffer-from lb))
|
|
|
|
(read-char (lex-buffer-ip lb)))
|
|
|
|
(read-char (lex-buffer-ip lb)))
|
|
|
@ -190,7 +198,8 @@
|
|
|
|
(set-lex-buffer-to! lb (cons char-in (lex-buffer-to lb)))
|
|
|
|
(set-lex-buffer-to! lb (cons char-in (lex-buffer-to lb)))
|
|
|
|
(cond
|
|
|
|
(cond
|
|
|
|
((eq? #\tab char-in)
|
|
|
|
((eq? #\tab char-in)
|
|
|
|
(let ((skip-amt 1));(- 8 (modulo (lex-buffer-col lb) 8))))
|
|
|
|
(let ((skip-amt (- 8 (modulo (lex-buffer-col lb) 8))))
|
|
|
|
|
|
|
|
(set-lex-buffer-tab-skips! lb (cons skip-amt (lex-buffer-tab-skips lb)))
|
|
|
|
(set-lex-buffer-col! lb (+ skip-amt (lex-buffer-col lb)))
|
|
|
|
(set-lex-buffer-col! lb (+ skip-amt (lex-buffer-col lb)))
|
|
|
|
(set-lex-buffer-offset! lb (+ skip-amt (lex-buffer-col lb)))))
|
|
|
|
(set-lex-buffer-offset! lb (+ skip-amt (lex-buffer-col lb)))))
|
|
|
|
((eq? #\newline char-in)
|
|
|
|
((eq? #\newline char-in)
|
|
|
@ -225,31 +234,30 @@
|
|
|
|
(else
|
|
|
|
(else
|
|
|
|
(cond
|
|
|
|
(cond
|
|
|
|
((eq? #\newline (car from))
|
|
|
|
((eq? #\newline (car from))
|
|
|
|
(set-lex-buffer-line!
|
|
|
|
(set-lex-buffer-line! lb (sub1 (lex-buffer-line lb)))
|
|
|
|
lb
|
|
|
|
(set-lex-buffer-col! lb (car (lex-buffer-line-lengths lb)))
|
|
|
|
(sub1 (lex-buffer-line lb)))
|
|
|
|
(set-lex-buffer-line-lengths! lb (cdr (lex-buffer-line-lengths lb))))
|
|
|
|
(set-lex-buffer-col!
|
|
|
|
((eq? #\tab (car from))
|
|
|
|
lb
|
|
|
|
(set-lex-buffer-col! lb (- (lex-buffer-col lb)
|
|
|
|
(car (lex-buffer-line-lengths lb)))
|
|
|
|
(car (lex-buffer-tab-skips lb))))
|
|
|
|
(set-lex-buffer-line-lengths!
|
|
|
|
(set-lex-buffer-offset! lb (- (add1 (lex-buffer-offset lb))
|
|
|
|
lb
|
|
|
|
(car (lex-buffer-tab-skips lb))))
|
|
|
|
(cdr (lex-buffer-line-lengths lb))))
|
|
|
|
(set-lex-buffer-tab-skips! lb (cdr (lex-buffer-tab-skips lb))))
|
|
|
|
(else
|
|
|
|
(else
|
|
|
|
(set-lex-buffer-col!
|
|
|
|
(set-lex-buffer-col! lb (sub1 (lex-buffer-col lb)))))
|
|
|
|
lb
|
|
|
|
(switch-buffers (cdr from)
|
|
|
|
(sub1 (lex-buffer-col lb)))))
|
|
|
|
(cons (car from) to)
|
|
|
|
(switch-buffers (cdr from)
|
|
|
|
(sub1 num-to-add)))))))
|
|
|
|
(cons (car from) to)
|
|
|
|
|
|
|
|
(sub1 num-to-add)))))))
|
|
|
|
|
|
|
|
(let-values (((ret new-from)
|
|
|
|
(let-values (((ret new-from)
|
|
|
|
(switch-buffers (lex-buffer-to lb)
|
|
|
|
(switch-buffers (lex-buffer-to lb)
|
|
|
|
(lex-buffer-from lb)
|
|
|
|
(lex-buffer-from lb)
|
|
|
|
i)))
|
|
|
|
i)))
|
|
|
|
(set-lex-buffer-from! lb new-from)
|
|
|
|
(set-lex-buffer-from! lb new-from)
|
|
|
|
(set-lex-buffer-to! lb null)
|
|
|
|
(set-lex-buffer-to! lb null)
|
|
|
|
(set-lex-buffer-offset! lb (- (lex-buffer-offset lb) i))
|
|
|
|
(set-lex-buffer-offset! lb (- (lex-buffer-offset lb) i))
|
|
|
|
(set-lex-buffer-line-lengths! lb null)
|
|
|
|
(set-lex-buffer-line-lengths! lb null)
|
|
|
|
ret)))
|
|
|
|
(set-lex-buffer-tab-skips! lb null)
|
|
|
|
|
|
|
|
ret)))
|
|
|
|
|
|
|
|
|
|
|
|
(define-struct position (offset line col))
|
|
|
|
(define-struct position (offset line col))
|
|
|
|
(define (get-position lb)
|
|
|
|
(define (get-position lb)
|
|
|
|