diff --git a/shared/rascal/src/export/BNF.rsc b/shared/rascal/src/export/BNF.rsc index a7233f69..10dc3af2 100644 --- a/shared/rascal/src/export/BNF.rsc +++ b/shared/rascal/src/export/BNF.rsc @@ -4,20 +4,20 @@ module export::BNF import lib::Rascalware; import syntax::BGF; -public str pp(BGFGrammar bgf) - = (isEmpty(bgf.roots) ? "" : "Roots: \n") - + pp(bgf.prods); +public str pp(grammar([],BGFProdList ps)) = pp(ps); +public default str pp(BGFGrammar bgf) = "Roots: \n"; -public str pp(BGFProdList ps) = joinStrings([pp(p) | p <- ps], "\n"); -public str pp(set[BGFProduction] ps) = joinStrings([pp(p) | p <- ps], "\n"); +public str pp(BGFProdList ps) = mapjoin(pp,ps,"\n"); -public str pp(BGFProduction p) - = (p.label!="" ? "[] " : "") - + " ::= ;"; +public str pp(set[BGFProduction] ps) = mapjoin(pp,ps,"\n"); -public str pp(BGFExprList es) = joinStrings([pp(e) | e <- es], " "); +public str pp(production("",str lhs, BGFExpression rhs)) = " ::= ;"; +public default str pp(BGFProduction p) = "[] ::= ;"; -public str ppc(BGFExprList es) = joinStrings([pp(e) | e <- es], " | "); +str pptop(sequence(BGFExprList es)) = pp(es); +default str pptop(BGFExpression e) = pp(e); + +public str pp(BGFExprList es) = mapjoin(pp,es," "); public str pp(epsilon()) = "EPSILON"; public str pp(empty()) = "EMPTY"; @@ -26,13 +26,26 @@ public str pp(val(integer())) = "INT"; public str pp(anything()) = "ANYTHING"; public str pp(terminal(str s)) = "\"\""; public str pp(nonterminal(str s)) = ""; -public str pp(selectable(s,e)) = "::"; +public str pp(selectable(s,e)) = "::"; public str pp(sequence(L)) = "()"; -public str pp(choice(L)) = "()"; +public str pp(choice(L)) = "()"; +public str pp(allof(L)) = "()"; public str pp(marked(e)) = "\<\>"; -public str pp(optional(e)) = "?"; -public str pp(plus(e)) = "+"; -public str pp(star(e)) = "*"; +public str pp(optional(e)) = "?"; +public str pp(not(e)) = "¬"; +public str pp(plus(e)) = "+"; +public str pp(star(e)) = "*"; public str pp(seplistplus(e1,e2)) = "{ }+"; public str pp(sepliststar(e1,e2)) = "{ }*"; public default str pp(BGFExpression e) = "UNKNOWN"; + +// pretty-print prefix metasymbols in parenthesis, others as usual +str pppre(BGFExpression e:not(e1)) = "()"; +str pppre(BGFExpression e:selectable(s,e)) = "()"; +default str pppre(BGFExpression e) = pp(e); + +// pretty-print postfix metasymbols in parenthesis, others as usual +str ppost(BGFExpression e:optional(e1)) = "()"; +str ppost(BGFExpression e:plus(e1)) = "()"; +str ppost(BGFExpression e:star(e1)) = "()"; +default str ppost(BGFExpression e) = pp(e); diff --git a/shared/rascal/src/lib/Rascalware.rsc b/shared/rascal/src/lib/Rascalware.rsc index 9da521e3..6da960a3 100644 --- a/shared/rascal/src/lib/Rascalware.rsc +++ b/shared/rascal/src/lib/Rascalware.rsc @@ -34,6 +34,9 @@ public void println(str s) = IO::println(s); public str joinStrings([], _) = ""; public str joinStrings(list[str] ss, str w) = (ss[0] | it + w + s | s <- tail(ss)); +public str mapjoin(&T1(&T2) f, list[&T2] xs, str sep) = joinStrings(mapper(xs,f),sep); +public str mapjoin(&T1(&T2) f, set[&T2] xs, str sep) = joinStrings(toList(mapper(xs,f)),sep); + //public bool multiseteq(list[&T] xs, list[&T] ys) = sort(xs) == sort(ys); public bool multiseteq(list[&T] xs, list[&T] ys) = sort(xs) == sort(ys); diff --git a/shared/xsl/bgf2bnf.xslt b/shared/xsl/bgf2bnf.xslt index 6bae2976..a43d5e46 100644 --- a/shared/xsl/bgf2bnf.xslt +++ b/shared/xsl/bgf2bnf.xslt @@ -82,7 +82,16 @@ - + + + ( + + ) + + + + + ? @@ -90,7 +99,7 @@ ! - + ( ) @@ -138,9 +147,7 @@ :: - + ( )