Example #1
0
    public static void Test(Argument args)
    {
        test_args = args;
        args.WriteLine("------------------------------------");
        args.WriteLine("  gzjs -- Transform Test");
        args.WriteLine("------------------------------------");
        Rgx::Match m = Rgx::Regex.Match(@"(i<144)", @"\b\d+\b|\b[\w\d]{2,}\b");

        if (m.Success)
        {
            args.WriteLine("OK: {0}", m.Value);
        }
        else
        {
            args.WriteLine("NG: reg_gzjs_directive");
        }

        Assert(Rgx::Regex.Match(@"else     return", @"\b +\b").Value, "     ");
        Assert(Rgx::Regex.Match(@"else     return", @"\b +\B").Value, "    ");
        Assert(Rgx::Regex.Match(@"else     return", @"\B +\b").Value, "    ");
        Assert(Rgx::Regex.Match(@"else     return", @"\B +\B").Value, "   ");
        {
            Rgx::Regex reg = new Rgx::Regex("^" + _.rex_braces + "$", Rgx::RegexOptions.Multiline);
            Assert(reg.Match(@" work(); ").Success, true);
            Assert(reg.Match(@" {work({a:b});} var c={0:1} ").Success, true);
            Assert(reg.Match(@" {work(a:b});} var c={} ").Success, false);
        }
        {
            Rgx::Regex reg = new Rgx::Regex(@"^\{" + _.rex_braces + @"\}$", Rgx::RegexOptions.Multiline);
            Assert(reg.Match(@"{ {work({a:b});} var c={0:1} }").Success, true);
            Assert(reg.Match(@"{ {work(a:b});} var c={} }").Success, false);
        }
    }
Example #2
0
        public static void TestRegexMatch1(afh.Application.Log log)
        {
            using (log.Lock()){
                StringRegex.RegLan reglan = new StringRegex.RegLan("こんにちは|さようなら");
                log.WriteLine("Regular Expression: {0}", reglan);
                foreach (StringRegex.Capture c in reglan.Matches("こんにちは さようなら こんにちは こんにちは さようなら"))
                {
                    log.WriteLine("Match! : {0}", c.Value);
                }

                reglan = new StringRegex.RegLan(@"(?<l>\d+)(?<o>(?:\+|\-))(?<r>\d+)");
                log.WriteLine("Regular Expression: {0}", reglan);
                foreach (StringRegex.Capture c in reglan.Matches("3+2+4+5 1+2+3 2+3 4+7 123+321"))
                {
                    int  l = int.Parse(c.Groups["l"].Last);
                    int  r = int.Parse(c.Groups["r"].Last);
                    bool o = c.Groups["o"].Last.Value == "+";
                    log.WriteLine("{0} を計算すると→: {1}", c, o?l + r:l - r);
                }

                log.WriteLine("== System.Text.RegularExpressions の場合 ==");
                Rgx::Regex rgx = new Rgx::Regex(@"(?<l>\d+)(?<o>(?:\+|\-))(?<r>\d+)");
                log.WriteLine("Regular Expression: {0}", rgx.ToString());
                foreach (Rgx::Match m in rgx.Matches("3+2+4+5 1+2+3 2+3 4+7 123+321"))
                {
                    int  l = int.Parse(m.Groups["l"].Value);
                    int  r = int.Parse(m.Groups["r"].Value);
                    bool o = m.Groups["o"].Value == "+";
                    log.WriteLine("{0} を計算すると→: {1}", m.Value, o?l + r:l - r);
                }
            }
        }
Example #3
0
    public static void Main(string[] args)
    {
        System.Console.WriteLine("--------------------------------------------------");
        System.Console.WriteLine("  Regex Replace         copyright 2009, kch.murase");
        System.Console.WriteLine("--------------------------------------------------");
        Argument cmdline = new Argument(args);

        if (cmdline.InputFile == null)
        {
            System.Console.WriteLine("! 有効な入力ファイルが指定されていないので終了します。");
            return;
        }
        if (cmdline.OutputFile == null)
        {
            System.Console.WriteLine("! 有効な出力ファイルが指定されていないので終了します。");
            return;
        }
        if (cmdline.Before == null || cmdline.After == null)
        {
            System.Console.WriteLine("! 置換に関する情報が不完全です。");
            return;
        }

        Rgx::Regex reg;

        try{
            reg = new Rgx::Regex(cmdline.Before, Rgx::RegexOptions.Multiline);
        }catch {
            System.Console.WriteLine("! 正規表現に誤りが含まれている可能性があります。");
            System.Console.WriteLine("!> 指定された正規表現 == {0}", cmdline.Before);
            return;
        }
        System.Console.WriteLine("file '{0}' に対する処理を実行します。", cmdline.InputFile);
        string content = System.IO.File.ReadAllText(cmdline.InputFile, System.Text.Encoding.UTF8);

        content = reg.Replace(content, delegate(Rgx::Match m){
            return(cmdline.GetProcessedAfter(m));
        });

        string outfile = cmdline.OutputFile;

        System.IO.File.WriteAllText(outfile, content, System.Text.Encoding.UTF8);
        System.Console.WriteLine("処理結果が無事に '" + outfile + "' に出力されました");
    }
Example #4
0
    private static void WriteCompatibleCss(Setting s, string content, string renderer)
    {
        renderer = renderer.ToLower();

        Rgx::Regex reg_renderer = new Rgx::Regex(
            "\\b" + renderer + "\\b",
            Rgx::RegexOptions.IgnoreCase | Rgx::RegexOptions.Compiled
            );
        string compat_content = reg_compat.Replace(content, delegate(Rgx::Match m){
            //if(m.Groups["renderer"].Value.ToLower()!=renderer)return "";
            if (!reg_renderer.IsMatch(m.Groups["renderer"].Value))
            {
                return(m.Groups["else"].Value ?? "");
            }
            return(m.Groups["body"].Value);
        });

        string outputfile = s.InputFileWithoutExtension + extensions[renderer];

        System.IO.File.WriteAllText(outputfile, compat_content, System.Text.Encoding.UTF8);
        System.Console.WriteLine(". '{0}' に出力をしました。", outputfile);
    }
Example #5
0
    private static void WriteCompatibleCss(Setting s,string content,string renderer)
    {
        renderer=renderer.ToLower();

        Rgx::Regex reg_renderer=new Rgx::Regex(
            "\\b"+renderer+"\\b",
            Rgx::RegexOptions.IgnoreCase|Rgx::RegexOptions.Compiled
        );
        string compat_content=reg_compat.Replace(content,delegate(Rgx::Match m){
            //if(m.Groups["renderer"].Value.ToLower()!=renderer)return "";
            if(!reg_renderer.IsMatch(m.Groups["renderer"].Value))return m.Groups["else"].Value??"";
            return m.Groups["body"].Value;
        });

        string outputfile=s.InputFileWithoutExtension+extensions[renderer];
        System.IO.File.WriteAllText(outputfile,compat_content,System.Text.Encoding.UTF8);
        System.Console.WriteLine(". '{0}' に出力をしました。",outputfile);
    }
Example #6
0
  public static void Test(Argument args){
    test_args=args;
    args.WriteLine("------------------------------------");
    args.WriteLine("  gzjs -- Transform Test");
    args.WriteLine("------------------------------------");
    Rgx::Match m=Rgx::Regex.Match(@"(i<144)",@"\b\d+\b|\b[\w\d]{2,}\b");
    if(m.Success){
      args.WriteLine("OK: {0}",m.Value);
    }else{
      args.WriteLine("NG: reg_gzjs_directive");
    }

    Assert(Rgx::Regex.Match(@"else     return",@"\b +\b").Value,"     ");
    Assert(Rgx::Regex.Match(@"else     return",@"\b +\B").Value,"    ");
    Assert(Rgx::Regex.Match(@"else     return",@"\B +\b").Value,"    ");
    Assert(Rgx::Regex.Match(@"else     return",@"\B +\B").Value,"   ");
    {
      Rgx::Regex reg=new Rgx::Regex("^"+_.rex_braces+"$",Rgx::RegexOptions.Multiline);
      Assert(reg.Match(@" work(); ").Success,true);
      Assert(reg.Match(@" {work({a:b});} var c={0:1} ").Success,true);
      Assert(reg.Match(@" {work(a:b});} var c={} ").Success,false);
    }
    {
      Rgx::Regex reg=new Rgx::Regex(@"^\{"+_.rex_braces+@"\}$",Rgx::RegexOptions.Multiline);
      Assert(reg.Match(@"{ {work({a:b});} var c={0:1} }").Success,true);
      Assert(reg.Match(@"{ {work(a:b});} var c={} }").Success,false);
    }
  }
Example #7
0
  public static string ProcessSource(string input,Argument _args){
    string output=input;
    Argument args=_args;
    string dirname=System.IO.Path.GetDirectoryName(args.OutputFile);

    // directives の読み取り
    Gen::Dictionary<string,string> tokenmap=new Gen::Dictionary<string,string>();
    Gen::List<ReplaceData> replaces=new Gen::List<ReplaceData>();

    output=reg_gzjs_directive.Replace(output,delegate(Rgx::Match m){
      // 先頭以外に改行が入っている物は無効
      if(0<m.Value.IndexOfAny("\r\n".ToCharArray()))return m.Value;
      
      switch(m.Groups["dir"].Value){
      case "outfile":
        args.OutputFile=System.IO.Path.Combine(
          System.IO.Path.GetDirectoryName(args.OutputFile),
          ReadArg(m.Groups["arg1"])
        );
        if(args.Verbose)
          args.WriteLine("#> output-file was set to '"+args.OutputFile+"'");
        return "\n";
      case "tokenmap":{
        string before=ReadArg(m.Groups["arg1"]);
        string after=ReadArg(m.Groups["arg2"]);
        tokenmap[before]=after;
        if(args.Verbose)
          args.WriteLine("#> token-mapping registered: "+before+" -> "+after+"");
        return "\n";
      }
      case "replace":{
        string before=ReadArg(m.Groups["arg1"]);
        string after=ReadArg(m.Groups["arg2"]);
        replaces.Add(new ReplaceData(before,after));
        if(args.Verbose)
          args.WriteLine("#> replace registered: "+before+" -> "+after+"");
        return "\n";
      }
      case "include":{
        if(args.Verbose)
          args.WriteLine("#gzjs-include");
        return Program.Include(ReadArg(m.Groups["arg1"]),args)??m.Value;
      }
      case "option":{
        string op=ReadArg(m.Groups["arg1"]);
        if(!args.ReadOption(op)){
          args.WriteLine("#gzjs-option > '{0}' は認識できない option です",op);
          return m.Value;
        }
        return "\n";
      }
      default:
        return m.Value;
      }
    });

    // コメント空白類の削除
    if(args.CutComment)
      output=RegExp.CutComment(output);

    // token 置き換え
    if(tokenmap.Count>0){
      string rex=null;
      foreach(string token in tokenmap.Keys){
        if(rex==null)rex=token;else rex+="|"+token;
      }
      rex=@"\b(?:"+rex+@")\b";
      output=new Rgx::Regex(rex).Replace(output,delegate(Rgx::Match m){
        return tokenmap[m.Value];
      });
    }

    // #gzjs-replace 実行
    foreach(ReplaceData r in replaces)
      output=r.Replace(output);

    return output;
  }
Example #8
0
 public ReplaceData(string before,string after){
   this.reg=new Rgx::Regex(before,Rgx::RegexOptions.Compiled|Rgx::RegexOptions.Multiline);
   this.rep=after;
 }
Example #9
0
 public static void benchRegexCompileClr1()
 {
     Rgx::Regex rgx = new Rgx::Regex(@"(?<l>\d+)(?<o>(?:\+|\-))(?<r>\d+)");
 }
Example #10
0
    public static string ProcessSource(string input, Argument _args)
    {
        string   output = input;
        Argument args   = _args;

        // directives の読み取り
        Gen::Dictionary <string, string> tokenmap = new Gen::Dictionary <string, string>();
        Gen::List <ReplaceData>          replaces = new Gen::List <ReplaceData>();

        output = reg_gzjs_directive.Replace(output, delegate(Rgx::Match m) {
            // 先頭以外に改行が入っている物は無効
            if (0 < m.Value.IndexOfAny("\r\n".ToCharArray()))
            {
                return(m.Value);
            }

            switch (m.Groups["dir"].Value)
            {
            case "outfile": {
                string dirname  = System.IO.Path.GetDirectoryName(args.OutputFile);
                args.OutputFile = System.IO.Path.Combine(dirname, ReadArg(m.Groups["arg1"]));
                if (args.Verbose)
                {
                    args.WriteLine("#> output-file was set to '" + args.OutputFile + "'");
                }
                return("\n");
            }

            case "tokenmap": {
                string before    = ReadArg(m.Groups["arg1"]);
                string after     = ReadArg(m.Groups["arg2"]);
                tokenmap[before] = after;
                if (args.Verbose)
                {
                    args.WriteLine("#> token-mapping registered: " + before + " -> " + after + "");
                }
                return("\n");
            }

            case "replace": {
                string before = ReadArg(m.Groups["arg1"]);
                string after  = ReadArg(m.Groups["arg2"]);
                replaces.Add(new ReplaceData(before, after));
                if (args.Verbose)
                {
                    args.WriteLine("#> replace registered: " + before + " -> " + after + "");
                }
                return("\n");
            }

            case "include": {
                if (args.Verbose)
                {
                    args.WriteLine("#gzjs-include");
                }
                return(Program.Include(ReadArg(m.Groups["arg1"]), args) ?? m.Value);
            }

            case "option": {
                string op = ReadArg(m.Groups["arg1"]);
                if (!args.ReadOption(op))
                {
                    args.WriteLine("#gzjs-option > '{0}' は認識できない option です", op);
                    return(m.Value);
                }
                return("\n");
            }

            default:
                return(m.Value);
            }
        });

        // コメント空白類の削除
        if (args.CutComment)
        {
            output = RegExp.CutComment(output);
        }

        // token 置き換え
        if (tokenmap.Count > 0)
        {
            string rex = null;
            foreach (string token in tokenmap.Keys)
            {
                if (rex == null)
                {
                    rex = token;
                }
                else
                {
                    rex += "|" + token;
                }
            }
            rex    = @"\b(?:" + rex + @")\b";
            output = new Rgx::Regex(rex).Replace(output, delegate(Rgx::Match m) {
                return(tokenmap[m.Value]);
            });
        }

        // #gzjs-replace 実行
        foreach (ReplaceData r in replaces)
        {
            output = r.Replace(output);
        }

        return(output);
    }
Example #11
0
 public ReplaceData(string before, string after)
 {
     this.reg = new Rgx::Regex(before, Rgx::RegexOptions.Compiled | Rgx::RegexOptions.Multiline);
     this.rep = after;
 }
Example #12
0
    public static void Main(string[] args)
    {
        System.Console.WriteLine("--------------------------------------------------");
        System.Console.WriteLine("  Regex Replace         copyright 2009, kch.murase");
        System.Console.WriteLine("--------------------------------------------------");
        Argument cmdline=new Argument(args);
        if(cmdline.InputFile==null){
            System.Console.WriteLine("! 有効な入力ファイルが指定されていないので終了します。");
            return;
        }
        if(cmdline.OutputFile==null){
            System.Console.WriteLine("! 有効な出力ファイルが指定されていないので終了します。");
            return;
        }
        if(cmdline.Before==null||cmdline.After==null){
            System.Console.WriteLine("! 置換に関する情報が不完全です。");
            return;
        }

        Rgx::Regex reg;
        try{
            reg=new Rgx::Regex(cmdline.Before,Rgx::RegexOptions.Multiline);
        }catch{
            System.Console.WriteLine("! 正規表現に誤りが含まれている可能性があります。");
            System.Console.WriteLine("!> 指定された正規表現 == {0}",cmdline.Before);
            return;
        }
        System.Console.WriteLine("file '{0}' に対する処理を実行します。",cmdline.InputFile);
        string content=System.IO.File.ReadAllText(cmdline.InputFile,System.Text.Encoding.UTF8);
        content=reg.Replace(content,delegate(Rgx::Match m){
            return cmdline.GetProcessedAfter(m);
        });

        string outfile=cmdline.OutputFile;
        System.IO.File.WriteAllText(outfile,content,System.Text.Encoding.UTF8);
        System.Console.WriteLine("処理結果が無事に '"+outfile+"' に出力されました");
    }