private Assembly LoadAssembly(string srcName, string nsName) { try { string srcDir = Path.GetDirectoryName(srcName); string outDir = "."; Scanner scanner = new Scanner(srcName); Parser parser = new Parser(scanner); string traceFileName = Path.Combine(srcDir ?? "", "trace.txt"); parser.trace = new StreamWriter(new FileStream(traceFileName, FileMode.Create)); parser.tab = new Tab(parser); parser.dfa = new DFA(parser); parser.pgen = new at.jku.ssw.Coco.ParserGen(parser); parser.tab.srcName = srcName; parser.tab.srcDir = srcDir; parser.tab.nsName = nsName; parser.tab.outDir = outDir; parser.Parse(); parser.trace.Close(); if (parser.errors.count != 0) { throw new InvalidOperationException($"Failed to parse grammar file! Found {parser.errors.count} errors."); } var parameters = new CompilerParameters { GenerateExecutable = false, GenerateInMemory = true, OutputAssembly = nsName, TreatWarningsAsErrors = false }; parameters.ReferencedAssemblies.Add("System.dll"); parameters.ReferencedAssemblies.Add("mscorlib.dll"); parameters.ReferencedAssemblies.Add("Microsoft.CSharp.dll"); parameters.ReferencedAssemblies.Add("XCompilR.Library.dll"); var codeProvider = new CSharpCodeProvider(new Dictionary<string, string> { { "CompilerVersion", "v4.0" } }); var results = codeProvider.CompileAssemblyFromFile(parameters, "Parser.cs", "Scanner.cs"); if (results.Errors.HasErrors || results.Errors.HasWarnings) { throw new InvalidOperationException($"Failed to generate dynamic assembly! Found {results.Errors.Count} errors."); } return results.CompiledAssembly; } catch (Exception exception) { throw new XCompileException($"Could not create {nsName} assembly!", exception); } }
public static bool Generate( string srcName, string ddtString, string frameDir, string nsName, bool directDebug, Action<string> writeMessage, Action<string> writeError) { string traceFileName = null; try { int pos = srcName.LastIndexOf('/'); if (pos < 0) pos = srcName.LastIndexOf('\\'); string file = srcName; string srcDir = srcName.Substring(0, pos + 1); Scanner scanner = new Scanner(file); Parser parser = new Parser(scanner); traceFileName = srcDir + "trace.txt"; parser.trace = new StreamWriter(new FileStream(traceFileName, FileMode.Create)); parser.tab = new Tab(parser); parser.dfa = new DFA(parser); parser.pgen = new ParserGen(parser); parser.pgen.DirectDebug = directDebug; parser.tab.srcName = srcName; parser.tab.srcDir = srcDir; parser.tab.nsName = nsName; parser.tab.frameDir = frameDir; if (ddtString != null) parser.tab.SetDDT(ddtString); parser.errors.WriteMessage = writeMessage; parser.errors.WriteError = writeError; parser.errors.realFile = srcName; parser.Parse(); parser.trace.Close(); FileInfo f = new FileInfo(traceFileName); if (f.Length == 0) f.Delete(); else writeMessage("trace output is in " + traceFileName); writeMessage(String.Format("{0} errors detected", parser.errors.count)); return parser.errors.count <= 0; } catch (IOException ex) { writeError(ex.Message); return false; } catch (FatalError ex) { writeError(ex.Message); return false; } }
public static int Main(string[] arg) { Console.WriteLine("Coco/R C# (Apr 23, 2010)"); string srcName = null, nsName = null, prefixName = null; string frameDir = null, ddtString = null, outDir = null; bool makeBackup = false; bool traceToFile = true; int retVal = 1; for (int i = 0; i < arg.Length; i++) { if (arg[i] == "-help") { printUsage(null); return 0; } else if (arg[i] == "-namespace") { if (++i == arg.Length) { printUsage("missing parameter on -namespace"); return retVal; } nsName = arg[i]; } else if (arg[i] == "-prefix") { if (++i == arg.Length) { printUsage("missing parameter on -prefix"); return retVal; } prefixName = arg[i]; } else if (arg[i] == "-frames") { if (++i == arg.Length) { printUsage("missing parameter on -frames"); return retVal; } frameDir = arg[i]; } else if (arg[i] == "-trace") { if (++i == arg.Length) { printUsage("missing parameter on -trace"); return retVal; } traceToFile = true; ddtString = arg[i]; } else if (arg[i] == "-trace2") { if (++i == arg.Length) { printUsage("missing parameter on -trace2"); return retVal; } traceToFile = false; ddtString = arg[i]; } else if (arg[i] == "-o") { if (++i == arg.Length) { printUsage("missing parameter on -o"); return retVal; } outDir = arg[i]; } else if (arg[i] == "-bak") { makeBackup = true; } else if (arg[i][0] == '-') { printUsage("Error: unknown option: '" + arg[i] + "'"); return retVal; } else if (srcName != null) { printUsage("grammar can only be specified once"); return retVal; } else { srcName = arg[i]; } } if (srcName != null) { string traceFileName = null; try { string srcDir = Path.GetDirectoryName(srcName); Scanner scanner = new Scanner(srcName); Parser parser = new Parser(scanner); parser.tab = new Tab(parser); parser.tab.srcName = srcName; parser.tab.srcDir = srcDir; parser.tab.nsName = nsName; parser.tab.prefixName = prefixName; parser.tab.frameDir = frameDir; parser.tab.outDir = (outDir != null) ? outDir : srcDir; parser.tab.SetDDT(ddtString); parser.tab.makeBackup = makeBackup; if (traceToFile) { traceFileName = Path.Combine(parser.tab.outDir, "trace.txt"); parser.tab.trace = new StreamWriter(new FileStream(traceFileName, FileMode.Create)); } parser.dfa = new DFA(parser); parser.pgen = new ParserGen(parser); parser.Parse(); if (traceToFile) { parser.tab.trace.Close(); FileInfo f = new FileInfo(traceFileName); if (f.Length == 0) f.Delete(); else Console.WriteLine("trace output is in " + traceFileName); } Console.WriteLine("{0} errors detected", parser.errors.count); if (parser.errors.count == 0) { retVal = 0; } } catch (IOException) { Console.WriteLine("-- could not open " + traceFileName); } catch (FatalError e) { Console.WriteLine("-- " + e.Message); } } else { printUsage(null); } return retVal; }
string tokenString; // used in declarations of literal tokens #endregion Fields #region Constructors /*-------------------------------------------------------------------------*/ public Parser(Scanner scanner) { this.scanner = scanner; errors = new Errors(); }
public static int Main(string[] arg) { Console.WriteLine("Coco/R (Oct 1, 2008)"); string srcName = null, nsName = null, frameDir = null, ddtString = null, traceFileName = null, outDir = null; int retVal = 1; for (int i = 0; i < arg.Length; i++) { if (arg[i] == "-namespace" && i < arg.Length - 1) nsName = arg[++i]; else if (arg[i] == "-frames" && i < arg.Length - 1) frameDir = arg[++i]; else if (arg[i] == "-trace" && i < arg.Length - 1) ddtString = arg[++i]; else if (arg[i] == "-o" && i < arg.Length - 1) outDir = arg[++i]; else srcName = arg[i]; } if (arg.Length > 0 && srcName != null) { try { string srcDir = Path.GetDirectoryName(srcName); Scanner scanner = new Scanner(srcName); Parser parser = new Parser(scanner); traceFileName = Path.Combine(srcDir, "trace.txt"); parser.trace = new StreamWriter(new FileStream(traceFileName, FileMode.Create)); parser.tab = new Tab(parser); parser.dfa = new DFA(parser); parser.pgen = new ParserGen(parser); parser.tab.srcName = srcName; parser.tab.srcDir = srcDir; parser.tab.nsName = nsName; parser.tab.frameDir = frameDir; parser.tab.outDir = (outDir != null) ? outDir : srcDir; if (ddtString != null) parser.tab.SetDDT(ddtString); parser.Parse(); parser.trace.Close(); FileInfo f = new FileInfo(traceFileName); if (f.Length == 0) f.Delete(); else Console.WriteLine("trace output is in " + traceFileName); Console.WriteLine("{0} errors detected", parser.errors.count); if (parser.errors.count == 0) { retVal = 0; } } catch (IOException) { Console.WriteLine("-- could not open " + traceFileName); } catch (FatalError e) { Console.WriteLine("-- " + e.Message); } } else { Console.WriteLine("Usage: Coco Grammar.ATG {{Option}}{0}" + "Options:{0}" + " -namespace <namespaceName>{0}" + " -frames <frameFilesDirectory>{0}" + " -trace <traceString>{0}" + " -o <outputDirectory>{0}" + "Valid characters in the trace string:{0}" + " A trace automaton{0}" + " F list first/follow sets{0}" + " G print syntax graph{0}" + " I trace computation of first sets{0}" + " J list ANY and SYNC sets{0}" + " P print statistics{0}" + " S list symbol table{0}" + " X list cross reference table{0}" + "Scanner.frame and Parser.frame files needed in ATG directory{0}" + "or in a directory specified in the -frames option.", Environment.NewLine); } return retVal; }
/// <summary> /// Called by SharpDevelop when your tool has to generate code. /// </summary> /// <param name="item"> /// The file for which your tool should generate code. /// </param> public void GenerateCode(FileProjectItem item, CustomToolContext context) { string dir = Path.GetDirectoryName(item.FileName); string traceFile = Path.Combine(dir, "trace.txt"); string parserOutFile = Path.Combine(dir, "Parser.cs"); string scannerOutFile = Path.Combine(dir, "Scanner.cs"); ErrorsInTaskService.ClearAllErrors(); CocoCategory.ClearText(); CocoCategory.AppendLine("Generating " + Path.GetFileName(item.FileName) + "..."); using (FileStream inputStream = File.OpenRead(item.FileName)) { Scanner scanner = new Scanner(inputStream); Parser parser = new Parser(scanner); parser.errors = new ErrorsInTaskService(item.FileName); parser.errors.errorStream = new MessageViewCategoryWriter(CocoCategory); parser.tab = new Tab(parser); parser.tab.trace = new LazyTextWriter(delegate { return new StreamWriter(traceFile); } ); parser.dfa = new DFA(parser); parser.pgen = new ParserGen(parser); parser.tab.srcName = item.FileName; parser.tab.srcDir = Path.GetDirectoryName(item.FileName); parser.tab.nsName = context.OutputNamespace; parser.tab.frameDir = parser.tab.srcDir; parser.tab.outDir = parser.tab.srcDir; try { parser.Parse(); } catch (FatalError err) { CocoCategory.AppendLine("Fatal error: " + err.Message); return; } finally { parser.tab.trace.Close(); } CocoCategory.AppendLine("Done. " + parser.errors.count + " error(s)."); } if (File.Exists(parserOutFile)) { context.EnsureOutputFileIsInProject(item, parserOutFile); } if (File.Exists(scannerOutFile)) { context.EnsureOutputFileIsInProject(item, scannerOutFile); } }
public override byte[] GenerateCode(string fileName, string fileContents) { var result = new StringBuilder(); try { using (var tempDir = new TempDirectory()) { string fileDir = Path.GetDirectoryName(fileName); string outDir = tempDir.DirectoryInfo.FullName; string scannerFile = Path.Combine(outDir, "scanner.cs"); string parserFile = Path.Combine(outDir, "parser.cs"); string traceFile = Path.Combine(outDir, "trace.txt"); var scanner = new Coco.Scanner(fileName); var parser = new Coco.Parser(scanner); using (var traceWriter = File.CreateText(traceFile)) { parser.errors.errorStream = traceWriter; parser.tab = new Coco.Tab(parser); parser.dfa = new Coco.DFA(parser); parser.pgen = new Coco.ParserGen(parser); parser.tab.srcName = fileName; parser.tab.srcDir = fileDir; parser.tab.nsName = this.DefaultNamespace; parser.tab.frameDir = fileDir; parser.tab.outDir = outDir; parser.tab.trace = traceWriter; parser.tab.SetDDT("P"); parser.Parse(); } if (File.Exists(scannerFile)) { result.AppendLine(File.ReadAllText(scannerFile)); } if (File.Exists(parserFile)) { result.AppendLine(File.ReadAllText(parserFile)); } if (File.Exists(traceFile)) { result.AppendLine("/*** OUTPUT ***"); result.AppendLine(File.ReadAllText(traceFile)); result.AppendLine("*/"); } } } catch (Exception ex) { result.Length = 0; result.AppendLine("/*** INTERNAL ERROR ***"); result.AppendLine(ex.ToString()); result.AppendLine("*/"); } return Encoding.UTF8.GetBytes(result.ToString()); }
public static int Main(string[] arg) { Console.WriteLine("Coco/R (Oct 1, 2008)"); string srcName = null, nsName = null, frameDir = null, ddtString = null, traceFileName = null, outDir = null; int retVal = 1; for (int i = 0; i < arg.Length; i++) { if (arg[i] == "-namespace" && i < arg.Length - 1) { nsName = arg[++i]; } else if (arg[i] == "-frames" && i < arg.Length - 1) { frameDir = arg[++i]; } else if (arg[i] == "-trace" && i < arg.Length - 1) { ddtString = arg[++i]; } else if (arg[i] == "-o" && i < arg.Length - 1) { outDir = arg[++i]; } else { srcName = arg[i]; } } if (arg.Length > 0 && srcName != null) { try { string srcDir = Path.GetDirectoryName(srcName); Scanner scanner = new Scanner(srcName); Parser parser = new Parser(scanner); traceFileName = Path.Combine(srcDir, "trace.txt"); parser.trace = new StreamWriter(new FileStream(traceFileName, FileMode.Create)); parser.tab = new Tab(parser); parser.dfa = new DFA(parser); parser.pgen = new ParserGen(parser); parser.tab.srcName = srcName; parser.tab.srcDir = srcDir; parser.tab.nsName = nsName; parser.tab.frameDir = frameDir; parser.tab.outDir = (outDir != null) ? outDir : srcDir; if (ddtString != null) { parser.tab.SetDDT(ddtString); } parser.Parse(); parser.trace.Close(); FileInfo f = new FileInfo(traceFileName); if (f.Length == 0) { f.Delete(); } else { Console.WriteLine("trace output is in " + traceFileName); } Console.WriteLine("{0} errors detected", parser.errors.count); if (parser.errors.count == 0) { retVal = 0; } } catch (IOException) { Console.WriteLine("-- could not open " + traceFileName); } catch (FatalError e) { Console.WriteLine("-- " + e.Message); } } else { Console.WriteLine("Usage: Coco Grammar.ATG {{Option}}{0}" + "Options:{0}" + " -namespace <namespaceName>{0}" + " -frames <frameFilesDirectory>{0}" + " -trace <traceString>{0}" + " -o <outputDirectory>{0}" + "Valid characters in the trace string:{0}" + " A trace automaton{0}" + " F list first/follow sets{0}" + " G print syntax graph{0}" + " I trace computation of first sets{0}" + " J list ANY and SYNC sets{0}" + " P print statistics{0}" + " S list symbol table{0}" + " X list cross reference table{0}" + "Scanner.frame and Parser.frame files needed in ATG directory{0}" + "or in a directory specified in the -frames option.", Environment.NewLine); } return(retVal); }