forked from GitHub/gf-core
bnf and ebnf printer: align ::= symbols.
This commit is contained in:
@@ -273,11 +273,18 @@ countRules :: CFG -> Int
|
||||
countRules = length . allRules
|
||||
|
||||
prCFG :: CFG -> String
|
||||
prCFG = unlines . map prRule . allRules
|
||||
prCFG = prProductions . map prRule . allRules
|
||||
where
|
||||
prRule r = lhsCat r ++ " ::= " ++ unwords (map prSym (ruleRhs r))
|
||||
prRule r = (lhsCat r, unwords (map prSym (ruleRhs r)))
|
||||
prSym = symbol id (\t -> "\""++ t ++"\"")
|
||||
|
||||
prProductions :: [(Cat,String)] -> String
|
||||
prProductions prods =
|
||||
unlines [rpad maxLHSWidth lhs ++ " ::= " ++ rhs | (lhs,rhs) <- prods]
|
||||
where
|
||||
maxLHSWidth = maximum $ 0:(map (length . fst) prods)
|
||||
rpad n s = s ++ replicate (n - length s) ' '
|
||||
|
||||
--
|
||||
-- * CFRule Utilities
|
||||
--
|
||||
|
||||
@@ -175,9 +175,9 @@ ungroupTokens = joinRE . mapRE (symbol (RESymbol . NonTerminal) (REConcat . map
|
||||
--
|
||||
|
||||
prSRG :: SRG -> String
|
||||
prSRG = unlines . map prRule . srgRules
|
||||
prSRG = prProductions . map prRule . srgRules
|
||||
where
|
||||
prRule (SRGRule c alts) = c ++ " ::= " ++ unwords (intersperse "|" (map prAlt alts))
|
||||
prRule (SRGRule c alts) = (c,unwords (intersperse "|" (map prAlt alts)))
|
||||
prAlt (SRGAlt _ _ rhs) = prRE prSym rhs
|
||||
prSym = symbol fst (\t -> "\""++ t ++"\"")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user