/// <summary> /// Swaps the current token with the new token, or does nothing if the stream is empty. /// </summary> public void Swap(TextUnit updatedText, TokenType updatedType = Token.DefaultTokenType) { if (!this.Done) { this.Tokens[this.Index] = new Token(updatedText, updatedType); } }
/// <summary> /// Rewrites the type to the intermediate C# representation. /// </summary> internal void Rewrite() { if (this.TypeTokens.Count == 0) { return; } var text = this.RewriteTypeTokens(); this.RewrittenTextUnit = new TextUnit(text, this.TypeTokens.First().TextUnit.Line); return; }
/// <summary> /// Rewrites a tuple, recursively. /// </summary> /// <param name="index">Index</param> protected void RewriteTuple(ref int index) { var tupleIdx = index; index++; int tupleSize = 1; bool expectsComma = false; while (index < this.RewrittenStmtTokens.Count && this.RewrittenStmtTokens[index].Type != TokenType.RightParenthesis) { if (this.RewrittenStmtTokens[index].Type == TokenType.WhiteSpace || this.RewrittenStmtTokens[index].Type == TokenType.NewLine) { index++; continue; } if ((!expectsComma && this.RewrittenStmtTokens[index].Type != TokenType.Identifier && this.RewrittenStmtTokens[index].Type != TokenType.This && this.RewrittenStmtTokens[index].Type != TokenType.True && this.RewrittenStmtTokens[index].Type != TokenType.False && this.RewrittenStmtTokens[index].Type != TokenType.Null && this.RewrittenStmtTokens[index].Type != TokenType.LeftParenthesis) || (expectsComma && this.RewrittenStmtTokens[index].Type != TokenType.Comma)) { break; } if (this.RewrittenStmtTokens[index].Type == TokenType.Identifier || this.RewrittenStmtTokens[index].Type == TokenType.This || this.RewrittenStmtTokens[index].Type == TokenType.True || this.RewrittenStmtTokens[index].Type == TokenType.Null || this.RewrittenStmtTokens[index].Type == TokenType.False) { index++; expectsComma = true; } else if (this.RewrittenStmtTokens[index].Type == TokenType.LeftParenthesis) { this.RewriteTuple(ref index); index++; expectsComma = true; } else if (this.RewrittenStmtTokens[index].Type == TokenType.Comma) { tupleSize++; expectsComma = false; index++; } } if (tupleSize > 1) { var tupleStr = "Container.Create("; var textUnit = new TextUnit(tupleStr, this.RewrittenStmtTokens[tupleIdx].TextUnit.Line); this.RewrittenStmtTokens[tupleIdx] = new Token(textUnit); } else { this.RewrittenStmtTokens.RemoveAt(index); this.RewrittenStmtTokens.RemoveAt(tupleIdx); } }
/// <summary> /// Rewrites the cloneable collection in method call. /// </summary> protected void RewriteCloneableCollectionInMethodCall() { if (this.Parent.Machine == null || this.Parent.State == null || !(this.Parent.Machine.FunctionDeclarations.Any(val => val.Identifier.TextUnit.Text. Equals(this.RewrittenStmtTokens[this.Index].TextUnit.Text)))) { return; } this.Index++; this.SkipWhiteSpaceTokens(); int counter = 0; while (this.Index < this.RewrittenStmtTokens.Count) { if (this.RewrittenStmtTokens[this.Index].Type == TokenType.LeftParenthesis) { counter++; } else if (this.RewrittenStmtTokens[this.Index].Type == TokenType.RightParenthesis) { counter--; } if (counter == 0) { break; } if (this.Parent.Machine.FieldDeclarations.Any(val => val.Identifier.TextUnit.Text.Equals( this.RewrittenStmtTokens[this.Index].TextUnit.Text))) { var field = this.Parent.Machine.FieldDeclarations.Find(val => val.Identifier.TextUnit.Text.Equals(this.RewrittenStmtTokens[this.Index]. TextUnit.Text)) as PFieldDeclaration; if (field.Type.Type != PType.Tuple && field.Type.Type != PType.Seq && field.Type.Type != PType.Map) { return; } var textUnit = new TextUnit("(", this.RewrittenStmtTokens[this.Index].TextUnit.Line); this.RewrittenStmtTokens.Insert(this.Index, new Token(textUnit)); this.Index++; this.RewriteMemberIdentifier(); var cloneStr = ".Clone() as " + field.Type.GetRewrittenText() + ")"; textUnit = new TextUnit(cloneStr, this.RewrittenStmtTokens[this.Index].TextUnit.Line); this.RewrittenStmtTokens.Insert(this.Index + 1, new Token(textUnit)); } this.Index++; } }
/// <summary> /// Returns a clone of the text unit. /// </summary> /// <param name="textUnit">TextUnit</param> /// <returns>TextUnit</returns> public static TextUnit Clone(TextUnit textUnit) { return(new TextUnit(textUnit.Text, textUnit.Line)); }
/// <summary> /// Rewrites the cloneable payload. /// </summary> private void RewriteCloneablePayload() { if (this.Parent.Machine == null || !(this.Parent.Machine.FieldDeclarations.Any(val => val.Identifier.TextUnit.Text. Equals(this.RewrittenStmtTokens[this.Index].TextUnit.Text)))) { return; } var field = this.Parent.Machine.FieldDeclarations.Find(val => val.Identifier.TextUnit.Text. Equals(this.RewrittenStmtTokens[this.Index].TextUnit.Text)) as PFieldDeclaration; if (field.Type.Type != PType.Tuple && field.Type.Type != PType.Seq && field.Type.Type != PType.Map) { return; } var textUnit = new TextUnit("(", this.RewrittenStmtTokens[this.Index].TextUnit.Line); this.RewrittenStmtTokens.Insert(this.Index, new Token(textUnit)); this.Index++; var cloneStr = ".Clone() as " + field.Type.GetRewrittenText() + ")"; textUnit = new TextUnit(cloneStr, this.RewrittenStmtTokens[this.Index].TextUnit.Line); if (this.Index + 1 == this.RewrittenStmtTokens.Count) { this.RewrittenStmtTokens.Add(new Token(textUnit)); } else { this.RewrittenStmtTokens.Insert(this.Index + 1, new Token(textUnit)); } }
/// <summary> /// Returns a clone of the text unit. /// </summary> /// <param name="textUnit">TextUnit</param> /// <returns>TextUnit</returns> public static TextUnit Clone(TextUnit textUnit) { return new TextUnit(textUnit.Text, textUnit.Line); }
/// <summary> /// Concatenates the two text units, which are assumed to be adjacent. /// </summary> public static TextUnit Add(TextUnit first, TextUnit second) { // We call SkipWhiteSpaceAndCommentTokens() between some tokens so the first may not end right at the second. Debug.Assert(first.Start + first.OriginalLength <= second.Start, "TextUnits overlap"); return(Create(first.Text + second.Text, first.OriginalLength + second.OriginalLength, first.Line, first.Start)); }
/// <summary> /// Initializes a new instance of the <see cref="TextUnit"/> class. /// </summary> public TextUnit(string text, TextUnit other) : this(text, other.Line, other.Start) { }
/// <summary> /// Rewrites the machine type. /// </summary> protected void RewriteMachineType() { var textUnit = new TextUnit("MachineId", this.RewrittenStmtTokens[this.Index].TextUnit.Line); this.RewrittenStmtTokens[this.Index] = new Token(textUnit, this.RewrittenStmtTokens[this.Index].Type); }