Esempio n. 1
0
        public void Run(bool grep_only)
        {
            GenerateAndCompileTemplates();

            CreateAllTemplateInstances();

            foreach (var full_pass_name in _piggy._application.OrderedPasses)
            {
                // Separate the pass name into "template name" "." "pass name"
                var pass_name_regex = new Regex("^(?<template_name>[^.]+)[.](?<pass_name>.*)$");
                var match           = pass_name_regex.Match(full_pass_name);
                if (!match.Success)
                {
                    throw new Exception("template.pass " + full_pass_name + " does not exist.");
                }
                var template_name = match.Groups["template_name"].Value;
                var pass_name     = match.Groups["pass_name"].Value;
                var template      = _piggy._templates.Find(t => t.TemplateName == template_name);
                var passes        = GetAllPassesNamed(template, pass_name);
                var regex         = new TreeRegEx(_piggy, passes, _instances[template.Type]);
                regex.Match();
                if (grep_only)
                {
                    OutputMatches(regex);
                }
                else
                {
                    PatternMatchingEngine(regex);
                }
            }
        }
Esempio n. 2
0
 public void OutputMatches(TreeRegEx re)
 {
     foreach (var x in re._top_level_matches)
     {
         var a = x;
         Console.WriteLine(TreeRegEx.GetText(a));
     }
 }
Esempio n. 3
0
        public void PatternMatchingEngine(TreeRegEx re)
        {
            // Step though all top level matches, then the path for each.
            foreach (var zz in re._top_level_matches)
            {
                var a = zz; // tree
                if (Piggy._debug_information)
                {
                    Console.Error.WriteLine("------");
                    Console.Error.WriteLine(a.GetText());
                }

                foreach (var path in re._matches_path_start[a])
                {
                    var pe = path.GetEnumerator();
                    pe.MoveNext();
                    for (;;)
                    {
                        Path cpe = pe.Current;
                        if (Piggy._debug_information)
                        {
                            Console.Error.WriteLine(cpe.LastEdge + " " + cpe.InputText.Truncate(40));
                        }
                        if (0 != (cpe.LastEdge.EdgeModifiers & (int)Edge.EdgeModifiersEnum.Text))
                        {
                            var x = cpe.LastEdge.Input;
                            {
                                var s  = x;
                                var s2 = s.Substring(2);
                                var s3 = s2.Substring(0, s2.Length - 2);
                                Console.Write(s3);
                            }
                        }
                        else if (cpe.LastEdge.IsCode)
                        {
                            // move back to find a terminal.
                            var        find = cpe;
                            IParseTree con  = null;
                            for (;;)
                            {
                                if (find == null)
                                {
                                    break;
                                }
                                if (find.LastEdge.IsEmpty || find.LastEdge.IsCode || find.LastEdge.IsText)
                                {
                                    find = find.Next;
                                    continue;
                                }
                                con = find.Input;
                                if (con != null)
                                {
                                    break;
                                }
                                find = find.Next;
                            }

                            while (con != null)
                            {
                                if (con as AstParserParser.NodeContext != null)
                                {
                                    break;
                                }
                                con = re._ast.Parents()[con];
                            }

                            var x = cpe.LastEdge.AstList;
                            if (x.Count() > 1)
                            {
                                throw new Exception("Cannot execute multiple code blocks.");
                            }
                            var      main     = _piggy._code_blocks[x.First()];
                            var      type     = re._current_type;
                            var      instance = re._instance;
                            object[] aa       = { new Tree(re._ast.Parents(), re._ast, con, re._common_token_stream) };
                            var      res      = main.Invoke(instance, aa);
                        }

                        if (!pe.MoveNext())
                        {
                            break;
                        }
                    }
                }
            }
        }