Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
reimplement RenameAll in Rascal; first working C# island parser
  • Loading branch information
grammarware committed Jan 22, 2013
1 parent ee4f3b8 commit a6687f3
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 5 deletions.
6 changes: 4 additions & 2 deletions shared/rascal/src/demo/IslandBoolean.rsc
Expand Up @@ -27,11 +27,13 @@ public void go()
g2 = transform(importG(lib::Tolerant::library.prods),g1).g;
g3 = vtransform(addlex,g2);
g4 = mutate([skeletonise],g3);
println(pp(g4));
g5 = vtransform(lastInlines,g4);
writeBGF(g5,|home:///preRsc.bgf|);
writeBGF(g5,|home:///grammar_preRsc.bgf|);
//g4 = readBGF(|home:///preRsc.bgf|);
println(pp(g5));
println(pprsc(g5));
g6 = mutate([changeDashedLower2GluedCamel], g5);
println(pprsc(g6));
//for (p:production(_,"namespace-body",_)<- g3.prods)
// iprintln(p);
}
Expand Down
58 changes: 58 additions & 0 deletions shared/rascal/src/demo/SkeletonCS.rsc
@@ -0,0 +1,58 @@
@contributor{BGF2Rascal automated exporter - SLPS - http://github.com/grammarware/slps/wiki/BGF2Rascal}
module demo::SkeletonCS

import ParseTree;
import util::IDE;
import IO;

layout WS = [\t\n\ ]* !>> [\t\n\ ];

start syntax CompilationUnit =
("using" LexNotSemicolon ";")*
GlobalAttributeSection*
NamespaceMemberDeclaration*;

syntax GlobalAttributeSection = "[" "assembly" ":" LexNotRightSquareBracket "]";

syntax NamespaceMemberDeclaration = AttributeSection* Modifier* NamespaceMemberDeclarationInsides;

//syntax LexNotSemicolon = LexNotSemicolonChunk+ () >> [;];
//lexical LexNotSemicolonChunk = ![;\ \t\n]* >> ![;\ \t\n];
lexical LexNotSemicolon = ![;]* !>> ![;];

syntax Modifier = @category="Constant" "new"
| "public"
| "protected"
| "internal"
| "private"
| "abstract"
| "sealed";

syntax AttributeSection = "[" LexNotRightSquareBracket "]";

syntax NamespaceMemberDeclarationInsides = MemberName LexNotWhitespace LexNotLeftCurly LexBalancedCurlies ";"?;

syntax MemberName = "namespace" | "class" | "struct" | "interface" | "enum" | "delegate";

lexical LexNotLeftCurly = ![{]* !>> ![{];

lexical LexNotWhitespace = ![\ \t]* !>> ![\ \t];

lexical LexBalancedCurlies = [{] (LexBalancedCurlies| LexNotCurly)* [}];

lexical LexNotRightSquareBracket = ![\]]* !>> ![\]];

lexical LexNotCurly = ![{}]* !>> ![{}];


public void main()
{
registerLanguage("Name", "cs", CompilationUnit(str input, loc org) {return parse(#CompilationUnit, input, org);});
println("Language registered.");
}

public void tryit()
{
println(parse(#CompilationUnit, |home:///projects/slps/shared/rascal/src/demo/Program.cs|));
//println("Language registered.");
}
26 changes: 23 additions & 3 deletions shared/rascal/src/export/Rascal.rsc
Expand Up @@ -5,22 +5,34 @@ module export::Rascal

import lib::Rascalware;
import syntax::BGF;
import String;
import String; //startsWith
import List; //getOneFrom

public str pprsc(BGFGrammar g) =
"@contributor{BGF2Rascal automated exporter - SLPS - http://github.com/grammarware/slps/wiki/BGF2Rascal}
'module Name
'
'import ParseTree;
'import util::IDE;
'import IO;
'
'<for(p<-g.prods){><pprsc(p)><}>
'layout WS = [\\t-\\n\\r\\ ]* !\>\> [\\t-\\n\\r\\ ];
'
'<for(p<-g.prods){><if(p.lhs in g.roots){><pprscroot(p)><} else {><pprsc(p)><}><}>
'
'public void main()
'{
' registerLanguage(\"Name\", \"ext\", <getOneFrom(g.roots)>(str input, loc org) {return parse(#<getOneFrom(g.roots)>, input, org);});
' println(\"Language registered.\");
'}
";

public str pprscroot(BGFProduction p) = "start syntax <p.lhs> = <pprscstop(p.rhs)>;\n\n";

public str pprsc(BGFProduction p)
{
//if (/not(_) := p)
if (startsWith(p.lhs,"lex-"))
if (startsWith(p.lhs,"Lex"))
return "lexical <p.lhs> = <pprscl(p.rhs)>;\n\n";
else
return "syntax <p.lhs> = <pprscstop(p.rhs)>;\n\n";
Expand Down Expand Up @@ -77,6 +89,14 @@ public str pprscl(BGFExpression::terminal(str t)) = "[<pprsct(t)>]";

public str pprscl(BGFExpression::choice(BGFExprList exprs)) = "[<mapjoin(pprsclin,exprs,"")>]";
public str pprscl(BGFExpression::star(BGFExpression e)) = "<pprscl(e)>* !\>\> <pprscl(e)>";
public str pprscl(BGFExpression::sequence(BGFExprList exprs)) = "<mapjoin(pprscl2,exprs," ")>";

public str pprscl2(BGFExpression::not(e)) = "!<pprscl2(e)>";
public str pprscl2(BGFExpression::terminal(str t)) = "[<pprsct(t)>]";
public str pprscl2(BGFExpression::nonterminal(str t)) = "<t>";
public str pprscl2(BGFExpression::star(BGFExpression e)) = "(<pprscl2(e)>)*";
public str pprscl2(BGFExpression::choice(BGFExprList es)) = "<mapjoin(pprscl2,es,"| ")>";

//public str pprscl(BGFExpression::star(e1:not(BGFExpression e2))) = "<pprscl(e1)>* !\>\> <pprscl(e2)>";
//

Expand Down
1 change: 1 addition & 0 deletions shared/rascal/src/mutate/Mutations.rsc
Expand Up @@ -8,6 +8,7 @@ extend mutate::DeYaccify;
extend mutate::InlineChains;
extend mutate::InlineLazy;
extend mutate::InlinePlus;
extend mutate::Naming;

import syntax::BGF;
import normal::BGF;
Expand Down
85 changes: 85 additions & 0 deletions shared/rascal/src/mutate/Naming.rsc
@@ -0,0 +1,85 @@
@contributor{Vadim Zaytsev - vadim@grammarware.net - SWAT, CWI}
@wiki{RenameAll}
module mutate::Naming

import lib::Rascalware;
import syntax::BGF;
import analyse::Metrics;
import String;
import List;

alias NamingConvention = tuple[Capitalisation cap, str sep];

data Capitalisation
= uppercase() // UPPERCASE
| lowercase() // lowercase
| mixedcase() // mixedCase
| camelcase() // CamelCase
;

bool isWordUpper(str w) = w == toUpperCase(w);
bool isWordLower(str w) = w == toLowerCase(w);
bool isWordCapital(str w) = isWordUpper(charAt(w,0)) && isWordUpper(stringChars(tail(chars(w))));

bool isPhraseUpper(list[str] ws) = (isWordUpper(ws[0]) | it && isWordUpper(w) | str w <- tail(ws));
bool isPhraseLower(list[str] ws) = (isWordLower(ws[0]) | it && isWordLower(w) | str w <- tail(ws));
bool isPhraseMixed(list[str] ws) = (isWordLower(ws[0]) | it && isWordCapital(w) | str w <- tail(ws));
bool isPhraseCamel(list[str] ws) = (isWordCapital(ws[0]) | it && isWordCapital(w) | str w <- tail(ws));

bool doesPhraseConform(list[str] ws, Capitalisation::uppercase()) = isPhraseUpper(ws);
bool doesPhraseConform(list[str] ws, Capitalisation::lowercase()) = isPhraseLower(ws);
bool doesPhraseConform(list[str] ws, Capitalisation::mixedcase()) = isPhraseMixed(ws);
bool doesPhraseConform(list[str] ws, Capitalisation::camelcase()) = isPhraseCamel(ws);

bool doesNameConform(str name, NamingConvention ns)
= doesPhraseConform(name2phrase(name,ns.sep,ns.cap), ns.cap);

list[str] name2phrase(str name, NamingConvention ns) = name2phrase(name, ns.sep, ns.cap);
list[str] name2phrase(str name, "", Capitalisation cap) = name2phrase(name, cap);
default list[str] name2phrase(str name, str sep, Capitalisation _) = split(sep,name);
list[str] name2phrase(str name, mixedcase()) = splitAtCapital(name);
list[str] name2phrase(str name, camelcase()) = splitAtCapital(name);
default list[str] name2phrase(str name, Capitalisation cap) = [name];

list[str] splitAtCapital(str name)
{
list[list[int]] res = [[charAt(name,0)]];
int cx = 0;
for (c <- tail(chars(name)))
if (65 <= c && c <= 90)
{res += [[c]]; cx+=1;}
else
res[cx] += [c];
return [stringChars(c) | c <- res];
}

test bool t1() = splitAtCapital("splitAtCapital") == ["split", "At", "Capital"];
test bool t2() = doesNameConform("compilation-unit", <lowercase(),"-">);

str toCapitalised(str w) = (toUpperCase(stringChar(charAt(w,0))) | it + toLowerCase(stringChar(c)) | c <- tail(chars(w)));

str phrase2name(list[str] ws, NamingConvention nc) = phrase2name(ws, nc.sep, nc.cap);
str phrase2name(list[str] ws, str sep, uppercase())
= (toUpperCase(ws[0]) | it + sep + toUpperCase(w) | w <- tail(ws));
str phrase2name(list[str] ws, str sep, lowercase())
= (toLowerCase(ws[0]) | it + sep + toLowerCase(w) | w <- tail(ws));
str phrase2name(list[str] ws, str sep, camelcase())
= (toCapitalised(ws[0]) | it + sep + toCapitalised(w) | w <- tail(ws));
str phrase2name(list[str] ws, str sep, mixedcase())
= (toLowerCase(ws[0]) | it + sep + toCapitalised(w) | w <- tail(ws));

public BGFGrammar changeConvention(BGFGrammar g, NamingConvention src, NamingConvention tgt)
{
XBGFSequence x = [];
for (n <- allNs(g))
if (doesNameConform(n,src))
x += [renameN(n,phrase2name(name2phrase(n,src),tgt))];
return transform(x,g);
}

public BGFGrammar changeDashedUpper2GluedCamel(BGFGrammar g)
= changeConvention(g, <uppercase(),"-">, <camelcase(),"">);
public BGFGrammar changeDashedLower2GluedCamel(BGFGrammar g)
= changeConvention(g, <lowercase(),"-">, <camelcase(),"">);


2 changes: 2 additions & 0 deletions shared/rascal/src/mutate/SubGrammar.rsc
Expand Up @@ -6,6 +6,8 @@ import lib::Rascalware;
import syntax::BGF;
import transform::library::Util;

public BGFGrammar subgrammar(BGFGrammar g) = subgrammar(g,g.roots);

public BGFGrammar subgrammar(BGFGrammar g, list[str] roots)
{
gr = grammar([],[]);
Expand Down

0 comments on commit a6687f3

Please sign in to comment.