diff --git a/beautiful-racket-demo/hdl-demo/parser.rkt b/beautiful-racket-demo/hdl-demo/grammar.rkt similarity index 77% rename from beautiful-racket-demo/hdl-demo/parser.rkt rename to beautiful-racket-demo/hdl-demo/grammar.rkt index 8223cb1..1d44080 100644 --- a/beautiful-racket-demo/hdl-demo/parser.rkt +++ b/beautiful-racket-demo/hdl-demo/grammar.rkt @@ -1,32 +1,16 @@ #lang brag chip-program : /"CHIP" chipname /"{" in-spec out-spec part-spec /"}" - @chipname : ID - in-spec : pin-spec - out-spec : pin-spec - @pin-spec : (/"IN" | /"OUT") pin [/"," pin]* /";" - /pin : ID [/"[" NUMBER /"]"] - @part-spec : /"PARTS:" part+ - part : partname /"(" wire-assign [/"," wire-assign]* /")" /";" - @partname : ID - /wire-assign : pin-range /"=" pin-val - /pin-range : ID [/"[" bus-range /"]"] - @bus-range : number [/"." /"." number] - -@pin-val : pin-range - | BINARY-NUMBER - | TRUE - | FALSE - +@pin-val : pin-range | BINARY-NUMBER | TRUE | FALSE @number : BINARY-NUMBER | NUMBER \ No newline at end of file diff --git a/beautiful-racket-demo/hdl-demo/main.rkt b/beautiful-racket-demo/hdl-demo/main.rkt index 9537fd6..fa033f3 100644 --- a/beautiful-racket-demo/hdl-demo/main.rkt +++ b/beautiful-racket-demo/hdl-demo/main.rkt @@ -1,4 +1,28 @@ #lang br/quicklang -(module reader br - (require "reader.rkt") +(require "grammar.rkt" + brag/support) + +(define-lex-abbrev reserved-terms + (union "CHIP" "IN" "OUT" "PARTS:" (char-set "[]{}(),;=."))) + +(define tokenize + (lexer-srcloc + [(:or (from/to "/*" "*/") (from/to "//" #\newline)) + (token 'COMMENT lexeme #:skip? #t)] + [(:or #\tab #\space #\newline) (token 'WS #:skip? #t)] + [reserved-terms lexeme] + ["true" (token 'TRUE #t)] + ["false" (token 'FALSE #f)] + ;; bugaboo: "10" is ambiguous: number or binary number? + [(:+ numeric) (token 'NUMBER (string->number lexeme))] + [(:+ (char-set "01")) (token 'BINARY-NUMBER (string->number lexeme 2))] + [(:seq alphabetic (:* (:or alphabetic numeric "-"))) (token 'ID (string->symbol lexeme))])) + +(module+ reader (provide read-syntax)) + +(define (read-syntax src ip) + (strip-context + (with-syntax ([PT (parse src (λ () (tokenize ip)))]) + #'(module hdl-mod hdl-demo/expander + PT)))) \ No newline at end of file diff --git a/beautiful-racket-demo/hdl-demo/reader.rkt b/beautiful-racket-demo/hdl-demo/reader.rkt deleted file mode 100644 index 8794ad7..0000000 --- a/beautiful-racket-demo/hdl-demo/reader.rkt +++ /dev/null @@ -1,8 +0,0 @@ -#lang br -(require "parser.rkt" "tokenizer.rkt") -(provide read-syntax) -(define (read-syntax src ip) - (strip-context - (with-syntax ([PT (parse src (tokenize ip))]) - #'(module hdl-mod hdl-demo/expander - PT)))) diff --git a/beautiful-racket-demo/hdl-demo/tokenizer.rkt b/beautiful-racket-demo/hdl-demo/tokenizer.rkt deleted file mode 100644 index 5b127de..0000000 --- a/beautiful-racket-demo/hdl-demo/tokenizer.rkt +++ /dev/null @@ -1,24 +0,0 @@ -#lang br -(require brag/support - racket/string) - -(provide tokenize) -(define (tokenize input-port) - (define (next-token) - (define get-token - (lexer-src-pos - [(union - (:seq "/*" (complement (:seq any-string "*/" any-string)) "*/") - (:seq "//" (repetition 1 +inf.0 (char-complement #\newline)) #\newline)) - (token 'COMMENT lexeme #:skip? #t)] - [(union #\tab #\space #\newline) (return-without-pos (get-token input-port))] - [(union "CHIP" "IN" "OUT" "PARTS:") lexeme] - [(char-set "[]{}(),;=.") lexeme] - ["true" (token 'TRUE #t)] - ["false" (token 'FALSE #f)] - [(repetition 1 +inf.0 numeric) (token 'NUMBER (string->number lexeme))] - ; bugaboo: "10" is ambiguous: number or binary number? - [(repetition 1 +inf.0 (char-set "01")) (token 'BINARY-NUMBER (string->number lexeme 2))] - [(:seq (repetition 1 1 alphabetic) (repetition 0 +inf.0 (union alphabetic numeric "-"))) (token 'ID (string->symbol lexeme))])) - (get-token input-port)) - next-token)