Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
reading BGF and XBGF completed in a kind of a neat fashion
- Loading branch information
1 parent
2ed2dbd
commit ad31398
Showing
5 changed files
with
81 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
@contributor{Vadim Zaytsev - vadim@grammarware.net - SWAT, CWI} | ||
module io::ReadBGF | ||
|
||
import IO; | ||
import syntax::BGF; | ||
import lang::xml::DOM; | ||
|
||
public BGFGrammar readBGF(loc f) | ||
{ | ||
if (document(element(namespace(_,"http://planet-sl.org/bgf"),"grammar",L)) := parseXMLDOMTrim(readFile(f))) | ||
return grammar([s | element(none(),"root",[charData(s)]) <- L],[mapprod(p) | p <- L, element(namespace(_,"http://planet-sl.org/bgf"),name,kids) := p]); | ||
else | ||
throw "<f> is not an BGF file"; | ||
} | ||
|
||
public BGFProduction mapprod(Node n) | ||
{ | ||
str label = ""; | ||
str lhs = ""; | ||
BGFExpression rhs; | ||
if (element(namespace(_,"http://planet-sl.org/bgf"),"production",kids) := n) | ||
{ | ||
for (k <- kids) | ||
switch (k) | ||
{ | ||
case element(none(),"label",[charData(str s)]) : label = s; | ||
case element(none(),"nonterminal",[charData(str s)]) : lhs = s; | ||
case element(namespace(_,"http://planet-sl.org/bgf"),"expression",[expr]): rhs = mapexpr(expr); | ||
} | ||
return production (label, lhs, rhs); | ||
} | ||
else | ||
throw "ERROR in mapprod:\n<n>"; | ||
} | ||
|
||
public BGFExpression mapexpr(Node n) | ||
{ | ||
switch(n) | ||
{ | ||
// just in case | ||
case element(namespace(_,"http://planet-sl.org/bgf"),"expression",[e]): return mapexpr(e); | ||
// regular alternatives | ||
case element(none(),"epsilon",[]): return epsilon(); | ||
case element(none(),"empty",[]): return empty(); | ||
case element(none(),"value",[charData("string")]): return val(string()); | ||
case element(none(),"value",[charData("int")]): return val(integer()); | ||
case element(none(),"any",[]): return anything(); | ||
case element(none(),"terminal",[charData(str s)]): return terminal(s); | ||
case element(none(),"nonterminal",[charData(str s)]): return nonterminal(s); | ||
case element(none(),"selectable",[element(none(),"selector",[charData(str s)]),expr]): return selectable(s,mapexpr(expr)); | ||
case element(none(),"sequence",kids): return sequence([mapexpr(k) | k <- kids]); | ||
case element(none(),"choice",kids): return choice([mapexpr(k) | k <- kids]); | ||
case element(none(),"marked",[expr]): return marked(mapexpr(expr)); | ||
case element(none(),"optional",[expr]): return optional(mapexpr(expr)); | ||
case element(none(),"plus",[expr]): return plus(mapexpr(expr)); | ||
case element(none(),"star",[expr]): return star(mapexpr(expr)); | ||
case element(none(),"starsepplus",[e1,e2]): return starsepplus(mapexpr(e1),mapexpr(e2)); | ||
case element(none(),"starsepstar",[e1,e2]): return starsepstar(mapexpr(e1),mapexpr(e2)); | ||
default: throw "ERROR in mapexpr:\n<n>"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters