refactor
parent
50eb4964cd
commit
6221ca5959
@ -0,0 +1,21 @@
|
||||
#lang br
|
||||
(require brag/support racket/contract)
|
||||
|
||||
(define basic-lexer
|
||||
(lexer-srcloc
|
||||
[(eof) eof]
|
||||
[whitespace (token lexeme #:skip? #t)]
|
||||
[(from/to "rem" "\n") (token 'REM lexeme)]
|
||||
[(:or "print" "goto" "end" "+" ":") lexeme]
|
||||
[(:+ numeric) (token 'INTEGER (string->number lexeme))]
|
||||
[(:or (:seq (:+ numeric) ".")
|
||||
(:seq (:* numeric) "." (:+ numeric)))
|
||||
(token 'DECIMAL (string->number lexeme))]
|
||||
[(from/to "\"" "\"")
|
||||
(token 'STRING (trim-ends "\"" lexeme "\""))]))
|
||||
|
||||
(provide
|
||||
(contract-out
|
||||
[basic-lexer
|
||||
(input-port? . -> .
|
||||
(or/c eof-object? string? srcloc-token?))]))
|
@ -1,15 +1,16 @@
|
||||
#lang br/quicklang
|
||||
(require "parser.rkt" "tokenizer.rkt" brag/support)
|
||||
(require "parser.rkt" "tokenizer.rkt")
|
||||
|
||||
(module+ reader (provide read-syntax))
|
||||
|
||||
(define (read-syntax path port)
|
||||
(define-values (line col pos) (port-next-location port))
|
||||
(define port+newline (input-port-append #f port (open-input-string "\n")))
|
||||
(define-values (pline pcol ppos) (port-next-location port))
|
||||
(define port+newline
|
||||
(input-port-append #f port (open-input-string "\n")))
|
||||
(port-count-lines! port+newline)
|
||||
(set-port-next-location! port+newline line col pos)
|
||||
(with-handlers ([exn:fail:parsing? (λ (exn) (displayln "Sorry!") (raise exn))])
|
||||
(define parse-tree (parse path (make-tokenizer port+newline)))
|
||||
(set-port-next-location! port+newline pline pcol ppos)
|
||||
(define parse-tree
|
||||
(parse path (make-tokenizer port+newline path)))
|
||||
(strip-bindings
|
||||
#`(module basic-mod basic-demo/expander
|
||||
#,parse-tree))))
|
||||
#,parse-tree)))
|
@ -1,8 +1,8 @@
|
||||
#lang basic-demo
|
||||
30 rem print "statement ignored"
|
||||
40 end
|
||||
30 rem print "ignored"
|
||||
50 print "never gets here"
|
||||
60 print "two" : print 1.2 + 1.8
|
||||
40 end
|
||||
60 print "three" : print 2 + 2
|
||||
70 goto 11 + 10.5 + 8.5
|
||||
10 print "one"
|
||||
20 print : goto 60 : end
|
@ -1,20 +1,14 @@
|
||||
#lang br
|
||||
(require brag/support)
|
||||
(provide (all-defined-out))
|
||||
(require "lexer.rkt" brag/support racket/contract)
|
||||
|
||||
(define basic-lexer
|
||||
(lexer-srcloc
|
||||
[(eof) eof]
|
||||
[whitespace (token lexeme #:skip? #t)]
|
||||
[(from/to "rem" "\n") (token 'REM lexeme)]
|
||||
[(:or "print" "goto" "end" "+" ":") lexeme]
|
||||
[(:+ numeric) (token 'INTEGER (string->number lexeme))]
|
||||
[(:or (:seq (:+ numeric) ".")
|
||||
(:seq (:* numeric) "." (:+ numeric)))
|
||||
(token 'DECIMAL (string->number lexeme))]
|
||||
[(from/to "\"" "\"") (token 'STRING (trim-ends "\"" lexeme "\""))]))
|
||||
|
||||
(define (make-tokenizer ip)
|
||||
(define (make-tokenizer ip [path #f])
|
||||
(port-count-lines! ip)
|
||||
(file-path path)
|
||||
(define (next-token) (basic-lexer ip))
|
||||
next-token)
|
||||
|
||||
(provide
|
||||
(contract-out
|
||||
[make-tokenizer
|
||||
((input-port?) (path?) . ->* .
|
||||
(-> (or/c eof-object? string? srcloc-token?)))]))
|
Loading…
Reference in New Issue