Advertisement
agmike

Getting TrainzScript sheat

Apr 3rd, 2014
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.49 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Text.RegularExpressions;
  7.  
  8.  
  9. namespace DefaultGSNamesCrawler
  10. {
  11.     static class Util
  12.     {
  13.         public static void ForEach(this MatchCollection matches, Action<Match> action)
  14.         {
  15.             for (int i = 0; i < matches.Count; i++) {
  16.                 action(matches[i]);
  17.             }
  18.         }
  19.     }
  20.     class Program
  21.     {
  22.         static void Main(string[] args)
  23.         {
  24.             var path = args[0];
  25.             var files = Directory.GetFiles(path, "*.gs");
  26.            
  27.             var classes = new HashSet<string>();
  28.             var methods = new HashSet<string>();
  29.             var constants = new HashSet<string>();
  30.             var classRegexp = new Regex(@"^\s*(\w+\s+)*class\s+(\w+)\b",
  31.                                         RegexOptions.Multiline | RegexOptions.Compiled);
  32.             var methodRegexp = new Regex(@"^\s*((?:\w+\s+)*)(\w+)\s+(\w+)\s*\(",
  33.                                         RegexOptions.Multiline | RegexOptions.Compiled);
  34.             var constantRegexp = new Regex(@"^\s*((?:\w+\s+)*define\s+(?:\w+\s+)*)(\w+)\s+(\w+)\s*=",
  35.                                         RegexOptions.Multiline | RegexOptions.Compiled);
  36.             foreach (var file in files) {
  37.                 var contents = File.ReadAllText(file);
  38.  
  39.                 classRegexp.Matches(contents).ForEach(m => classes.Add(m.Groups[2].Value));
  40.                 methodRegexp.Matches(contents).ForEach(m => {
  41.                                                            if (m.Groups[1].Value.Contains("public"))
  42.                                                                methods.Add(m.Groups[3].Value);
  43.                                                        });
  44.                 constantRegexp.Matches(contents).ForEach(m => {
  45.                     if (m.Groups[1].Value.Contains("public"))
  46.                         constants.Add(m.Groups[3].Value);
  47.                 });
  48.             }
  49.  
  50.             var output = new StringBuilder();
  51.             output.AppendFormat(@"\b({0})\b", String.Join("|", classes.OrderBy(s => s).ToArray()));
  52.             output.AppendLine();
  53.             output.AppendFormat(@"\b({0})\b", String.Join("|", methods.OrderBy(s => s).ToArray()));
  54.             output.AppendLine();
  55.             output.AppendFormat(@"\b({0})\b", String.Join("|", constants.OrderBy(s => s).ToArray()));
  56.             File.WriteAllText("output.txt", output.ToString());
  57.         }
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement