git @ Cat's Eye Technologies Fountain / 96870b4
Construct a grammar and return it Chris Pressey 1 year, 1 month ago
3 changed file(s) with 21 addition(s) and 9 deletion(s). Raw diff Collapse all Expand all
88 Production(Production, ntname, params, backtrackMode, constituents),
99 Grammar(Grammar),
1010 depictExpr, depictExprs, depictProduction, depictGrammar, depictVars,
11 startSymbol, productionExpr, productionParams, getFormals,
11 startSymbol, productionExpr, productionParams, getFormals, mergeGrammars
1212 ) where
1313
1414 import Data.List (intercalate)
9292 if nt == (ntname prod) then params prod else getFormals nt (Grammar rest)
9393 getFormals nt (Grammar []) = error ("Production '" ++ nt ++ "' not found")
9494
95 mergeGrammars :: Grammar -> Grammar -> Grammar
96 mergeGrammars (Grammar ps1) (Grammar ps2) = Grammar $ ps1 ++ ps2
140140 ps' <- collectLoopsProductions ps
141141 return $ Grammar ps'
142142
143 extractLoops :: Grammar -> (Grammar, (Integer, CollectedLoops))
144 extractLoops g = runState (collectLoopsGrammar g) (0, [])
143 extractLoops :: Grammar -> Grammar
144 extractLoops g =
145 let
146 (g', (_count, loops)) = runState (collectLoopsGrammar g) (0, [])
147 loopsG = loops2grammar loops
148 g'' = mergeGrammars g' loopsG
149 in
150 g''
151 where
152 loops2grammar loops = Grammar $ map (loop2production) loops
153 loop2production (name, expr) =
154 Production{
155 ntname=name,
156 params=[], -- FIXME collect and retain them
157 backtrackMode=Nothing,
158 constituents=expr -- FIXME transform it
159 }
22 -- SPDX-License-Identifier: LicenseRef-BSD-2-Clause-X-Fountain
33
44 module Main (main) where
5
6 import Data.List (intercalate)
75
86 import System.Environment
97 import System.Exit
6159 output $ Grammar.depictGrammar grammar'
6260 ["extractloops", grammarFileName] -> do
6361 grammar <- loadSource grammarFileName
64 let (grammar', (_count, loops)) = Preprocessor.extractLoops grammar
62 let grammar' = Preprocessor.extractLoops grammar
6563 putStrLn $ Grammar.depictGrammar grammar'
66 let depictPair = \(name, expr) -> name ++ ":" ++ Grammar.depictExpr expr
67 let depictPairs = \pairs -> (intercalate ", " (map (depictPair) pairs))
68 putStrLn $ depictPairs loops
6964 ("parse":grammarFileName:textFileName:initialParams) -> do
7065 grammar <- loadSource grammarFileName
7166 let grammar' = Preprocessor.preprocessGrammarForParsing grammar