Ejemplo n.º 1
0
        /// <summary>
        /// Visit a parse tree produced by <see cref="Generated.Z80AsmParser.asmline"/>.
        /// </summary>
        /// <param name="context">The parse tree.</param>
        /// <return>The visitor result.</return>
        public override object VisitAsmline(Z80AsmParser.AsmlineContext context)
        {
            if (IsInvalidContext(context))
            {
                return(null);
            }

            _label       = null;
            _labelSpan   = null;
            _keywordSpan = null;
            _numbers.Clear();
            _identifiers.Clear();
            _sourceLine  = context.Start.Line;
            _firstPos    = context.Start.Column;
            _comment     = null;
            _commentSpan = null;

            // --- Obtain comments
            var lastChild = context.GetChild(context.ChildCount - 1);

            if (lastChild is Z80AsmParser.LabelContext labelContext)
            {
                // --- Handle label-only lines
                VisitLabel(labelContext);
                return(AddLine(new LabelOnlyLine(), context));
            }

            if (lastChild is Z80AsmParser.CommentContext commentContext)
            {
                _comment     = commentContext.GetText();
                _commentSpan = new TextSpan(commentContext.Start.StartIndex,
                                            commentContext.Start.StopIndex + 1);

                // --- Handle comment-only lines
                if (context.ChildCount == 1)
                {
                    return(AddLine(new CommentOnlyLine(), context));
                }
            }

            var line = base.VisitAsmline(context);

            // --- Let's save lines with parsing errors, too.
            return(context.exception != null
                ? AddLine(new ParserErrorLine(), context)
                : line);
        }
Ejemplo n.º 2
0
        public override object VisitAsmline(Z80AsmParser.AsmlineContext context)
        {
            CurrentLabel      = null;
            CurrentLabelColon = false;
            CurrentComment    = null;
            LabelSpan         = null;
            KeywordSpan       = null;
            CommentSpan       = null;
            IssueToEmit       = null;
            IsFieldAssignment = false;
            NumberSpans       = null;
            IdentifierSpans   = null;
            StringSpans       = null;
            FunctionSpans     = null;
            SemiVarSpans      = null;
            MacroParamSpans   = null;
            MacroParamNames   = null;
            SemiVarSpans      = null;
            StatementSpans    = null;
            OperandSpans      = null;
            MnemonicSpans     = null;

            if (context?.Start == null || context.Stop == null)
            {
                return(null);
            }

            var startIndex = context.start.StartIndex;
            var stopIndex  = context.stop.StopIndex;

            CurrentSourceText = InputStream.GetText(new Interval(startIndex, stopIndex));

            CurrentSourceLine = context.Start.Line;
            FirstColumn       = context.Start.Column;
            FirstPosition     = context.Start.StartIndex;
            LastPosition      = context.Stop.StopIndex;

            object mainInstructionPart = null;

            // --- Obtain label
            var labelCtx = context.label();

            if (labelCtx != null)
            {
                CurrentLabel      = labelCtx.GetChild(0).NormalizeToken();
                CurrentLabelColon = labelCtx.COLON() != null;
                LabelSpan         = new TextSpan(labelCtx.Start.StartIndex, labelCtx.Start.StopIndex + 1);
            }

            // --- Obtain line body/directive
            var lineBodyCtx = context.lineBody();

            if (lineBodyCtx != null)
            {
                // --- Special case, when a macro parameters is used as the main line
                LastInstructionPos = lineBodyCtx.Stop.StopIndex;
                var macroParamCtx = lineBodyCtx.macroParam();
                if (macroParamCtx != null)
                {
                    VisitMacroParam(macroParamCtx);
                    mainInstructionPart = new MacroParamLine(macroParamCtx.IDENTIFIER()?.NormalizeToken());
                }
                else
                {
                    mainInstructionPart = VisitLineBody(context.lineBody());
                }
            }
            else if (context.directive() != null)
            {
                mainInstructionPart = VisitDirective(context.directive());
            }

            // --- Obtain comment
            if (context.comment() != null)
            {
                var commentCtx = context.comment();
                CurrentComment = commentCtx.GetText();
                CommentSpan    = new TextSpan(commentCtx.Start.StartIndex, commentCtx.Stop.StopIndex + 1);
            }

            // --- Now, we have every part of the line, and create some special main instruction part
            if (context.exception != null)
            {
                mainInstructionPart = new ParserErrorLine();
            }
            else if (mainInstructionPart == null && (CurrentLabel != null || CurrentComment != null))
            {
                mainInstructionPart = new NoInstructionLine();
                if (CurrentLabel != null && !CurrentLabelColon)
                {
                    var statementFound = true;
                    switch (CurrentLabel.ToLower())
                    {
                    case "continue":
                        mainInstructionPart = new ContinueStatement();
                        break;

                    case "break":
                        mainInstructionPart = new BreakStatement();
                        break;

                    case "endm":
                    case "mend":
                        mainInstructionPart = new MacroEndStatement();
                        break;

                    case "endl":
                    case "lend":
                        mainInstructionPart = new LoopEndStatement();
                        break;

                    case "proc":
                        mainInstructionPart = new ProcStatement();
                        break;

                    case "endp":
                    case "pend":
                        mainInstructionPart = new ProcEndStatement();
                        break;

                    case "repeat":
                        mainInstructionPart = new RepeatStatement();
                        break;

                    case "endw":
                    case "wend":
                        mainInstructionPart = new WhileEndStatement();
                        break;

                    case "ends":
                        mainInstructionPart = new StructEndStatement();
                        break;

                    case "else":
                        mainInstructionPart = new ElseStatement();
                        break;

                    case "endif":
                        mainInstructionPart = new IfEndStatement();
                        break;

                    default:
                        statementFound = false;
                        break;
                    }

                    if (statementFound)
                    {
                        KeywordSpan  = new TextSpan(context.Start.StartIndex, context.Start.StopIndex + 1);
                        CurrentLabel = null;
                    }
                }
            }

            return(mainInstructionPart is SourceLineBase sourceLine
                ? AddLine(sourceLine, context)
                : mainInstructionPart);
        }
Ejemplo n.º 3
0
        public override object VisitAsmline(Z80AsmParser.AsmlineContext context)
        {
            CurrentLabel      = null;
            CurrentComment    = null;
            LabelSpan         = null;
            KeywordSpan       = null;
            CommentSpan       = null;
            IssueToEmit       = null;
            IsFieldAssignment = false;
            NumberSpans       = null;
            IdentifierSpans   = null;
            StringSpans       = null;
            FunctionSpans     = null;
            SemiVarSpans      = null;
            MacroParamSpans   = null;
            MacroParamNames   = null;
            SemiVarSpans      = null;
            StatementSpans    = null;
            OperandSpans      = null;
            MnemonicSpans     = null;

            if (context?.Start == null || context.Stop == null)
            {
                return(null);
            }

            var startIndex = context.start.StartIndex;
            var stopIndex  = context.stop.StopIndex;

            CurrentSourceText = InputStream.GetText(new Interval(startIndex, stopIndex));

            CurrentSourceLine = context.Start.Line;
            FirstColumn       = context.Start.Column;
            FirstPosition     = context.Start.StartIndex;
            LastPosition      = context.Stop.StopIndex;

            object mainInstructionPart = null;

            // --- Obtain label
            var labelCtx = context.label();

            if (labelCtx != null)
            {
                CurrentLabel = labelCtx.GetChild(0).NormalizeToken();
                LabelSpan    = new TextSpan(labelCtx.Start.StartIndex, labelCtx.Start.StopIndex + 1);
            }

            // --- Obtain line body/directive
            var lineBodyCtx = context.lineBody();

            if (lineBodyCtx != null)
            {
                // --- Special case, when a macro parameters is used as the main line
                LastInstructionPos = lineBodyCtx.Stop.StopIndex;
                var macroParamCtx = lineBodyCtx.macroParam();
                if (macroParamCtx != null)
                {
                    VisitMacroParam(macroParamCtx);
                    mainInstructionPart = new MacroParamLine(macroParamCtx.IDENTIFIER()?.NormalizeToken());
                }
                else
                {
                    mainInstructionPart = VisitLineBody(context.lineBody());
                }
            }
            else if (context.directive() != null)
            {
                mainInstructionPart = VisitDirective(context.directive());
            }

            // --- Obtain comment
            if (context.comment() != null)
            {
                var commentCtx = context.comment();
                CurrentComment = commentCtx.GetText();
                CommentSpan    = new TextSpan(commentCtx.Start.StartIndex, commentCtx.Stop.StopIndex + 1);
            }

            // --- Now, we have every part of the line, and create some special main instruction part
            if (context.exception != null)
            {
                mainInstructionPart = new ParserErrorLine();
            }
            else if (mainInstructionPart == null && (CurrentLabel != null || CurrentComment != null))
            {
                // --- Either a label only or a comment only line
                mainInstructionPart = new NoInstructionLine();
            }

            return(mainInstructionPart is SourceLineBase sourceLine
                ? AddLine(sourceLine, context)
                : mainInstructionPart);
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Visit a parse tree produced by <see cref="Z80AsmParser.asmline"/>.
 /// <para>
 /// The default implementation returns the result of calling <see cref="AbstractParseTreeVisitor{Result}.VisitChildren(IRuleNode)"/>
 /// on <paramref name="context"/>.
 /// </para>
 /// </summary>
 /// <param name="context">The parse tree.</param>
 /// <return>The visitor result.</return>
 public virtual Result VisitAsmline([NotNull] Z80AsmParser.AsmlineContext context)
 {
     return(VisitChildren(context));
 }
Ejemplo n.º 5
0
 /// <summary>
 /// Exit a parse tree produced by <see cref="Z80AsmParser.asmline"/>.
 /// <para>The default implementation does nothing.</para>
 /// </summary>
 /// <param name="context">The parse tree.</param>
 public virtual void ExitAsmline([NotNull] Z80AsmParser.AsmlineContext context)
 {
 }