From: Mike D. Lowis Date: Fri, 28 Sep 2012 20:52:44 +0000 (-0400) Subject: Refactored lexer to cleanup the character matching X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=4edcf7e04c5520f327b47386fc31bfb19f4f4bb0;p=archive%2Fdlang-scm.git Refactored lexer to cleanup the character matching --- diff --git a/source/lexer.scm b/source/lexer.scm index b9b5709..da5cbdc 100644 --- a/source/lexer.scm +++ b/source/lexer.scm @@ -19,32 +19,31 @@ (dlang/whitespace in)) ; Comment - ((char=? (chobj-char ch) #\#) + ((chobj-char=? ch #\#) (dlang/comment in)) ; Number - ((or - (and (char=? (chobj-char ch) #\-) (dlang/integer? (buf-lookahead! in 2))) - (char-numeric? (chobj-char ch))) + ((or (and (chobj-char=? ch #\-) (dlang/integer? (buf-lookahead! in 2))) + (char-numeric? (chobj-char ch))) (dlang/number in)) ; Character - ((char=? (chobj-char ch) #\') (dlang/character in)) + ((chobj-char=? ch #\') (dlang/character in)) ; String - ((char=? (chobj-char ch) #\") (dlang/string in)) + ((chobj-char=? ch #\") (dlang/string in)) ; Symbol - ((char=? (chobj-char ch) #\$) (dlang/symbol in)) + ((chobj-char=? ch #\$) (dlang/symbol in)) ; Punctuation and Parens - ((char=? (chobj-char ch) #\() + ((chobj-char=? ch #\() (token 'lpar (string (chobj-char (buf-consume! in))) location)) - ((char=? (chobj-char ch) #\)) + ((chobj-char=? ch #\)) (token 'rpar (string (chobj-char (buf-consume! in))) location)) - ((char=? (chobj-char ch) #\,) + ((chobj-char=? ch #\,) (token 'comma (string (chobj-char (buf-consume! in))) location)) - ((char=? (chobj-char ch) #\;) + ((chobj-char=? ch #\;) (token 'term (string (chobj-char (buf-consume! in))) location)) ; Id @@ -52,7 +51,7 @@ (dlang/id in)))) (if (and (not (eof-object? tok)) (equal? "end" (token-text tok))) - (token-type-set! tok 'term)) + (token-type-set! tok 'term)) tok)) (define (dlang/whitespace in) @@ -70,23 +69,20 @@ (define (dlang/comment? in) (and (not (eof-object? (buf-lookahead! in 1))) - (not (char=? (chobj-char (buf-lookahead! in 1)) #\newline)))) + (not (chobj-char=? (buf-lookahead! in 1) #\newline)))) (define (dlang/number in) (define location (buf-posdata in)) (token 'number (string-append - (if (and (not (eof-object? (buf-lookahead! in 1))) - (char=? #\- (chobj-char (buf-lookahead! in 1)))) - (string (chobj-char (buf-consume! in))) "") + (if (chobj-char=? (buf-lookahead! in 1) #\-) + (string (chobj-char (buf-consume! in))) "") (dlang/integer in) - (if (and (not (eof-object? (buf-lookahead! in 1))) - (char=? (chobj-char (buf-lookahead! in 1)) #\.)) - (dlang/decimal in) "") - (if (and (not (eof-object? (buf-lookahead! in 1))) - (or (char=? (chobj-char (buf-lookahead! in 1)) #\e) - (char=? (chobj-char (buf-lookahead! in 1)) #\E))) - (dlang/exponent in) "")) + (if (chobj-char=? (buf-lookahead! in 1) #\.) + (dlang/decimal in) "") + (if (or (chobj-char=? (buf-lookahead! in 1) #\e) + (chobj-char=? (buf-lookahead! in 1) #\E)) + (dlang/exponent in) "")) location)) (define (dlang/integer in) diff --git a/source/parse-utils.scm b/source/parse-utils.scm index b9c6ed5..320823c 100644 --- a/source/parse-utils.scm +++ b/source/parse-utils.scm @@ -43,6 +43,10 @@ (and (char=? (chobj-char cho1) (chobj-char cho2)) (posdata=? (chobj-pos cho1) (chobj-pos cho2)))) +(define (chobj-char=? obj ch) + (and (not (eof-object? obj)) + (char=? (chobj-char obj) ch))) + (define (charport-read chprt) (define ch (read-char (charport-port chprt))) (cond