public void ParserGeneratedStageErrors() { var grammarText = $@"grammar {TestGrammarName}; start: rule1+; rule: DIGIT; CHAR: [a-z]+; DIGIT: [0-9]+; WS: [ \r\n\t]+ -> skip;"; var workflow = new Workflow(GrammarFactory.CreateDefaultCombinedAndFill(grammarText, TestGrammarName, ".")); var state = workflow.Process(); Assert.AreEqual(WorkflowStage.ParserGenerated, state.Stage, state.Exception?.ToString()); var grammarSource = new CodeSource(TestGrammarName + ".g4", File.ReadAllText(TestGrammarName + ".g4")); char separator = Path.DirectorySeparatorChar; string testGrammarFullName = $"{Environment.CurrentDirectory}{separator}.{separator}{TestGrammarName}.g4"; ParserGeneratedState parserGeneratedState = state as ParserGeneratedState; string str = new ParsingError(2, 24, $"error(56): {testGrammarFullName}:2:24: reference to undefined rule: rule1", grammarSource, WorkflowStage.ParserGenerated).ToString(); CollectionAssert.AreEquivalent( new [] { new ParsingError(2, 24, $"error(56): {testGrammarFullName}:2:24: reference to undefined rule: rule1", grammarSource, WorkflowStage.ParserGenerated), }, parserGeneratedState.Errors); }
public ParserGeneratedState Generate(GrammarCheckedState state, CancellationToken cancellationToken = default) { Grammar grammar = state.InputState.Grammar; _result = new ParserGeneratedState(state, PackageName, Runtime, GenerateListener, GenerateVisitor); Generate(grammar, state, cancellationToken); return(_result); }
public ParserCompiledState Compile(ParserGeneratedState state, CancellationToken cancellationToken = default) { _grammar = state.GrammarCheckedState.InputState.Grammar; Runtime runtime = state.Runtime; _result = new ParserCompiledState(state); _currentRuntimeInfo = RuntimeInfo.InitOrGetRuntimeInfo(runtime); Processor processor = null; try { string runtimeSource = runtime.GetGeneralRuntimeName(); string runtimeDir = Path.Combine(RuntimesDirName, runtimeSource); string runtimeLibraryPath = RuntimeLibrary ?? Path.Combine(runtimeDir, _currentRuntimeInfo.RuntimeLibrary); string workingDirectory = Path.Combine(ParserGenerator.HelperDirectoryName, _grammar.Name, runtime.ToString()); var generatedFiles = new List <string>(); _grammarCodeMapping = new Dictionary <string, List <TextSpanMapping> >(); string generatedGrammarName = runtime != Runtime.Go ? _grammar.Name : _grammar.Name.ToLowerInvariant(); if (_grammar.Type != GrammarType.Lexer) { GetGeneratedFileNames(state.GrammarCheckedState, generatedGrammarName, workingDirectory, generatedFiles, false); } GetGeneratedFileNames(state.GrammarCheckedState, generatedGrammarName, workingDirectory, generatedFiles, true); CopyCompiledSources(runtime, workingDirectory); if (_grammar.Type != GrammarType.Lexer) { if (state.IncludeListener) { GetGeneratedListenerOrVisitorFiles(generatedGrammarName, workingDirectory, generatedFiles, false); } if (state.IncludeVisitor) { GetGeneratedListenerOrVisitorFiles(generatedGrammarName, workingDirectory, generatedFiles, true); } } string arguments = ""; switch (runtime) { case Runtime.CSharpOptimized: case Runtime.CSharpStandard: arguments = PrepareCSharpFiles(workingDirectory, runtimeDir); break; case Runtime.Java: arguments = PrepareJavaFiles(generatedFiles, runtimeDir, workingDirectory, runtimeLibraryPath); break; case Runtime.Python2: case Runtime.Python3: arguments = PreparePythonFiles(generatedFiles, runtimeDir, workingDirectory); break; case Runtime.JavaScript: arguments = PrepareJavaScriptFiles(generatedFiles, workingDirectory, runtimeDir); break; case Runtime.Go: arguments = PrepareGoFiles(generatedFiles, runtimeDir, workingDirectory); break; case Runtime.Php: arguments = PreparePhpFiles(generatedFiles, runtimeDir, workingDirectory); break; } PrepareParserCode(workingDirectory, runtimeDir); _buffer = new List <string>(); _processedMessages = new HashSet <string>(); _result.Command = _currentRuntimeInfo.RuntimeToolName + " " + arguments; processor = new Processor(_currentRuntimeInfo.RuntimeToolName, arguments, workingDirectory); processor.CancellationToken = cancellationToken; processor.ErrorDataReceived += ParserCompilation_ErrorDataReceived; processor.OutputDataReceived += ParserCompilation_OutputDataReceived; processor.Start(); if (_buffer.Count > 0) { if (runtime.IsPythonRuntime()) { AddPythonError(); } else if (runtime == Runtime.JavaScript) { AddJavaScriptError(); } } cancellationToken.ThrowIfCancellationRequested(); } catch (Exception ex) { _result.Exception = ex; if (!(ex is OperationCanceledException)) { AddError(new ParsingError(ex, WorkflowStage.ParserCompiled)); } } finally { processor?.Dispose(); } return(_result); }