You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
brag/codegen/reader.rkt

48 lines
1.7 KiB
Racket

8 years ago
#lang s-exp syntax/module-reader
yaragg/codegen/expander
8 years ago
#:read my-read
#:read-syntax my-read-syntax
#:info my-get-info
#:whole-body-readers? #t
(require yaragg/rules/parser
yaragg/rules/lexer
yaragg/rules/stx
yaragg/rules/rule-structs)
8 years ago
7 years ago
(define (my-read in) (syntax->datum (my-read-syntax #f in)))
(define ((my-parser-error-handler src) tok-ok? tok-name tok-value start-pos end-pos)
(raise-syntax-error
#f
(format "Error while parsing grammar near: ~a [line=~a, column=~a, position=~a]"
tok-value
(pos-line start-pos)
(pos-col start-pos)
(pos-offset start-pos))
(datum->syntax #f
(string->symbol (format "~a" tok-value))
(list src
(pos-line start-pos)
(pos-col start-pos)
(pos-offset start-pos)
(if (and (number? (pos-offset end-pos))
(number? (pos-offset start-pos)))
(- (pos-offset end-pos)
(pos-offset start-pos))
#f)))))
8 years ago
(define (my-read-syntax src in)
(define tokenizer (tokenize in))
7 years ago
(define rules (parameterize ([current-parser-error-handler (my-parser-error-handler src)])
(grammar-parser tokenizer)))
(for/list ([r (in-list rules)])
(rule->stx src r)))
8 years ago
(define (my-get-info key default default-filter)
(case key
[(color-lexer) (dynamic-require 'yaragg/private/colorer 'color-brag (λ () #f))]
[(drracket:indentation) (dynamic-require 'yaragg/private/indenter 'indent-brag (λ () #f))]
7 years ago
[else (default-filter key default)]))
8 years ago