Inheritance: IToken, IID
Exemple #1
0
 protected virtual void OnTokenMatch(Token token)
 {
     if (this.TokenMatch != null)
     {
         this.TokenMatch(this, token);
     }
 }
Exemple #2
0
 public AnchorTag(Match match, Token parentToken)
     : base(match)
 {
     this.parentToken = parentToken;
 }        
        public virtual void Place(StringBuilder sb, Token token)
        {
            int offset = 0;

            if (this.Pseudo == null || this.Pseudo.Length == 0)
            {
                this.HandleMatch(this.Regex.Match(sb.ToString()), token, sb, 0, 0, ref offset);
            }
            else
            {
                MatchCollection matches = this.Regex.Matches(sb.ToString());
                int count = matches.Count;

                for (int i = 0; i < count; i++)
                {
                    if (this.HandleMatch(matches[i], token, sb, i, count, ref offset))
                    {
                        break;
                    }
                }
            }
        }
        protected virtual bool HandleMatch(Match match, Token token, StringBuilder sb, int index, int count, ref int offset)
        {
            Group group;

            if (this.GroupName != null && this.GroupName.Length > 0)
            {
                group = match.Groups[this.GroupName];
            }
            else
            {
                group = match.Groups[this.GroupIndex];
            }

            PseudoMatch pseudoMatch = this.IsPseudoMatch(group, token, index, count);

            if (!pseudoMatch.Match)
            {
                return pseudoMatch.CancelSearch;
            }

            int pos;

            switch (this.Position)
            {
                case SelectorPosition.After:
                    pos = group.Index + group.Length;                    
                    break;
                case SelectorPosition.Before:
                    pos = group.Index;
                    break;
                case SelectorPosition.Replace:
                    pos = group.Index;
                    sb.Remove(pos+offset, group.Length);                    
                    break;
                default:
                    throw new IndexOutOfRangeException();
            }

            sb.Insert(pos+offset, token.Output);

            if (this.Position == SelectorPosition.Replace)
            {
                offset -= group.Length;
            }
            
            offset += token.Output != null ? token.Output.Length : 0;

            return pseudoMatch.CancelSearch;
        }
        public virtual List<string> Matches(string text, Token token)
        {
            List<string> list = new List<string>();
            MatchCollection matches = this.Regex.Matches(text);
            int count = matches.Count;

            for (int i = 0; i < count; i++)
            {
                Group group;

                if (this.GroupName != null && this.GroupName.Length > 0)
                {
                    group = matches[i].Groups[this.GroupName];
                }
                else
                {
                    group = matches[i].Groups[this.GroupIndex];
                }

                if (this.Pseudo == null || this.Pseudo.Length == 0)
                {
                    list.Add(group.Value);
                    break;
                }

                PseudoMatch pseudoMatch = this.IsPseudoMatch(group, token, i, count);

                if (pseudoMatch.Match)
                {
                    list.Add(group.Value);
                }

                if (pseudoMatch.CancelSearch)
                {
                    break;
                }
            }

            return list;
        }
        protected virtual PseudoMatch IsPseudoMatch(Group group, Token token, int index, int count)
        {
            if (this.Pseudo == null || this.Pseudo.Length == 0)
            {
                return new PseudoMatch(true, true);
            }

            switch (this.Pseudo)
            {
                case "all":
                    return new PseudoMatch(true, false);
                case "first":
                    return new PseudoMatch(index == 0, index == 0);
                case "last":
                    return new PseudoMatch(index == (count - 1), index == (count - 1));
                case "odd":
                    return new PseudoMatch((index & 1) != 0, false);
                case "even":
                    return new PseudoMatch((index & 1) == 0, false);
                case "nth":
                    int nth = int.Parse(this.PseudoArg);
                    return new PseudoMatch(((index + 1) % nth == 0), false);
                case "eq":
                    int eq = int.Parse(this.PseudoArg);
                    return new PseudoMatch(index == eq, index == eq);
                case "gt":
                    int gt = int.Parse(this.PseudoArg);
                    return new PseudoMatch(index > gt, false);
                case "lt":
                    int lt = int.Parse(this.PseudoArg);
                    return new PseudoMatch(index < lt, index >= lt);                
            }

            return new PseudoMatch(false, true);
        }
 public TokenNotUniqueException(Token token)
 {
     this.token = token;
 }