Пример #1
0
        /// <summary>
        /// Returns a visitor with the results of a single parsing pass
        /// </summary>
        /// <param name="textToParse">Z80 assembly code to parse</param>
        /// <returns>
        /// Visitor with the syntax tree
        /// </returns>
        protected virtual List <Z80TestParserErrorInfo> ParseWithErrors(string textToParse)
        {
            var inputStream = new AntlrInputStream(textToParse);
            var lexer       = new Z80TestLexer(inputStream);
            var tokenStream = new CommonTokenStream(lexer);
            var parser      = new Z80TestParser(tokenStream);
            var context     = parser.compileUnit();
            var visitor     = new Z80TestVisitor();

            visitor.Visit(context);
            return(parser.SyntaxErrors);
        }
Пример #2
0
        /// <summary>
        /// Returns a visitor with the results of a single parsing pass
        /// </summary>
        /// <param name="textToParse">Z80 assembly code to parse</param>
        /// <param name="expectedErrors">Number of errors expected</param>
        /// <returns>
        /// Visitor with the syntax tree
        /// </returns>
        protected virtual ExpressionNode ParseExpr(string textToParse, int expectedErrors = 0)
        {
            var inputStream = new AntlrInputStream(textToParse);
            var lexer       = new Z80TestLexer(inputStream);
            var tokenStream = new CommonTokenStream(lexer);
            var parser      = new Z80TestParser(tokenStream);
            var context     = parser.expr();
            var visitor     = new Z80TestVisitor();

            parser.SyntaxErrors.Count.ShouldBe(expectedErrors);
            return((ExpressionNode)visitor.VisitExpr(context));
        }
Пример #3
0
        /// <summary>
        /// Returns a visitor with the results of a single parsing pass
        /// </summary>
        /// <param name="textToParse">Z80 assembly code to parse</param>
        /// <param name="expectedErrors">Number of errors expected</param>
        /// <returns>
        /// Visitor with the syntax tree
        /// </returns>
        private static TestOptionsNode ParseTestOptions(string textToParse, int expectedErrors = 0)
        {
            var inputStream = new AntlrInputStream(textToParse);
            var lexer       = new Z80TestLexer(inputStream);
            var tokenStream = new CommonTokenStream(lexer);
            var parser      = new Z80TestParser(tokenStream);
            var context     = parser.testOptions();
            var visitor     = new Z80TestVisitor();
            var result      = (TestOptionsNode)visitor.VisitTestOptions(context);

            parser.SyntaxErrors.Count.ShouldBe(expectedErrors);
            return(result);
        }
Пример #4
0
        /// <summary>
        /// Returns a visitor with the results of a single parsing pass
        /// </summary>
        /// <param name="textToParse">Z80 assembly code to parse</param>
        /// <param name="expectedErrors">Number of errors expected</param>
        /// <returns>
        /// Visitor with the syntax tree
        /// </returns>
        protected virtual Z80TestVisitor Parse(string textToParse, int expectedErrors = 0)
        {
            var inputStream = new AntlrInputStream(textToParse);
            var lexer       = new Z80TestLexer(inputStream);
            var tokenStream = new CommonTokenStream(lexer);
            var parser      = new Z80TestParser(tokenStream);
            var context     = parser.compileUnit();
            var visitor     = new Z80TestVisitor();

            visitor.Visit(context);
            parser.SyntaxErrors.Count.ShouldBe(expectedErrors);

            return(visitor);
        }
Пример #5
0
        /// <summary>
        /// Executes the parse phase of the compilation
        /// </summary>
        /// <param name="plan">Test plan</param>
        /// <param name="sourcetext">Source text</param>
        /// <param name="testSetNodes">TestSetNode as the result of compilation</param>
        /// <returns>True, if compilation successful; otherwise, false</returns>
        private bool ExecuteParse(TestFilePlan plan, string sourcetext, out List <TestSetNode> testSetNodes)
        {
            // --- Parse the source text
            var inputStream = new AntlrInputStream(sourcetext);
            var lexer       = new Z80TestLexer(inputStream);
            var tokenStream = new CommonTokenStream(lexer);
            var parser      = new Z80TestParser(tokenStream);
            var context     = parser.compileUnit();
            var visitor     = new Z80TestVisitor();

            visitor.Visit(context);
            testSetNodes = visitor.Compilation.TestSets;

            // --- Collect syntax errors
            foreach (var error in parser.SyntaxErrors)
            {
                ReportError(plan, error);
            }

            return(parser.SyntaxErrors.Count == 0);
        }
Пример #6
0
        /// <summary>
        /// Parses the entire source code
        /// </summary>
        private async void ParseDocument()
        {
            // --- Do not start parsing over existing parsing
            if (_isProcessing)
            {
                return;
            }
            _isProcessing = true;

            await Task.Run(() =>
            {
                try
                {
                    // --- Get the entire text of the source code
                    var span   = new SnapshotSpan(_buffer.CurrentSnapshot, 0, _buffer.CurrentSnapshot.Length);
                    var source = _buffer.CurrentSnapshot.GetText(span);

                    // --- Let's use the Z80 assembly parser to obtain tags
                    var inputStream    = new AntlrInputStream(source);
                    var lexer          = new Z80TestLexer(inputStream);
                    var tokenStream    = new CommonTokenStream(lexer);
                    var parser         = new Z80TestParser(tokenStream);
                    var context        = parser.compileUnit();
                    var commentWalker  = new ParseTreeWalker();
                    var parserListener = new Z80TestParserListener(tokenStream);
                    commentWalker.Walk(parserListener, context);
                    _commentSpans   = parserListener.CommentSpans;
                    _z80TestVisitor = new Z80TestVisitor();
                    _z80TestVisitor.Visit(context);

                    // --- Code is parsed, sign the change
                    ClassificationChanged?.Invoke(this, new ClassificationChangedEventArgs(span));
                }
                finally
                {
                    _isProcessing = false;
                }
            });
        }
        /// <summary>Gets all the tags that intersect the specified spans. </summary>
        /// <returns>A <see cref="T:Microsoft.VisualStudio.Text.Tagging.TagSpan`1" /> for each tag.</returns>
        /// <param name="spans">The spans to visit.</param>
        public IEnumerable <ITagSpan <Z80TestTokenTag> > GetTags(NormalizedSnapshotSpanCollection spans)
        {
            // --- Just for the sake of being thorough...
            if (spans.Count <= 0)
            {
                yield break;
            }

            // --- Obtain and parse the entire snapshot
            var snapshot    = spans[0].Snapshot;
            var source      = snapshot.GetText();
            var inputStream = new AntlrInputStream(source);
            var lexer       = new Z80TestLexer(inputStream);
            var tokenStream = new CommonTokenStream(lexer);
            var parser      = new Z80TestParser(tokenStream);
            var context     = parser.compileUnit();
            var visitor     = new Z80TestVisitor();

            visitor.Visit(context);

            foreach (var curSpan in spans)
            {
                var currentLine = curSpan.Start.GetContainingLine();
                var lineNo      = currentLine.LineNumber;
                var textOfLine  = currentLine.GetText();
                if (string.IsNullOrWhiteSpace(textOfLine))
                {
                    continue;
                }

                var collectedSpans = new List <TagSpan <Z80TestTokenTag> >();
                Visit(currentLine, visitor.Compilation, lineNo + 1, collectedSpans);
                foreach (var span in collectedSpans)
                {
                    yield return(span);
                }
            }
        }