Safe Haskell | None |
---|---|
Language | Haskell2010 |
We define a simple domain-specific language for context-free languages.
TODO we still need to make sure to handle NTs correctly. It should be that
we write [X,Y]
in multidim cases and then we check in rules if [X,Y]
is
available ... of course for [X,eps]
we then need to check if eps
is an
epsilon symbol.
- data GrammarEnv = GrammarEnv {}
- verbose :: Lens' GrammarEnv Bool
- env :: Lens' GrammarEnv (Map String Grammar)
- emit :: Lens' GrammarEnv (Seq Grammar)
- current :: Lens' GrammarEnv Grammar
- test :: MonadIO m => m (Maybe (Seq Grammar))
- parse :: String -> Result (Seq Grammar)
- parseEverything :: Parse m () -> Parse m (Seq Grammar)
- parseGrammar :: Parse m ()
- setIndices :: Parse m [Index]
- parseEmitGrammar :: Parse m ()
- parseNormStartEps :: Parse m ()
- parseOutside :: Parse m ()
- parseCommands :: Parse m ()
- fgIdents :: TokenParsing m => IdentifierStyle m
- newGrammarName :: Parse m String
- knownGrammarName :: Parse m Grammar
- parseSyntacticDecl :: EvalReq -> Parse m SynTermEps
- parseSynTermDecl :: EvalReq -> Parse m SynTermEps
- parseTermDecl :: Parse m SynTermEps
- parseStartSym :: Parse m Symbol
- data EvalReq
- knownSynVar :: EvalReq -> Stately m Symbol
- knownSynTerm :: EvalReq -> Stately m Symbol
- parseIndex :: EvalReq -> Stately m [Index]
- knownTermVar :: EvalReq -> Stately m Symbol
- knownSymbol :: EvalReq -> Stately m Symbol
- parseRule :: Parse m [Rule]
- updateSplitCounts :: [Symbol] -> [Symbol]
- expandIndexed :: Rule -> Parse m [Rule]
- type Parse m a = (TokenParsing m, MonadState GrammarEnv (Unlined m), MonadState GrammarEnv m, MonadPlus m) => m a
- type Stately m a = (TokenParsing m, MonadState GrammarEnv m, MonadPlus m) => m a
- newtype GrammarParser m a = GrammarParser {
- runGrammarParser :: StateT GrammarEnv m a
- data Result a :: * -> *
Documentation
data GrammarEnv Source
The environment captures both the current grammar we work with
(current
) as well as everything we have parsed until now (env
).
parseEverything :: Parse m () -> Parse m (Seq Grammar) Source
Parse everything in the grammar source. The additional argument, normally
empty :: Alternative f a
, allows for providing additional parsing
capabilities -- e.g. for grammar products..
parseGrammar :: Parse m () Source
The basic parser, which generates a grammar from a description.
setIndices :: Parse m [Index] Source
Collect all indices and set them as active
parseEmitGrammar :: Parse m () Source
Which of the intermediate grammar to actually emit as code or text in
TeX. Single line: Emit: KnownGrammarName
parseNormStartEps :: Parse m () Source
Normalize start and epsilon rules in a known Source:
, thereby
generating a new grammar.
parseOutside :: Parse m () Source
Try to generate an outside grammar from an inside grammar. The From:
name is looked up in the environment.
Outside: NAME From: (inside)NAME //
parseCommands :: Parse m () Source
Some additional commands that change the parsing state.
TODO MonoidOfPairs
should generate an adapter function that turns any
2-tape eval function into its k-tape version. This means collecting all
name pairs, then emitting the corresponding adapter. We'll also need
a monoidal function for combining pairs. (this is along the lines of
sum-of-pairs).
Helper parsers
fgIdents :: TokenParsing m => IdentifierStyle m Source
newGrammarName :: Parse m String Source
parseSyntacticDecl :: EvalReq -> Parse m SynTermEps Source
Parses a syntactic (or non-terminal) symbol (for the corresponding
index type). Cf. parseSynTermDecl
.
parseSynTermDecl :: EvalReq -> Parse m SynTermEps Source
Parses a syntactic terminal declaration; an inside syntactic variable in an outside context.
parseStartSym :: Parse m Symbol Source
The syntactic variable here needs to either have no index at all, have a grammar-based index, or have a fully calculated index.
EvalFull | Happens when we actually emit a grammar product (in development) |
EvalRule | Happens when we work through the rules |
EvalSymb | Happens when we encounter |
EvalGrammar | Happens when we define grammar-global parameters |
knownSynVar :: EvalReq -> Stately m Symbol Source
knownSynTerm :: EvalReq -> Stately m Symbol Source
parseIndex :: EvalReq -> Stately m [Index] Source
Parses indices { ... }
within curly brackets (braces
).
When parsing the EvalSymb
case, indexed symbols are being created.
Parsing in rules is handled via EvalRule
and actually requires us
saying which explicit index we use.
knownTermVar :: EvalReq -> Stately m Symbol Source
knownSymbol :: EvalReq -> Stately m Symbol Source
Parses an already known symbol, either syntactic or terminal.
TODO Correctly parse inside-syntactics in outside grammars? Do we want this explicitly?
updateSplitCounts :: [Symbol] -> [Symbol] Source
For split syntactic variables used in split manner (i.e. @S -> X Y X Y)
TODO error control!
expandIndexed :: Rule -> Parse m [Rule] Source
Once we have parsed a rule, we still need to extract all active indices in the rule, and enumerate over them. This will finally generate the set of rules we are interested in.
type Parse m a = (TokenParsing m, MonadState GrammarEnv (Unlined m), MonadState GrammarEnv m, MonadPlus m) => m a Source
type Stately m a = (TokenParsing m, MonadState GrammarEnv m, MonadPlus m) => m a Source
newtype GrammarParser m a Source
MonadState GrammarEnv (Unlined (GrammarParser Parser)) | |
Monad m => MonadState GrammarEnv (GrammarParser m) | |
(Functor m, MonadPlus m) => Alternative (GrammarParser m) | |
Monad m => Monad (GrammarParser m) | |
Functor m => Functor (GrammarParser m) | |
MonadPlus m => MonadPlus (GrammarParser m) | |
(Monad m, Functor m) => Applicative (GrammarParser m) | |
(MonadPlus m, CharParsing m) => TokenParsing (GrammarParser m) | |
(MonadPlus m, CharParsing m) => CharParsing (GrammarParser m) | |
(MonadPlus m, Parsing m) => Parsing (GrammarParser m) |