Ejemplo n.º 1
0
        private RunResult GetResult(string pattern, string matchable)
        {
            var loader = new PegLoader(new[] { "." });
            var loaded = loader.Parse(pattern);
            var runner = PatternCompiler.Default.Compile(new Pattern()
            {
                Data = loaded
            });
            var result = runner.Run(matchable);

            return(result);
        }
Ejemplo n.º 2
0
 public void BasicLoading()
 {
     var loader = new PegLoader(new[] { "." });
     var result = loader.Parse("Test::A");
 }
Ejemplo n.º 3
0
        static int Main(string[] args)
        {
            var commandLineApplication = new CommandLineApplication(throwOnUnexpectedArg: false);

            var pattern = commandLineApplication.Argument("pattern", "The PEG to match.");

            var files = commandLineApplication.Argument("files", "Files to match.", multipleValues: true);

            var outputJson = commandLineApplication.Option(
                "-j |--output-json",
                "Output matches as structured JSON data. Captures can be used to split data into separate JSON fields. Like this: {name: expression}",
                CommandOptionType.NoValue);

            var captureGroup = commandLineApplication.Option(
                "-c | --capture-group",
                "Prints the capture group instead of the entire match.",
                CommandOptionType.SingleValue
                );

            var libraryDirs = commandLineApplication.Option("-l |--libraries", "Additional PEG library paths.", CommandOptionType.MultipleValue);

            commandLineApplication.HelpOption("-? | -h | --help");

            commandLineApplication.OnExecute(() =>
            {
                if (pattern?.Value == null)
                {
                    Console.WriteLine("Missing pattern.");
                    return(-1);
                }

                try
                {
                    var captureKeyAllocator = outputJson.HasValue() || captureGroup.HasValue() ? new CaptureKeyAllocator() : null;

                    var loader     = new PegLoader(CreateLibraryPaths(libraryDirs), captureKeyAllocator);
                    var inputFiles = LoadInputAsync(files.Values).ToArray();

                    // TODO: Check whether the interpreter is faster for parsing the initial pattern
                    var expression = loader.Parse(pattern.Value);
                    var runner     = BuildRunnerFromExpression(PatternCompiler.Default, expression);

                    var settings = new JsonSerializerSettings()
                    {
                        Formatting = Formatting.Indented,
                    };

                    foreach (var(fileName, data) in inputFiles)
                    {
                        var text = data.GetAwaiter().GetResult();

                        var captures = new List <Capture>();
                        if (runner.Run(text.Data, 0, text.Length, captures).IsSuccessful)
                        {
                            if (outputJson.HasValue())
                            {
                                var conv = new CapturesToNodesConverter(new string(text.Data), captureKeyAllocator, captures);
                                var json = new NodeToJsonConverter(conv.ToNodes(), settings);
                                Console.WriteLine(json.ToJson());
                            }
                            else if (captureGroup.HasValue())
                            {
                                var key = captureKeyAllocator[captureGroup.Value()];
                                foreach (var capture in captures)
                                {
                                    if (capture.Key == key)
                                    {
                                        Console.WriteLine($"{fileName}: {new string(text.Data, capture.StartPosition, capture.EndPosition - capture.StartPosition)}");
                                    }
                                }
                            }
                            else
                            {
                                foreach (var capture in captures)
                                {
                                    Console.WriteLine($"{fileName}: {new string(text.Data, capture.StartPosition, capture.EndPosition - capture.StartPosition)}");
                                }
                            }
                        }
                    }
                }
                catch (CompilationException e)
                {
                    Console.WriteLine(e.Message);
                    return(1);
                }
                catch (PegParsingException e)
                {
                    Console.WriteLine(e.Message);
                    return(2);
                }

                return(0);
            });

            return(commandLineApplication.Execute(args));
        }