From 46f6ab0c36231ed008347a7061b8603b8759684a Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Mon, 30 Jan 2017 18:50:43 -0800 Subject: [PATCH] use newlines in lexer --- beautiful-racket-demo/basic-demo/expander.rkt | 4 ++-- beautiful-racket-demo/basic-demo/lexer-test.rkt | 6 ++++-- beautiful-racket-demo/basic-demo/lexer.rkt | 2 +- beautiful-racket-demo/basic-demo/parser.rkt | 4 ++-- 4 files changed, 9 insertions(+), 7 deletions(-) diff --git a/beautiful-racket-demo/basic-demo/expander.rkt b/beautiful-racket-demo/basic-demo/expander.rkt index 6421d41..724cdeb 100644 --- a/beautiful-racket-demo/basic-demo/expander.rkt +++ b/beautiful-racket-demo/basic-demo/expander.rkt @@ -20,8 +20,8 @@ #:source #'LINE-NUMBER)] [ORIG-LOC caller-stx]) (syntax/loc caller-stx - (define (LINE-NUMBER-ID #:srcloc? [srcloc #f]) - (if srcloc + (define (LINE-NUMBER-ID #:srcloc? [loc #f]) + (if loc (syntax-srcloc #'ORIG-LOC) (begin (void) STATEMENT ...)))))) diff --git a/beautiful-racket-demo/basic-demo/lexer-test.rkt b/beautiful-racket-demo/basic-demo/lexer-test.rkt index 227ca99..d20de3a 100644 --- a/beautiful-racket-demo/basic-demo/lexer-test.rkt +++ b/beautiful-racket-demo/basic-demo/lexer-test.rkt @@ -11,8 +11,10 @@ (srcloc 'string #f #f 1 1)))) (check-equal? (lex "rem ignored\n") - (list (srcloc-token (token 'REM "rem ignored\n") - (srcloc 'string #f #f 1 12)))) + (list (srcloc-token (token 'REM "rem ignored") + (srcloc 'string #f #f 1 11)) + (srcloc-token (token 'NEWLINE) + (srcloc 'string #f #f 12 1)))) (check-equal? (lex "print") (list (srcloc-token "print" diff --git a/beautiful-racket-demo/basic-demo/lexer.rkt b/beautiful-racket-demo/basic-demo/lexer.rkt index ead2034..1983c2c 100644 --- a/beautiful-racket-demo/basic-demo/lexer.rkt +++ b/beautiful-racket-demo/basic-demo/lexer.rkt @@ -7,7 +7,7 @@ (lexer-srcloc [(eof) (return-without-srcloc eof)] [(from/stop-before "rem" "\n") (token 'REM lexeme)] - [(:seq "\n" digits) (token 'LINE-NUMBER (string->number (string-trim lexeme)))] + ["\n" (token 'NEWLINE)] [whitespace (token lexeme #:skip? #t)] [(:or "print" "goto" "end" "+" ":") lexeme] [digits (token 'INTEGER (string->number lexeme))] diff --git a/beautiful-racket-demo/basic-demo/parser.rkt b/beautiful-racket-demo/basic-demo/parser.rkt index 778988d..0dced96 100644 --- a/beautiful-racket-demo/basic-demo/parser.rkt +++ b/beautiful-racket-demo/basic-demo/parser.rkt @@ -1,7 +1,7 @@ #lang brag -b-program : b-line* +b-program : [b-line] (/NEWLINE+ [b-line])* b-line : @b-line-number [b-statement] (/":" [b-statement])* -b-line-number : LINE-NUMBER +b-line-number : INTEGER @b-statement : b-rem | b-print | b-goto