BNFC / bnfc

BNF Converter
http://bnfc.digitalgrammars.com/
586 stars 165 forks source link

`define` and `position token` #378

Open andreasabel opened 3 years ago

andreasabel commented 3 years ago

This fails in some backends:

CNil.     Chars ::=                 ;
cSg.      Chars ::= Char            ;
CSnoc.    Chars ::= Chars Dot Char  ;

define    cSg c = CSnoc CNil (Dot ".") c;

position token     Dot '.'          ;
andreasabel commented 3 years ago

Suggested fix:

  1. If the AST is not aware of positions, just put an invalid position there (Dot (0,0) ".").

    cSg :: Char -> Chars
    cSg c = CSnoc CNil (Dot ((0,0), ".")) c
    
    newtype Dot = Dot ((C.Int, C.Int), String)
  2. In Haskell/Functor, make position tokens polymorphic.

    cSg :: a -> Char -> Chars' a
    cSg a c = CSnoc a (CNil a) (Dot (a, ".")) c
    
    type Dot = Dot' BNFC'Position
    newtype Dot' a = Dot (a, String)
     deriving (C.Eq, C.Ord, C.Show, C.Read, C.Functor, C.Foldable, C.Traversable)