aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIan Hinder <ian.hinder@aei.mpg.de>2012-01-26 14:32:08 -0600
committerIan Hinder <ian.hinder@aei.mpg.de>2012-01-26 14:32:08 -0600
commitc1e21a206e5b6a489ead878e14188b6a6967e1a7 (patch)
treef5eddd7bfb1615e2d009f8cf8d75cc2f8f651389
parent7a91c22cc98937bc6fb85c2054c14c7524f5360b (diff)
KrancScript.m: Use Piraha to parse .kranc files
Currently we just print the XML structure to standard output.
-rw-r--r--Tools/CodeGen/KrancScript.m34
1 files changed, 31 insertions, 3 deletions
diff --git a/Tools/CodeGen/KrancScript.m b/Tools/CodeGen/KrancScript.m
index 2e82e7b..3076610 100644
--- a/Tools/CodeGen/KrancScript.m
+++ b/Tools/CodeGen/KrancScript.m
@@ -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] :=