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.
40 lines
1.6 KiB
Racket
40 lines
1.6 KiB
Racket
8 years ago
|
#lang racket/base
|
||
|
|
||
3 years ago
|
(require yaragg/support)
|
||
8 years ago
|
|
||
|
(provide current-source
|
||
|
current-parser-error-handler
|
||
|
current-tokenizer-error-handler)
|
||
|
|
||
|
;; During parsing, we should define the source of the input.
|
||
6 years ago
|
(define current-source (make-parameter 'unknown))
|
||
8 years ago
|
|
||
|
|
||
|
;; When an parse error happens, we call the current-parser-error-handler:
|
||
|
(define current-parser-error-handler
|
||
|
(make-parameter
|
||
|
(lambda (tok-name tok-value offset line col span)
|
||
|
(raise (exn:fail:parsing
|
||
7 years ago
|
(format "Encountered parsing error near ~e (token ~e) while parsing ~e [line=~a, column=~a, offset=~a]"
|
||
|
tok-value tok-name
|
||
|
(current-source)
|
||
|
line col offset)
|
||
|
(current-continuation-marks)
|
||
|
(list (srcloc (current-source) line col offset span)))))))
|
||
8 years ago
|
|
||
|
;; When a tokenization error happens, we call the current-tokenizer-error-handler.
|
||
|
(define current-tokenizer-error-handler
|
||
|
(make-parameter
|
||
|
(lambda (tok-type tok-value offset line column span)
|
||
|
(raise (exn:fail:parsing
|
||
7 years ago
|
(string-append
|
||
7 years ago
|
(format "Encountered unexpected token of type ~e (value ~e) while parsing"
|
||
|
(if (memq tok-type (map string->symbol '("\n" "\t" "\r")))
|
||
|
(format "~a" tok-type)
|
||
|
tok-type) tok-value)
|
||
7 years ago
|
(if (or (current-source) line column offset)
|
||
|
(format " ~e [line=~a, column=~a, offset=~a]" (current-source) line column offset)
|
||
|
""))
|
||
8 years ago
|
(current-continuation-marks)
|
||
|
(list (srcloc (current-source) line column offset span)))))))
|