Exemple #1
0
 public abstract Template Visit(ExprMatch expr);
Exemple #2
0
        public override Template Visit(ExprMatch expr)
        {
            Template template = new Template("([&]()<type>{\n    <list; separator=\" \">\n})()");
            if (expr.Type != null)
            {
                Template type = new Template(" -> <type> ");
                type.Add("type", expr.Type.Accept(this));
                template.Add("type", type);
            }
            else
            {
                template.Add("type", "");
            }
            List<Template> list = new List<Template>();

            Template match_expr = expr.Expr == null ? null : expr.Expr.Accept(this);
            bool isFirst = true;
            foreach (var x in expr.List)
            {
                if (x.Condition is ExprConst)
                {
                    ExprConst expr_const = (ExprConst)x.Condition;
                    if (expr_const.Type == ConstType.Ident && expr_const.Text == "_")
                    {
                        continue;
                    }
                }

                Template node = null;
                if (isFirst)
                {
                    node = new Template("if (<condition>) {\n    return <expr>;\n}");
                    isFirst = false;
                }
                else
                {
                    node = new Template("else if (<condition>) {\n    return <expr>;\n}");
                }

                if (match_expr == null)
                {
                    node.Add("condition", x.Condition.Accept(this));
                }
                else
                {
                    Template condition = new Template("<expr> == (<case>)");
                    condition.Add("expr", match_expr);
                    condition.Add("case", x.Condition.Accept(this));
                    node.Add("condition", condition);
                }
                node.Add("expr", x.Expr.Accept(this));
                list.Add(node);
            }

            foreach (var x in expr.List)
            {
                if (x.Condition is ExprConst)
                {
                    ExprConst expr_const = (ExprConst)x.Condition;
                    if (expr_const.Type == ConstType.Ident && expr_const.Text == "_")
                    {
                        var node = new Template("else {\n    return <expr>;\n}");
                        node.Add("expr", x.Expr.Accept(this));
                        list.Add(node);
                    }
                }
            }

            template.Add("list", list);
            return template;
        }
	private Expr match_expr()
	{
		EnterRule_match_expr();
		EnterRule("match_expr", 59);
		TraceIn("match_expr", 59);
		Expr value = default(Expr);


		Expr a = default(Expr);
		SugarType tp = default(SugarType);
		ExprMatchItem b = default(ExprMatchItem);


			List<ExprMatchItem> list = new List<ExprMatchItem>();

		try { DebugEnterRule(GrammarFileName, "match_expr");
		DebugLocation(753, 1);
		try
		{
			// SugarWalker.g:758:2: ( ^( Match_Expr (a= expr )? (tp= type_name )? (b= match_item )+ ) )
			DebugEnterAlt(1);
			// SugarWalker.g:758:5: ^( Match_Expr (a= expr )? (tp= type_name )? (b= match_item )+ )
			{
			DebugLocation(758, 5);
			DebugLocation(758, 7);
			Match(input,Match_Expr,Follow._Match_Expr_in_match_expr2958); 

			Match(input, TokenTypes.Down, null); 
			DebugLocation(758, 18);
			// SugarWalker.g:758:18: (a= expr )?
			int alt87=2;
			try { DebugEnterSubRule(87);
			try { DebugEnterDecision(87, false);
			int LA87_0 = input.LA(1);

			if (((LA87_0>=Expr_Access && LA87_0<=Expr_Alloc_Equal)||(LA87_0>=Expr_Bin && LA87_0<=Expr_Where)||LA87_0==IDENT||LA87_0==Match_Expr||LA87_0==Match_Tuple||LA87_0==NUMBER||LA87_0==STRING||LA87_0==89||LA87_0==92||LA87_0==96||LA87_0==99||LA87_0==104||LA87_0==110||LA87_0==113||LA87_0==118||LA87_0==121||LA87_0==128||LA87_0==131||LA87_0==135||LA87_0==183||LA87_0==DOUBLE))
			{
				alt87 = 1;
			}
			} finally { DebugExitDecision(87); }
			switch (alt87)
			{
			case 1:
				DebugEnterAlt(1);
				// SugarWalker.g:758:19: a= expr
				{
				DebugLocation(758, 20);
				PushFollow(Follow._expr_in_match_expr2963);
				a=expr();
				PopFollow();


				}
				break;

			}
			} finally { DebugExitSubRule(87); }

			DebugLocation(758, 28);
			// SugarWalker.g:758:28: (tp= type_name )?
			int alt88=2;
			try { DebugEnterSubRule(88);
			try { DebugEnterDecision(88, false);
			int LA88_0 = input.LA(1);

			if (((LA88_0>=Type_Array && LA88_0<=Type_Ident)||(LA88_0>=Type_Ref && LA88_0<=Type_Template)))
			{
				alt88 = 1;
			}
			} finally { DebugExitDecision(88); }
			switch (alt88)
			{
			case 1:
				DebugEnterAlt(1);
				// SugarWalker.g:758:29: tp= type_name
				{
				DebugLocation(758, 31);
				PushFollow(Follow._type_name_in_match_expr2970);
				tp=type_name();
				PopFollow();


				}
				break;

			}
			} finally { DebugExitSubRule(88); }

			DebugLocation(758, 44);
			// SugarWalker.g:758:44: (b= match_item )+
			int cnt89=0;
			try { DebugEnterSubRule(89);
			while (true)
			{
				int alt89=2;
				try { DebugEnterDecision(89, false);
				int LA89_0 = input.LA(1);

				if ((LA89_0==Match_Expr_Item))
				{
					alt89 = 1;
				}


				} finally { DebugExitDecision(89); }
				switch (alt89)
				{
				case 1:
					DebugEnterAlt(1);
					// SugarWalker.g:758:45: b= match_item
					{
					DebugLocation(758, 46);
					PushFollow(Follow._match_item_in_match_expr2977);
					b=match_item();
					PopFollow();

					DebugLocation(758, 58);
					 list.Add(b); 

					}
					break;

				default:
					if (cnt89 >= 1)
						goto loop89;

					EarlyExitException eee89 = new EarlyExitException( 89, input );
					DebugRecognitionException(eee89);
					throw eee89;
				}
				cnt89++;
			}
			loop89:
				;

			} finally { DebugExitSubRule(89); }


			Match(input, TokenTypes.Up, null); 

			DebugLocation(759, 2);

					value = new ExprMatch(a, tp, list);
				

			}

		}
		catch (RecognitionException re)
		{
			ReportError(re);
			Recover(input,re);
		}
		finally
		{
			TraceOut("match_expr", 59);
			LeaveRule("match_expr", 59);
			LeaveRule_match_expr();
		}
		DebugLocation(762, 1);
		} finally { DebugExitRule(GrammarFileName, "match_expr"); }
		return value;

	}