/// <summary> /// Adds tokens to this node. The first item in the sequence should be identical to the current token of this node (if there is one) /// </summary> /// <param name="seq"></param> internal void AddTokens(IEnumerable<Token> seq, Token[] original) { // first addition var item = seq.FirstOrDefault(); if (item == null) { return; } #if DEBUG // if we've already got a token it should be the same as this being added if (this.Token != null && !this.Token.Equals(item)) { throw new ArgumentException(); } #endif this.Token = item; if (children.Count == 0) { TokenTreeNode kid = new TokenTreeNode(); kid.AddTokens(seq.Skip(1), original); if (kid.Token != null) { children.Add(kid); } else { // this is a terminal for this particular sequence this.fullSequence = original; } } else { TokenTreeNode match = this.FindMatch(seq.Skip(1).FirstOrDefault()); if (match == null) { // no matching child TokenTreeNode t = new TokenTreeNode(); t.AddTokens(seq.Skip(1), original); if (t.Token != null) { this.children.Add(t); } else { // this is a terminal for this particular sequence this.fullSequence = original; } } else { match.AddTokens(seq.Skip(1), original); } } }
internal void AddTokens(Token[] seq) { if (seq.Length < 1) { throw new ArgumentException(); } if (root == null) { this.root = new TokenTreeNode(); TokenTreeNode t = new TokenTreeNode(); t.AddTokens(seq, seq); root.AddChild(t); } else { TokenTreeNode match = this.root.FindMatch(seq[0]); if (match == null) { // no matching child TokenTreeNode t = new TokenTreeNode(); t.AddTokens(seq, seq); if (t.Token != null) { root.AddChild(t); } } else { match.AddTokens(seq, seq); } } count++; }