Skip to content

Commit

Permalink
[BNFC#176] Haskell: Store node position in the AST
Browse files Browse the repository at this point in the history
When using bnfc to generate haskell code with the --functor option, the
parser will no store the begining position of the corresponding code in
each node. This might be useful, for instance, to report errors.

Note that this is possibly a larger change than it had to be because I
tried something new: instead of generating code by putting together
strings (or the slightly fancier PrettyPrint.Doc), I refactored the code
that makes happy production to generate an AST which is then
pretty-printed. Both the AST types and the pretty printer being of
course generated by BNFC itself! This seems like an obvious thing to do
but for some reason we never did.

The main advantage is that it makes much better use of the type
checker (whene everything is a string, it doesn't mean much that the
type checking passes...).

In addition, you get much cleaner code by getting rid of all the
separators, keywords, symbols and parentheses that are now inserted by the
pretty printer.
  • Loading branch information
gdetrez authored and Igor Kotrasinski committed Dec 31, 2018
1 parent 4e7f434 commit c75bdad
Show file tree
Hide file tree
Showing 6 changed files with 632 additions and 160 deletions.
3 changes: 3 additions & 0 deletions source/BNFC.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ Executable bnfc
BNFC.Backend.Haskell.MkSharedString
BNFC.Backend.Haskell.HsOpts
BNFC.Backend.Haskell.Utils
BNFC.Backend.Haskell.AbsHappy
BNFC.Backend.Haskell.PrintHappy

-- Profile
BNFC.Backend.HaskellProfile
BNFC.Backend.HaskellProfile.CFtoHappyProfile
Expand Down
32 changes: 32 additions & 0 deletions source/src/BNFC/Backend/Haskell/AbsHappy.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DeriveGeneric #-}
module BNFC.Backend.Haskell.AbsHappy where

-- Haskell module generated by the BNF converter


import Data.Data (Data,Typeable)
import GHC.Generics (Generic)
newtype Ident = Ident String
deriving (Eq, Ord, Show, Read, Data, Typeable, Generic)
newtype Terminal = Terminal String
deriving (Eq, Ord, Show, Read, Data, Typeable, Generic)
data Production = P Ident Type [RightHandSide]
deriving (Eq, Ord, Show, Read, Data, Typeable, Generic)

data RightHandSide = Rhs [Symbol] Expression
deriving (Eq, Ord, Show, Read, Data, Typeable, Generic)

data Symbol = NonTerm Ident | QuotedTerm Terminal | IdentTerm Ident
deriving (Eq, Ord, Show, Read, Data, Typeable, Generic)

data Expression
= EIdent Ident
| EPair Expression Expression
| EApp Expression Expression
deriving (Eq, Ord, Show, Read, Data, Typeable, Generic)

data Type
= TIdent Ident | TPair Type Type | TList Type | TApp Type Type
deriving (Eq, Ord, Show, Read, Data, Typeable, Generic)

Loading

0 comments on commit c75bdad

Please sign in to comment.