예제 #1
0
파일: Program.cs 프로젝트: mpkorstanje/berp
        private static void GenerateParserInternal(Options options)
        {
            Console.WriteLine(options.GetHeader());
            Console.WriteLine();
            Console.WriteLine("Generating parser for grammar '{0}' using template '{1}'.", Path.GetFileName(options.Grammar), Path.GetFileName(options.Template));

            Console.WriteLine("Loading grammar...");
            var grammarDefinition = File.ReadAllText(options.Grammar);
            var parser            = new BerpGrammar.Parser();
            var ruleSet           = parser.Parse(new TokenScanner(new StringReader(grammarDefinition)));

            if (!string.IsNullOrWhiteSpace(options.SettingsOverride))
            {
                var overrides = options.SettingsOverride.Split(',');
                foreach (var settingSpec in overrides)
                {
                    var parts        = settingSpec.Split('=');
                    var settingValue = parts[1].Trim();
                    if (settingValue.StartsWith("!"))
                    {
                        settingValue = File.ReadAllText(settingValue.TrimStart('!'));
                    }
                    ruleSet.Settings[parts[0].Trim()] = settingValue;
                }
            }

            int tokenCount = ruleSet.Count(r => r is TokenRule);

            Console.WriteLine("The grammar was loaded with {0} tokens and {1} rules.", tokenCount, ruleSet.Count() - tokenCount);

            if (options.DiagnosticsMode)
            {
                Console.WriteLine(ruleSet.ToString(true));
                Console.WriteLine("---------------");
            }

            Console.WriteLine("Calculating parser states...");
            var states = StateCalculator.CalculateStates(ruleSet);

            Console.WriteLine("{0} states calculated for the parser.", states.Count);

            if (options.DiagnosticsMode)
            {
                foreach (var state in states.Values)
                {
                    PrintStateTransitions(state);
                    PrintStateBranches(state.Branches, state.Id);
                }
            }

            Console.WriteLine("Generating parser class...");
            var generator = new Generator(ruleSet.Settings);

            generator.Generate(options.Template, ruleSet, states, options.OutputFile);
        }
예제 #2
0
파일: Program.cs 프로젝트: eric-leihe/berp
        private static void GenerateParserInternal(Options options)
        {
            Console.WriteLine(options.GetHeader());
            Console.WriteLine();
            Console.WriteLine("Generating parser for grammar '{0}' using template '{1}'.", Path.GetFileName(options.Grammar), Path.GetFileName(options.Template));

            Console.WriteLine("Loading grammar...");
            var grammarDefinition = File.ReadAllText(options.Grammar);
            var parser            = new BerpGrammar.Parser();
            var ruleSet           = parser.Parse(new TokenScanner(new StringReader(grammarDefinition)));

            int tokenCount = ruleSet.Count(r => r is TokenRule);

            Console.WriteLine("The grammar was loaded with {0} tokens and {1} rules.", tokenCount, ruleSet.Count() - tokenCount);

            if (options.DiagnosticsMode)
            {
                Console.WriteLine(ruleSet.ToString(true));
                Console.WriteLine("---------------");
            }

            Console.WriteLine("Calculating parser states...");
            var states = StateCalculator.CalculateStates(ruleSet);

            Console.WriteLine("{0} states calculated for the parser.", states.Count);

            if (options.DiagnosticsMode)
            {
                foreach (var state in states.Values)
                {
                    PrintStateTransitions(state);
                    PrintStateBranches(state.Branches, state.Id);
                }
            }

            Console.WriteLine("Generating parser class...");
            var generator = new Generator(ruleSet.GetSetting("Namespace", "ParserGen"), ruleSet.GetSetting("ClassName", "Parser"), ruleSet.GetSetting("TargetNamespace", (string)null), ruleSet.GetSetting("TargetClassName", "Ast"));

            generator.Generate(options.Template, ruleSet, states, options.OutputFile);
        }