Пример #1
0
        public static void SetUp(Module module, Grammar grammar)
        {
            if (pattern == null)
            {
                BlockCommentPrototype.pattern
                    = new AbstractPattern(null, "BlockCommentPrototype");

                BlockCommentPrototype.pattern.SetType(typeof(BlockCommentPrototype));
                grammar.PatternDefined(BlockCommentPrototype.pattern);

                string         expression = "s('/*' *(s(!('*/') a(0 any))) '*/')";
                ParseGraphNode parseGraph = BootstrapParser.Parse(expression,
                                                                  new Pattern[] { BlockCommentPrototype.pattern });

                pattern = new ConcretePattern(null, "BlockComment",
                                              parseGraph);

                pattern.SetType(typeof(BlockComment));

                BlockCommentPrototype.pattern.AddAltPattern(pattern);
            }

            module.SetName("BlockComment", typeof(BlockComment));
            grammar.PatternDefined(pattern);
        }
Пример #2
0
        public Grammar BuildGrammar(string grammar, params string[] implicitNames)
        {
            var resultGrammar = new Grammar();

            foreach (var name in implicitNames)
            {
                resultGrammar.SetImplicit(name);
            }

            parser.Lock();

            ISemanticNode semantics = parser.Parse(grammar);

            var root = (BranchSemanticNode)semantics;

            foreach (BranchSemanticNode decl in root.Children.Cast <BranchSemanticNode>())
            {
                var left = (LeafSemanticNode)decl.Children[0];
                var name = left.Value;

                ISemanticNode right = decl.Children[1];

                if (right is BranchSemanticNode branch)
                {
                    Rule rule = Interpret(resultGrammar, right);
                    resultGrammar.DefineRule(name, rule);
                }
                else if (right is LeafSemanticNode leaf)
                {
                    switch ((EbnfNodeType)leaf.NodeType)
                    {
                    case EbnfNodeType.Identifier:
                        resultGrammar.DefineRule(name, resultGrammar.ReferenceRule(leaf.Value));
                        break;

                    case EbnfNodeType.String:
                        resultGrammar.DefineString(name, leaf.Value);
                        break;

                    case EbnfNodeType.Regex:
                        resultGrammar.DefineRegex(name, leaf.Value);
                        break;

                    default:
                        throw new Exception();
                    }
                }
                else
                {
                    throw new Exception();
                }
            }

            return(resultGrammar);
        }
Пример #3
0
        public static void SetUp(Module module, Grammar grammar)
        {
            if (pattern == null)
            {
                string         expression = "l(text o((whitespace null) t( s( '\\\"' *( a( s( !(a('\\\\' '\\\"')) any ) s('\\\\' a('\\\\' 'r' 'n' 't' '\\\"' '\\\'' '0') ) ) ) '\\\"' ) ) ) )";
                ParseGraphNode parseGraph = BootstrapParser.Parse(expression, null);

                pattern = new ConcretePattern(null, "String", parseGraph);
                pattern.SetType(typeof(String));
            }

            module.SetName("String", typeof(String));
            grammar.PatternDefined(pattern);
        }
Пример #4
0
        public static void SetUp(Module module, Grammar grammar)
        {
            if (pattern == null)
            {
                string expression = "s('//' *(s(!(a( s('\r' ?('\n')) '\n' )) any)))";

                ParseGraphNode parseGraph = BootstrapParser.Parse(expression, null);

                pattern = new ConcretePattern(null, "LineComment", parseGraph);
                pattern.SetType(typeof(LineComment));
            }

            module.SetName("LineComment", typeof(LineComment));
            grammar.PatternDefined(pattern);
        }
Пример #5
0
        public static void SetUp(Module module, Grammar grammar)
        {
            if (pattern == null)
            {
                string expression = "+(a(' ' '\\t' s('\\r' ?('\\n')) '\\n'))";

                ParseGraphNode parseGraph = BootstrapParser.Parse(expression, null);

                pattern = new ConcretePattern(null, "DefaultWhitespace", parseGraph);
                pattern.SetType(typeof(DefaultWhitespace));
            }

            module.SetName("DefaultWhitespace", typeof(DefaultWhitespace));
            grammar.PatternDefined(pattern);
        }
Пример #6
0
        public static void SetUp(Module module, Grammar grammar)
        {
            if (pattern == null)
            {
                string expression = "l(number o((whitespace null) t(s(+(r('0' '9')) ?(s('.' +(r('0' '9'))))))))";

                ParseGraphNode parseGraph = BootstrapParser.Parse(expression, null);

                pattern = new ConcretePattern(null, "Number", parseGraph);
                pattern.SetType(typeof(Number));
            }

            module.SetName("Number", typeof(Number));
            grammar.PatternDefined(pattern);
        }
Пример #7
0
        public new static void SetUp(Module module, Grammar grammar)
        {
            if (pattern == null)
            {
                ParseGraphNode parseGraph = BootstrapParser.Parse("'.'", null);

                pattern = new ConcretePattern(null, "AnyPatternExpression", parseGraph);
                pattern.SetType(typeof(AnyPatternExpression));

                PatternExpression.pattern.AddAltPattern(pattern);
            }

            module.SetName("AnyPatternExpression", typeof(AnyPatternExpression));
            grammar.PatternDefined(pattern);
        }
Пример #8
0
        public new static void SetUp(Module module, Grammar grammar)
        {
            if (pattern == null)
            {
                string    expression = "s('option' l(optionKey 0) ?(s('=' o((dropPrecedence true) l(optionValue 1)))) ';')";
                Pattern[] patterns   = { Name.pattern, Expression.pattern };

                ParseGraphNode parseGraph = BootstrapParser.Parse(expression, patterns);

                pattern = new ConcretePattern(null, "Option", parseGraph);
                pattern.SetType(typeof(Option));
            }

            module.SetName("Option", typeof(Option));
            grammar.PatternDefined(pattern);
        }
Пример #9
0
        public static void SetUp(Module module, Grammar grammar)
        {
            if (pattern == null)
            {
                string    expression = "o((whitespace 0) l(statements *(1)))";
                Pattern[] patterns   = { Whitespace.pattern, TopLevelStatement.pattern };

                ParseGraphNode parseGraph = BootstrapParser.Parse(expression, patterns);

                pattern = new ConcretePattern(null, "Program", parseGraph);
                pattern.SetType(typeof(Program));
            }

            module.SetName("Program", typeof(Program));
            grammar.PatternDefined(pattern);
        }
Пример #10
0
        public static void SetUp(Module module, Grammar grammar)
        {
            if (pattern == null)
            {
                string    expression = "+(a(0 1 2))";
                Pattern[] patterns   = { LineComment.pattern, BlockComment.pattern, DefaultWhitespace.pattern };

                ParseGraphNode parseGraph = BootstrapParser.Parse(expression, patterns);

                pattern = new ConcretePattern(null, "Whitespace", parseGraph);
                pattern.SetType(typeof(Whitespace));
            }

            module.SetName("Whitespace", typeof(Whitespace));
            grammar.PatternDefined(pattern);
        }
Пример #11
0
        public new static void SetUp(Module module, Grammar grammar)
        {
            if (pattern == null)
            {
                string    expression = "l(statement 0)";
                Pattern[] parameters = { Statement.pattern };

                ParseGraphNode parseGraph = BootstrapParser.Parse(expression, parameters);

                pattern = new ConcretePattern(null, "TopLevelStatement", parseGraph);
                pattern.SetType(typeof(TopLevelStatement));
            }

            module.SetName("TopLevelStatement", typeof(TopLevelStatement));
            grammar.PatternDefined(pattern);
        }
Пример #12
0
        public new static void SetUp(Module module, Grammar grammar)
        {
            if (pattern == null)
            {
                string    expression = "s('pattern' l(body 0))";
                Pattern[] patterns   = { BlockPatternExpression.pattern };

                ParseGraphNode parseGraph = BootstrapParser.Parse(expression, patterns);

                pattern = new ConcretePattern(null, "PatternMember", parseGraph);
                pattern.SetType(typeof(PatternMember));

                Member.pattern.AddAltPattern(pattern);
            }

            module.SetName("PatternMember", typeof(PatternMember));
            grammar.PatternDefined(pattern);
        }
Пример #13
0
        public new static void SetUp(Module module, Grammar grammar)
        {
            if (pattern == null)
            {
                string    expression = "s('{' l(options *(0)) o((dropPrecedence true) l(body 1)) '}')";
                Pattern[] patterns   = { Option.pattern, PatternExpression.pattern };

                ParseGraphNode parseGraph = BootstrapParser.Parse(expression, patterns);

                pattern = new ConcretePattern(null, "BlockPatternExpression", parseGraph);
                pattern.SetType(typeof(BlockPatternExpression));

                PatternExpression.pattern.AddAltPattern(pattern);
            }

            module.SetName("BlockPatternExpression", typeof(BlockPatternExpression));
            grammar.PatternDefined(pattern);
        }
Пример #14
0
        public new static void SetUp(Module module, Grammar grammar)
        {
            if (pattern == null)
            {
                string    expression = "o((rightRecursive true) s('!' l(body 0)))";
                Pattern[] patterns   = { PatternExpression.pattern };

                ParseGraphNode parseGraph = BootstrapParser.Parse(expression, patterns);

                pattern = new ConcretePattern(null, "NotPatternExpression", parseGraph);
                pattern.SetType(typeof(NotPatternExpression));

                PatternExpression.pattern.AddAltPattern(pattern);
            }

            module.SetName("NotPatternExpression", typeof(NotPatternExpression));
            grammar.PatternDefined(pattern);
        }
Пример #15
0
        public new static void SetUp(Module module, Grammar grammar)
        {
            if (pattern == null)
            {
                string    expression = "s('try' l(tryBody 0) 'catch' ?(s('(' l(catchVariable 1) ')')) l(catchBody 0))";
                Pattern[] parameters = { Statement.pattern, Expression.pattern };

                ParseGraphNode parseGraph = BootstrapParser.Parse(expression, parameters);

                pattern = new ConcretePattern(null, "TryStatement", parseGraph);
                pattern.SetType(typeof(TryStatement));

                Statement.pattern.AddAltPattern(pattern);
            }

            module.SetName("TryStatement", typeof(TryStatement));
            grammar.PatternDefined(pattern);
        }
Пример #16
0
        public new static void SetUp(Module module, Grammar grammar)
        {
            if (pattern == null)
            {
                string    expression = "s('new' l(type o((exclude 1) 0)) '(' o((dropPrecedence true) l(parameters ?(s(0 *(s(',' 0)))))) ')')";
                Pattern[] parameters = { Expression.pattern, CallExpression.pattern };

                ParseGraphNode parseGraph = BootstrapParser.Parse(expression, parameters);

                pattern = new ConcretePattern(null, "NewExpression", parseGraph);
                pattern.SetType(typeof(NewExpression));

                Expression.pattern.AddAltPattern(pattern);
            }

            module.SetName("NewExpression", typeof(NewExpression));
            grammar.PatternDefined(pattern);
        }
Пример #17
0
        public new static void SetUp(Module module, Grammar grammar)
        {
            if (pattern == null)
            {
                string    expression = "s('{' l(body *(0)) '}')";
                Pattern[] patterns   = { Statement.pattern };

                ParseGraphNode parseGraph = BootstrapParser.Parse(expression, patterns);

                pattern = new ConcretePattern(null, "CompoundStatement", parseGraph);
                pattern.SetType(typeof(CompoundStatement));

                Statement.pattern.AddAltPattern(pattern);
            }

            module.SetName("CompoundStatement", typeof(CompoundStatement));
            grammar.PatternDefined(pattern);
        }
        public new static void SetUp(Module module, Grammar grammar)
        {
            if (pattern == null)
            {
                string    expression = "l(name a(0 1))";
                Pattern[] patterns   = { MemberExpression.pattern, NameExpression.pattern };

                ParseGraphNode parseGraph = BootstrapParser.Parse(expression, patterns);

                pattern = new ConcretePattern(null, "ReferencePatternExpression", parseGraph);
                pattern.SetType(typeof(ReferencePatternExpression));

                PatternExpression.pattern.AddAltPattern(pattern);
            }

            module.SetName("ReferencePatternExpression", typeof(ReferencePatternExpression));
            grammar.PatternDefined(pattern);
        }
Пример #19
0
        public new static void SetUp(Module module, Grammar grammar)
        {
            if (pattern == null)
            {
                string    expression = "s('field' l(name 0) ';')";
                Pattern[] patterns   = { Name.pattern };

                ParseGraphNode parseGraph = BootstrapParser.Parse(expression, patterns);

                pattern = new ConcretePattern(null, "FieldMember", parseGraph);
                pattern.SetType(typeof(FieldMember));

                Member.pattern.AddAltPattern(pattern);
            }

            module.SetName("FieldMember", typeof(FieldMember));
            grammar.PatternDefined(pattern);
        }
Пример #20
0
        public new static void SetUp(Module module, Grammar grammar)
        {
            if (pattern == null)
            {
                string    expression = "o((leftRecursive true) s(l(a 0) '<' l(b 0)))";
                Pattern[] parameters = { Expression.pattern };

                ParseGraphNode parseGraph = BootstrapParser.Parse(expression, parameters);

                pattern = new ConcretePattern(null, "LessExpression", parseGraph);
                pattern.SetType(typeof(LessExpression));

                ComparisonExpression.pattern.AddAltPattern(pattern);
            }

            module.SetName("LessExpression", typeof(LessExpression));
            grammar.PatternDefined(pattern);
        }
Пример #21
0
        public new static void SetUp(Module module, Grammar grammar)
        {
            if (pattern == null)
            {
                string    expression = "s('class' l(name 0) ?(s(':' l(baseType a(2 3)))) a(';' s('{' l(members *(1)) '}')))";
                Pattern[] patterns   = { Name.pattern, Member.pattern, MemberExpression.pattern, NameExpression.pattern };

                ParseGraphNode parseGraph = BootstrapParser.Parse(expression, patterns);

                pattern = new ConcretePattern(null, "ClassStatement", parseGraph);
                pattern.SetType(typeof(ClassStatement));

                Statement.pattern.AddAltPattern(pattern);
            }

            module.SetName("ClassStatement", typeof(ClassStatement));
            grammar.PatternDefined(pattern);
        }
Пример #22
0
        public new static void SetUp(Module module, Grammar grammar)
        {
            if (pattern == null)
            {
                string    expression = "s('(' o((dropPrecedence true) l(body 0)) ')')";
                Pattern[] parameters = { Expression.pattern };

                ParseGraphNode parseGraph = BootstrapParser.Parse(expression, parameters);

                pattern = new ConcretePattern(null, "ParenExpression", parseGraph);
                pattern.SetType(typeof(ParenExpression));

                Expression.pattern.AddAltPattern(pattern);
            }

            module.SetName("ParenExpression", typeof(ParenExpression));
            grammar.PatternDefined(pattern);
        }
Пример #23
0
        public new static void SetUp(Module module, Grammar grammar)
        {
            if (pattern == null)
            {
                string    expression = "o((recursive false) s(l(label 0) ':' l(body 1)))";
                Pattern[] patterns   = { Name.pattern, PatternExpression.pattern };

                ParseGraphNode parseGraph = BootstrapParser.Parse(expression, patterns);

                pattern = new ConcretePattern(null, "LabelPatternExpression", parseGraph);
                pattern.SetType(typeof(LabelPatternExpression));

                PatternExpression.pattern.AddAltPattern(pattern);
            }

            module.SetName("LabelPatternExpression", typeof(LabelPatternExpression));
            grammar.PatternDefined(pattern);
        }
Пример #24
0
        public new static void SetUp(Module module, Grammar grammar)
        {
            if (pattern == null)
            {
                string    expression = "s('return' l(expression 0) ';')";
                Pattern[] parameters = { Expression.pattern };

                ParseGraphNode parseGraph = BootstrapParser.Parse(expression, parameters);

                pattern = new ConcretePattern(null, "ReturnStatement", parseGraph);
                pattern.SetType(typeof(ReturnStatement));

                Statement.pattern.AddAltPattern(pattern);
            }

            module.SetName("ReturnStatement", typeof(ReturnStatement));
            grammar.PatternDefined(pattern);
        }
Пример #25
0
        public new static void SetUp(Module module, Grammar grammar)
        {
            if (pattern == null)
            {
                string    expression = "s('if' '(' l(condition 0) ')' l(trueBody 1) ?(s('else' l(falseBody 1))))";
                Pattern[] parameters = { Expression.pattern, Statement.pattern };

                ParseGraphNode parseGraph = BootstrapParser.Parse(expression, parameters);

                pattern = new ConcretePattern(null, "IfStatement", parseGraph);
                pattern.SetType(typeof(IfStatement));

                Statement.pattern.AddAltPattern(pattern);
            }

            module.SetName("IfStatement", typeof(IfStatement));
            grammar.PatternDefined(pattern);
        }
Пример #26
0
        public new static void SetUp(Module module, Grammar grammar)
        {
            if (pattern == null)
            {
                string    expression = "s('precedence' l(a a(0 1)) l(relation o((buildTextNodes true) a('<' '=' '>'))) l(b a(0 1)) ';')";
                Pattern[] patterns   = { MemberExpression.pattern, NameExpression.pattern };

                ParseGraphNode parseGraph = BootstrapParser.Parse(expression, patterns);

                pattern = new ConcretePattern(null, "SetPrecedenceStatement", parseGraph);
                pattern.SetType(typeof(SetPrecedenceStatement));

                Statement.pattern.AddAltPattern(pattern);
            }

            module.SetName("SetPrecedenceStatement", typeof(SetPrecedenceStatement));
            grammar.PatternDefined(pattern);
        }
        public new static void SetUp(Module module, Grammar grammar)
        {
            if (pattern == null)
            {
                string    expression = "o((recursive false) l(nodes s(0 +(0))))";
                Pattern[] patterns   = { PatternExpression.pattern };

                ParseGraphNode parseGraph = BootstrapParser.Parse(expression, patterns);

                pattern = new ConcretePattern(null, "SequencePatternExpression", parseGraph);
                pattern.SetType(typeof(SequencePatternExpression));

                PatternExpression.pattern.AddAltPattern(pattern);
            }

            module.SetName("SequencePatternExpression", typeof(SequencePatternExpression));
            grammar.PatternDefined(pattern);
        }
Пример #28
0
        public new static void SetUp(Module module, Grammar grammar)
        {
            if (pattern == null)
            {
                string    expression = "o((leftRecursive true) s(l(body 0) l(reps o((buildTextNodes true) a('?' '*' '+')))))";
                Pattern[] patterns   = { PatternExpression.pattern };

                ParseGraphNode parseGraph = BootstrapParser.Parse(expression, patterns);

                pattern = new ConcretePattern(null, "RepeatPatternExpression", parseGraph);
                pattern.SetType(typeof(RepeatPatternExpression));

                PatternExpression.pattern.AddAltPattern(pattern);
            }

            module.SetName("RepeatPatternExpression", typeof(RepeatPatternExpression));
            grammar.PatternDefined(pattern);
        }
Пример #29
0
        public new static void SetUp(Module module, Grammar grammar)
        {
            if (pattern == null)
            {
                string    expression = "l(name 0)";
                Pattern[] parameters = { Name.pattern };

                ParseGraphNode parseGraph = BootstrapParser.Parse(expression, parameters);

                pattern = new ConcretePattern(null, "NameExpression", parseGraph);
                pattern.SetType(typeof(NameExpression));

                Expression.pattern.AddAltPattern(pattern);
            }

            module.SetName("NameExpression", typeof(NameExpression));
            grammar.PatternDefined(pattern);
        }
Пример #30
0
        public new static void SetUp(Module module, Grammar grammar)
        {
            if (pattern == null)
            {
                string    expression = "o((leftRecursive true) s(l(callable 0) '(' o((dropPrecedence true) l(parameters ?(s(0 *(s(',' 0)))))) ')'))";
                Pattern[] parameters = { Expression.pattern };

                ParseGraphNode parseGraph = BootstrapParser.Parse(expression, parameters);

                pattern = new ConcretePattern(null, "CallExpression", parseGraph);
                pattern.SetType(typeof(CallExpression));

                Expression.pattern.AddAltPattern(pattern);
            }

            module.SetName("CallExpression", typeof(CallExpression));
            grammar.PatternDefined(pattern);
        }