|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define-module (language elisp lexer) |
|
#:use-module (ice-9 regex) |
|
#:export (get-lexer get-lexer/1)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define (lexer-error port msg . args) |
|
(apply error msg args)) |
|
|
|
|
|
|
|
|
|
(define (set-char-bit chr bit) |
|
(logior chr (ash 1 bit))) |
|
|
|
|
|
|
|
|
|
|
|
(define (is-char? tested should-be) |
|
(and (not (eof-object? tested)) |
|
(char=? tested should-be))) |
|
|
|
|
|
|
|
|
|
|
|
(define (real-character chr) |
|
(if (< chr 256) |
|
(integer->char chr) |
|
#\nul)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
(define (add-control chr) |
|
(let ((real (real-character chr))) |
|
(if (char-alphabetic? real) |
|
(- (char->integer (char-upcase real)) (char->integer #\@)) |
|
(case real |
|
((#\?) 127) |
|
((#\@) 0) |
|
(else (set-char-bit chr 26)))))) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define (charcode-escape port base digits early-return) |
|
(let iterate ((result 0) |
|
(procdigs 0)) |
|
(if (and digits (>= procdigs digits)) |
|
result |
|
(let* ((cur (read-char port)) |
|
(value (cond |
|
((char-numeric? cur) |
|
(- (char->integer cur) (char->integer #\0))) |
|
((char-alphabetic? cur) |
|
(let ((code (- (char->integer (char-upcase cur)) |
|
(char->integer #\A)))) |
|
(if (< code 0) |
|
#f |
|
(+ code 10)))) |
|
(else #f))) |
|
(valid (and value (< value base)))) |
|
(if (not valid) |
|
(if (or (not digits) early-return) |
|
(begin |
|
(unread-char cur port) |
|
result) |
|
(lexer-error port |
|
"invalid digit in escape-code" |
|
base |
|
cur)) |
|
(iterate (+ (* result base) value) (1+ procdigs))))))) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define basic-escape-codes |
|
'((#\a . 7) |
|
(#\b . 8) |
|
(#\t . 9) |
|
(#\n . 10) |
|
(#\v . 11) |
|
(#\f . 12) |
|
(#\r . 13) |
|
(#\e . 27) |
|
(#\s . 32) |
|
(#\d . 127))) |
|
|
|
(define (get-character port in-string) |
|
(let ((meta-bits `((#\A . 22) |
|
(#\s . 23) |
|
(#\H . 24) |
|
(#\S . 25) |
|
(#\M . ,(if in-string 7 27)))) |
|
(cur (read-char port))) |
|
(if (char=? cur #\\) |
|
|
|
(let* ((escaped (read-char port)) |
|
(esc-code (assq-ref basic-escape-codes escaped)) |
|
(meta (assq-ref meta-bits escaped))) |
|
(cond |
|
|
|
|
|
|
|
((and meta (is-char? (peek-char port) #\-)) |
|
(if (not (char=? (read-char port) #\-)) |
|
(error "expected - after control sequence")) |
|
(set-char-bit (get-character port in-string) meta)) |
|
|
|
(esc-code esc-code) |
|
|
|
((and (char>=? escaped #\0) (char<? escaped #\8)) |
|
(begin |
|
(unread-char escaped port) |
|
(charcode-escape port 8 3 #t))) |
|
|
|
|
|
(else |
|
(case escaped |
|
((#\^) (add-control (get-character port in-string))) |
|
((#\C) |
|
(if (is-char? (peek-char port) #\-) |
|
(begin |
|
(if (not (char=? (read-char port) #\-)) |
|
(error "expected - after control sequence")) |
|
(add-control (get-character port in-string))) |
|
escaped)) |
|
((#\x) (charcode-escape port 16 #f #t)) |
|
((#\u) (charcode-escape port 16 4 #f)) |
|
((#\U) (charcode-escape port 16 8 #f)) |
|
(else (char->integer escaped)))))) |
|
|
|
|
|
(char->integer cur)))) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define integer-regex (make-regexp "^[+-]?[0-9]+\\.?$")) |
|
|
|
(define float-regex |
|
(make-regexp |
|
"^[+-]?([0-9]+\\.?[0-9]*|[0-9]*\\.?[0-9]+)(e[+-]?[0-9]+)?$")) |
|
|
|
|
|
|
|
|
|
(define no-escape-punctuation (string->char-set "-+=*/_~!@$%^&:<>{}?.")) |
|
|
|
(define (get-symbol-or-number port) |
|
(let iterate ((result-chars '()) |
|
(had-escape #f)) |
|
(let* ((c (read-char port)) |
|
(finish (lambda () |
|
(let ((result (list->string |
|
(reverse result-chars)))) |
|
(values |
|
(cond |
|
((and (not had-escape) |
|
(regexp-exec integer-regex result)) |
|
'integer) |
|
((and (not had-escape) |
|
(regexp-exec float-regex result)) |
|
'float) |
|
(else 'symbol)) |
|
result)))) |
|
(need-no-escape? (lambda (c) |
|
(or (char-numeric? c) |
|
(char-alphabetic? c) |
|
(char-set-contains? |
|
no-escape-punctuation |
|
c))))) |
|
(cond |
|
((eof-object? c) (finish)) |
|
((need-no-escape? c) (iterate (cons c result-chars) had-escape)) |
|
((char=? c #\\) (iterate (cons (read-char port) result-chars) #t)) |
|
(else |
|
(unread-char c port) |
|
(finish)))))) |
|
|
|
|
|
|
|
|
|
|
|
(define (get-circular-marker port) |
|
(call-with-values |
|
(lambda () |
|
(let iterate ((result 0)) |
|
(let ((cur (read-char port))) |
|
(if (char-numeric? cur) |
|
(let ((val (- (char->integer cur) (char->integer #\0)))) |
|
(iterate (+ (* result 10) val))) |
|
(values result cur))))) |
|
(lambda (id type) |
|
(case type |
|
((#\#) `(circular-ref . ,id)) |
|
((#\=) `(circular-def . ,id)) |
|
(else (lexer-error port |
|
"invalid circular marker character" |
|
type)))))) |
|
|
|
|
|
|
|
|
|
(define lexical-binding-regexp |
|
(make-regexp |
|
"-\\*-(|.*;)[ \t]*lexical-binding:[ \t]*([^;]*[^ \t;]).*-\\*-")) |
|
|
|
(define (lex port) |
|
(define (lexical-binding-value string) |
|
(and=> (regexp-exec lexical-binding-regexp string) |
|
(lambda (match) |
|
(not (member (match:substring match 2) '("nil" "()")))))) |
|
(let* ((return (let ((file (if (file-port? port) |
|
(port-filename port) |
|
#f)) |
|
(line (1+ (port-line port))) |
|
(column (1+ (port-column port)))) |
|
(lambda (token value) |
|
(let ((obj (cons token value))) |
|
(set-source-property! obj 'filename file) |
|
(set-source-property! obj 'line line) |
|
(set-source-property! obj 'column column) |
|
obj)))) |
|
|
|
|
|
(c (read-char port))) |
|
(cond |
|
|
|
((eof-object? c) (return 'eof c)) |
|
|
|
((char-whitespace? c) (lex port)) |
|
|
|
|
|
|
|
((and (char=? c #\.) |
|
(char-whitespace? (peek-char port))) |
|
(return 'dot #f)) |
|
|
|
(else |
|
(case c |
|
|
|
((#\;) |
|
(if (= (port-line port) 0) |
|
(let iterate ((chars '())) |
|
(let ((cur (read-char port))) |
|
(if (or (eof-object? cur) (char=? cur #\newline)) |
|
(let ((string (list->string (reverse chars)))) |
|
(return 'set-lexical-binding-mode! |
|
(lexical-binding-value string))) |
|
(iterate (cons cur chars))))) |
|
(let iterate () |
|
(let ((cur (read-char port))) |
|
(if (or (eof-object? cur) (char=? cur #\newline)) |
|
(lex port) |
|
(iterate)))))) |
|
|
|
((#\?) |
|
(return 'character (get-character port #f))) |
|
|
|
|
|
|
|
|
|
((#\") |
|
(let iterate ((result-chars '())) |
|
(let ((cur (read-char port))) |
|
(case cur |
|
((#\") |
|
(return 'string (list->string (reverse result-chars)))) |
|
((#\\) |
|
(let ((escaped (read-char port))) |
|
(case escaped |
|
((#\newline #\space) |
|
(iterate result-chars)) |
|
(else |
|
(unread-char escaped port) |
|
(unread-char cur port) |
|
(iterate |
|
(cons (integer->char (get-character port #t)) |
|
result-chars)))))) |
|
(else (iterate (cons cur result-chars))))))) |
|
((#\#) |
|
(let ((c (read-char port))) |
|
(case c |
|
((#\0 #\1 #\2 #\3 #\4 #\5 #\6 #\7 #\8 #\9) |
|
(unread-char c port) |
|
(let ((mark (get-circular-marker port))) |
|
(return (car mark) (cdr mark)))) |
|
((#\') |
|
(return 'function #f)) |
|
((#\:) |
|
(call-with-values |
|
(lambda () (get-symbol-or-number port)) |
|
(lambda (type str) |
|
(return 'symbol (make-symbol str)))))))) |
|
|
|
((#\() (return 'paren-open #f)) |
|
((#\)) (return 'paren-close #f)) |
|
((#\[) (return 'square-open #f)) |
|
((#\]) (return 'square-close #f)) |
|
((#\') (return 'quote #f)) |
|
((#\`) (return 'backquote #f)) |
|
|
|
((#\,) |
|
(if (is-char? (peek-char port) #\@) |
|
(if (not (char=? (read-char port) #\@)) |
|
(error "expected @ in unquote-splicing") |
|
(return 'unquote-splicing #f)) |
|
(return 'unquote #f))) |
|
|
|
|
|
|
|
(else |
|
(unread-char c port) |
|
(call-with-values |
|
(lambda () (get-symbol-or-number port)) |
|
(lambda (type str) |
|
(case type |
|
((symbol) |
|
|
|
|
|
|
|
|
|
|
|
(if (zero? (string-length str)) |
|
(begin |
|
|
|
|
|
(read-char port) |
|
(error "invalid character in input" c)) |
|
(return 'symbol (string->symbol str)))) |
|
((integer) |
|
|
|
|
|
|
|
|
|
(return |
|
'integer |
|
(let ((num (inexact->exact (string->number str)))) |
|
(if (not (integer? num)) |
|
(error "expected integer" str num)) |
|
num))) |
|
((float) |
|
(return 'float (let ((num (string->number str))) |
|
(if (exact? num) |
|
(error "expected inexact float" |
|
str |
|
num)) |
|
num))) |
|
(else (error "wrong number/symbol type" type))))))))))) |
|
|
|
|
|
|
|
|
|
(define (get-lexer port) |
|
(lambda () (lex port))) |
|
|
|
|
|
|
|
|
|
|
|
(define (get-lexer/1 port) |
|
(let ((lex (get-lexer port)) |
|
(finished #f) |
|
(paren-level 0)) |
|
(lambda () |
|
(if finished |
|
(cons 'eof ((@ (ice-9 binary-ports) eof-object))) |
|
(let ((next (lex)) |
|
(quotation #f)) |
|
(case (car next) |
|
((paren-open square-open) |
|
(set! paren-level (1+ paren-level))) |
|
((paren-close square-close) |
|
(set! paren-level (1- paren-level))) |
|
((quote backquote unquote unquote-splicing circular-def) |
|
(set! quotation #t))) |
|
(if (and (not quotation) (<= paren-level 0)) |
|
(set! finished #t)) |
|
next))))) |
|
|