Ejemplo n.º 1
0
        private void Build()
        {
            genGrammar.Text = "";
            genLexer.Text = "";
            genParser.Text = "";

            Parser parser = new Parser();
            Generator generator = new Generator();

            Node<NodeType> node;
            try
            {
                node = parser.Parse(grammarText.Text);

                Grammar grammar = generator.BuildGrammar(node);

                _grammarName = grammar.Name;

                RefreshGrammar(grammar);

                genLexer.Text = generator.GenerateLexer(grammar);
                genParser.Text = generator.GenerateParser(grammar);

                Compile();

            }
            catch (Exception exception)
            {
                _parser = null;
                _parseMethod = null;

                MessageBox.Show(exception.Message);
            }
        }
Ejemplo n.º 2
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");
        }
Ejemplo n.º 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));
        }
Ejemplo n.º 4
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 });
        }