Ejemplo n.º 1
0
        public CodeString BetweenOperatos(CodeString Code)
        {
            if (UseMarker)
            {
                var StrLength = Code.Length;
                if (StrLength < 2 || Code[0] != Marker || Code[StrLength - 1] != Marker)
                {
                    return(new CodeString());
                }
            }

            var StartRes = Code.StartsWith(Operators, Skip);
            var EndRes   = Code.EndsWith(Operators, Skip);

            if (StartRes.Index == -1 || EndRes.Index == -1)
            {
                return(new CodeString());
            }
            if (StartRes.Index != EndRes.Index)
            {
                return(new CodeString());
            }

            var Length = StartRes.String.Length;

            if (EndRes.Position < Length)
            {
                return(new CodeString());
            }

            Code = Code.Substring(Length, Code.Length - Length * 2);
            if (Code.Find(Operators, Skip).Index != -1)
            {
                return(new CodeString());
            }
            return(Code);
        }
Ejemplo n.º 2
0
        public ExprRecResult Recognize(CodeString Code, PluginRoot Plugin, ref ExpressionNode Ret)
        {
            if (Code.Length == 0)
            {
                return(ExprRecResult.Unknown);
            }

            var OldCode   = Code;
            var State     = Plugin.State;
            var Container = Plugin.Container;
            var Global    = Container.GlobalContainer;

            var RadixSpecified = false;
            var Radix          = 10;

            var Sign = RecognizerHelper.GetSign(ref Code);

            if (Code.Length == 0)
            {
                return(ExprRecResult.Unknown);
            }

            if (AtStartStrings != null)
            {
                var Result = Code.StartsWith(AtStartStrings, Skip);
                if (Result.Index != -1)
                {
                    Code = Code.TrimmedSubstring(State, Result.String.Length);
                    if (!Code.IsValid)
                    {
                        return(ExprRecResult.Failed);
                    }

                    RadixSpecified = true;
                    Radix          = AtStart[Result.Index].Radix;
                }
            }

            if (AtEndStrings != null)
            {
                var Result = Code.EndsWith(AtEndStrings, Skip);
                if (Result.Index != -1)
                {
                    if (Radix != -1)
                    {
                        State.Messages.Add(MessageId.MultipleRadix, Code);
                        return(ExprRecResult.Failed);
                    }

                    Code = Code.TrimmedSubstring(State, Result.String.Length);
                    if (!Code.IsValid)
                    {
                        return(ExprRecResult.Failed);
                    }

                    RadixSpecified = true;
                    Radix          = AtStart[Result.Index].Radix;
                }
            }

            if (RecognizerHelper.GetSign(ref Code))
            {
                Sign = !Sign;
            }
            if (Code.Length == 0 || (!char.IsDigit(Code[0]) && !RadixSpecified))
            {
                return(ExprRecResult.Unknown);
            }

            var Options = new ConvStrToNumberOptions();
            var Type    = TypeExtractor(Container, ref Code);

            if (Type == null)
            {
                Options.Type = ConstValueType.Unknown;
            }
            else
            {
                var RType = Type.RealId as Type;
                Options.Type = RType.ConstValueType;
            }

            Code = Code.Trim();
            if (Code.Length == 0)
            {
                return(ExprRecResult.Unknown);
            }

            Options.Radix           = Radix;
            Options.Number          = Code.String;
            Options.LetterCase      = LetterCase.OnlyUpper;
            Options.EnableENotation = true;
            Options.Sign            = Sign;

            ConstValue Value;
            var        Res = RecognizerHelper.ConvStrToNumber(State, Code, Options, out Value);

            if (Res == SimpleRecResult.Unknown)
            {
                return(ExprRecResult.Unknown);
            }
            else if (Res == SimpleRecResult.Failed)
            {
                return(ExprRecResult.Failed);
            }

            if (Type == null)
            {
                Type = Constants.GetDefaultType(Container, Value.Type);
                if (Type.RealId is NumberType && !Value.CheckBounds(Type))
                {
                    var SystemType = Type.GetType();

                    do
                    {
                        var RType = Type.RealId as Type;
                        Type = Global.CommonIds.GetIdentifier(SystemType, RType.Size * 2);

                        if (Type == null)
                        {
                            if (typeof(SignedType).IsEquivalentTo(SystemType))
                            {
                                Type = Global.CommonIds.GetLargestType(typeof(UnsignedType));
                            }
                            else
                            {
                                Type = Global.CommonIds.GetLargestType(typeof(SignedType));
                            }
                            break;
                        }
                    } while (!Value.CheckBounds(Type));
                }
            }

            if (!Value.CheckBounds(State, Type, OldCode))
            {
                return(ExprRecResult.Failed);
            }

            var Flags = Options.Type == ConstValueType.Unknown ?
                        ExpressionFlags.AutoConvert : ExpressionFlags.None;

            Ret = new ConstExpressionNode(Type, Value, OldCode, Flags);
            return(ExprRecResult.Succeeded);
        }