ModuleBNFC-2.9.5Haskell2010
BNFC.Backend.Base
Backend base module.
Defines the type of the backend and some useful functions.
- 4 types
- 3 values
- PackageBNFC-2.9.5
- Exports8
- LanguageHaskell2010
- LicenceBSD-3-Clause
- SourceBase.hs
Define the type of the backend functions. For more purity, instead of having each backend writing the generated files to disk, they return a list of pairs containing the (relative) file path and the file content. This allow for 1) easier testing, 2) implement common options like changing the output dir or providing a diff instead of overwritting the files on a highter level and 3) more purity.
The writer monad provides a more convenient API to generate the list. Note that we still use the IO monad for now because some backends insist on printing stuff to the screen while generating the files.
A result file of a backend.
Constructors
GeneratedFilefileName :: FilePathName of the file to write.
makeComment :: MakeCommentFunction to generate a comment. Used to prefix the file with a stamp ("Generated by BNFC").
fileContent :: StringContent of the file to write.
Instances2Eq, Show
Eq GeneratedFileDefined in BNFC-2.9.5 · BNFC.Backend.BaseShow GeneratedFileDefined in BNFC-2.9.5 · BNFC.Backend.Base
Type of comment-generating functions.
Named after execWriter, this function execute the given backend and returns the generated file paths and contents.
A specialized version of tell that adds a file and its content to the list of generated files.
Lift a computation from the IO monad. This allows us to run IO computations in any monadic stack, so long as it supports these kinds of operations (i.e. IO is the base monad for the stack).
Example
import Control.Monad.Trans.State -- from the "transformers" library
printState :: Show s => StateT s IO ()
printState = do
state <- get
liftIO $ print stateHad we omitted liftIO, we would have ended up with this error:
• Couldn't match type ‘IO’ with ‘StateT s IO’
Expected type: StateT s IO ()
Actual type: IO ()The important part here is the mismatch between StateT s IO () and IO ().
Luckily, we know of a function that takes an IO a and returns an (m a): liftIO,
enabling us to run the program and see the expected results:
> evalStateT printState "hello"
"hello"
> evalStateT printState 3
3
Write a set of files to disk. the first argument is the root directory
inside which all the generated files will be written. This root directory
and sub-directories will be created as needed (ex: if the files contains a
a/b/file.txt, writeFiles will create the directories $ROOT/a and
$ROOT/a/b)