Пример #1
0
        public void Sql()
        {
            var parser = new Core.GrammarGrammar.Parser();

            var node = parser.Parse(@"Sql
            defs
            Statement : select StartOrFieldList
            StartOrFieldList : star | FieldList
            FieldList : FieldDef [comma FieldDef]
            FieldDef : [Agg openBracket] Field [closeBracket]
            Agg : count | min | max | avg
            Field : [openSquare] Text [closeSquare]
            texts
            Text : "".*""
            keywords
            select: select
            count: count
            min: min
            max: max
            avg: avg
            punctuation
            star: ""*""
            comma: ,
            openBracket: (
            closeBracket : )
            openSquare: ""[""
            closeSquare: ""]""");

            var generator = new Generator();
            var builder = new Builder();

            string nodeTree = generator.GenerateNodeTree(node);

            var grammar = generator.BuildGrammar(node);

            string actual = GenerateAndBuildParser(grammar, "select abc def ghi");
        }
Пример #2
0
        private string GenerateAndBuildParser(Grammar grammar, string text)
        {
            Generator generator = new Generator();
            Builder builder = new Builder();
            string lexerDef = generator.GenerateLexer(grammar);

            string parserDef = generator.GenerateParser(grammar);

            var assembly = builder.Build(lexerDef, parserDef);

            object parser = Activator.CreateInstance(assembly.GetType("Xxx.Parser"));
            object walker = Activator.CreateInstance(assembly.GetType("Xxx.Walker"));

            var node = parser.GetType().GetMethod("Parse").Invoke(parser, new object[] { text });
            return (string)walker.GetType().GetMethod("NodesToString").Invoke(walker, new[] { node });
        }
Пример #3
0
        private void Run(string grammarDef, string input, string expected)
        {
            var parser = new Core.GrammarGrammar.Parser();

            var generator = new Generator();
            var builder = new Builder();

            var node = parser.Parse(grammarDef);

               var tree=  generator.GenerateNodeTree(node);

            var grammar =  generator.BuildGrammar(node);

            string actual = GenerateAndBuildParser(grammar, input);

            Assert.That(actual, Is.EqualTo(expected));
        }
Пример #4
0
        private void Compile()
        {
            Builder builder = new Builder();

            try
            {
                var assembly = builder.Build(genLexer.Text, genParser.Text);

                var parserType = assembly.GetType($"Xxx.{_grammarName}Parser");
                _parser = Activator.CreateInstance(parserType);
                _parseMethod = parserType.GetMethod("Parse");

                var walkerType = assembly.GetType("Xxx.Walker");
                _walker = Activator.CreateInstance(walkerType);
                _nodesToStringMethod = walkerType.GetMethod("NodesToString");
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message);
            }
        }