1
0
mirror of https://github.com/twiglet/cs2j.git synced 2025-01-18 13:15:17 +01:00

simplify doFile: Stop passing streams around

This commit is contained in:
Kevin Glynn 2010-10-23 10:00:25 +02:00
parent 0a2b669ac4
commit 3be0349115

View File

@ -20,7 +20,7 @@ namespace RusticiSoftware.Translator.CSharp
{ {
private const string VERSION = "2009.1.1.x"; private const string VERSION = "2009.1.1.x";
public delegate void FileProcessor(string fName, Stream s); public delegate void FileProcessor(string fName);
// show gui explorer of parse tree // show gui explorer of parse tree
internal static bool showTree = false; internal static bool showTree = false;
@ -120,21 +120,22 @@ namespace RusticiSoftware.Translator.CSharp
.Add ("exappdir=", dirs => addDirectories(exAppRoot, dirs)) .Add ("exappdir=", dirs => addDirectories(exAppRoot, dirs))
.Add ("exclude=", dirs => addDirectories(exclude, dirs)) .Add ("exclude=", dirs => addDirectories(exclude, dirs))
; ;
//TODO: fix enum dump
// Final argument is translation target // Final argument is translation target
remArgs = p.Parse (args); remArgs = p.Parse (args);
// Load .Net templates // Load .Net templates
foreach (string r in netRoot) foreach (string r in netRoot)
doFile(new FileInfo(r), ".xml", addNetTranslation, exNetRoot); doFile(r, ".xml", addNetTranslation, exNetRoot);
// Load Application Class Signatures (i.e. generate templates) // Load Application Class Signatures (i.e. generate templates)
if (appRoot.Count == 0) if (appRoot.Count == 0)
// By default translation target is application root // By default translation target is application root
appRoot.Add(remArgs[0]); appRoot.Add(remArgs[0]);
foreach (string r in appRoot) foreach (string r in appRoot)
doFile(new FileInfo(r), ".cs", addAppSigTranslation, exAppRoot); // parse it doFile(r, ".cs", addAppSigTranslation, exAppRoot); // parse it
if (dumpXmls) if (dumpXmls)
{ {
// Get package name and convert to directory name // Get package name and convert to directory name
@ -153,12 +154,12 @@ namespace RusticiSoftware.Translator.CSharp
w.Close(); w.Close();
} }
} }
// keving: comment out for now doFile(new FileInfo(args[i]), ".cs", translateFile, exclude); // parse it doFile(remArgs[0], ".cs", translateFile, exclude); // parse it
if (enumXmlWriter != null) if (dumpEnums)
{ {
enumXmlWriter.WriteEndElement(); enumXmlWriter.WriteEndElement();
enumXmlWriter.Close(); }
} enumXmlWriter.Close();
} }
else else
{ {
@ -181,65 +182,57 @@ namespace RusticiSoftware.Translator.CSharp
// Call processFile on all files below f that have the given extension // Call processFile on all files below f that have the given extension
public static void doFile(FileInfo f, string ext, FileProcessor processFile, IList<string> excludes) public static void doFile(string root, string ext, FileProcessor processFile, IList<string> excludes)
{ {
string canonicalPath = Path.GetFullPath(root);
// If this is a directory, walk each file/dir in that directory // If this is a directory, walk each file/dir in that directory
if (!excludes.Contains(Path.GetFullPath(f.FullName).ToLower())) if (!excludes.Contains(canonicalPath.ToLower()))
{ {
if (Directory.Exists(f.FullName)) if (Directory.Exists(canonicalPath))
{ {
string[] files = Directory.GetFileSystemEntries(f.FullName); string[] files = Directory.GetFileSystemEntries(canonicalPath);
for (int i = 0; i < files.Length; i++) for (int i = 0; i < files.Length; i++)
doFile(new FileInfo(Path.Combine(f.FullName, files[i])), ext, processFile, excludes); doFile(Path.Combine(canonicalPath, files[i]), ext, processFile, excludes);
} }
else if ((f.Name.Length > ext.Length) && f.Name.Substring(f.Name.Length - ext.Length).Equals(ext)) else if ((Path.GetFileName(canonicalPath).Length > ext.Length) && canonicalPath.Substring(canonicalPath.Length - ext.Length).Equals(ext))
{ {
FileStream fs = null; if (verbosity >= 2) Console.WriteLine(" " + canonicalPath);
if (verbosity >= 2) Console.WriteLine(" " + f.FullName);
try try
{ {
Stream s = new FileStream(f.FullName, FileMode.Open, FileAccess.Read);
processFile(f.FullName, s); processFile(canonicalPath);
} }
catch (Exception e) catch (Exception e)
{ {
Console.Error.WriteLine("\nCannot process file: " + f.FullName); Console.Error.WriteLine("\nCannot process file: " + canonicalPath);
Console.Error.WriteLine("exception: " + e); Console.Error.WriteLine("exception: " + e);
} }
finally
{
if (fs != null) fs.Close();
}
} }
} }
} }
public static CommonTreeNodeStream parseFile(string fullName, Stream s) public static BufferedTreeNodeStream parseFile(string fullName)
{ {
CommonTokenStream tokens = null;
CommonTokenStream tokens = null;
Console.WriteLine("Parsing " + Path.GetFileName(fullName)); if (verbosity > 2) Console.WriteLine("Parsing " + Path.GetFileName(fullName));
PreProcessor lex = new PreProcessor(); PreProcessor lex = new PreProcessor();;
lex.AddDefine(macroDefines);
ICharStream input = new ANTLRFileStream(fullName); ICharStream input = new ANTLRFileStream(fullName);
lex.CharStream = input; lex.CharStream = input;
tokens = new CommonTokenStream(lex); tokens = new CommonTokenStream(lex);
csParser p = new csParser(tokens); csParser p = new csParser(tokens);
object parser_rt = p.compilation_unit(); csParser.compilation_unit_return parser_rt;
// Sometimes ANTLR returns a CommonErrorNode if we can't parse the file parser_rt = p.compilation_unit();
if (parser_rt is CommonErrorNode) ITree parse_tree = (ITree)parser_rt.Tree;
{ if (verbosity > 2) Console.Out.WriteLine(parse_tree.ToStringTree());
Console.WriteLine(((CommonErrorNode)parser_rt).trappedException.Message);
return null;
}
CommonTree tree = (CommonTree)((RuleReturnScope)parser_rt).Tree; BufferedTreeNodeStream nodes = new BufferedTreeNodeStream(parse_tree);
// Check if we didn't get an AST
// This often happens if your grammar and tree grammar don't match if (nodes == null)
if (tree == null)
{ {
if (tokens.Count > 0) if (tokens.Count > 0)
{ {
@ -249,18 +242,6 @@ namespace RusticiSoftware.Translator.CSharp
{ {
// the file was empty, this is not an error. // the file was empty, this is not an error.
} }
return null;
}
// Get the AST stream
CommonTreeNodeStream nodes = new CommonTreeNodeStream(tree);
// Add the tokens for DumpNodes, otherwise there are no token names to print out.
nodes.TokenStream = tokens;
// Dump the tree nodes if -dumpcsharp is passed on the command line.
if (dumpCSharp)
{
AntlrUtils.AntlrUtils.DumpNodes(nodes);
} }
return nodes; return nodes;
@ -268,29 +249,32 @@ namespace RusticiSoftware.Translator.CSharp
} }
// Here's where we do the real work... // Here's where we do the real work...
public static void addNetTranslation(string fullName, Stream s) public static void addNetTranslation(string fullName)
{ {
TypeRepTemplate t = TypeRepTemplate.newInstance(s); Stream s = new FileStream(fullName, FileMode.Open, FileAccess.Read);
TypeRepTemplate t = TypeRepTemplate.newInstance(s);
appEnv[t.TypeName] = t; appEnv[t.TypeName] = t;
} }
// Here's where we do the real work... // Here's where we do the real work...
public static void addAppSigTranslation(string fullName, Stream s) public static void addAppSigTranslation(string fullName)
{ {
CommonTreeNodeStream t = parseFile(fullName, s); BufferedTreeNodeStream nodes = parseFile(fullName);
if (t != null) if (nodes != null)
{ {
// A prescan of all files to build an environment mapping qualified name to typereptemplate
// CSharpEnvBuilder envBuilder = new CSharpEnvBuilder(); TemplateExtracter templateWalker = new TemplateExtracter(nodes);
// envBuilder.compilationUnit(t, null, appEnv); templateWalker.DebugLevel = 10;
templateWalker.compilation_unit();
} }
} }
// Here's where we do the real work... // Here's where we do the real work...
//public static void translateFile(string fullName, ICharStream s) public static void translateFile(string fullName)
//{ {
// string f = Path.GetFileName(fullName); BufferedTreeNodeStream nodes = parseFile(fullName);
if (dumpCSharp) AntlrUtils.AntlrUtils.DumpNodes(new CommonTreeNodeStream(nodes.TreeSource));
// ASTNode t = parseFile(f, s); // ASTNode t = parseFile(f, s);
// if (t != null) // if (t != null)
// { // {
@ -387,7 +371,7 @@ namespace RusticiSoftware.Translator.CSharp
// writer.compilationUnit(netTx.getAST(), w, enumXmlWriter, filter); // writer.compilationUnit(netTx.getAST(), w, enumXmlWriter, filter);
// w.Close(); // w.Close();
// } // }
// double elapsedTime = ((DateTime.Now.Ticks - startTime) / TimeSpan.TicksPerMillisecond) / 1000.0; // double elapsedTime = ((DateTime.Now.Ticks - startTime) / TimeSpan.TicksPerMillisecond) / 1000.0;
// //System.Console.Out.WriteLine(writer.ToString()); // //System.Console.Out.WriteLine(writer.ToString());
@ -395,6 +379,6 @@ namespace RusticiSoftware.Translator.CSharp
// System.Console.Out.WriteLine(""); // System.Console.Out.WriteLine("");
// System.Console.Out.WriteLine("Pretty-printed {0} in: {1} seconds.", f, elapsedTime); // System.Console.Out.WriteLine("Pretty-printed {0} in: {1} seconds.", f, elapsedTime);
// } // }
//} }
} }
} }