private static ITokenParameter ParseNumber(string text, ref int i)
        {
            var match = RegexNumeric.Match(text, i);

            if (!match.Success)
            {
                throw new InvalidOperationException(
                          $"Could not parse numeric value from the beginning of {text.Substring(i)}");
            }
            var numericText = match.Value.ToUpper();

            i += match.Value.Length - 1;
            if (numericText.EndsWith("L"))
            {
                return(LiteralParameter.Create(Convert.ToInt64(match.Value.Substring(0, match.Value.Length - 1))));
            }
            if (numericText.EndsWith("F"))
            {
                return(LiteralParameter.Create(Convert.ToSingle(match.Value.Substring(0, match.Value.Length - 1),
                                                                CultureInfo.InvariantCulture)));
            }
            if (match.Groups[1].Value != "")
            {
                // Captured text with the decimal separator
                return(LiteralParameter.Create(Convert.ToDecimal(match.Value, CultureInfo.InvariantCulture)));
            }
            return(LiteralParameter.Create(Convert.ToInt32(match.Value)));
        }
Exemple #2
0
        private static ITokenParameter ParseParameter(string text, ref int i)
        {
            var firstChar = text[i];

            while (char.IsWhiteSpace(firstChar))
            {
                firstChar = text[++i];
            }
            if (firstChar == ')')
            {
                return(null);
            }
            if (firstChar == '"' || firstChar == '\'')
            {
                return(StringParameter.Parse(text, firstChar, ref i));
            }
            if (char.IsDigit(firstChar))
            {
                return(ParseNumber(text, ref i));
            }
            if (text.Length >= i + 3 && text.Substring(i, 3) == "__.")
            {
                var startIndex = i;
                var tokens     = ParseTokens(text, ref i);
                return(new StaticTraversalParameter(tokens, text.Substring(startIndex, i - startIndex)));
            }
            if (text.Substring(i, 2).StartsWith("P."))
            {
                return(new PParameter(ParseTokens(text, ref i)));
            }
            var parameterText  = text.Substring(i, text.IndexOf(')', i) - i);
            var separatorIndex = parameterText.IndexOf(',');

            if (separatorIndex >= 0)
            {
                parameterText = parameterText.Substring(0, separatorIndex);
            }
            parameterText = parameterText.Trim();
            if (parameterText == "")
            {
                return(null);
            }
            if (parameterText == "true" || parameterText == "false")
            {
                i += parameterText.Length - 1;
                return(LiteralParameter.Create(Convert.ToBoolean(parameterText)));
            }
            if (RegexEnum.IsMatch(parameterText))
            {
                i += parameterText.Length - 1;
                return(new TraversalEnumParameter(parameterText));
            }
            if (RegexParam.IsMatch(parameterText))
            {
                i += parameterText.Length - 1;
                return(new ContextBasedParameter(parameterText));
            }
            throw new NotSupportedException($"Parameter {parameterText} not supported");
        }
Exemple #3
0
        public bool Equals(LiteralParameter <T> other)
        {
            if (Value == null)
            {
                return(other.Value == null);
            }

            return(Value.Equals(other.Value));
        }
Exemple #4
0
        public void Traversal_Parser_Should_Parse_Into_Tokens()
        {
            var items = new[]
            {
                Tuple.Create("g.V().count()", new[] { new Token("count") }),
                Tuple.Create("g.V().values(\"name\")",
                             new[] { new Token("values", new StringParameter("name")) }),
                Tuple.Create("g.V().constant(123l)",
                             new[] { new Token("constant", new[] { LiteralParameter.Create(123L) }) }),
                Tuple.Create("g.V().constant(123)",
                             new[] { new Token("constant", new[] { LiteralParameter.Create(123) }) }),
                Tuple.Create("g.V().constant(123.50)",
                             new[] { new Token("constant", new[] { LiteralParameter.Create(123.50m) }) }),
                Tuple.Create("g.V().constant(123.1f)",
                             new[] { new Token("constant", new[] { LiteralParameter.Create(123.1f) }) }),
                Tuple.Create("g.V().has(\"no\").count()",
                             new[] { new Token("has", new StringParameter("no")), new Token("count") }),
                Tuple.Create("g.V().has(\"lang\", \"java\")",
                             new[] { new Token("has", new[] { new StringParameter("lang"), new StringParameter("java") }) }),
                Tuple.Create("g.V().where(__.in(\"knows\"))",
                             new[] { new Token("where", new[] { new StaticTraversalParameter(
                                                                    new[] { new Token("__"), new Token("in", new StringParameter("knows")) }, "__.in(\"knows\")") }) }),
                Tuple.Create("g.V().has(\"age\",P.gt(27))",
                             new[] { new Token("has", new ITokenParameter[] { new StringParameter("age"),
                                                                              new PParameter(
                                                                                  new[] { new Token("P"), new Token("gt", LiteralParameter.Create(27)) }) }) }),
                Tuple.Create("g.V().count(Scope.local)",
                             new[] { new Token("count", new TraversalEnumParameter("Scope.local")) }),
                Tuple.Create("g.V().\n  count()", new[] { new Token("count") }),
                Tuple.Create("g.V().\n has ( \"a\" ) \n.  \ncount()",
                             new[] { new Token("has", new StringParameter("a")), new Token("count") }),
                Tuple.Create("g.V().choose(__.outE(),__.as(\"a\"))", new []
                {
                    new Token("choose", new ITokenParameter[] {
                        new StaticTraversalParameter(new[] { new Token("__"), new Token("outE") }, "__.outE()"),
                        new StaticTraversalParameter(
                            new[] { new Token("__"), new Token("as", new StringParameter("a")) }, "__.as(\"a\")")
                    })
                })
            };

            foreach (var item in items)
            {
                var parts = TraversalParser.ParseTraversal(item.Item1);
                Assert.Equal(item.Item2, parts.Skip(2));
            }
        }