public override Template Visit(Enum enum_def) { if (enum_def.Attribute.Find(x => x.Name == "ToString") != null) { Attr attr = enum_def.Attribute.Find(x => x.Name == "ToString"); FuncDef func = new FuncDef(); func.Type = new IdentType("const char*"); func.Name = attr.Args.Count() == 0 ? "ToString" : attr.Args.First(); func.Args.Add(new ExprAlloc(new IdentType("const " + enum_def.Name + "&"), "_t_value", null, AllocType.Declare)); List <StmtSwitchItem> switch_list = new List <StmtSwitchItem>(); foreach (var item in enum_def.Values) { StmtBlock block = new StmtBlock(); block.StmtList.Add(new StmtReturn(new ExprConst("\"" + item + "\"", ConstType.String))); switch_list.Add(new StmtSwitchItem(new List <Expr> { new ExprConst(enum_def.Name + "::" + item, ConstType.Ident) }, block)); } StmtBlock default_block = new StmtBlock(); { default_block.StmtList.Add(new StmtExpr(new ExprCall(new ExprConst("throw", ConstType.Ident), null, new List <Expr> { new ExprConst("\"Not Found\"", ConstType.String) }))); } StmtSwitch stmt_switch = new StmtSwitch(new ExprConst("_t_value", ConstType.Ident), switch_list, default_block); StmtBlock body = new StmtBlock(); body.StmtList.Add(stmt_switch); func.Body = body; return(func.Accept(this)); } else { throw new NotSupportedException(); } }
private Attr attribute_item() { EnterRule_attribute_item(); EnterRule("attribute_item", 8); TraceIn("attribute_item", 8); Attr value = default(Attr); CommonTree c = default(CommonTree); string a = default(string); string b = default(string); value = new Attr(); try { DebugEnterRule(GrammarFileName, "attribute_item"); DebugLocation(118, 1); try { // SugarWalker.g:123:2: ( ^( Attribute (a= ident |c= 'const' ) (b= attribute_args )* ) ) DebugEnterAlt(1); // SugarWalker.g:123:4: ^( Attribute (a= ident |c= 'const' ) (b= attribute_args )* ) { DebugLocation(123, 4); DebugLocation(123, 6); Match(input,Attribute,Follow._Attribute_in_attribute_item488); Match(input, TokenTypes.Down, null); DebugLocation(123, 16); // SugarWalker.g:123:16: (a= ident |c= 'const' ) int alt13=2; try { DebugEnterSubRule(13); try { DebugEnterDecision(13, false); int LA13_0 = input.LA(1); if ((LA13_0==IDENT)) { alt13 = 1; } else if ((LA13_0==124)) { alt13 = 2; } else { NoViableAltException nvae = new NoViableAltException("", 13, 0, input); DebugRecognitionException(nvae); throw nvae; } } finally { DebugExitDecision(13); } switch (alt13) { case 1: DebugEnterAlt(1); // SugarWalker.g:123:17: a= ident { DebugLocation(123, 18); PushFollow(Follow._ident_in_attribute_item493); a=ident(); PopFollow(); DebugLocation(123, 25); value.Name = a; } break; case 2: DebugEnterAlt(2); // SugarWalker.g:123:48: c= 'const' { DebugLocation(123, 49); c=(CommonTree)Match(input,124,Follow._124_in_attribute_item501); DebugLocation(123, 58); value.Name = "const"; } break; } } finally { DebugExitSubRule(13); } DebugLocation(123, 86); // SugarWalker.g:123:86: (b= attribute_args )* try { DebugEnterSubRule(14); while (true) { int alt14=2; try { DebugEnterDecision(14, false); int LA14_0 = input.LA(1); if ((LA14_0==IDENT||LA14_0==NUMBER||LA14_0==STRING)) { alt14 = 1; } } finally { DebugExitDecision(14); } switch ( alt14 ) { case 1: DebugEnterAlt(1); // SugarWalker.g:123:87: b= attribute_args { DebugLocation(123, 88); PushFollow(Follow._attribute_args_in_attribute_item509); b=attribute_args(); PopFollow(); DebugLocation(123, 104); value.Args.Add(b) ; } break; default: goto loop14; } } loop14: ; } finally { DebugExitSubRule(14); } Match(input, TokenTypes.Up, null); } } catch (RecognitionException re) { ReportError(re); Recover(input,re); } finally { TraceOut("attribute_item", 8); LeaveRule("attribute_item", 8); LeaveRule_attribute_item(); } DebugLocation(124, 1); } finally { DebugExitRule(GrammarFileName, "attribute_item"); } return value; }
private Attr attribute_item() { EnterRule_attribute_item(); EnterRule("attribute_item", 8); TraceIn("attribute_item", 8); Attr value = default(Attr); CommonTree c = default(CommonTree); CommonTree d = default(CommonTree); CommonTree e = default(CommonTree); CommonTree f = default(CommonTree); string a = default(string); string b = default(string); value = new Attr(); try { DebugEnterRule(GrammarFileName, "attribute_item"); DebugLocation(146, 1); try { // SugarWalker.g:151:2: ( ^( Attribute (a= ident |c= 'const' |d= 'static' |e= 'public' |f= 'virtual' ) (b= attribute_args )* ) ) DebugEnterAlt(1); // SugarWalker.g:151:4: ^( Attribute (a= ident |c= 'const' |d= 'static' |e= 'public' |f= 'virtual' ) (b= attribute_args )* ) { DebugLocation(151, 4); DebugLocation(151, 6); Match(input,Attribute,Follow._Attribute_in_attribute_item496); Match(input, TokenTypes.Down, null); DebugLocation(151, 16); // SugarWalker.g:151:16: (a= ident |c= 'const' |d= 'static' |e= 'public' |f= 'virtual' ) int alt14=5; try { DebugEnterSubRule(14); try { DebugEnterDecision(14, false); switch (input.LA(1)) { case IDENT: { alt14 = 1; } break; case 142: { alt14 = 2; } break; case 166: { alt14 = 3; } break; case 163: { alt14 = 4; } break; case 178: { alt14 = 5; } break; default: { NoViableAltException nvae = new NoViableAltException("", 14, 0, input); DebugRecognitionException(nvae); throw nvae; } } } finally { DebugExitDecision(14); } switch (alt14) { case 1: DebugEnterAlt(1); // SugarWalker.g:151:17: a= ident { DebugLocation(151, 18); PushFollow(Follow._ident_in_attribute_item501); a=ident(); PopFollow(); DebugLocation(151, 25); value.Name = a; } break; case 2: DebugEnterAlt(2); // SugarWalker.g:151:48: c= 'const' { DebugLocation(151, 49); c=(CommonTree)Match(input,142,Follow._142_in_attribute_item509); DebugLocation(151, 58); value.Name = "const"; } break; case 3: DebugEnterAlt(3); // SugarWalker.g:151:87: d= 'static' { DebugLocation(151, 88); d=(CommonTree)Match(input,166,Follow._166_in_attribute_item517); DebugLocation(151, 98); value.Name = "static"; } break; case 4: DebugEnterAlt(4); // SugarWalker.g:151:128: e= 'public' { DebugLocation(151, 129); e=(CommonTree)Match(input,163,Follow._163_in_attribute_item525); DebugLocation(151, 139); value.Name = "public"; } break; case 5: DebugEnterAlt(5); // SugarWalker.g:151:169: f= 'virtual' { DebugLocation(151, 170); f=(CommonTree)Match(input,178,Follow._178_in_attribute_item533); DebugLocation(151, 181); value.Name = "virtual"; } break; } } finally { DebugExitSubRule(14); } DebugLocation(151, 211); // SugarWalker.g:151:211: (b= attribute_args )* try { DebugEnterSubRule(15); while (true) { int alt15=2; try { DebugEnterDecision(15, false); int LA15_0 = input.LA(1); if ((LA15_0==IDENT||LA15_0==NUMBER||LA15_0==STRING)) { alt15 = 1; } } finally { DebugExitDecision(15); } switch ( alt15 ) { case 1: DebugEnterAlt(1); // SugarWalker.g:151:212: b= attribute_args { DebugLocation(151, 213); PushFollow(Follow._attribute_args_in_attribute_item541); b=attribute_args(); PopFollow(); DebugLocation(151, 229); value.Args.Add(b) ; } break; default: goto loop15; } } loop15: ; } finally { DebugExitSubRule(15); } Match(input, TokenTypes.Up, null); } } catch (RecognitionException re) { ReportError(re); Recover(input,re); } finally { TraceOut("attribute_item", 8); LeaveRule("attribute_item", 8); LeaveRule_attribute_item(); } DebugLocation(152, 1); } finally { DebugExitRule(GrammarFileName, "attribute_item"); } return value; }