private List <LogMessage> RunLeMPAndCaptureErrors(UString inputText) { MessageHolder messages = new MessageHolder(); MacroProcessor lemp = NewLemp(0xFFFF, EcsLanguageService.Value).With(mp => mp.Sink = messages); IListSource <LNode> inputCode = EcsLanguageService.Value.Parse(inputText, MessageSink.Default); lemp.ProcessSynchronously(LNode.List(inputCode)); return(messages.List.Where(m => m.Severity == Severity.Error).ToList()); }
private static string Process(MacroProcessor MP, string codeStr, int lineZero) { VList <LNode> output = VList <LNode> .Empty; var msgs = new MessageHolder(); using (MessageSink.PushCurrent(msgs)) { var code = EcsLanguageService.Value.Parse(codeStr, msgs); if (!msgs.List.Any(m => m.Severity >= Severity.Error)) { output = MP.ProcessSynchronously(LNode.List(code)); } } foreach (var m in msgs.List) { // Goal: add lineNo to line number of error message to get correct line number object loc = MessageSink.LocationOf(m.Context); SourcePos pos = default(SourcePos); if (loc is SourceRange) { pos = ((SourceRange)loc).Start; } else if (loc is SourcePos) { pos = ((SourcePos)loc); } else { pos = new SourcePos("Unknown", 0, 0); } pos = new SourcePos(pos.FileName, pos.Line + lineZero, pos.PosInLine); MessageSink.Current.Write(m.Severity, pos, m.Format, m.Args); } if (msgs.List.Any(m => m.Severity >= Severity.Error)) { return(null); } else { return(EcsLanguageService.Value.Print(output, null, null, " ", "\n")); } }
protected void Test(LNodeList input, MacroProcessor lemp, string expected, IParsingService outLang) { // The current printer affects the assert macro and contract macros, // so we'll want to set it up before running LeMP using (LNode.SetPrinter((ILNodePrinter)outLang)) { var inputCode = input; var results = lemp.ProcessSynchronously(inputCode); var expectCode = outLang.Parse(expected, MessageSink.Default); if (!results.SequenceEqual(expectCode)) { // TEST FAILED, print error string resultStr = results.Select(n => ((ILNodePrinter)outLang).Print(n)).Join("\n"); Assert.AreEqual(TestCompiler.StripExtraWhitespace(expected), TestCompiler.StripExtraWhitespace(resultStr)); // In some tests, the text is equal even though the trees are different, // typically because of differences in #trivia attributes between the two. Console.WriteLine("(minor dif)"); // it's OK, but print a hint that this occurred. } } }