/// <summary> /// Reads the next variable declaration statement from the file and returns it. /// </summary> /// <param name="parentProxy">Represents the parent item.</param> /// <param name="unsafeCode">Indicates whether the code being parsed resides in an unsafe code block.</param> /// <returns>Returns the statement.</returns> private VariableDeclarationStatement GetVariableDeclarationStatement(CodeUnitProxy parentProxy, bool unsafeCode) { Param.AssertNotNull(parentProxy, "parentProxy"); Param.Ignore(unsafeCode); bool constant = false; var statementProxy = new CodeUnitProxy(this.document); // Get the first symbol and make sure it is an unknown word or a const. Symbol symbol = this.PeekNextSymbol(); Token firstToken = null; if (symbol.SymbolType == SymbolType.Const) { this.AdvanceToNextCodeSymbol(statementProxy); constant = true; this.GetToken(statementProxy, TokenType.Const, SymbolType.Const); symbol = this.PeekNextSymbol(); } if (symbol.SymbolType != SymbolType.Other) { throw this.CreateSyntaxException(); } // Get the expression representing the type. var variableDeclarationExpressionProxy = new CodeUnitProxy(this.document); LiteralExpression type = this.GetTypeTokenExpression(variableDeclarationExpressionProxy, unsafeCode, true); if (type == null || type.Children.Count == 0) { throw new SyntaxException(this.document, firstToken.LineNumber); } // Get the rest of the declaration. this.AdvanceToNextCodeSymbol(variableDeclarationExpressionProxy); VariableDeclarationExpression variableDeclarationExpression = this.GetVariableDeclarationExpression(variableDeclarationExpressionProxy, statementProxy, type, ExpressionPrecedence.None, unsafeCode); // Get the closing semicolon. this.GetToken(statementProxy, TokenType.Semicolon, SymbolType.Semicolon); var statement = new VariableDeclarationStatement(statementProxy, variableDeclarationExpression, constant); parentProxy.Children.Add(statement); return statement; }
/// <summary> /// Parses and returns a field. /// </summary> /// <param name="elementProxy">Proxy for the element being created.</param> /// <param name="parent">The parent of the element.</param> /// <param name="unsafeCode">Indicates whether the code is marked as unsafe.</param> /// <param name="generated">Indicates whether the code is marked as generated code.</param> /// <param name="attributes">The attributes on the element.</param> /// <returns>Returns the element.</returns> private Field GetField(CodeUnitProxy elementProxy, Element parent, bool unsafeCode, bool generated, ICollection<Attribute> attributes) { Param.AssertNotNull(elementProxy, "elementProxy"); Param.AssertNotNull(parent, "parent"); Param.Ignore(unsafeCode); Param.Ignore(generated); Param.Ignore(attributes); // Get the modifiers and access. AccessModifierType accessModifier = AccessModifierType.Private; Dictionary<TokenType, Token> modifiers = this.GetElementModifiers(elementProxy, ref accessModifier, FieldModifiers); unsafeCode |= modifiers.ContainsKey(TokenType.Unsafe); var declarationExpressionProxy = new CodeUnitProxy(this.document); // Get the field type. var fieldTypeExpressionProxy = new CodeUnitProxy(this.document); TypeToken fieldType = this.GetTypeToken(fieldTypeExpressionProxy, unsafeCode, true); var fieldTypeExpression = new LiteralExpression(fieldTypeExpressionProxy, fieldType); declarationExpressionProxy.Children.Add(fieldTypeExpression); // Get all of the variable declarators. string firstFieldName = this.GetFieldDeclarators(declarationExpressionProxy, unsafeCode, fieldType); if (string.IsNullOrWhiteSpace(firstFieldName)) { throw this.CreateSyntaxException(); } var variableDeclarationStatementProxy = new CodeUnitProxy(this.document); var declarationExpression = new VariableDeclarationExpression(declarationExpressionProxy, fieldTypeExpression); variableDeclarationStatementProxy.Children.Add(declarationExpression); var variableDeclarationStatement = new VariableDeclarationStatement(variableDeclarationStatementProxy, declarationExpression); elementProxy.Children.Add(variableDeclarationStatement); var field = new Field(elementProxy, firstFieldName, attributes, fieldType, unsafeCode); // Get the trailing semicolon. this.GetToken(elementProxy, TokenType.Semicolon, SymbolType.Semicolon); return field; }