Exemplo n.º 1
0
        public void RequiredSequenceTest(string data, int ruleNumber, bool shouldPass)
        {
            var          parser = new LexicalParser <TokenType>(_Rules[ruleNumber]);
            ParserResult result = parser.Parse(data);

            result.Should().NotBeNull();
            result.IsSuccess.Should().Be(shouldPass);
        }
Exemplo n.º 2
0
        public void LetNodeSingleFailTests(string data)
        {
            var          parser = new LexicalParser <TokenType>(_onlyPlusRules);
            ParserResult result = parser.Parse(data);

            result.Should().NotBeNull();
            result.IsSuccess.Should().BeFalse();
        }
Exemplo n.º 3
0
        public void ForumlaTest(string data, bool shouldPass)
        {
            var          parser = new LexicalParser <TokenType>(_forumalRules);
            ParserResult result = parser.Parse(data);

            result.Should().NotBeNull();
            result.IsSuccess.Should().Be(shouldPass);
        }
Exemplo n.º 4
0
 public ExpressionLanguageAST Parser(string expression)
 {
     LexicalParser l = new LexicalParser();
     l.SetParseContent(expression);
     SemanticParser s = new SemanticParser();
     s.SetParseContent(l.Parse());
     return s.Parse();
 }
Exemplo n.º 5
0
        public void NoClassNameFailTest()
        {
            IEnumerable <string> data = new List <string>
            {
                "public class",
                "{",
                "    public static string Cleanse(string name)",
                "}",
            };

            var          parser = new LexicalParser <TokenType>(_rules);
            ParserResult result = parser.Parse(string.Join(" ", data));

            result.Should().NotBeNull();
            result.IsSuccess.Should().BeFalse();
        }
Exemplo n.º 6
0
        public JoinPointDefination(Position position, string advisorName, string pattern)
        {
            _position = position;
            _advisorName = advisorName;
            _pattern = pattern;

            LexicalParser lp = new LexicalParser();
            lp.SetParseContent(_pattern);
            var tokens = lp.Parse();
            try
            {
                Init(tokens);
            }
            catch (Exception ex)
            {
                ExceptionHelper.ThrowWeaveSyntaxInitialize(_pattern, ex);
            }
        }
Exemplo n.º 7
0
        public void NoStaticTest()
        {
            IEnumerable <string> data = new List <string>
            {
                "public class TestClass",
                "{",
                "    public static string Cleanse(string name)",
                "}",
            };

            var          parser = new LexicalParser <TokenType>(_rules);
            ParserResult result = parser.Parse(string.Join(" ", data));

            result.Should().NotBeNull();
            result.IsSuccess.Should().BeTrue();

            result.RootNode.OfType <Symbol <TokenType> >().Any(x => Language.SymStatic.Equals(x)).Should().BeFalse();
            result.RootNode.OfType <Expression <TokenType> >().Single(x => Language.ClassName.Equals(x)).Value.Should().Be("TestClass");
            result.RootNode.OfType <Expression <TokenType> >().Any(x => Language.SubclassName.Equals(x)).Should().BeFalse();
        }
Exemplo n.º 8
0
        public void LetDifferentOpsValues(string data, string[] testValues)
        {
            var          parser = new LexicalParser <TokenType>(_Rules);
            ParserResult result = parser.Parse(data);

            result.Should().NotBeNull();
            result.IsSuccess.Should().BeTrue();

            result.RootNode.OfType <Expression <TokenType> >().Single(x => Language.VariableName.Equals(x)).Value.Should().Be("MyVariable");
            result.RootNode.OfType <Expression <TokenType> >().Single(x => Language.VariableType.Equals(x)).Value.Should().Be("double");

            var values = result.RootNode.OfType <Expression <TokenType> >()
                         .Where(x => Language.Variable.Equals(x));

            values.Count().Should().Be(testValues.Length);

            values.Select(x => x.Value).OrderBy(x => x)
            .Zip(testValues.OrderBy(x => x), (o, i) => new { o, i })
            .All(x => x.o == x.i)
            .Should().BeTrue();
        }
Exemplo n.º 9
0
        static double ExecuteExpression(string expression, Dictionary <string, NumberType> variables, out IList <Token> lexics)
        {
            lexics = LexicalParser.Parse(expression).ToList();
            SyntaxTree    syntaxTree = SyntaxParser.Parse(lexics);
            DynamicMethod method     = ArithmeticTreeCompiler.Compile(syntaxTree);

            var undefinedVariables = syntaxTree.VariableNames.Except(variables.Keys).ToList();

            if (undefinedVariables.Count != 0)
            {
                throw new ArgumentException($"Undefined variables found: {string.Join(", ", undefinedVariables)}");
            }

            object[] args =
                syntaxTree.VariableNames
                .OrderBy(i => i)
                .Select(i => variables[i])
                .Cast <object>()
                .ToArray();

            return((double)method.Invoke(null, args));
        }
Exemplo n.º 10
0
        private QueryNode ParseData(IEnumerable <string> data, bool shouldPass)
        {
            string rawData = string.Join(" ", data);

            var          parser = new LexicalParser <TokenType>(_rules);
            ParserResult result = parser.Parse(rawData);

            result.IsSuccess.Should().Be(shouldPass, result?.LastGood?.ToString() ?? "<no good last>");
            if (!result.IsSuccess)
            {
                return(null);
            }

            // ====================================================================================
            string rowsetName = result.RootNode
                                .OfType <Expression <TokenType> >()
                                .FirstOrDefault(x => Language.Rowset.Equals(x))
                                ?.Value;

            // ====================================================================================
            IEnumerable <Rowset> rowsetList = result.RootNode
                                              .OfType <Expression <TokenType> >()
                                              .Where(x => Language.RefRowset.Equals(x) || Language.RefRowsetAlias.Equals(x))
                                              .Partition((l, x) => !Language.RefRowsetAlias.Equals(x))
                                              .Select(x => new Rowset(x));

            // ====================================================================================
            IEnumerable <ColumnName> columnList = result.RootNode
                                                  .Where(x => Language.ColumnReference.Equals(x) || Language.SymAs.Equals(x) || Language.ColumnName.Equals(x))
                                                  .Partition((l, x) => Language.ColumnName.Equals(l.LastOrDefault()))
                                                  .Select(x => new ColumnName(x));

            IEnumerable <Join> joinList = result.RootNode
                                          .Where(x => Language.JoinReference.Equals(x))
                                          .Partition(2)
                                          .Select(x => new Join(x));

            return(new QueryNode(rowsetName, rowsetList, columnList, joinList));
        }
Exemplo n.º 11
0
        public void LetNodeSingleVariableTest()
        {
            IEnumerable <string> data = new List <string>
            {
                "let EmptyLong long? = null;",
            };

            var          parser = new LexicalParser <TokenType>(_onlyPlusRules);
            ParserResult result = parser.Parse(string.Join(" ", data));

            result.Should().NotBeNull();
            result.IsSuccess.Should().BeTrue();

            result.RootNode.OfType <Expression <TokenType> >().Single(x => Language.VariableName.Equals(x)).Value.Should().Be("EmptyLong");
            result.RootNode.OfType <Expression <TokenType> >().Single(x => Language.VariableType.Equals(x)).Value.Should().Be("long?");

            var values = result.RootNode.OfType <Expression <TokenType> >()
                         .Where(x => Language.Variable.Equals(x));

            values.Count().Should().Be(1);
            values.First().Value.Should().Be("null");
        }
Exemplo n.º 12
0
        public void FullClassTest()
        {
            IEnumerable <string> data = new List <string>
            {
                "public static class NameCleaner",
                "{",
                "    public static string Cleanse(string name)",
                "    {",
                "        Dictionary<string, string> changes = new Dictionary<string, string>();",
                "",
                "        changes.Add(\" Twp\", \" Township\");",
                "        changes.Add(\", Town of\", \"\");",
                "        changes.Add(\" Town of\", \"\");",
                "        changes.Add(\", Village of\", \"\");",
                "",
                "        foreach (KeyValuePair<string, string> kvp in changes)",
                "        {",
                "            if (true == name.EndsWith(kvp.Key))",
                "            {",
                "                name = name.Substring(0, name.Length - kvp.Key.Length) + kvp.Value;",
                "            }",
                "        }",
                "",
                "        return name;",
                "    }",
                "}",
            };

            var          parser = new LexicalParser <TokenType>(_rules);
            ParserResult result = parser.Parse(string.Join(" ", data));

            result.Should().NotBeNull();
            result.IsSuccess.Should().BeTrue();

            result.RootNode.OfType <Symbol <TokenType> >().Any(x => Language.SymStatic.Equals(x)).Should().BeTrue();
            result.RootNode.OfType <Expression <TokenType> >().Single(x => Language.ClassName.Equals(x)).Value.Should().Be("NameCleaner");
            result.RootNode.OfType <Expression <TokenType> >().Any(x => Language.SubclassName.Equals(x)).Should().BeFalse();
        }
Exemplo n.º 13
0
        public TemplateAST Parse(string path)
        {
            string templateFile = Path.Combine(_engine.Path, path);

            _log.Debug("Begin parse template file [{0}].", templateFile);

            if (!File.Exists(templateFile))
                ExceptionHelper.ThrowFileNotFound(templateFile);

            using (StreamReader r = new StreamReader(templateFile))
            {
                string template = r.ReadToEnd();
                LexicalParser lp = new LexicalParser();
                lp.SetParseContent(template);
                SemanticParser sp = new SemanticParser();
                sp.SetParseContent(lp.Parse());
                TemplateAST ast = sp.Parse();

                _log.Debug("Parse template file [{0}] success.", templateFile);

                ParseIncludeTemplate(ast);
                return ast;
            }
        }
Exemplo n.º 14
0
        public string RenderRaw(string textTemplate)
        {
            textTemplate.ThrowIfNullArgument(nameof(textTemplate));

            try
            {
                LexicalParser lp = new LexicalParser();
                lp.SetParseContent(textTemplate);
                SemanticParser sp = new SemanticParser();
                sp.SetParseContent(lp.Parse());
                TemplateAST ast = sp.Parse();

                return this.Render(ast);
            }
            catch (ELParseException)
            {
                throw;
            }
            catch (EvalException)
            {
                throw;
            }
            catch (TemplateParseException)
            {
                throw;
            }
            catch (TemplateRenderException)
            {
                throw;
            }
            catch (Exception ex)
            {
                ExceptionHelper.ThrowWrapped(ex);
                return string.Empty;
            }
        }