mbutterick / brag

Racket DSL for generating parsers from BNF grammars [moved to https://git.matthewbutterick.com/mbutterick/brag]
https://git.matthewbutterick.com/mbutterick/brag
MIT License
61 stars 12 forks source link

More than one entry to the grammar gives unclear error message #38

Closed tbmreza closed 2 years ago

tbmreza commented 2 years ago

For example:

; x.rkt
#lang racket

(require brag/support)
(require "./parser.rkt")

(define tkns (list "?"))
(syntax->datum (parse tkns))

; parser.rkt
#lang brag

<order-matters>  : "?"
<literal>        : '"' <TEXT> '"' | "'" <TEXT> "'"

gives:

'(<order-matters> "?")

If I reorder the rule to last, it cries.

; parser.rkt
#lang brag

<literal>        : '"' <TEXT> '"' | "'" <TEXT> "'"
<order-matters>  : "?"
Encountered parsing error near "?" (token '?) while parsing 'unknown [line=#f, column=#f, offset=#f]
  location...:
   unknown
  context...:
   /path/to/Library/Racket/8.4/pkgs/brag-lib/brag/private/internal-support.rkt:16:3
   /path/to/Library/Racket/8.4/pkgs/brag-lib/brag/codegen/expander.rkt:82:23: rule-parser
   /path/to/x.rkt:7:0
   body of "/path/to/x.rkt"

Is this expected?

mbutterick commented 2 years ago

Yes, this is the documented behavior of parse: "Parses a series of tokens according to the rules in the grammar, using the first rule of the grammar for the initial production".