Skip to content

Commit

Permalink
KrancScript.m: Use Piraha to parse .kranc files
Browse files Browse the repository at this point in the history
Currently we just print the XML structure to standard output.
  • Loading branch information
ianhinder committed Jan 26, 2012
1 parent 7a91c22 commit c1e21a2
Showing 1 changed file with 31 additions and 3 deletions.
34 changes: 31 additions & 3 deletions Tools/CodeGen/KrancScript.m
Expand Up @@ -18,18 +18,46 @@
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*)

BeginPackage["KrancScript`", {"Errors`", "Helpers`", "Kranc`"}];
BeginPackage["KrancScript`", {"Errors`", "Helpers`", "Kranc`", "JLink`"}];

CreateThornFromKrancScript;

Begin["`Private`"];

InstallJava[];

(* The JRE does not share a current directory with the Mathematica
kernel, so relative paths have to be converted to absolute paths.
It is not possible to change the JRE working directory. *)

absPath[s_String] :=
If[StringTake[s,1] === $PathnameSeparator, s, FileNameJoin[{Directory[],s}]];

fullKrancDir = absPath[KrancDirectory];

Print[AddToClassPath[
FileNameJoin[{fullKrancDir, "Tools","PirahaPEG","piraha.jar"}]]];

DefFn[
parseScript[filename_String] :=
Module[
{},
{g,m,c,sw,dout,xmlString,xml},
g = JavaNew["edu.lsu.cct.piraha.Grammar"];
g@compileFile[JavaNew["java.io.File", FileNameJoin[{fullKrancDir, "Auxiliary", "Grammars","kranc2.peg"}]]];

c = Grammar`readContents[JavaNew["java.io.File", absPath@filename]];

m = g@matcher["thorn", c];

If[!m@match[0], ThrowError["Failed to parse input file: ",m@near[]@toString[]]];

{}]];
sw = JavaNew["java.io.StringWriter"];
dout = JavaNew["edu.lsu.cct.piraha.DebugOutput", JavaNew["java.io.PrintWriter", sw]];
m@dumpMatchesXML[dout];
dout@flush[];
xmlString = sw@toString[];
xml = ImportString[xmlString, "XML"];
xml]];

DefFn[
CreateThornFromKrancScript[filename_String] :=
Expand Down

0 comments on commit c1e21a2

Please sign in to comment.