Example #1
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);
        }
Example #2
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);
        }