bnf and ebnf printer: align ::= symbols.

This commit is contained in:
bjorn
2008-09-26 09:54:09 +00:00
parent 93e9d60765
commit d54ba6f112
2 changed files with 11 additions and 4 deletions

View File

@@ -273,11 +273,18 @@ countRules :: CFG -> Int
countRules = length . allRules countRules = length . allRules
prCFG :: CFG -> String prCFG :: CFG -> String
prCFG = unlines . map prRule . allRules prCFG = prProductions . map prRule . allRules
where where
prRule r = lhsCat r ++ " ::= " ++ unwords (map prSym (ruleRhs r)) prRule r = (lhsCat r, unwords (map prSym (ruleRhs r)))
prSym = symbol id (\t -> "\""++ t ++"\"") 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 -- * CFRule Utilities
-- --

View File

@@ -175,9 +175,9 @@ ungroupTokens = joinRE . mapRE (symbol (RESymbol . NonTerminal) (REConcat . map
-- --
prSRG :: SRG -> String prSRG :: SRG -> String
prSRG = unlines . map prRule . srgRules prSRG = prProductions . map prRule . srgRules
where 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 prAlt (SRGAlt _ _ rhs) = prRE prSym rhs
prSym = symbol fst (\t -> "\""++ t ++"\"") prSym = symbol fst (\t -> "\""++ t ++"\"")