Example #1
0
        public void BehaviorTest()
        {
            //changing the root after having children
            Regex      r  = new Regex(@"(((\w+)\s+)*)function\s+(\w+)");
            Selection  s  = new Selection("public static function findObjId");
            RSelection s2 = s.Match(r);

            s2.Replace(new Dictionary <string, string>()
            {
                { "4", "methodName" },
                { "3", "unstatic" },
            });
            s.Sel(0, 6).Replace("unpublic");//changing root
            s2.Replace(new Dictionary <string, string>()
            {
                { "3", "ee" },   //these pointers are now inaccurate+unpredictable because children are 'unlinked' from root
                { "4", "oo" },
            });
            Assert.AreEqual("public ee function oome", s.Value);//this result is unpredictable. This is what it produces atm (behavior)

            //this is what you should do instead:
            s  = new Selection("public static function findObjId");
            s2 = s.Match(r);
            s2.Replace(new Dictionary <string, string>()
            {
                { "4", "methodName" },
                { "3", "unstatic" },
            });
            s.Sel(0, 6).Replace("unpublic"); //after changing root...
            s.Commit();                      //reset all pointers
            s2 = s.Match(r);                 //evaluate the pointers again
            s2.Replace(new Dictionary <string, string>()
            {
                { "3", "ee" },
                { "4", "oo" },
            });
            Assert.AreEqual("unpublic ee function oo", s.Value);//this is what we would have expected

            //or like this:
            s = new Selection("public static function findObjId");
            s.Match(r).Replace(new Dictionary <string, string>()
            {
                { "4", "methodName" },
                { "3", "unstatic" },
            });
            s.Sel(0, 6).Replace("unpublic");
            s.Match(r).Replace(new Dictionary <string, string>() //NB: pointers are being re-evaluated here
            {
                { "3", "ee" },
                { "4", "oo" },
            });
            Assert.AreEqual("unpublic ee function oo", s.Value);
        }
Example #2
0
        public void TranslateBlock(Selection sel)
        {
            var matches = sel.Matches(rChoice);

            //foreach (RSelection rsel in sel.Matches(rChoice))
            //{
            //    //rsel.Replace(new Dictionary<string, ReplaceDelegate>()
            //    //{
            //    //    {"choice", (Selection choice) =>
            //    //        {
            //    //            Debug(choice);
            //    //            return choice.Value;
            //    //        }}
            //    //});
            //    rsel.Replace(TranslateChoice(rsel.Group("choice")));
            //    rsel.Replace("b(" + rsel.Value + ")");
            //}
            for (int i = 0; i < matches.Count; i++)
            {
                RSelection rsel = matches[i];
                Debug(rsel);

                rsel.Replace(TranslateChoice(rsel.Group("choice")) /*+((i<matches.Count-1)?",":"")*/);
                //rsel.Replace("b(" + rsel.Value + ")");
                //rsel.Commit();
                //rsel.Matches("\\|").ForEach(x => x.Replace(""));
                //rsel.Replace(rsel.Value.Replace("|", ""));
            }
            sel.Replace("c(" + sel.Value + ")");
        }
Example #3
0
        private void TranslateExpr(Selection input)
        {
            string value = input.Value;

            //ltrim
            input.Match("^" + WS, RegexOptions.Multiline).Replace("");
            //rtrim
            input.Match(WS + "$", RegexOptions.Multiline).Replace("");

            //xml direct accessing -> indexer accessing
            foreach (RSelection xmlAccSel in input.Matches(b(
                                                               @"xml([A-Z]\w+|)", n("parts", r(b(WS, e("."), WS, IDENTIFIER), true))
                                                               )))
            {
                xmlAccSel.Replace(new Dictionary <string, ReplaceDelegate>()
                {
                    { "parts", (Selection parts) => {
                          foreach (RSelection part in parts.Matches(b(WS, e("."), WS, n("identifier", IDENTIFIER))))
                          {
                              if (part.Group("identifier").Value.Trim() != "hasOwnProperty")
                              {
                                  part.Replace("[\"${identifier}\"]");
                              }
                          }
                          return(parts.Value);
                      } }
                });
            }

            //attribute operator
            foreach (RSelection attrSel in input.Matches(b(
                                                             e("."), e("@"), n("identifier", IDENTIFIER)
                                                             )))
            {
                attrSel.Replace(".attr(\"${identifier}\")");
            }

            //casting to parsing
            RSelection castSel     = input.Match(b("^" + n("type", IDENTIFIER), n("rest", e("(") + ANY)) + "$");
            string     castSelType = castSel.Group("type").Value;

            if (!string.IsNullOrWhiteSpace(castSelType))
            {
                castSel.Replace("${rest}.to" + char.ToUpper(castSelType[0]) + castSelType.Substring(1) + "()");
            }
        }