private static void genWalkerVariants(GenContext ctx, NodeVariantsDecl node) { ctx.Appendfnl("public virtual void walk{0}({1} __node)", node.id, ToNs(ctx, node.id)); ctx.Appendfnl("{{"); ctx.IncreaseIndent(); ctx.Appendfnl("__node.Match"); ctx.Appendfnl("("); ctx.IncreaseIndent(); for (var i = 0; i < node.variants.Count; ++i) { var isLast = i == node.variants.Count - 1; var variant = node.variants[i]; ctx.Appendfnl("walk{0}{1}", variant.id, isLast ? "" : ","); } ctx.DecreaseIndent(); ctx.Appendfnl(");"); ctx.DecreaseIndent(); ctx.Appendfnl("}}"); foreach (var variant in node.variants) { genWalkerVariants(ctx, node, variant); } }
private static void genWalkerVariants(GenContext ctx, NodeVariantsDecl variants, NodeVariantDecl variant) { ctx.Appendfnl("public virtual void walk{0}({1} __node)", variant.id, ToNs(ctx, variant.id)); ctx.Appendfnl("{{"); ctx.IncreaseIndent(); var nodeName = variant.id; var fields = variants.commonFieldsBefore.Concat(variant.fields).Concat(variants.commonFieldsAfter).ToList(); foreach (var field in fields) { genWalkerField(ctx, nodeName, field); } ctx.DecreaseIndent(); ctx.Appendfnl("}}"); }
private static void genVariantsNode(GenContext ctx, NodeVariantsDecl nodeVariantsDecl) { ctx.Appendfnl("public abstract partial class {0} : {1}", nodeVariantsDecl.id, ctx.prg.baseClass.name); ctx.Appendfnl("{{"); ctx.IncreaseIndent(); foreach (var attr in nodeVariantsDecl.attributes) { ctx.Appendfnl("public {0} {1} {{ get; set; }}", attr.type, attr.id); } foreach (var field in nodeVariantsDecl.commonFieldsBefore.Concat(nodeVariantsDecl.commonFieldsAfter)) { GenerateField(ctx, field); } foreach (var variant in nodeVariantsDecl.variants) { genExpectMethod(ctx, nodeVariantsDecl, variant); } ctx.DecreaseIndent(); ctx.Appendfnl("}}"); foreach (var variant in nodeVariantsDecl.variants) { genVariantNode(ctx, nodeVariantsDecl, variant); } ctx.Appendfnl("public static class {0}_MatcherExtensions", nodeVariantsDecl.id); ctx.Appendfnl("{{"); ctx.IncreaseIndent(); genMatcher(ctx, nodeVariantsDecl, false); genMatcher(ctx, nodeVariantsDecl, true); ctx.DecreaseIndent(); ctx.Appendfnl("}}"); ctx.NewLine(); }
private static void genVariantNode(GenContext ctx, NodeVariantsDecl nodeVariantsDecl, NodeVariantDecl variant) { ctx.Appendfnl("public partial class {0} : {1}", variant.id, ToNs(ctx, nodeVariantsDecl.id)); ctx.Appendfnl("{{"); ctx.IncreaseIndent(); foreach (var field in variant.fields) { GenerateField(ctx, field); } foreach (var attr in variant.attributes) { ctx.Appendfnl("public {0} {1} {{ get; set; }}", attr.type, attr.id); } var fields = nodeVariantsDecl.commonFieldsBefore.Concat(variant.fields).Concat(nodeVariantsDecl.commonFieldsAfter).ToList(); { // default constructor ctx.Appendfnl("public {0}()", variant.id); ctx.Appendfnl("{{"); foreach (var field in fields) { ctx.IncreaseIndent(); if (field.many) { ctx.Appendfnl("this.{0} = new {1}();", field.id, FieldToCsharpType(ctx, field)); } ctx.DecreaseIndent(); } ctx.Appendfnl("}}"); } if (fields.Count > 0) { // constructor with parameters var fieldsArgs = fields.Select(x => string.Format("{0} {1}_p", FieldToCsharpType(ctx, x), x.id)); ctx.Appendfnl("public {0}({1})", variant.id, string.Join(", ", fieldsArgs)); ctx.Appendfnl("{{"); ctx.IncreaseIndent(); foreach (var field in fields) { var expr = field.id + "_p"; if (field.many) { expr = expr + " ?? new " + FieldToCsharpType(ctx, field) + "()"; } ctx.Appendfnl("this.{0} = {1};", field.id, expr); } ctx.DecreaseIndent(); ctx.Appendfnl("}}"); } genReplaceChildNode(ctx, fields); genToString(ctx, variant.id, fields, nodeVariantsDecl.attributes.Concat(variant.attributes).ToList()); ctx.DecreaseIndent(); ctx.Appendfnl("}}"); ctx.NewLine(); }
private static void genMatcher(GenContext ctx, NodeVariantsDecl nodeVariantsDecl, bool isFn) { var args = nodeVariantsDecl.variants .Select(x => string.Format( "{0} {1}_fn", isFn ? string.Format("global::System.Func<{0}, T>", ToNs(ctx, x.id)) : string.Format("global::System.Action<{0}>", ToNs(ctx, x.id)), x.id ) ).ToList(); args.Add(string.Format("{0} _null_case_fn = null", isFn ? "global::System.Func<T>" : "global::System.Action")); ctx.Appendfnl( "public static {0} Match{1}(this {2} __this, {3})", isFn ? "T" : "void", isFn ? "<T>" : "", ToNs(ctx, nodeVariantsDecl.id), string.Join(", ", args) ); ctx.Appendfnl("{{"); ctx.IncreaseIndent(); for (int i = 0; i < nodeVariantsDecl.variants.Count; ++i) { var attr = nodeVariantsDecl.variants[i]; ctx.Appendfnl("{0}if (__this is {1})", i == 0 ? "" : "else ", ToNs(ctx, attr.id)); ctx.Appendfnl("{{"); ctx.IncreaseIndent(); ctx.Appendfnl("{0}{1}_fn(({2})__this);", isFn ? "return " : "", attr.id, ToNs(ctx, attr.id)); ctx.DecreaseIndent(); ctx.Appendfnl("}}"); } ctx.Appendfnl("else if (__this == null && _null_case_fn != null)"); ctx.Appendfnl("{{"); ctx.IncreaseIndent(); ctx.Appendfnl("{0}_null_case_fn();", isFn ? "return " : ""); ctx.DecreaseIndent(); ctx.Appendfnl("}}"); ctx.Appendfnl("else"); ctx.Appendfnl("{{"); ctx.IncreaseIndent(); ctx.Appendfnl("throw new global::System.Exception(\"Failed match on {0}\");", nodeVariantsDecl.id); ctx.DecreaseIndent(); ctx.Appendfnl("}}"); ctx.DecreaseIndent(); ctx.Appendfnl("}}"); }
private static void genExpectMethod(GenContext ctx, NodeVariantsDecl nodeVariantsDecl, NodeVariantDecl variant) { ctx.Appendfnl("public {0} Expect{1}()", ToNs(ctx, variant.id), variant.id); ctx.Appendfnl("{{"); ctx.IncreaseIndent(); { ctx.Appendfnl("return ({0})this;", ToNs(ctx, variant.id)); } ctx.DecreaseIndent(); ctx.Appendfnl("}}"); }
private NodeVariantsDecl nodeVariants() { EnterRule_nodeVariants(); EnterRule("nodeVariants", 7); TraceIn("nodeVariants", 7); NodeVariantsDecl r = default(NodeVariantsDecl); IToken ID10 = default(IToken); List<FieldDecl> c = default(List<FieldDecl>); NodeVariantDecl v1 = default(NodeVariantDecl); NodeVariantDecl v2 = default(NodeVariantDecl); List<AttributeDecl> common_attributes11 = default(List<AttributeDecl>); try { DebugEnterRule(GrammarFileName, "nodeVariants"); DebugLocation(49, 8); try { // ADL.g:50:5: ( ID EQ (c= commonFields )? v1= nodeVariant ( PIPE v2= nodeVariant )* (c= commonFields )? ( common_attributes )? SEMI ) DebugEnterAlt(1); // ADL.g:50:7: ID EQ (c= commonFields )? v1= nodeVariant ( PIPE v2= nodeVariant )* (c= commonFields )? ( common_attributes )? SEMI { DebugLocation(50, 7); ID10=(IToken)Match(input,ID,Follow._ID_in_nodeVariants299); DebugLocation(50, 10); Match(input,EQ,Follow._EQ_in_nodeVariants301); DebugLocation(50, 13); r = new NodeVariantsDecl { id = (ID10!=null?ID10.Text:default(string)) }; DebugLocation(51, 5); // ADL.g:51:5: (c= commonFields )? int alt6=2; try { DebugEnterSubRule(6); try { DebugEnterDecision(6, false); int LA6_1 = input.LA(1); if ((LA6_1==COMMON_FIELDS)) { alt6 = 1; } } finally { DebugExitDecision(6); } switch (alt6) { case 1: DebugEnterAlt(1); // ADL.g:52:9: c= commonFields { DebugLocation(52, 10); PushFollow(Follow._commonFields_in_nodeVariants321); c=commonFields(); PopFollow(); DebugLocation(53, 9); r.commonFieldsBefore = c; } break; } } finally { DebugExitSubRule(6); } DebugLocation(57, 7); PushFollow(Follow._nodeVariant_in_nodeVariants346); v1=nodeVariant(); PopFollow(); DebugLocation(57, 20); r.variants.Add(v1); DebugLocation(57, 48); // ADL.g:57:48: ( PIPE v2= nodeVariant )* try { DebugEnterSubRule(7); while (true) { int alt7=2; try { DebugEnterDecision(7, false); int LA7_1 = input.LA(1); if ((LA7_1==PIPE)) { alt7 = 1; } } finally { DebugExitDecision(7); } switch ( alt7 ) { case 1: DebugEnterAlt(1); // ADL.g:57:49: PIPE v2= nodeVariant { DebugLocation(57, 49); Match(input,PIPE,Follow._PIPE_in_nodeVariants351); DebugLocation(57, 56); PushFollow(Follow._nodeVariant_in_nodeVariants355); v2=nodeVariant(); PopFollow(); DebugLocation(57, 69); r.variants.Add(v2); } break; default: goto loop7; } } loop7: ; } finally { DebugExitSubRule(7); } DebugLocation(58, 5); // ADL.g:58:5: (c= commonFields )? int alt8=2; try { DebugEnterSubRule(8); try { DebugEnterDecision(8, false); int LA8_1 = input.LA(1); if ((LA8_1==COMMON_FIELDS)) { alt8 = 1; } } finally { DebugExitDecision(8); } switch (alt8) { case 1: DebugEnterAlt(1); // ADL.g:59:9: c= commonFields { DebugLocation(59, 10); PushFollow(Follow._commonFields_in_nodeVariants377); c=commonFields(); PopFollow(); DebugLocation(60, 9); r.commonFieldsAfter = c; } break; } } finally { DebugExitSubRule(8); } DebugLocation(64, 5); // ADL.g:64:5: ( common_attributes )? int alt9=2; try { DebugEnterSubRule(9); try { DebugEnterDecision(9, false); int LA9_1 = input.LA(1); if ((LA9_1==COMMON_ATTRIBUTES)) { alt9 = 1; } } finally { DebugExitDecision(9); } switch (alt9) { case 1: DebugEnterAlt(1); // ADL.g:64:5: common_attributes { DebugLocation(64, 5); PushFollow(Follow._common_attributes_in_nodeVariants400); common_attributes11=common_attributes(); PopFollow(); } break; } } finally { DebugExitSubRule(9); } DebugLocation(65, 5); r.attributes = common_attributes11 ?? new List<AttributeDecl>(); DebugLocation(68, 5); Match(input,SEMI,Follow._SEMI_in_nodeVariants413); } } catch (RecognitionException re) { ReportError(re); Recover(input,re); } finally { TraceOut("nodeVariants", 7); LeaveRule("nodeVariants", 7); LeaveRule_nodeVariants(); } DebugLocation(68, 8); } finally { DebugExitRule(GrammarFileName, "nodeVariants"); } return r; }