Exemplo n.º 1
0
 public GrammarToken(TraceryGrammar grammar, int start, GrammarToken parent)
 {
     Raw     = "";
     Grammar = grammar;
     Start   = start;
     Parent  = parent;
 }
Exemplo n.º 2
0
 private void AddCharToToken(char ch, ref string output, GrammarToken token, bool escaped = false)
 {
     if (token == null)
     {
         output += ch;
         return;
     }
     token.FindLowestOpenToken().AddChar(ch, escaped);
 }
Exemplo n.º 3
0
        public void AddChild(GrammarToken child)
        {
            var open = FindLowestOpenToken();

            if (open.Children == null)
            {
                open.Children = new List <GrammarToken>();
            }
            open.Children.Add(child);
            child.Parent = open;
        }
Exemplo n.º 4
0
        public void ReplaceInnerText(GrammarToken innerToken)
        {
            // Update the Resolved text, replacing inner token Raw with Resolved.
            var start = Resolved.IndexOf(innerToken.Raw);

            if (start < 0 || start >= Resolved.Length)
            {
                return;
            }
            Resolved = Resolved.Substring(0, start) + innerToken.Resolved + Resolved.Substring(start + innerToken.Raw.Length);

            // Update the positions of any IndecesOfInterest relative to replaced text.
            if (innerToken.Raw.Length == innerToken.Resolved.Length)
            {
                return;
            }
            var difference = innerToken.Raw.Length - innerToken.Resolved.Length;

            if (IndecesOfInterest == null || IndecesOfInterest.Count == 0)
            {
                return;
            }
            for (var i = 0; i < IndecesOfInterest.Count; i++)
            {
                if (start <= IndecesOfInterest[i])
                {
                    IndecesOfInterest[i] -= difference;
                    if (IndecesOfInterest[i] < 0)
                    {
                        IndecesOfInterest.RemoveAt(i);
                        i--;
                    }
                }
                else
                {
                    continue;
                }
            }
        }
Exemplo n.º 5
0
        internal string ParseInner(string input)
        {
            if (String.IsNullOrEmpty(input))
            {
                return(input);
            }
            var output  = "";
            var escaped = false;

            GrammarToken token = null;

            for (var i = 0; i < input.Length; i++)
            {
                var ch = input[i];
                if (escaped)
                {
                    AddCharToToken(ch, ref output, token, true);
                    escaped = false;
                    continue;
                }
                if (ch == '\\')
                {
                    AddCharToToken(ch, ref output, token, true);
                    escaped = true;
                    continue;
                }

                switch (ch)
                {
                case '[':
                    // Start new action token.
                    var newToken = new ActionToken(this, i + 1, token);
                    if (token == null)
                    {
                        token = newToken;
                    }
                    else
                    {
                        token.AddChild(newToken);
                    }
                    newToken.AddChar(ch);
                    break;

                case ']':
                    // Close highest action token. If any inner tokens are unfinished, they resolve to their raw text.
                    var action = token == null ? null : token.FindLowestOpenOfType(TagType.Action);
                    if (action == null)
                    {
                        // No open action. Add ] to text as normal.
                        AddCharToToken(ch, ref output, token);
                        break;
                    }
                    else
                    {
                        action.AddChar(ch);
                    }
                    action.Resolve();
                    break;

                case '#':
                    // If lowest open node is a tag, close it. Otherwise, open a new tag.
                    if (token == null)
                    {
                        token = new TagToken(this, i + 1, null);
                        token.AddChar(ch);
                        break;
                    }
                    var lowest = token.FindLowestOpenToken();
                    if (lowest.Type == TagType.Tag)
                    {
                        lowest.AddChar(ch);
                        lowest.Resolve();
                        break;
                    }
                    var newTag = new TagToken(this, i + 1, lowest);
                    lowest.AddChild(newTag);
                    newTag.AddChar(ch);
                    break;

                default:
                    AddCharToToken(ch, ref output, token);
                    break;
                }

                if (token != null && token.IsResolved)
                {
                    output += token.Resolved;
                    token   = null;
                }
            }

            return(output);
        }
Exemplo n.º 6
0
 public ActionToken(TraceryGrammar grammar, int start, GrammarToken parent) : base(grammar, start, parent)
 {
     Type = TagType.Action;
 }
Exemplo n.º 7
0
 public TagToken(TraceryGrammar grammar, int start, GrammarToken parent) : base(grammar, start, parent)
 {
     Type = TagType.Tag;
 }