/// <inheritdoc /> public bool Equals(TokenCollection other) { if (other == null) { return(false); } if (this.Count != other.Count) { return(false); } for (int i = 0; i < other.Count; i++) { if (this[i] != other[i]) { return(false); } } return(true); }
/// <summary> /// Splits a collection into substrings that are based on the kind in the separator array. /// </summary> /// <param name="coll">The <see cref="TokenCollection"/>.</param> /// <param name="start">The zero-based <see cref="TokenCollection"/> index at which the range starts.</param> /// <param name="end">The zero-based <see cref="TokenCollection"/> index at which the range ends.</param> /// <param name="kinds">A kind array.</param> /// <returns></returns> public static TokenCollection[] Split(this TokenCollection coll, int start, int end, params TokenKind[] kinds) { //var kinds = new TokenKind[] { TokenKind.Arithmetic, TokenKind.Colon, TokenKind.Comma, TokenKind.Dot, TokenKind.Logic, TokenKind.Punctuation }; List <TokenCollection> tcs = new List <TokenCollection>(); //start = GetValidIndex(start); //end = GetValidIndex(end); if (end > coll.Count) { end = coll.Count; } int x = start, y = 0; var pos = new Stack <TokenKind>(); for (int i = start; i < end; i++) { y = i; if (coll[i].TokenKind == TokenKind.LeftParentheses || coll[i].TokenKind == TokenKind.LeftBracket || coll[i].TokenKind == TokenKind.LeftBrace) { pos.Push(coll[i].TokenKind); if (pos.Count == 1 && (IsInKinds(coll[i].TokenKind, kinds))) { if (x != y) { tcs.Add(coll[x, y]); x = y; } continue; } } if (coll[i].TokenKind == TokenKind.RightParentheses || coll[i].TokenKind == TokenKind.RightBrace || coll[i].TokenKind == TokenKind.RightBracket ) { if (pos.Count > 0 && ( (pos.Peek() == TokenKind.LeftParentheses && coll[i].TokenKind == TokenKind.RightParentheses) || (pos.Peek() == TokenKind.LeftBrace && coll[i].TokenKind == TokenKind.RightBrace) || (pos.Peek() == TokenKind.LeftBracket && coll[i].TokenKind == TokenKind.RightBracket) )) { pos.Pop(); if (pos.Count == 0 && (IsInKinds(coll[i].TokenKind, kinds))) { if (x != y) { tcs.Add(coll[x, ++y]); x = y; } continue; } } else { throw new ParseException($"syntax error near {coll[i].ToString()} on {coll.ToString()}", coll[i].BeginLine, coll[i].BeginColumn); } } if (pos.Count == 0 && IsInKinds(coll[i].TokenKind, kinds)) { if (y > x) { tcs.Add(coll[x, y]); } x = i + 1; var child = new TokenCollection(); child.Add(coll[i]); tcs.Add(child); } if (i == end - 1 && y >= x) { tcs.Add(coll[x, y + 1]); x = i + 1; } } return(tcs.ToArray()); }
/// <summary> /// Splits a collection into substrings that are based on the kind in the separator array. /// </summary> /// <param name="start">The zero-based <see cref="TokenCollection"/> index at which the range starts.</param> /// <param name="end">The zero-based <see cref="TokenCollection"/> index at which the range ends.</param> /// <param name="kinds">A kind array.</param> /// <returns></returns> public TokenCollection[] Split(int start, int end, params TokenKind[] kinds) { List <TokenCollection> tc = new List <TokenCollection>(); start = GetValidIndex(start); end = GetValidIndex(end); if (end > this.Count) { end = this.Count; } int x = start, y = 0; var pos = new Stack <TokenKind>(); for (int i = start; i < end; i++) { y = i; if (this[i].TokenKind == TokenKind.LeftParentheses || this[i].TokenKind == TokenKind.LeftBracket || this[i].TokenKind == TokenKind.LeftBrace) { pos.Push(this[i].TokenKind); } if (this[i].TokenKind == TokenKind.RightParentheses || this[i].TokenKind == TokenKind.RightBrace || this[i].TokenKind == TokenKind.RightBracket ) { if (pos.Count > 0 && ( (pos.Peek() == TokenKind.LeftParentheses && this[i].TokenKind == TokenKind.RightParentheses) || (pos.Peek() == TokenKind.LeftBrace && this[i].TokenKind == TokenKind.RightBrace) || (pos.Peek() == TokenKind.LeftBracket && this[i].TokenKind == TokenKind.RightBracket) )) { pos.Pop(); } else { throw new Exception.ParseException(string.Concat("syntax error near ", this[i].TokenKind.ToString(), this), this[i].BeginLine, this[i].BeginColumn); } } if ((pos.Count == 0 && IsInKinds(this[i].TokenKind, kinds)) || (pos.Count == 1 && pos.Peek() == TokenKind.LeftBracket && this[i].TokenKind == TokenKind.LeftBracket)) { if (y > x) { tc.Add(this[x, y]); } if (this[i].TokenKind == TokenKind.LeftBracket) { x = i; continue; } else { x = i + 1; TokenCollection coll = new TokenCollection(); coll.Add(this[i]); tc.Add(coll); } } if (i == end - 1 && y >= x) { //if (x == 0 && y == i) //{ // throw new Exception.ParseException(string.Concat("Unexpected tag:", this), this[0].BeginLine, this[0].BeginColumn); //} tc.Add(this[x, y + 1]); x = i + 1; } } return(tc.ToArray()); }
/// <summary> /// Splits a collection into substrings that are based on the kind in the separator array. /// </summary> /// <param name="coll">The <see cref="TokenCollection"/>.</param> /// <param name="kinds">A kind array.</param> /// <returns></returns> public static TokenCollection[] Split(this TokenCollection coll, params TokenKind[] kinds) { return(Split(coll, 0, coll.Count, kinds)); }