Exemplo n.º 1
0
        internal static Inst Parse(LexContext input)
        {
            Inst result = new Inst();

            _SkipCommentsAndWhiteSpace(input);
            var l = input.Line;
            var c = input.Column;
            var p = input.Position;

            result.Line = l;
            var id = _ParseIdentifier(input);

            switch (id)
            {
            case "regex":
                _SkipWhiteSpace(input);
                result.Opcode = Regex;
                input.Expecting('(');
                input.Advance();
                result.Expr = Ast.Parse(input);
                input.Expecting(')');
                input.Advance();
                _SkipToNextInstruction(input);
                break;

            case "match":
                _SkipWhiteSpace(input);
                var ll  = input.CaptureBuffer.Length;
                var neg = false;
                if ('-' == input.Current)
                {
                    neg = true;
                    input.Advance();
                }
                if (!input.TryReadDigits())
                {
                    throw new ExpectingException("Illegal operand in match instruction. Expecting integer", input.Line, input.Column, input.Position, input.FileOrUrl, "integer");
                }
                var i = int.Parse(input.GetCapture(ll));
                if (neg)
                {
                    i = -i;
                }
                result.Opcode = Match;
                result.Value  = i;
                _SkipToNextInstruction(input);
                break;

            /*case "jmp":
             *      _SkipWhiteSpace(input);
             *      var lbl = _ParseIdentifier(input);
             *      result.Opcode = Jmp;
             *      result.Name = lbl;
             *      _SkipToNextInstruction(input);
             *      break;*/
            case "jmp":
                _SkipWhiteSpace(input);
                result.Opcode = Jmp;
                result.Labels = _ParseLabels(input);
                _SkipToNextInstruction(input);
                break;

            case "switch":
                _SkipWhiteSpace(input);
                result.Opcode = Switch;
                _ParseCases(result, input);
                _SkipToNextInstruction(input);
                break;

            case "any":
                result.Opcode = Any;
                _SkipToNextInstruction(input);
                break;

            case "char":
                _SkipWhiteSpace(input);
                result.Opcode = Char;
                result.Value  = _ParseChar(input);
                _SkipToNextInstruction(input);
                break;

            case "set":
                _SkipWhiteSpace(input);
                result.Opcode = Set;
                result.Ranges = _ParseRanges(input);
                _SkipToNextInstruction(input);
                break;

            case "nset":
                _SkipWhiteSpace(input);
                result.Opcode = NSet;
                result.Ranges = _ParseRanges(input);
                _SkipToNextInstruction(input);
                break;

            case "ucode":
                _SkipWhiteSpace(input);
                ll = input.CaptureBuffer.Length;
                if (!input.TryReadDigits())
                {
                    throw new ExpectingException("Illegal operand in ucode instruction. Expecting integer", input.Line, input.Column, input.Position, input.FileOrUrl, "integer");
                }
                i             = int.Parse(input.GetCapture(ll));
                result.Opcode = UCode;
                result.Value  = i;
                _SkipToNextInstruction(input);
                break;

            case "nucode":
                _SkipWhiteSpace(input);
                ll = input.CaptureBuffer.Length;
                if (!input.TryReadDigits())
                {
                    throw new ExpectingException("Illegal operand in nucode instruction. Expecting integer", input.Line, input.Column, input.Position, input.FileOrUrl, "integer");
                }
                i             = int.Parse(input.GetCapture(ll));
                result.Opcode = NUCode;
                result.Value  = i;
                _SkipToNextInstruction(input);
                break;

            case "save":
                _SkipWhiteSpace(input);
                ll = input.CaptureBuffer.Length;
                if (!input.TryReadDigits())
                {
                    throw new ExpectingException("Illegal operand in save instruction. Expecting integer", input.Line, input.Column, input.Position, input.FileOrUrl, "integer");
                }
                i             = int.Parse(input.GetCapture(ll));
                result.Opcode = Save;
                result.Value  = i;
                _SkipToNextInstruction(input);
                break;

            default:
                if (':' != input.Current)
                {
                    throw new ExpectingException("Expecting instruction or label", l, c, p, input.FileOrUrl, "match", "jmp", "jmp", "any", "char", "set", "nset", "ucode", "nucode", "save", "label");
                }
                input.Advance();
                result.Opcode = Label;
                result.Name   = id;
                break;
            }
            _SkipCommentsAndWhiteSpace(input);
            return(result);
        }