public void TestAutomaticProperty() { var p = new PropertyBody(PropertyBodyType.Get); p.IsAutomaticProperty = true; Assert.AreEqual("get;" + SourceCodeGenerator.NewLine , SourceCodeGenerator.Write(SourceCodeLanguage.CSharp, p)); }
private static TopLevelEntity ParseIndexProperty( ParserContext context, ClassLikeDefinition parent, Token firstToken, Dictionary <string, Token> modifiers, CSharpType type, TokenStream tokens) { tokens.PopExpected("["); CSharpType indexType = CSharpType.Parse(tokens); Token indexVariableName = tokens.PopWord(); tokens.PopExpected("]"); tokens.PopExpected("{"); PropertyDefinition indexProperty = new PropertyDefinition(firstToken, modifiers, type, null, parent); PropertyBody getter = null; PropertyBody setter = null; while (tokens.IsNext("get") || tokens.IsNext("set")) { Token getOrSetToken = tokens.Peek(); bool isGet = getOrSetToken.Value == "get"; if (!isGet && setter != null) { tokens.PopExpected("}"); // intentionally throw } if (isGet && getter != null) { tokens.PopExpected("set"); //intentionally throw } tokens.Pop(); // get/set already fetched with Peek() above. PropertyBody body = new PropertyBody(getOrSetToken, new Dictionary <string, Token>(), isGet, indexProperty); Executable[] code = ExecutableParser.ParseCodeBlock(context, tokens, body, true); body.Code = code; if (isGet) { getter = body; } else { setter = body; } } indexProperty.IndexType = indexType; indexProperty.IndexVariableName = indexVariableName; indexProperty.Getter = getter; indexProperty.Setter = setter; return(indexProperty); }
private static TopLevelEntity ParseClassProperty( ParserContext context, ClassLikeDefinition parent, Token firstToken, Dictionary <string, Token> modifiers, CSharpType type, Token propertyName, TokenStream tokens) { tokens.PopExpected("{"); PropertyDefinition propertyDefinition = new PropertyDefinition(firstToken, modifiers, type, propertyName, parent); PropertyBody getter = null; PropertyBody setter = null; while (!tokens.IsNext("}") && (getter == null || setter == null)) { Token bodyFirstToken = tokens.Peek(); Dictionary <string, Token> bodyModifiers = ParseModifiers(context, tokens); if (tokens.IsNext("get") && getter == null) { Token getToken = tokens.Pop(); getter = new PropertyBody(bodyFirstToken, bodyModifiers, true, propertyDefinition); if (!tokens.PopIfPresent(";")) { getter.Code = ExecutableParser.ParseCodeBlock(context, tokens, getter, true); } } else if (tokens.IsNext("set") && setter == null) { Token setToken = tokens.Pop(); setter = new PropertyBody(bodyFirstToken, bodyModifiers, false, propertyDefinition); if (!tokens.PopIfPresent(";")) { setter.Code = ExecutableParser.ParseCodeBlock(context, tokens, setter, true); } } else if (getter == null) { tokens.PopExpected("get"); // intentionally throw } else { tokens.PopExpected("set"); // intentionally throw } } tokens.PopExpected("}"); propertyDefinition.Getter = getter; propertyDefinition.Setter = setter; return(propertyDefinition); }
public void TestPropertyBasicFeature() { var p = new PropertyBody(PropertyBodyType.Get); p.Body.Add(new CodeBlock(SourceCodeLanguage.CSharp, "return _Age;")); var cs = new CSharpSourceCodeGenerator(); cs.CurrentIndentLevel = 1; cs.Write(p); Assert.AreEqual(" get" + SourceCodeGenerator.NewLine + " {" + SourceCodeGenerator.NewLine + " return _Age;" + SourceCodeGenerator.NewLine + " }" + SourceCodeGenerator.NewLine , cs.ToString()); }