Esempio n. 1
0
            public override void Visit(Nonterminal expression, GrammarFactory <T> context)
            {
                // Captured nonterminals are given an index of -1 when created.
                if (expression.Index == -1)
                {
                    expression.Index = context.nonterminalIndex++;
                    context.grammar.Nonterminals.Add(expression);

                    // For these we still want to propagate the walk since there could be more nested content
                    base.Visit(expression, context);
                }
                else
                {
                    // We don't propagate the walk since we don't want to follow into any other nonterminals
                }
            }
Esempio n. 2
0
        public static T Create()
        {
            ForeignGrammarFactory <T> factory = new ForeignGrammarFactory <T>();
            T result = proxyGenerator.CreateClassProxy <T>(factory);

            int nonterminalIndex = 0;

            // Now initialize the grammar
            foreach (MethodInfo method in typeof(T).GetMethods())
            {
                if (method.ReturnType == typeof(Expression) && method.GetParameters().Length == 0 && !method.IsSpecialName)
                {
                    ForeignNonterminal rule = new ForeignNonterminal();
                    rule.Name = method.Name;
                    factory.rules[rule.Name] = rule;
                }
            }

            T tokenizerGrammar = GrammarFactory <T> .Create();

            result.SetTokenizer(tokenizerGrammar, tokenizerGrammar.Tokenizer);

            return(result);
        }
Esempio n. 3
0
        public static T Create(object[] args)
        {
            GrammarFactory <T> factory = new GrammarFactory <T>();
            T result = Proxy.CreateProxy <T>(factory.InvocationHandler);

            factory.grammar = result;
            result.NotifyCreated(args);

            // Now initialize the grammar
            foreach (MethodInfo method in typeof(T).GetMethods())
            {
                if (method.ReturnType == typeof(Expression) && method.GetParameters().Length == 0 && !method.IsSpecialName)
                {
                    Nonterminal rule = new Nonterminal();
                    rule.Name  = method.Name;
                    rule.Index = factory.nonterminalIndex++;
                    result.Nonterminals.Add(rule);

//                    if (method.HasAttribute<StartRuleAttribute>())
//                        result.StartRule = rule;

                    factory.rules[method.Name] = rule;
                }
            }
            foreach (MethodInfo method in typeof(T).GetMethods())
            {
                if (method.ReturnType == typeof(Expression) && method.GetParameters().Length == 0 && !method.IsSpecialName)
                {
                    method.Invoke(result, null);
                }
            }
            foreach (MethodInfo method in typeof(T).GetMethods())
            {
                if (method.ReturnType == typeof(Expression) && method.GetParameters().Length == 0 && !method.IsSpecialName)
                {
                    Nonterminal rule = factory.rules[method.Name];
                    if (method.HasAttribute <TokenizerAttribute>())
                    {
                        result.Tokenizer = rule;
                    }
                    TokenAttribute tokenAttribute = method.GetAttribute <TokenAttribute>();
                    if (tokenAttribute != null)
                    {
                        if (!tokenAttribute.TokenizeChildren)
                        {
                            rule.IsToken = true;
                            if (tokenAttribute.IsOmitted)
                            {
                                rule.IsTokenOmitted = true;
                            }
                        }
                        else
                        {
                            OrderedChoice children = (OrderedChoice)rule.Expression;
                            foreach (var child in children.Expressions)
                            {
                                Nonterminal childRule = (Nonterminal)child;
                                childRule.IsToken = true;
                            }
                        }
                    }
                }
            }

            FieldInfo tokenizerGrammarField = typeof(T).GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance).Where(o => typeof(Grammar).IsAssignableFrom(o.FieldType) && o.HasAttribute <TokenizerAttribute>()).FirstOrDefault();

            if (tokenizerGrammarField != null)
            {
                Grammar tokenizerGrammar = (Grammar)tokenizerGrammarField.GetValue(result);
                if (tokenizerGrammar.Tokenizer != null)
                {
                    result.SetTokenizer(tokenizerGrammar, tokenizerGrammar.Tokenizer);
                }
            }

            return(result);
        }