void AsyncParser_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e) { AsyncParserResultType Result = new AsyncParserResultType(); //make sure that double typed numbers are converted with decimal point (not comma!) to string System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture("en-US"); try { AsyncParserJob job = e.Argument as AsyncParserJob; Result.DocumentID = job.DocumentID; Tikz_ParseTree tp = TikzParser.Parse(job.code); Result.ParseTree = tp; //include any styles from include files via \input cmd string inputfile = ""; try { //find input files using Regex MatchCollection files = InputsRegex.Matches(job.code); foreach (Match file in files) { //open, read, parse, and close each included file. inputfile = file.Groups["file"].ToString(); if (File.Exists(inputfile)) { StreamReader sr = new StreamReader(inputfile); string inputcode = sr.ReadToEnd(); sr.Close(); Tikz_ParseTree tp2 = TikzParser.ParseInputFile(inputcode); //if tp2 == null there probably was nothing useful included. if (tp2 != null) { //every style that was found in included file, add it to parse tree of main file. foreach (KeyValuePair <string, Tikz_Option> style in tp2.styles) { if (!Result.ParseTree.styles.ContainsKey(style.Key)) { Result.ParseTree.styles.Add(style.Key, style.Value); } else { ParserException pe = new ParserException(""); TexOutputParser.TexError te = new TexOutputParser.TexError(); te.Message = "Style [" + style.Key + "] is defined multiple times. Check position " + style.Value.StartPosition() + " in " + inputfile + " and this definition."; te.pos = Result.ParseTree.styles[style.Key].StartPosition(); te.severity = Severity.WARNING; pe.e = te; throw pe; } } } } } } catch (Exception ex) { Result.Warning = ex; Result.WarningSource = inputfile; } } catch (Exception ex) { //never set e.Cancel = true; //if you do, you cannot access e.Result from AsyncParser_RunWorkerCompleted. Result.Error = ex; Result.WarningSource = ShortFileName; } finally { e.Result = Result; } }
void AsyncParser_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e) { AsyncParserResultType Result = e.Result as AsyncParserResultType; if (Result == null) { throw new Exception("AsyncParser_RunWorkerCompleted() can only handle e.Result of type AsyncParserResultType!"); } // in case of outdated parse -> ignore if (Result.DocumentID != DocumentID) { return; } //check if error occurred if (Result.Error != null && Result.Error is RecognitionException) { RecognitionException ex = Result.Error as RecognitionException; string errmsg = ANTLRErrorMsg.ToString(ex, simpletikzParser.tokenNames); MainWindow.AddStatusLine("Couldn't parse code. " + errmsg, true); if (ex.Line == 0 && ex.CharPositionInLine == -1) { addProblemMarker(errmsg, Document.LineCount, 0, Severity.ERROR, ShortFileName); } else { addProblemMarker(errmsg, ex.Line, ex.CharPositionInLine, Severity.ERROR, ShortFileName); } ParseTree = null; TikzStyles.Clear(); } else if (Result.Error != null) { string errmsg = Result.Error.GetType().ToString(); if (Result.Error is Exception) { errmsg += ":" + ((Exception)Result.Error).Message; } MainWindow.AddStatusLine("Couldn't parse code. " + errmsg, true); ParseTree = null; TikzStyles.Clear(); } else { // parsing succesfull Tikz_ParseTree tp = Result.ParseTree as Tikz_ParseTree; ParseTree = tp; // if no other changes are pending, we can turn on editing again AllowEditing = true; // fill the style list UpdateStyleList(); //now check if a warning occured. That would be a parser error in an included file. if (Result.Warning != null && Result.Warning is RecognitionException) { RecognitionException ex = Result.Warning as RecognitionException; string errmsg = ANTLRErrorMsg.ToString(ex, simpletikzParser.tokenNames); MainWindow.AddStatusLine("Couldn't parse included file. " + errmsg, true); if (ex.Line == 0 && ex.CharPositionInLine == -1) { addProblemMarker(errmsg, Document.LineCount, 0, Severity.WARNING, Result.WarningSource); } else { addProblemMarker(errmsg, ex.Line, ex.CharPositionInLine, Severity.WARNING, Result.WarningSource); } } else if (Result.Warning != null && Result.Warning is ParserException) { ParserException pe = Result.Warning as ParserException; addProblemMarker(this, pe.e); } else if (Result.Warning != null && Result.Warning is Exception) { string errmsg = ((Exception)Result.Warning).Message; MainWindow.AddStatusLine("Couldn't parse included file " + Result.WarningSource + ". " + errmsg, true); } } // Restart parser if necessary ParseNeeded = ParseNeeded; }
// Unfortunately, due to a debugger "bug", the exception has to be caught and transferred into a cancelled event // this cancel event type is AsyncParserResultType. It is passed to AsyncParser_RunWorkerCompleted(). void AsyncParser_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e) { AsyncParserResultType Result = new AsyncParserResultType(); //make sure that double typed numbers are converted with decimal point (not comma!) to string System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture("en-US"); try { AsyncParserJob job = e.Argument as AsyncParserJob; Tikz_ParseTree tp = TikzParser.Parse(job.code); Result.ParseTree = tp; Result.DocumentID = job.DocumentID; //include any styles from include files via \input cmd string inputfile = ""; try { //find input files using Regex Regex InputsRegex = new Regex(@"(^[^%]*|\n[^\n%]*?)\\input{(?<file>.*)}", RegexOptions.Compiled); MatchCollection files = InputsRegex.Matches(job.code); foreach (Match file in files) { //open, read, parse, and close each included file. inputfile = file.Groups["file"].ToString(); if (File.Exists(inputfile)) { StreamReader sr = new StreamReader(inputfile); string inputcode = sr.ReadToEnd(); sr.Close(); Tikz_ParseTree tp2 = TikzParser.ParseInputFile(inputcode); //if tp2 == null there probably was nothing useful included. if (tp2 != null) { //every style that was found in included file, add it to parse tree of main file. foreach (KeyValuePair<string, Tikz_Option> style in tp2.styles) { if(! Result.ParseTree.styles.ContainsKey(style.Key)) { Result.ParseTree.styles.Add(style.Key, style.Value); } else { ParserException pe = new ParserException(""); TexOutputParser.TexError te = new TexOutputParser.TexError(); te.Message = "Style [" + style.Key + "] is defined multiple times. Check position " + style.Value.StartPosition() + " in " + inputfile + " and this definition."; te.pos = Result.ParseTree.styles[style.Key].StartPosition(); te.severity = Severity.WARNING; pe.e = te; throw pe; } } } } } } catch (Exception ex) { Result.Warning = ex; Result.WarningSource = inputfile; } } catch (Exception ex) { //never set e.Cancel = true; //if you do, you cannot access e.Result from AsyncParser_RunWorkerCompleted. Result.Error = ex; Result.WarningSource = CurFile; } finally { e.Result = Result; } }