From c1e21a206e5b6a489ead878e14188b6a6967e1a7 Mon Sep 17 00:00:00 2001 From: Ian Hinder Date: Thu, 26 Jan 2012 14:32:08 -0600 Subject: KrancScript.m: Use Piraha to parse .kranc files Currently we just print the XML structure to standard output. --- Tools/CodeGen/KrancScript.m | 34 +++++++++++++++++++++++++++++++--- 1 file 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] := -- cgit v1.2.3