private static string FieldToCsharpType(GenContext ctx, FieldDecl field) { var type = ToNs(ctx, field.type); if (field.many) { type = "global::System.Collections.Generic.List<" + type + ">"; } return type; }
private static void GenerateField(GenContext ctx, FieldDecl field) { var type = FieldToCsharpType(ctx, field); var name = field.id ?? (field.many ? field.type : field.type); ctx.Appendfnl("[global::System.CLSCompliant(false)]"); ctx.Appendfnl("protected {0} __{1};", type, name); ctx.Appendfnl("public {0} {1}", type, name); ctx.Appendfnl("{{"); ctx.IncreaseIndent(); { ctx.Appendfnl("get {{ return __{0}; }}", name); ctx.Appendfnl("set"); ctx.Appendfnl("{{"); ctx.IncreaseIndent(); { if (!field.optional) { ctx.Appendfnl("if (value == null)", name); ctx.Appendfnl("{{"); ctx.IncreaseIndent(); { ctx.Appendfnl("throw new global::System.Exception(\"Invalid AST Node assignment: {0} can not be null\");", name); } ctx.DecreaseIndent(); ctx.Appendfnl("}}"); } ctx.Appendfnl("if (__{0} != null)", name); ctx.Appendfnl("{{"); ctx.IncreaseIndent(); { if (field.many) { ctx.Appendfnl("foreach (var __item in __{0})", name); ctx.Appendfnl("{{"); ctx.IncreaseIndent(); { ctx.Appendfnl("if (__item.Parent == this)"); ctx.Appendfnl("{{"); ctx.IncreaseIndent(); { ctx.Appendfnl("__item.Parent = null;"); } ctx.DecreaseIndent(); ctx.Appendfnl("}}"); } ctx.DecreaseIndent(); ctx.Appendfnl("}}"); } else { ctx.Appendfnl("if ( __{0}.Parent == this)", name); ctx.Appendfnl("{{"); ctx.IncreaseIndent(); { ctx.Appendfnl(" __{0}.Parent = null;", name); } ctx.DecreaseIndent(); ctx.Appendfnl("}}"); } } ctx.DecreaseIndent(); ctx.Appendfnl("}}"); ctx.Appendfnl("__{0} = value;", name); ctx.Appendfnl("if (__{0} != null)", name); ctx.Appendfnl("{{"); ctx.IncreaseIndent(); { if (field.many) { ctx.Appendfnl("foreach (var __item in __{0})", name); ctx.Appendfnl("{{"); ctx.IncreaseIndent(); { ctx.Appendfnl("__item.Parent = this;"); } ctx.DecreaseIndent(); ctx.Appendfnl("}}"); } else { ctx.Appendfnl(" __{0}.Parent = this;", name); } } ctx.DecreaseIndent(); ctx.Appendfnl("}}"); } ctx.DecreaseIndent(); ctx.Appendfnl("}}"); } ctx.DecreaseIndent(); ctx.Appendfnl("}}"); }
private static void genWalkerField(GenContext ctx, string nodeName, FieldDecl field) { if (field.many) { ctx.Appendfnl("for (var __idx = 0; __idx < __node.{0}.Count; ++__idx)", field.id); ctx.Appendfnl("{{"); ctx.IncreaseIndent(); ctx.Appendfnl("this.walk{0}(__node.{1}[__idx]);", field.type, field.id); ctx.DecreaseIndent(); ctx.Appendfnl("}}"); } else { if (!field.optional) { ctx.Appendfnl("if (__node.{0} == null)", field.id); ctx.Appendfnl("{{"); ctx.IncreaseIndent(); ctx.Appendfnl("throw new global::System.InvalidOperationException(\"Required field {0} of node {1} is empty\");", field.id, nodeName); ctx.DecreaseIndent(); ctx.Appendfnl("}}"); } ctx.Appendfnl("if (__node.{0} != null)", field.id); ctx.Appendfnl("{{"); ctx.IncreaseIndent(); ctx.Appendfnl("this.walk{0}(__node.{1});", field.type, field.id); ctx.DecreaseIndent(); ctx.Appendfnl("}}"); } }
private FieldDecl field() { EnterRule_field(); EnterRule("field", 14); TraceIn("field", 14); FieldDecl r = default(FieldDecl); IToken typeId = default(IToken); IToken q = default(IToken); IToken a = default(IToken); IToken nameId = default(IToken); try { DebugEnterRule(GrammarFileName, "field"); DebugLocation(106, 5); try { // ADL.g:107:5: (typeId= ID (q= QUESTION |a= ASTERISK )? (nameId= ID )? ) DebugEnterAlt(1); // ADL.g:107:7: typeId= ID (q= QUESTION |a= ASTERISK )? (nameId= ID )? { DebugLocation(107, 13); typeId=(IToken)Match(input,ID,Follow._ID_in_field642); DebugLocation(107, 17); // ADL.g:107:17: (q= QUESTION |a= ASTERISK )? int alt18=3; try { DebugEnterSubRule(18); try { DebugEnterDecision(18, false); int LA18_1 = input.LA(1); if ((LA18_1==QUESTION)) { alt18 = 1; } else if ((LA18_1==ASTERISK)) { alt18 = 2; } } finally { DebugExitDecision(18); } switch (alt18) { case 1: DebugEnterAlt(1); // ADL.g:107:18: q= QUESTION { DebugLocation(107, 19); q=(IToken)Match(input,QUESTION,Follow._QUESTION_in_field647); } break; case 2: DebugEnterAlt(2); // ADL.g:107:29: a= ASTERISK { DebugLocation(107, 30); a=(IToken)Match(input,ASTERISK,Follow._ASTERISK_in_field651); } break; } } finally { DebugExitSubRule(18); } DebugLocation(107, 48); // ADL.g:107:48: (nameId= ID )? int alt19=2; try { DebugEnterSubRule(19); try { DebugEnterDecision(19, false); int LA19_1 = input.LA(1); if ((LA19_1==ID)) { alt19 = 1; } } finally { DebugExitDecision(19); } switch (alt19) { case 1: DebugEnterAlt(1); // ADL.g:107:48: nameId= ID { DebugLocation(107, 48); nameId=(IToken)Match(input,ID,Follow._ID_in_field657); } break; } } finally { DebugExitSubRule(19); } DebugLocation(107, 53); r = new FieldDecl { type = (typeId!=null?typeId.Text:default(string)), id = (nameId!=null?nameId.Text:default(string)) ?? (typeId!=null?typeId.Text:default(string)), optional = q != null, many = a != null }; } } catch (RecognitionException re) { ReportError(re); Recover(input,re); } finally { TraceOut("field", 14); LeaveRule("field", 14); LeaveRule_field(); } DebugLocation(110, 5); } finally { DebugExitRule(GrammarFileName, "field"); } return r; }