public ParsingError(int beginLine, int beginColumn, int endLine, int endColumn, string message, CodeSource codeSource, WorkflowStage stage, bool isWarning = false) : this(new LineColumnTextSpan(beginLine, beginColumn, endLine, endColumn, codeSource).GetTextSpan(), message, stage, isWarning) { Message = message; WorkflowStage = stage; }
private ParsingError GenerateError(string data, string codeFileName, int line, int column, string rest) { ParsingError error; TextSpan textSpan; if (_grammarCodeMapping.TryGetValue(codeFileName, out List <TextSpanMapping> textSpanMappings)) { string grammarFileName = GetGrammarFromCodeFileName(_currentRuntimeInfo, codeFileName); textSpan = TextHelpers.GetSourceTextSpanForLine(textSpanMappings, line, grammarFileName); error = new ParsingError(textSpan, $"{grammarFileName}:{textSpan.GetLineColumn().BeginLine}:{rest}", WorkflowStage.ParserCompilied); } else { Dictionary <string, CodeSource> grammarFilesData = _result.ParserGeneratedState.GrammarCheckedState.GrammarFilesData; CodeSource codeSource = grammarFilesData.FirstOrDefault(file => file.Key.EndsWith(codeFileName, StringComparison.OrdinalIgnoreCase)).Value; textSpan = codeSource != null ? new LineColumnTextSpan(line, column, codeSource).GetTextSpan() : TextSpan.Empty; error = new ParsingError(textSpan, data, WorkflowStage.ParserCompilied); } return(error); }
private void GetGeneratedFileNames(GrammarCheckedState grammarCheckedState, string generatedGrammarName, string workingDirectory, List <string> generatedFiles, bool lexer) { string grammarNameExt; if (_grammar.Type == GrammarType.Combined) { grammarNameExt = _grammar.Files.FirstOrDefault(file => Path.GetExtension(file) .Equals(Grammar.AntlrDotExt, StringComparison.OrdinalIgnoreCase)); } else { string postfix = lexer ? Grammar.LexerPostfix : Grammar.ParserPostfix; grammarNameExt = _grammar.Files.FirstOrDefault(file => file.Contains(postfix) && Path.GetExtension(file).Equals(Grammar.AntlrDotExt, StringComparison.OrdinalIgnoreCase)); } string shortGeneratedFile = generatedGrammarName + (lexer ? _currentRuntimeInfo.LexerPostfix : _currentRuntimeInfo.ParserPostfix) + "." + _currentRuntimeInfo.Extensions[0]; string generatedFileDir = workingDirectory; Runtime runtime = _currentRuntimeInfo.Runtime; if ((runtime == Runtime.Java || runtime == Runtime.Go) && !string.IsNullOrWhiteSpace(_result.ParserGeneratedState.PackageName)) { generatedFileDir = Path.Combine(generatedFileDir, _result.ParserGeneratedState.PackageName); } string generatedFile = Path.Combine(generatedFileDir, shortGeneratedFile); generatedFiles.Add(generatedFile); CodeSource codeSource = new CodeSource(generatedFile, File.ReadAllText(generatedFile)); _grammarCodeMapping[shortGeneratedFile] = TextHelpers.Map(grammarCheckedState.GrammarActionsTextSpan[grammarNameExt], codeSource, lexer); }
private void AddCSharpError(string data) { if (data.Contains(": error CS")) { var errorString = Helpers.FixEncoding(data); ParsingError error; CodeSource grammarSource = CodeSource.Empty; try { // Format: // Lexer.cs(106,11): error CS0103: The name 'a' does not exist in the current context var strs = errorString.Split(':'); int leftParenInd = strs[0].IndexOf('('); string codeFileName = strs[0].Remove(leftParenInd); string grammarFileName = GetGrammarFromCodeFileName(_currentRuntimeInfo, codeFileName); string lineColumnString = strs[0].Substring(leftParenInd); lineColumnString = lineColumnString.Substring(1, lineColumnString.Length - 2); // Remove parenthesis. var strs2 = lineColumnString.Split(','); int line = int.Parse(strs2[0]); int column = int.Parse(strs2[1]); string rest = string.Join(":", strs.Skip(1)); error = GenerateError(data, codeFileName, line, column, rest); } catch { error = new ParsingError(errorString, grammarSource, WorkflowStage.ParserCompilied); } AddError(error); } }
public void CollectInfo(CodeSource grammarSource, ANTLRv4Parser.GrammarSpecContext context) { GrammarSource = grammarSource; var walker = new ParseTreeWalker(); walker.Walk(this, context); }
public LineColumnTextSpan(int startLine, int startColumn, int endLine, int endColumn, CodeSource source) { BeginLine = startLine; BeginColumn = startColumn; EndLine = endLine; EndColumn = endColumn; Source = source; }
public TextSpan(int start, int length, CodeSource codeSource) { Source = codeSource ?? throw new ArgumentNullException(nameof(codeSource)); if (start < 0) { throw new ArgumentOutOfRangeException(nameof(start)); } if (start + length < start) { throw new ArgumentOutOfRangeException(nameof(length)); } Start = start; Length = length; }
public static List <TextSpanMapping> Map(List <CodeInsertion> source, CodeSource destinationSource, bool lexer) { var result = new List <TextSpanMapping>(source.Count); int destInd = 0; IEnumerable <CodeInsertion> sortedSource = source.Where(s => s.Lexer == lexer).OrderBy(s => s.Predicate); foreach (CodeInsertion s in sortedSource) { destInd = destinationSource.Text.IgnoreWhitespaceIndexOf(s.Text, destInd); result.Add(new TextSpanMapping { SourceTextSpan = s.TextSpan, DestTextSpan = new TextSpan(destInd, s.Text.Length, destinationSource) }); destInd += s.Text.Length; } return(result); }
public static TextSpan GetTextSpan(this ParserRuleContext ruleContext, CodeSource source) { var start = ruleContext.Start; if (start.Text == "<EOF>") { return(TextSpan.Empty); } var stop = ruleContext.Stop; if (stop == null) { if (ruleContext.Parent is ParserRuleContext parentParserRuleContext) { stop = parentParserRuleContext.Stop; } } var result = new TextSpan(start.StartIndex, stop.StopIndex - start.StartIndex + 1, source); return(result); }
public GrammarCheckedState Check(InputState inputState, CancellationToken cancellationToken = default) { var grammar = inputState.Grammar; var result = new GrammarCheckedState(inputState); try { var antlrErrorListener = new AntlrErrorListener(); antlrErrorListener.ErrorEvent += ErrorEvent; antlrErrorListener.ErrorEvent += (sender, error) => { lock (result.Errors) { result.Errors.Add(error); } }; foreach (string grammarFileName in grammar.Files) { string code = File.ReadAllText(Path.Combine(grammar.Directory, grammarFileName)); var inputStream = new AntlrInputStream(code); var codeSource = new CodeSource(grammarFileName, inputStream.ToString()); result.GrammarFilesData.Add(grammarFileName, codeSource); string extension = Path.GetExtension(grammarFileName); if (extension != Grammar.AntlrDotExt) { continue; } antlrErrorListener.CodeSource = codeSource; var antlr4Lexer = new ANTLRv4Lexer(inputStream); antlr4Lexer.RemoveErrorListeners(); antlr4Lexer.AddErrorListener(antlrErrorListener); var codeTokenSource = new ListTokenSource(antlr4Lexer.GetAllTokens()); cancellationToken.ThrowIfCancellationRequested(); var codeTokenStream = new CommonTokenStream(codeTokenSource); var antlr4Parser = new ANTLRv4Parser(codeTokenStream); antlr4Parser.RemoveErrorListeners(); antlr4Parser.AddErrorListener(antlrErrorListener); var tree = antlr4Parser.grammarSpec(); var grammarInfoCollectorListener = new GrammarInfoCollectorListener(); grammarInfoCollectorListener.CollectInfo(antlrErrorListener.CodeSource, tree); var shortFileName = Path.GetFileNameWithoutExtension(grammarFileName); result.GrammarActionsTextSpan[grammarFileName] = grammarInfoCollectorListener.CodeInsertions; if (grammarFileName.Contains(Grammar.LexerPostfix)) { result.LexerSuperClass = grammarInfoCollectorListener.SuperClass; } if (grammarFileName.Contains(Grammar.ParserPostfix)) { result.ParserSuperClass = grammarInfoCollectorListener.SuperClass; } if (!shortFileName.Contains(Grammar.LexerPostfix)) { result.Rules = grammarInfoCollectorListener.Rules; cancellationToken.ThrowIfCancellationRequested(); } } } catch (Exception ex) { result.Exception = ex; if (!(ex is OperationCanceledException)) { ErrorEvent?.Invoke(this, new ParsingError(ex, WorkflowStage.GrammarChecked)); } } return(result); }
public ParsingError(int line, int column, string message, CodeSource codeSource, WorkflowStage stage) : this(new LineColumnTextSpan(line, column, codeSource).GetTextSpan(), message, stage) { Message = message; WorkflowStage = stage; }
public static TextSpan GetTextSpan(this ITerminalNode node, CodeSource source) { return(GetTextSpan(node.Symbol, source)); }
private void Generate(Grammar grammar, GrammarCheckedState state, CancellationToken cancellationToken) { Processor processor = null; try { string runtimeDirectoryName = Path.Combine(HelperDirectoryName, grammar.Name, Runtime.ToString()); if ((Runtime == Runtime.Java || Runtime == Runtime.Go) && !string.IsNullOrWhiteSpace(PackageName)) { runtimeDirectoryName = Path.Combine(runtimeDirectoryName, PackageName); } if (Directory.Exists(runtimeDirectoryName)) { Directory.Delete(runtimeDirectoryName, true); } Directory.CreateDirectory(runtimeDirectoryName); cancellationToken.ThrowIfCancellationRequested(); RuntimeInfo runtimeInfo = RuntimeInfo.InitOrGetRuntimeInfo(Runtime); var jarGenerator = GeneratorTool ?? Path.Combine("Generators", runtimeInfo.JarGenerator); foreach (string grammarFileName in state.InputState.Grammar.Files) { string extension = Path.GetExtension(grammarFileName); if (extension != Grammar.AntlrDotExt) { continue; } _currentGrammarSource = state.GrammarFilesData[grammarFileName]; var arguments = $@"-jar ""{jarGenerator}"" ""{Path.Combine(grammar.Directory, grammarFileName)}"" " + $@"-o ""{runtimeDirectoryName}"" " + $"-Dlanguage={runtimeInfo.DLanguage} " + $"{(GenerateVisitor ? "-visitor" : "-no-visitor")} " + $"{(GenerateListener ? "-listener" : "-no-listener")}"; if (!string.IsNullOrWhiteSpace(PackageName)) { arguments += " -package " + PackageName; } else if (Runtime == Runtime.Go) { arguments += " -package main"; } if (grammarFileName.Contains(Grammar.LexerPostfix) && state.LexerSuperClass != null) { arguments += " -DsuperClass=" + state.LexerSuperClass; } if (grammarFileName.Contains(Grammar.ParserPostfix) && state.ParserSuperClass != null) { arguments += " -DsuperClass=" + state.ParserSuperClass; } _result.Command = "java " + arguments; processor = new Processor("java", arguments, "."); processor.CancellationToken = cancellationToken; processor.ErrorDataReceived += ParserGeneration_ErrorDataReceived; processor.OutputDataReceived += ParserGeneration_OutputDataReceived; processor.Start(); cancellationToken.ThrowIfCancellationRequested(); } } catch (Exception ex) { _result.Exception = ex; if (!(ex is OperationCanceledException)) { ErrorEvent?.Invoke(this, new ParsingError(ex, WorkflowStage.ParserGenerated)); } } finally { processor?.Dispose(); } }
public static string FormatErrorMessage(CodeSource codeSource, int line, int charPositionInLine, string msg, bool isWarning = false) { return($"{(isWarning ? "Warning" : "Error")}: {Path.GetFileName(codeSource.Name)}:{line}:{charPositionInLine}: {msg}"); }
public static TextSpan FromBounds(int start, int end, CodeSource source) { return(new TextSpan(start, end - start, source)); }
public ParsingError(string message, CodeSource codeSource, WorkflowStage stage) : this(1, 1, message, codeSource, stage) { }
public LineColumnTextSpan(int line, int column, CodeSource source) : this(line, column, line, column, source) { }
public ParsingError(string message, CodeSource codeSource, WorkflowStage stage, bool isWarning = false) : this(1, 1, message, codeSource, stage, isWarning) { }
public TextParsedState(ParserCompiliedState parserCompiliedState, CodeSource text) { ParserCompiliedState = parserCompiliedState ?? throw new ArgumentNullException(nameof(parserCompiliedState)); Text = text ?? throw new ArgumentNullException(nameof(text)); }
public static TextSpan GetTextSpan(this IToken token, CodeSource source) { var result = new TextSpan(token.StartIndex, token.StopIndex - token.StartIndex + 1, source); return(result); }