Example #1
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 #2
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 #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) + "()");
            }
        }
Example #4
0
        private string TranslateChoice(Selection selection)
        {
            Debug(selection);

            bool          addSep = false;
            StringBuilder sb     = new StringBuilder();

            sb.Append("b(");

            var parts = selection.Matches(rPart);

            if (parts[parts.Count - 1].Value.Trim() == "|")
            {
                addSep = true;
                parts.RemoveAt(parts.Count - 1);
            }
            for (int i = 0; i < parts.Count; i++)
            {
                RSelection part = parts[i];
                Debug(part);
                string val = part.Value;

                if (val.StartsWith("\"") && val.EndsWith("\""))
                {
                    //string
                    sb.Append("e(\"");
                    sb.Append(Grammar.e1(val.Substring(1, val.Length - 2)));
                    sb.Append("\")");
                }
                else if (val.StartsWith("[") && val.EndsWith("]"))
                {
                    //optional
                    sb.Append("o(");
                    TranslateBlock(part.Sel(1, val.Length - 2));
                    sb.Append(part.Value.Substring(1, part.Value.Length - 2));
                    //sb.Append(TranslateChoice(part.Sel(1,val.Length-2)));
                    sb.Append(")");
                }
                else if (val.StartsWith("{") && val.EndsWith("}"))
                {
                    //repeated
                    sb.Append("r(");
                    TranslateBlock(part.Sel(1, val.Length - 2));
                    sb.Append(part.Value.Substring(1, part.Value.Length - 2));
                    //sb.Append(TranslateChoice(part.Sel(1, val.Length - 2)));
                    sb.Append(")");
                }
                else
                {
                    //assuming identifier
                    sb.Append(val);
                }

                if (i < parts.Count - 1)
                {
                    sb.Append(",");
                }
            }
            sb.Append(")");
            if (addSep)
            {
                sb.Append(",");
            }
            return(sb.ToString());//todo
        }