static void BuildSyntax(CompilerFrontEnd cfe) { int Successes = 0, skip = -1; Boolean DoSkip = false; if (paramSkipUntil != null && int.TryParse(paramSkipUntil, out skip)) { DoSkip = true; } foreach (var cur in pending.AsIndexable()) { if (DoSkip && cur.Index < skip) { continue; } try { if (ProcessFile(cfe, cur.Value, cur.Total, cur.Index, false)) { ++Successes; } } catch (Exception e) { WriteLine(ConCol.Error, "Exception processing file [{0}]: {1}", cur.Value.Name, e.Message); e.BreakException(); } } WriteLine(Successes == pending.Count ? ConCol.Success : ConCol.Warning, "Compiled {0}/{1}", Successes, pending.Count); }
private void MainForm_Load(object sender, EventArgs e) { Cfe = new CompilerFrontEnd(); Cfe.Options = new CompilerOptions(); try { if (File.Exists(SettingsFile)) { using (var s = File.OpenRead(SettingsFile)) Cfe.Options.LoadOptions(s); } else { using (var s = File.OpenWrite(SettingsFile)) Cfe.Options.SaveOptions(s); } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } PropertyEditWindow = new OptionEdit(Cfe.Options); GraphWindow = new GraphView(this); EvalWindow = new OptionEdit(EvalOptions); if (!Directory.Exists(SnippetPath)) { Directory.CreateDirectory(SnippetPath); } SnipFillDir(null); richTextBoxInput.Focus(); richTextBoxInput.Select(); }
static Boolean ProcessFile(CompilerFrontEnd cfe, FileInfo file, int total, int current, Boolean basic = true) { SyntaxBuilder sb = null; Boolean OscarKilo = false; ConsoleKey ck; while (!OscarKilo) { sb = new SyntaxBuilder(cfe, file.FullName); WriteLine(file.Name); SyntaxBuilder.Stages last = (SyntaxBuilder.Stages)(-1); while (sb.Step()) { if (last != sb.Stage) { Write(ConCol.Info, "\r({0}/{1}): {2}", current + 1, total, (last = sb.Stage).ToString()); } } WriteLine(); if (!sb.OscarKilo) { if (!sb.Success) { WriteLine(ConCol.Error, "Unable to process [{0}]:", file.Name); } if (sb.Warns.Count > 0) { WriteLine(ConCol.Warning, "Warnings:"); foreach (var m in sb.Warns.AsIndexable()) { WriteLine(ConCol.Warning, "{0}) {1}", m.Index + 1, m.Value); } WriteLine(); } if (sb.Errors.Count > 0) { WriteLine(ConCol.Error, "Errors:"); foreach (var m in sb.Errors.AsIndexable()) { WriteLine(ConCol.Error, "{0}) {1}", m.Index + 1, m.Value); } if (sb.Errors.Count > 0) { ExecuteOnError(file.FullName, sb.Errors[0].Location.Value); } } WriteLine("Press anything..."); Console.ReadKey(true); ck = CharQuestion("Do you want to (c)ontinue or (t)ry again?", ConsoleKey.C, ConsoleKey.T, ConsoleKey.F); if (ck == ConsoleKey.C) { break; } } else { OscarKilo = true; } } if (sb.Success) { cfe.UserDefinedDOM = sb.GetFinalDOM(ValidationBuilder.GetExtlessName(file.Name)); } if (sb.Success && !basic) { String targetName = paramCopyName == null ? file.Name : paramCopyName; String finalTarget = Path.Combine(paramOutputFolder, targetName); String targetPacked = "{0}.packed{1}".Fmt(ValidationBuilder.GetExtlessName(targetName), (new FileInfo(targetName)).Extension), finalTargetPacked = null; if (paramCopyName != null || paramCrossCheck) { CheckOutputFolder(); WriteLine(ConCol.Info, "Outputing generated source."); using (var sw = new StreamWriter(finalTarget)) sw.Write(cfe.UserDefinedDOM.Express(0)); WriteLine(ConCol.Info, "Emitted generated file [{0}].".Fmt(targetName)); } if (paramEmitPacked) { finalTargetPacked = Path.Combine(paramOutputFolder, targetPacked); try { using (StreamWriter sw = new StreamWriter(finalTargetPacked)) sb.SavePacked(sw); WriteLine(ConCol.Success, "Emitted packed source [{0}].", finalTargetPacked); } catch (Exception e) { WriteLine(ConCol.Error, "Unable to emit packed source of [{0}]: {1}", targetPacked, e.Message); e.BreakException(); return(false); } } if (paramCrossCheck) { try { CodeDOMModule A, B; CheckOutputFolder(); A = cfe.UserDefinedDOM; if (!ProcessFile(cfe, new FileInfo(finalTarget), total, current)) { WriteLine(ConCol.Error, "Cross check failed! Please contact Chaz pronto!"); throw new Exception("Cross check failed. Compiler isn't up to standard."); } B = cfe.UserDefinedDOM; var d = CodeDOMComparer.CompareDOM(A, B); if (d.Count == 0) { WriteLine(ConCol.Success, "Crosscheck Original To Copy Success!"); } else { WriteLine(ConCol.Warning, "Crosscheck returned differences:"); foreach (var item in d.AsIndexable()) { WriteLine(ConCol.Warning, "{0}> {1}", item.Index + 1, item.Value.ToString()); } throw new Exception("Cross check failed. Compiler isn't up to standard."); } if (paramEmitPacked) { WriteLine(ConCol.Info, "Now cross checking with packed [{0}].", finalTargetPacked); if (!ProcessFile(cfe, new FileInfo(finalTargetPacked), total, current)) { WriteLine(ConCol.Error, "Packed cross check failed! Please contact Chaz pronto!"); throw new Exception("Packed cross check failed. Compiler isn't up to standard."); } B = cfe.UserDefinedDOM; d = CodeDOMComparer.CompareDOM(A, B); if (d.Count == 0) { WriteLine(ConCol.Success, "Crosscheck Original To Packed Success!"); } else { WriteLine(ConCol.Warning, "Packed crosscheck returned differences:"); foreach (var item in d.AsIndexable()) { WriteLine(ConCol.Warning, "{0}> {1}", item.Index + 1, item.Value.ToString()); } throw new Exception("Packed Cross check failed. Compiler isn't up to standard."); } } cfe.UserDefinedDOM = A; } catch (Exception e) { WriteLine(ConCol.Error, "Crosscheck of file [{0}] failed: {1}".Fmt(targetName, e.Message)); e.BreakException(); return(false); } } } return(sb.Success); }
static void Validate(CompilerFrontEnd cfe) { ValidationBuilder v = new ValidationBuilder(cfe.Options, global, paramSetScriptExt, paramBasePath, paramMainPath); List <ValidationBuilder.ModuleInfo> files = new List <ValidationBuilder.ModuleInfo>(); ValidationBuilder.ModuleInfo moduleInfo = new ValidationBuilder.ModuleInfo(); while (v.NextFile(ref moduleInfo)) { if (moduleInfo.Paths == null) { WriteLine(ConCol.Error, "Path error for {0}, path difference bug!", moduleInfo.Name); continue; } files.Add(moduleInfo); } int Successes = 0; foreach (var mi in files.AsIndexable()) { if (ProcessFile(cfe, mi.Value.Info, mi.Total, mi.Index)) { ++Successes; cfe.UserDefinedDOM.FullPath = mi.Value.Info.FullName; v.Ca.AddNewModule(cfe.UserDefinedDOM, mi.Value.Paths); } } if (v.Ca.FinalAnalyze()) //TODO: Does root CM even become resolved? { WriteLine(ConCol.Success, "All symbols are resolved."); } if (paramValidationLog == null) { WriteLine(ConCol.Warning, v.BuildMessageStats()); WriteLine(ConCol.Warning, "Warnings ({0} Total):".Fmt(v.Ca.Warnings.Count)); foreach (var m in v.Ca.Warnings.AsIndexable()) { if (m.Index < 100) { WriteLine(ConCol.Warning, "{0}) {1}", m.Index + 1, m.Value.ToString()); } else { WriteLine(ConCol.Info, "Number of warnings exceed 100. To view all of them, compile with a validation log."); break; } } WriteLine(ConCol.Error, "Errors ({0} Total):".Fmt(v.Ca.Errors.Count)); foreach (var m in v.Ca.Errors.AsIndexable()) { if (m.Index < 100) { WriteLine(ConCol.Error, "{0}) {1}", m.Index + 1, m.Value.ToString()); } else { WriteLine(ConCol.Info, "Number of errors exceed 100. To view all of them, compile with a validation log."); break; } } if (v.Ca.Errors.Count > 0) { ExecuteOnError(v.Ca.Errors[0].Module.DOM.FullPath, v.Ca.Errors[0].Target.Location); } } else if (!paramPackLog) { WriteLine(ConCol.Info, "No Warning/Error Messages will be shown here, all messages will be written to {0}", paramValidationLog); using (var sw = new StreamWriter(paramValidationLog)) { sw.WriteLine(v.BuildMessageStats()); sw.WriteLine("Warnings ({0}):".Fmt(v.Ca.Warnings.Count)); sw.WriteLine(); sw.WriteLine(); foreach (var m in v.Ca.Warnings) { sw.WriteLine("{0:D3}) {1}", m.MsgType, m.ToString()); } sw.WriteLine("Errors ({0}):".Fmt(v.Ca.Errors.Count)); sw.WriteLine(); sw.WriteLine(); foreach (var m in v.Ca.Errors) { sw.WriteLine("{0:D3}) {1}", m.MsgType, m.ToString()); } } } else { using (var sw = new StreamWriter(paramValidationLog)) { XmlSerializer xs = new XmlSerializer(typeof(GameScriptCompiler.ContextAnalyzation.MessageObject[])); xs.Serialize(sw, v.GetMessageObjects()); } } if (paramStubOut != null) { if (StubGen.CheckUnSpecifics(v.Ca.Errors)) { using (var sw = new StreamWriter(paramStubOut)) { StubGen.StubOut(sw, v.Ca.Errors, cfe.Options); WriteLine(ConCol.Info, "Wrote function stubs [{0}]", paramStubOut); } } else { WriteLine(ConCol.Warning, "Cannot write stubs with these particular errors."); } } if (Successes == files.Count) { WriteLine(ConCol.Success, "Succussfully syntaxed all files!"); } else { WriteLine(ConCol.Warning, "Completed with warnings/errrors."); } }
static int _Main(string[] args) { ColorInit(); var fields = Args.GetFieldsFromPrefix <Program>("param"); List <String> targets = new List <string>(); var r = Args.SetArgs(null, args, ref targets, fields); if (args.Length == 0 || r == Args.ArgResult.IncompleteOrInvalid) { Console.WriteLine("Need arguments or invalid arguments."); Console.WriteLine(); Console.WriteLine(Args.GetUsage(fields, null)); return(0); } if (paramBasePath != null && targets.Count > 0) { WriteLine(ConCol.Warning, "Cannot build syntax targets and perform validation together. Either provide just syntax targets " + "or provide a base path for validation with /BasePath."); return(1); } CompilerFrontEnd cfe = new CompilerFrontEnd(); //Get the settings file from the current module directory, not working directory. Or else D&D would use incorrect paths. if (paramSettings == null) { paramSettings = Path.Combine((new FileInfo(Assembly.GetExecutingAssembly().Location)).DirectoryName, defaultSettings); } if (!File.Exists(paramSettings)) { using (FileStream fs = new FileStream(paramSettings, FileMode.Create)) cfe.Options.SaveOptions(fs); WriteLine(ConCol.Info, "Writing settings to [{0}]".Fmt(paramSettings)); } else { using (FileStream fs = new FileStream(paramSettings, FileMode.Open)) cfe.Options.LoadOptions(fs); WriteLine(ConCol.Info, "Loaded settings from [{0}]".Fmt(paramSettings)); using (FileStream fs = new FileStream(paramSettings, FileMode.Create)) cfe.Options.SaveOptions(fs); } if (paramClearOutput) { WriteLine(ConCol.Info, "Clearing output folder"); var di = new DirectoryInfo(paramOutputFolder); if (di.Exists) { di.Delete(true); } di.Create(); } if (paramSetGlobal != null) { try { if (!ProcessFile(cfe, new FileInfo(paramSetGlobal), 1, 0)) { return(1); } } catch (Exception e) { WriteLine(ConCol.Error, "Error reading global file [{0}]: {1}", paramSetGlobal, e.Message); e.BreakException(); } global = cfe.UserDefinedDOM; if (!global.IsDeclarative) { WriteLine(ConCol.Error, "Global module must be set declarative with \"#set_global_declarative\"."); Console.ReadKey(true); return(1); } } foreach (var file in targets) { try { if (File.Exists(file)) { pending.Add(new FileInfo(file)); } else if (Directory.Exists(file)) { foreach (var _file in (new DirectoryInfo(file)).EnumerateFiles(fileMask, SearchOption.AllDirectories)) { pending.Add(_file); } } else { WriteLine(ConCol.Warning, "Could not find file/folder [{0}]".Fmt(file)); } } catch (Exception e) { WriteLine(ConCol.Error, "Could not add file [{0}] to pending: {1}".Fmt(file, e.Message)); e.BreakException(); } } if (paramBasePath == null) { BuildSyntax(cfe); } else { Validate(cfe); } return(0); }