Пример #1
0
        private void CollectProductions()
        {
            allProductions = new Dictionary <string, FieldInfo>();

            symbols     = new Dictionary <string, FieldInfo>();
            terminals   = new Dictionary <string, FieldInfo>();
            whitespaces = new Dictionary <string, FieldInfo>();
            comments    = new Dictionary <string, FieldInfo>();
            rules       = new Dictionary <string, FieldInfo>();

            ruleDependencies = new Dictionary <string, HashSet <string> >();

            // First path, collect symbols etc.
            foreach (var f in grammarType.GetFields(BindingFlags.Public | BindingFlags.Static))
            {
                if (f.FieldType == typeof(Expression <Grammar.Symbol>))
                {
                    symbols.Add(f.Name, f);
                    allProductions.Add(f.Name, f);
                }
                else if (f.FieldType == typeof(Expression <Grammar.Terminal>))
                {
                    terminals.Add(f.Name, f);
                    allProductions.Add(f.Name, f);
                }
                else if (f.FieldType == typeof(Expression <Grammar.Whitespace>))
                {
                    whitespaces.Add(f.Name, f);
                    allProductions.Add(f.Name, f);
                }
                else if (f.FieldType == typeof(Expression <Grammar.Comment>))
                {
                    comments.Add(f.Name, f);
                    allProductions.Add(f.Name, f);
                }
                else if (f.FieldType == typeof(Expression <Grammar.Rule>))
                {
                    rules.Add(f.Name, f);
                    allProductions.Add(f.Name, f);
                    ruleDependencies.Add(f.Name, new HashSet <string>());
                }
            }

            // Second pass, collect rule dependencies
            foreach (var f in rules.Values)
            {
                var vis = new RuleVisitor();
                vis.Visit((Expression)f.GetValue(null));

                foreach (var dr in vis.Rules)
                {
                    if (rules.ContainsKey(dr.Name) && !ruleDependencies[dr.Name].Contains(f.Name) && f.Name != dr.Name)
                    {
                        ruleDependencies[dr.Name].Add(f.Name);
                    }
                }
            }
        }
Пример #2
0
        private void CollectProductions()
        {
            allProductions = new Dictionary<string, FieldInfo>();

            symbols = new Dictionary<string, FieldInfo>();
            terminals = new Dictionary<string, FieldInfo>();
            whitespaces = new Dictionary<string, FieldInfo>();
            comments = new Dictionary<string, FieldInfo>();
            rules = new Dictionary<string, FieldInfo>();

            ruleDependencies = new Dictionary<string, HashSet<string>>();

            // First path, collect symbols etc.
            foreach (var f in grammarType.GetFields(BindingFlags.Public | BindingFlags.Static))
            {
                if (f.FieldType == typeof(Expression<Grammar.Symbol>))
                {
                    symbols.Add(f.Name, f);
                    allProductions.Add(f.Name, f);
                }
                else if (f.FieldType == typeof(Expression<Grammar.Terminal>))
                {
                    terminals.Add(f.Name, f);
                    allProductions.Add(f.Name, f);
                }
                else if (f.FieldType == typeof(Expression<Grammar.Whitespace>))
                {
                    whitespaces.Add(f.Name, f);
                    allProductions.Add(f.Name, f);
                }
                else if (f.FieldType == typeof(Expression<Grammar.Comment>))
                {
                    comments.Add(f.Name, f);
                    allProductions.Add(f.Name, f);
                }
                else if (f.FieldType == typeof(Expression<Grammar.Rule>))
                {
                    rules.Add(f.Name, f);
                    allProductions.Add(f.Name, f);
                    ruleDependencies.Add(f.Name, new HashSet<string>());
                }
            }

            // Second pass, collect rule dependencies
            foreach (var f in rules.Values)
            {
                var vis = new RuleVisitor();
                vis.Visit((Expression)f.GetValue(null));

                foreach (var dr in vis.Rules)
                {
                    if (rules.ContainsKey(dr.Name) && !ruleDependencies[dr.Name].Contains(f.Name) && f.Name != dr.Name)
                    {
                        ruleDependencies[dr.Name].Add(f.Name);
                    }
                }
            }
        }