Skip to content

Commit

Permalink
improve/fix pretty-printing of Boolean grammars
Browse files Browse the repository at this point in the history
  • Loading branch information
grammarware committed Jan 14, 2013
1 parent 4d493d2 commit 8b822b7
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 20 deletions.
43 changes: 28 additions & 15 deletions shared/rascal/src/export/BNF.rsc
Expand Up @@ -4,20 +4,20 @@ module export::BNF
import lib::Rascalware;
import syntax::BGF;

public str pp(BGFGrammar bgf)
= (isEmpty(bgf.roots) ? "" : "Roots: <bgf.roots>\n")
+ pp(bgf.prods);
public str pp(grammar([],BGFProdList ps)) = pp(ps);
public default str pp(BGFGrammar bgf) = "Roots: <bgf.roots>\n<pp(bgf.prods)>";

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!="" ? "[<p.label>] " : "")
+ "<p.lhs> ::= <pp(p.rhs)> ;";
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)) = "<lhs> ::= <pptop(rhs)> ;";
public default str pp(BGFProduction p) = "[<p.label>] <p.lhs> ::= <pptop(p.rhs)> ;";

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";
Expand All @@ -26,13 +26,26 @@ public str pp(val(integer())) = "INT";
public str pp(anything()) = "ANYTHING";
public str pp(terminal(str s)) = "\"<s>\"";
public str pp(nonterminal(str s)) = "<s>";
public str pp(selectable(s,e)) = "<s>::<pp(e)>";
public str pp(selectable(s,e)) = "<s>::<ppost(e)>";
public str pp(sequence(L)) = "(<pp(L)>)";
public str pp(choice(L)) = "(<ppc(L)>)";
public str pp(choice(L)) = "(<mapjoin(pp,es," | ")>)";
public str pp(allof(L)) = "(<mapjoin(pp,es," & ")>)";
public str pp(marked(e)) = "\<<pp(e)>\>";
public str pp(optional(e)) = "<pp(e)>?";
public str pp(plus(e)) = "<pp(e)>+";
public str pp(star(e)) = "<pp(e)>*";
public str pp(optional(e)) = "<pppre(e)>?";
public str pp(not(e)) = <ppost(e)>";
public str pp(plus(e)) = "<pppre(e)>+";
public str pp(star(e)) = "<pppre(e)>*";
public str pp(seplistplus(e1,e2)) = "{<pp(e1)> <pp(e2)>}+";
public str pp(sepliststar(e1,e2)) = "{<pp(e1)> <pp(e2)>}*";
public default str pp(BGFExpression e) = "UNKNOWN";

// pretty-print prefix metasymbols in parenthesis, others as usual
str pppre(BGFExpression e:not(e1)) = "(<pp(e)>)";
str pppre(BGFExpression e:selectable(s,e)) = "(<pp(e)>)";
default str pppre(BGFExpression e) = pp(e);

// pretty-print postfix metasymbols in parenthesis, others as usual
str ppost(BGFExpression e:optional(e1)) = "(<pp(e)>)";
str ppost(BGFExpression e:plus(e1)) = "(<pp(e)>)";
str ppost(BGFExpression e:star(e1)) = "(<pp(e)>)";
default str ppost(BGFExpression e) = pp(e);
3 changes: 3 additions & 0 deletions shared/rascal/src/lib/Rascalware.rsc
Expand Up @@ -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);

Expand Down
17 changes: 12 additions & 5 deletions shared/xsl/bgf2bnf.xslt
Expand Up @@ -82,15 +82,24 @@
</xsl:template>

<xsl:template match="optional">
<xsl:apply-templates select="./*"/>
<xsl:choose>
<xsl:when test="bgf:expression/not or bgf:expression/selectable">
<xsl:text>(</xsl:text>
<xsl:apply-templates select="./*"/>
<xsl:text>)</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="./*"/>
</xsl:otherwise>
</xsl:choose>
<xsl:text>?</xsl:text>
</xsl:template>

<!-- Boolean grammars support -->
<xsl:template match="not">
<xsl:text>!</xsl:text>
<xsl:choose>
<xsl:when test="bgf:expression/optional or bgf:expression/not">
<xsl:when test="bgf:expression/optional or bgf:expression/plus or bgf:expression/star">
<xsl:text>(</xsl:text>
<xsl:apply-templates select="./*"/>
<xsl:text>)</xsl:text>
Expand Down Expand Up @@ -138,9 +147,7 @@
<xsl:value-of select="selector"/>
<xsl:text>::</xsl:text>
<xsl:choose>
<xsl:when test="local-name(bgf:expression/*) = 'star'
or local-name(bgf:expression/*) = 'optional'
or local-name(bgf:expression/*) = 'plus'">
<xsl:when test="bgf:expression/star or bgf:expression/optional or bgf:expression/plus">
<xsl:text>(</xsl:text>
<xsl:apply-templates select="bgf:expression"/>
<xsl:text>)</xsl:text>
Expand Down

0 comments on commit 8b822b7

Please sign in to comment.