v6.3-exception
Matthew Butterick 7 years ago
parent 545b4229fb
commit f21e0f7c20

@ -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

@ -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))))

@ -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))))

@ -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)
Loading…
Cancel
Save