Skip to main content

module lang::sdf2::util::SDF2Grammar

rascal-0.40.16

Convert SDF2 grammars to an (unnormalized) Rascal internal grammar representation (Grammar).

Usage

import lang::sdf2::util::SDF2Grammar;

Dependencies

import IO;
import String;
import Set;
import List;
import Map;
import ParseTree;
import Grammar;
import lang::rascal::grammar::definition::Characters;
import lang::sdf2::\syntax::Sdf2;

function label

Symbol label(str s, conditional(Symbol t, set[Condition] cs))

function conditional

Symbol conditional(conditional(Symbol s, set[Condition] c1), set[Condition] c2)

function sdf2grammar

GrammarDefinition sdf2grammar(loc input)

GrammarDefinition sdf2grammar(str main, loc input)

GrammarDefinition sdf2grammar(SDF def)

function injectStarts

Grammar::Grammar injectStarts(Grammar::Grammar g)

function sdf2grammar

GrammarDefinition sdf2grammar(str main, SDF def)

function split

GrammarDefinition split(GrammarDefinition def)

function removeDirectProductionCycle

GrammarDefinition removeDirectProductionCycle(GrammarDefinition def)

function isProductionCycle

bool isProductionCycle(\prod(_, [sing], _), Production b)

default bool isProductionCycle(Production a, Production b)

function addLexicalChaining

GrammarDefinition addLexicalChaining(GrammarDefinition def)

function striprec

Symbol striprec(Symbol s)

function strip

Symbol strip(label(str _, Symbol s))

Symbol strip(conditional(Symbol s, set[Condition] _))

default Symbol strip(Symbol s)

function split

Grammar::Grammar split(Grammar::Grammar g)

data Production

data Production  
= temp()
;

function isNotEmpty

bool isNotEmpty(Production p)

function removeEmptyProductions

Grammar::Grammar removeEmptyProductions(Grammar::Grammar g)

function keep

Production keep(Production source, Symbol s)

function getModule

GrammarModule getModule(Module m)

function moduleName

str moduleName(/<pre:.*>\/<post:.*>/)

str moduleName(/languages::<rest:.*>/)

default str moduleName(str i)

function getImports

set[str] getImports(Module m)

function applyConditions

GrammarDefinition applyConditions(GrammarDefinition d,  map[Symbol from,Symbol to] conds)

function illegalPriorities

Grammar::Grammar illegalPriorities(Grammar::Grammar g)

function dup

&T dup(&T g)

function getProductions

set[Production] getProductions(Module \mod)

set[Production] getProductions(SDF sd)

set[Production] getProductions(Prod* prods, bool isLex)

function fixParameters

set[Production] fixParameters(set[Production] input)

function labelName

str labelName("")

default str labelName(str s)

function getProduction

set[Production] getProduction(Prod P, bool isLex)

function getConditions

set[Symbol] getConditions(SDF m)

function getRestrictions

set[Symbol] getRestrictions(Restriction* restrictions, bool isLex)

function getRestriction

set[Symbol] getRestriction(Restriction restriction, bool isLex)

function getLookaheads

set[Symbol] getLookaheads(Lookaheads ls)

function getPriorities

set[Production] getPriorities({Priority ","}* priorities, bool isLex)

function getPriority

Production getPriority(Group group, bool isLex)

Production getPriority(Priority p, bool isLex)

function definedSymbol

Symbol definedSymbol((&T <: Tree) v, bool isLex)

function getStartSymbols

set[Symbol] getStartSymbols(Module \mod)

function getSymbols

list[Symbol] getSymbols((Syms) `<Sym* ss>`, bool isLex)

function getSymbol

Symbol getSymbol(Sym sym, bool isLex)

function alt

Symbol alt({alt(set[Symbol] ss), *Symbol rest})

function unescape

str unescape(Sym s)

str unescape(StrCon s)

str unescape(SingleQuotedStrCon s)

function unescapeStr

str unescapeStr(str chars)

function getCharClass

Symbol getCharClass(Class cc)

function getCharRange

CharRange getCharRange(Range r)

function getCharacter

int getCharacter(Character c)

function getAttributes

set[Attr] getAttributes(Attrs as)

function getAttribute

set[Attr] getAttribute(Attribute m)

function getAssociativity

Associativity getAssociativity(Assoc as)

function separgs2symbols

list[Symbol] separgs2symbols({Sym ","}+ args, bool isLex)

Tests

test test1

test bool test1() = sdf2grammar(
(SDF) `definition module X exports context-free syntax "abc" -\> ABC`).modules["X"].grammar.rules[sort("ABC")] ==
choice(sort("ABC"), {prod(sort("ABC"),[lit("abc")],{})});

test test2

test bool test2() = rs := sdf2grammar(
(SDF) `definition
'module PICOID
'exports
'lexical syntax
' [a-z] [a-z0-9]* -\> PICO-ID
'lexical restrictions
' PICO-ID -/- [a-z0-9]`).modules["PICOID"].grammar.rules
&& prod(lex("PICO-ID"),[\char-class([range(97,122)]),\conditional(\iter-star(\char-class([range(97,122),range(48,57)])),{\not-follow(\char-class([range(97,122),range(48,57)]))})],{}) == rs[lex("PICO-ID")]
;

test test3

test bool test3() = rs := sdf2grammar(
(SDF) `definition
'module StrChar
'exports
' lexical syntax
' ~[\\0-\\31\\n\\t\\"\\\\] -\> StrChar {cons("normal")}`).modules["StrChar"].grammar.rules
&& prod(label("normal",sort("StrChar")),[\char-class([range(26,33),range(35,91),range(93,65535)])],{}) == rs[sort("StrChar")]
;

test test4

test bool test4() = getProductions((SDF) `definition module A exports syntax A -\> B`) ==
{prod(sort("B"),[sort("A")],{})};

test test5

test bool test5() = getProductions((SDF) `definition module A exports lexical syntax A -\> B`) ==
{prod(lex("B"),[sort("A")],{})};

test test6

test bool test6() = getProductions((SDF) `definition module A exports lexical syntax A -\> B B -\> C`) ==
{prod(lex("C"),[sort("B")],{}),prod(lex("B"),[sort("A")],{})};

test test7

test bool test7() = getProductions((SDF) `definition module A exports context-free syntax A -\> B`) ==
{prod(sort("B"),[sort("A")],{})};

test test9

test bool test9() = getProductions((SDF) `definition module A exports priorities A -\> B \> C -\> D`) ==
{prod(sort("B"),[sort("A")],{}),prod(sort("D"),[sort("C")],{})};

test test9_2

test bool test9_2() = getProductions((SDF) `definition module A exports priorities B "*" B -\> B \> B "+" B -\> B`) ==
{priority(sort("B"),[prod(sort("B"),[sort("B"),lit("*"),sort("B")],{}),prod(sort("B"),[sort("B"),lit("+"),sort("B")],{})])};

test test10

test bool test10() = getProduction((Prod) `PICO-ID ":" TYPE -\> ID-TYPE`, false) == 
{prod(sort("ID-TYPE"),[sort("PICO-ID"),lit(":"),sort("TYPE")],{})};

test test11

test bool test11() = getProduction((Prod) `PICO-ID ":" TYPE -\> ID-TYPE`, true) == 
{prod(lex("ID-TYPE"),[sort("PICO-ID"),lit(":"),sort("TYPE")],{})};

test test12

test bool test12() = getProduction((Prod) `PICO-ID ":" TYPE -\> ID-TYPE {cons("decl"), left}`, false) ==
{prod(sort("ID-TYPE"),[sort("PICO-ID"), lit(":"), sort("TYPE")],{\assoc(left())})};

test test13

test bool test13() = getProduction((Prod) `[\\ \\t\\n\\r]   -\> LAYOUT {cons("whitespace")}`, true) == 
{prod(\lex("LAYOUT"),[\char-class([range(32,32),range(9,9),range(10,10),range(13,13)])],{})};

test test14

test bool test14() = getProduction((Prod) `{~[\\n]* [\\n]}* -\> Rest`, true) ==
{prod(sort("Rest"),[\iter-star-seps(\iter-star(\char-class([range(0,9),range(11,65535)])),[\char-class([range(10,10)])])],{})};

test test18

test bool test18() = getRestriction((Restriction) `-/- [a-z]`, true) == {};

test test19

test bool test19() = getRestriction((Restriction) `ID -/- [a-z]`, true) == 
{conditional(sort("ID"),{\not-follow(\char-class([range(97,122)]))})};

test test21

test bool test21() = getLookaheads((Lookaheads) `[a-z]`) == 
{\char-class([range(97,122)])};

test test22

test bool test22() = getLookaheads((Lookaheads) `[a-z] . [0-9]`) ==
{};

test test23

test bool test23() = getLookaheads((Lookaheads) `[a-z]  | [\\"]`) ==
{\char-class([range(97,122)]),
\char-class([range(34,34)])};

test test24

test bool test24() = getPriority((Group) `A -\> B`, false) == 
prod(sort("B"),[sort("A")],{});

test test25

test bool test25() = getPriority((Group) `A -\> B .`, false) ==  
prod(sort("B"),[sort("A")],{});

test test26

test bool test26() = getPriority((Group) `A -\> B \<1\>`, false) == 
prod(sort("B"),[sort("A")],{});

test test27

test bool test27() = getPriority((Group) `{A -\> B C -\> D}`, false) == 
choice(sort("B"),{prod(sort("D"),[sort("C")],{}),prod(sort("B"),[sort("A")],{})});

test test28

test bool test28() = getPriority((Group) `{left: A -\> B}`, false) == 
\associativity(sort("B"),\left(),{prod(sort("B"),[sort("A")],{})});

test test29

test bool test29() = getPriority((Group) `{left: A -\> B B -\> C}`, false) ==
\associativity(sort("B"),\left(),{prod(sort("C"),[sort("B")],{}),prod(sort("B"),[sort("A")],{})});

test test30

test bool test30() = getPriority((Priority) `A -\> B`, false) == 
priority(sort("B"),[prod(sort("B"),[sort("A")],{})]);

test test31

test bool test31() = getPriority((Priority) `A -\> B .`, false) == 
priority(sort("B"),[prod(sort("B"),[sort("A")],{})]);

test test32

test bool test32() = getPriority((Priority) `A -\> B \<1\>`, false) == 
prod(sort("B"),[sort("A")],{});

test test33

test bool test33() = getPriority((Priority) `{A -\> B C -\> D}`, false) == 
priority(sort("B"),[choice(sort("B"),{prod(sort("D"),[sort("C")],{}),prod(sort("B"),[sort("A")],{})})]);

test test34

test bool test34() = getPriority((Priority) `A -\> B \> C -\> D`, false) ==
priority(sort("B"),[prod(sort("B"),[sort("A")],{}),prod(sort("D"),[sort("C")],{})]);

test test35

test bool test35() = getPriority((Priority) `A -\> B \> C -\> D \> E -\> F`, false) ==
priority(sort("B"),[prod(sort("B"),[sort("A")],{}),prod(sort("D"),[sort("C")],{}),prod(sort("F"),[sort("E")],{})]);

test test36

test bool test36() = getStartSymbols((Module) `module M exports context-free start-symbols A B C`) == 
{sort("A"), sort("B"), sort("C")};

test test37

test bool test37() = getStartSymbols((Module) `module M exports lexical start-symbols A B C`) == 
{sort("A"), sort("B"), sort("C")};

test test38

test bool test38() = getStartSymbols((Module) `module M exports start-symbols A B C`) == 
{sort("A"), sort("B"), sort("C")};

test test39

test bool test39() = getSymbols((Syms) `A B "ab"`, true) == [sort("A"), sort("B"), lit("ab")];

test test40

test bool test40() = getSymbol((Sym) `"abc"`, false)        == lit("abc");

test test41

test bool test41() = getSymbol((Sym) `"a\\\\c"`, false)         == lit("a\\c");

test test42

test bool test42() = getSymbol((Sym) `"a\>c"`, false)       == lit("a\>c");

test test43

test bool test43() = getSymbol((Sym) `ABC`, false)          == sort("ABC");

test test44

test bool test44() = getSymbol((Sym) `'abc'`, false)        == cilit("abc");

test test45

test bool test45() = getSymbol((Sym) `abc : ABC`, false)    == label("abc",sort("ABC"));

test test46

test bool test46() = getSymbol((Sym) `"abc" : ABC`, false)  == label("abc",sort("ABC"));

test test47

test bool test47() = getSymbol((Sym) `A[[B]]`, false)       == \parameterized-sort("A", [sort("B")]);

test test48

test bool test48() = getSymbol((Sym) `A?`, false)           == opt(sort("A"));

test test49

test bool test49() = getSymbol((Sym) `[a]`, false)          == \char-class([range(97,97)]);

test test50

test bool test50() = getSymbol((Sym) `A*`, false)           == \iter-star-seps(sort("A"),[\layouts("LAYOUTLIST")]);

test test51

test bool test51() = getSymbol((Sym) `A+`, false)           == \iter-seps(sort("A"),[\layouts("LAYOUTLIST")]);

test test52

test bool test52() = getSymbol((Sym) `A*?`, false)          == opt(\iter-star-seps(sort("A"),[\layouts("LAYOUTLIST")]));

test test53

test bool test53() = getSymbol((Sym) `A+?`, false)          == opt(\iter-seps(sort("A"),[\layouts("LAYOUTLIST")]));

test test54

test bool test54() = getSymbol((Sym) `{A "x"}*`, false)         == \iter-star-seps(sort("A"),[\layouts("LAYOUTLIST"),lit("x"),\layouts("LAYOUTLIST")]);

test test55

test bool test55() = getSymbol((Sym) `{A "x"}+`, false)         == \iter-seps(sort("A"),[\layouts("LAYOUTLIST"),lit("x"),\layouts("LAYOUTLIST")]);

test test56

test bool test56() = getSymbol((Sym) `{A "x"}*?`, false)    == opt(\iter-star-seps(sort("A"),[\layouts("LAYOUTLIST"),lit("x"),\layouts("LAYOUTLIST")]));

test test57

test bool test57() = getSymbol((Sym) `{A "x"}+?`, false)    == opt(\iter-seps(sort("A"),[\layouts("LAYOUTLIST"),lit("x"),\layouts("LAYOUTLIST")]));

test test58

test bool test58() = getSymbol((Sym) `A*`, true)            == \iter-star(sort("A"));

test test59

test bool test59() = getSymbol((Sym) `A+`, true)            == \iter(sort("A"));

test test60

test bool test60() = getSymbol((Sym) `A*?`, true)           == opt(\iter-star(sort("A")));

test test61

test bool test61() = getSymbol((Sym) `A+?`, true)           == opt(\iter(sort("A")));

test test62

test bool test62() = getSymbol((Sym) `{A "x"}*`, true)      == \iter-star-seps(sort("A"),[lit("x")]);

test test63

test bool test63() = getSymbol((Sym) `{A "x"}+`, true)      == \iter-seps(sort("A"),[lit("x")]);

test test64

test bool test64() = getSymbol((Sym) `{A "x"}*?`, true)         == opt(\iter-star-seps(sort("A"),[lit("x")]));

test test65

test bool test65() = getSymbol((Sym) `{A "x"}+?`, true)         == opt(\iter-seps(sort("A"),[lit("x")]));

test testUn1

test bool testUn1() = unescape((StrCon) `"abc"`)    == "abc";

test testUn2

test bool testUn2() = unescape((StrCon) `"a\\nc"`)  == "a\nc";

test testUn3

test bool testUn3() = unescape((StrCon) `"a\\"c"`)  == "a\"c";

test testUn4

test bool testUn4() = unescape((StrCon) `"a\\\\c"`)     == "a\\c";

test testUn5

test bool testUn5() = unescape((StrCon) `"a\\\\\\"c"`)  == "a\\\"c";

test testUn6

test bool testUn6() = unescape((SingleQuotedStrCon) `'abc'`)  == "abc";

test testUn7

test bool testUn7() = unescape((SingleQuotedStrCon) `'a\\nc'`) == "a\nc";

test testUn8

test bool testUn8() = unescape((SingleQuotedStrCon) `'a\\'c'`) == "a\'c";

test testUn9

test bool testUn9() = unescape((SingleQuotedStrCon) `'a\\\\c'`) == "a\\c";

test un20

test bool un20() =unescapeStr("abc")    == "abc";

test un21

test bool un21() =unescapeStr("a\nbc")  == "a\nbc";

test un22

test bool un22() =unescapeStr("a\\\nbc") == "a\\\nbc";

test un23

test bool un23() =unescapeStr("a\"bc")  == "a\"bc";

test un24

test bool un24() =unescapeStr("a\\\"bc") == "a\"bc";

test un25

test bool un25() =unescapeStr("a\\bc")  == "a\bc";

test un26

test bool un26() =unescapeStr("a\\\\tc") == "a\\tc";

test un27

test bool un27() =unescapeStr("a\>b")    == "a\>b";

test un28

test bool un28() =unescapeStr("a\<b")    == "a\<b";

test testCC1

test bool testCC1() = getCharClass((Class) `[]`)         == \char-class([]);

test testCC2

test bool testCC2() = getCharClass((Class) `[a]`)        == \char-class([range(97,97)]);

test testCC3

test bool testCC3() = getCharClass((Class) `[a-z]`)      == \char-class([range(97,122)]);

test testCC4

test bool testCC4() = getCharClass((Class) `[a-z0-9]`)   == \char-class([range(97,122), range(48,57)]);

test testCC5

test bool testCC5() = getCharClass((Class) `([a])`)      == \char-class([range(97,97)]);

test testCC6

test bool testCC6() = getCharClass((Class) `~[a]`)       == complement(\char-class([range(97,97)]));

test testCC7

test bool testCC7() = getCharClass((Class) `[a] /\\ [b]`) == intersection(\char-class([range(97,97)]), \char-class([range(98,98)]));

test testCC8

test bool testCC8() = getCharClass((Class) `[a] \\/ [b]`) == union(\char-class([range(97,97)]), \char-class([range(98,98)]));

test testCC9

test bool testCC9() = getCharClass((Class) `[a] / [b]`)  == difference(\char-class([range(97,97)]), \char-class([range(98,98)]));

test testCC10

test bool testCC10() = getCharClass((Class) `[\\n]`)       == \char-class([range(10,10)]);

test testCC11

test bool testCC11() = getCharClass((Class) `[\\t\\n]`)     == \char-class([range(9,9), range(10,10)]);

test testCC12

test bool testCC12() = getCharClass((Class) `~[\\0-\\31\\n\\t\\"\\\\]`) ==
complement(\char-class([range(0,25),range(10,10),range(9,9),range(34,34),range(92,92)]));

test testCC13

test bool testCC13() = getCharClass((Class) `[\\"]`)       == \char-class([range(34,34)]);

test testCR1

test bool testCR1() = getCharRange((Range) `a`)     == range(97,97);

test testCR2

test bool testCR2() = getCharRange((Range) `a-z`)   == range(97,122);

test testCR3

test bool testCR3() = getCharRange((Range) `\\n`)   ==  range(10,10);

test testCR4

test bool testCR4() = getCharRange((Range) `\\1-\\31`)  ==  range(1,25);

test testAs

test bool testAs() = getAttributes((Attrs) `{left, cons("decl")}`) == {\assoc(\left()),\tag("cons"("decl"))};

test testAs2

test bool testAs2() = getAttribute((Attribute) `left`)        == {\assoc(\left())};

test testAssoc

test bool testAssoc() = getAssociativity((Assoc) `left`) == \left();