Beispiel #1
0
        private void Execute(GeneratorExecutionContext context, AdditionalText file)
        {
            CFG grammar = CFGParser.Parse(file.GetText().ToString());

            grammar = grammar.ApplyOptions(GetOptions(context, file)).ToGreibachNormalForm();
            CFGSubtypingAPIGenerator generator = new CFGSubtypingAPIGenerator(grammar, Path.GetFileNameWithoutExtension(file.Path),
                                                                              GetBoolConfiguration("FluentAPI", context, file));

            context.AddSource(generator.NamespaceName(), SourceText.From(generator.PrintAPI(), Encoding.UTF8));
        }
        public static void Main(String[] args)
        {
            var options = new SomeOptions();

            Parser.Default.ParseArguments <SomeOptions>(args).WithParsed(parsed => options = parsed);
            if (options.Input == null || options.Name == null)
            {
                return;
            }
            CFG grammar = null;

            try
            {
                grammar = CFGParser.Parse(File.ReadAllText(options.Input));
            } catch (Exception e)
            {
                Console.WriteLine($"Could not parse file {options.Input}:\n{e.Message}");
                return;
            }
            try
            {
                if (options.Fluent)
                {
                    grammar = grammar.Reversed();
                }
            } catch (Exception e)
            {
                Console.WriteLine($"Error when reversing grammar:\n{e.Message}");
                return;
            }
            try
            {
                grammar = grammar.ToGreibachNormalForm();
            } catch (Exception e)
            {
                Console.WriteLine($"Error when converting grammar to Greibach normal form:\n{e.Message}");
                return;
            }
            string result = null;

            try
            {
                result = new CFGSubtypingAPIGenerator(grammar, options.Name, options.Fluent).PrintAPI();
            }
            catch (Exception e)
            {
                Console.WriteLine($"Error when generating API:\n{e.Message}");
                return;
            }
            try
            {
                if (options.Output == null)
                {
                    Console.WriteLine(result);
                }
                else
                {
                    File.WriteAllText(options.Output, result);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine($"Error when writing output:\n{e.Message}");
                return;
            }
        }