/// <summary> /// Parses the list of expressions. /// </summary> /// <param name="expressions"> /// The list of expressions. /// </param> /// <param name="parentExpression"> /// The parent expression, if there is one. /// </param> /// <param name="parentElement"> /// The element that contains the expressions. /// </param> /// <param name="parentClass"> /// The class that the element belongs to. /// </param> /// <param name="members"> /// The collection of members of the parent class. /// </param> private void CheckClassMemberRulesForExpressions( IEnumerable <Expression> expressions, Expression parentExpression, CsElement parentElement, ClassBase parentClass, Dictionary <string, List <CsElement> > members) { Param.AssertNotNull(expressions, "expressions"); Param.AssertNotNull(parentElement, "parentElement"); Param.Ignore(parentExpression); Param.Ignore(parentClass); Param.Ignore(members); // Loop through each of the expressions in the list. foreach (Expression expression in expressions) { // If the expression is a variable declarator expression, we don't want to match against the identifier tokens. if (expression.ExpressionType == ExpressionType.VariableDeclarator) { VariableDeclaratorExpression declarator = expression as VariableDeclaratorExpression; if (declarator.Initializer != null) { this.CheckClassMemberRulesForExpression(declarator.Initializer, parentExpression, parentElement, parentClass, members); } } else { this.CheckClassMemberRulesForExpression(expression, parentExpression, parentElement, parentClass, members); } } }
/// <summary> /// Parses and returns the declarators for a property (C#6). /// </summary> /// <param name="propertyReference">A reference to the field.</param> /// <param name="unsafeCode">Indicates whether the code is marked as unsafe.</param> /// <param name="propertyType">The field type.</param> /// <param name="identifierTokenNode">The identifier token node (Should be the property name).</param> /// <returns> /// Returns the declarators. /// </returns> private IList<VariableDeclaratorExpression> ParsePropertyDeclarators(Reference<ICodePart> propertyReference, bool unsafeCode, TypeToken propertyType, Node<CsToken> identifierTokenNode) { Param.AssertNotNull(propertyReference, nameof(propertyReference)); Param.Ignore(unsafeCode); Param.AssertNotNull(propertyType, nameof(propertyType)); Param.AssertNotNull(identifierTokenNode, nameof(propertyType)); List<VariableDeclaratorExpression> declarators = new List<VariableDeclaratorExpression>(); Symbol symbol = this.GetNextSymbol(propertyReference); Reference<ICodePart> expressionReference = new Reference<ICodePart>(); Expression initialization = null; while (symbol.SymbolType != SymbolType.Semicolon) { symbol = this.GetNextSymbol(expressionReference); if (symbol.SymbolType == SymbolType.Equals) { this.tokens.Add(this.GetOperatorToken(OperatorType.Equals, expressionReference)); initialization = this.GetNextExpression(ExpressionPrecedence.None, expressionReference, unsafeCode); if (initialization == null) { throw this.CreateSyntaxException(); } } VariableDeclaratorExpression variableDeclarationExpression = new VariableDeclaratorExpression( new CsTokenList(this.tokens, identifierTokenNode, this.tokens.Last), new LiteralExpression(this.tokens, identifierTokenNode), initialization); expressionReference.Target = variableDeclarationExpression; declarators.Add(variableDeclarationExpression); // Get next symbol without change index. symbol = this.symbols.Peek(1); } // Return the declarators as a read-only collection. return declarators.ToArray(); }
/// <summary> /// Parses and returns the declarators for a field. /// </summary> /// <param name="fieldReference"> /// A reference to the field. /// </param> /// <param name="unsafeCode"> /// Indicates whether the code is marked as unsafe. /// </param> /// <param name="fieldType"> /// The field type. /// </param> /// <returns> /// Returns the declarators. /// </returns> private IList<VariableDeclaratorExpression> ParseFieldDeclarators(Reference<ICodePart> fieldReference, bool unsafeCode, TypeToken fieldType) { Param.AssertNotNull(fieldReference, "fieldReference"); Param.Ignore(unsafeCode); Param.AssertNotNull(fieldType, "fieldType"); List<VariableDeclaratorExpression> declarators = new List<VariableDeclaratorExpression>(); Symbol symbol = this.GetNextSymbol(fieldReference); while (symbol.SymbolType != SymbolType.Semicolon) { Reference<ICodePart> expressionReference = new Reference<ICodePart>(); // Get the identifier. CsToken identifier = this.GetElementNameToken(expressionReference, unsafeCode, true); Node<CsToken> identifierTokenNode = this.tokens.InsertLast(identifier); Expression initialization = null; // Check whether there is an equals sign. symbol = this.GetNextSymbol(expressionReference); if (symbol.SymbolType == SymbolType.Equals) { this.tokens.Add(this.GetOperatorToken(OperatorType.Equals, expressionReference)); // Get the expression after the equals sign. If the expression starts with an // opening curly bracket, then this is an initialization expression or an // anonymous type initialization expression. symbol = this.GetNextSymbol(expressionReference); if (symbol.SymbolType == SymbolType.OpenCurlyBracket) { // Determine whether this is an array or an anonymous type. if (fieldType.Text == "var" || (fieldType.Text != "Array" && fieldType.Text != "System.Array" && !fieldType.Text.Contains("["))) { initialization = this.GetAnonymousTypeInitializerExpression(expressionReference, unsafeCode); } else { initialization = this.GetArrayInitializerExpression(unsafeCode); } } else { initialization = this.GetNextExpression(ExpressionPrecedence.None, expressionReference, unsafeCode); } if (initialization == null) { throw this.CreateSyntaxException(); } } VariableDeclaratorExpression variableDeclarationExpression = new VariableDeclaratorExpression( new CsTokenList(this.tokens, identifierTokenNode, this.tokens.Last), new LiteralExpression(this.tokens, identifierTokenNode), initialization); expressionReference.Target = variableDeclarationExpression; declarators.Add(variableDeclarationExpression); // If the next symbol is a comma, continue. symbol = this.GetNextSymbol(fieldReference); if (symbol.SymbolType == SymbolType.Comma) { this.tokens.Add(this.GetToken(CsTokenType.Comma, SymbolType.Comma, fieldReference)); symbol = this.GetNextSymbol(fieldReference); } } // Return the declarators as a read-only collection. return declarators.ToArray(); }
/// <summary> /// The save static field declarator expression into constructor. /// </summary> /// <param name="variableDeclaratorExpression"> /// The variable declarator expression. /// </param> private void SaveStaticFieldDeclaratorExpressionIntoConstructor( VariableDeclaratorExpression variableDeclaratorExpression) { this.Save(variableDeclaratorExpression.ParentVariable.Type, this.cppWriter, SavingOptions.None); this.cppWriter.Write(" "); this.cppWriter.Write(this.currentClassNamespace); this.cppWriter.Write("::"); this.cppWriter.Write(variableDeclaratorExpression.Identifier); this.SaveSuffix( variableDeclaratorExpression.ParentVariable.Type, variableDeclaratorExpression.Initializer, this.cppWriter); if (variableDeclaratorExpression.Initializer != null) { this.cppWriter.Write(" = "); @switch(variableDeclaratorExpression.Initializer); // this.destCPP.WriteLine(";"); } }
/// <summary> /// The save field declarator expression into constructor. /// </summary> /// <param name="variableDeclaratorExpression"> /// The variable declarator expression. /// </param> /// <param name="first"> /// The first. /// </param> /// <returns> /// The save field declarator expression into constructor. /// </returns> private bool SaveFieldDeclaratorExpressionIntoConstructor( VariableDeclaratorExpression variableDeclaratorExpression, bool first) { if (variableDeclaratorExpression.Initializer == null) { return false; } if (!first) { this.cppWriter.WriteLine(','); } this.cppWriter.Write(variableDeclaratorExpression.Identifier); if (variableDeclaratorExpression.Initializer != null) { this.cppWriter.Write("("); @switch(variableDeclaratorExpression.Initializer); this.cppWriter.Write(")"); } return true; }
/// <summary> /// The save. /// </summary> /// <param name="variableDeclaratorExpression"> /// The variable declarator expression. /// </param> private void Save(VariableDeclaratorExpression variableDeclaratorExpression) { if (this.saveVariablesMode == SaveVariablesMode.DefaultSourceInitializers) { var nakedClassName = this.GetNameBase(this.ClassContext.Class.Declaration.Name); this.cppWriter.Write(nakedClassName); this.cppWriter.Write("::"); } if (this.saveVariablesMode == SaveVariablesMode.Default) { this.SavePrefix( variableDeclaratorExpression.ParentVariable.Type, variableDeclaratorExpression.Initializer, this.cppWriter); } this.cppWriter.Write(variableDeclaratorExpression.Identifier); if (this.saveVariablesMode != SaveVariablesMode.Default) { this.SaveSuffix( variableDeclaratorExpression.ParentVariable.Type, variableDeclaratorExpression.Initializer, this.cppWriter, true); } if (variableDeclaratorExpression.Initializer != null && this.saveVariablesMode != SaveVariablesMode.DoNotSaveInitializers) { this.cppWriter.Write(" = "); @switch(variableDeclaratorExpression.Initializer); } }