Esempio n. 1
0
 public static LNode Namespace(LNode node, IMacroContext context)
 {
     if (node.ArgCount == 2 && !node.Args.Last.Calls(S.Braces))
     {
         context.DropRemainingNodes = true;
         return(node.PlusArg(F.Braces(context.RemainingNodes).PlusAttr(F.TriviaNewline)));
     }
     return(null);
 }
Esempio n. 2
0
 public static LNode Namespace(LNode node, IMacroContext context)
 {
     if (node.ArgCount == 2 && !node.Args.Last.Calls(S.Braces))
     {
         context.DropRemainingNodes = true;
         node = node.PlusArg(F.Braces(context.RemainingNodes).PlusAttr(F.TriviaNewline));
         // avoid artifact: `namespace Xyz` tends to be on one line, but we don't want the output to be.
         return(node.SetStyle(node.Style & ~NodeStyle.OneLiner));
     }
     return(null);
 }
Esempio n. 3
0
        public static LNode on_throw(LNode node, IMacroContext context)
        {
            LNode firstArg, rest, on_handler = ValidateOnStmt(node, context, out rest, out firstArg);

            if (on_handler == null)
            {
                return(null);
            }
            on_handler = on_handler.PlusArg(F.Call(S.Throw));
            return(TransformOnCatch(node, firstArg, rest, on_handler));
        }
Esempio n. 4
0
            private LNode GenerateIfElseChain(PredictionTree tree, LNode[] branchCode, ref LNode laVar, MSet <int> switchCases)
            {
                // From the prediction table, generate a chain of if-else
                // statements in reverse, starting with the final "else" clause.
                // Skip any branches that have been claimed for use in a switch()
                LNode ifChain  = null;
                bool  usedTest = false;

                for (int i = tree.Children.Count - 1; i >= 0; i--)
                {
                    if (switchCases.Contains(i))
                    {
                        continue;
                    }

                    if (ifChain == null)
                    {
                        ifChain = branchCode[i];
                    }
                    else
                    {
                        usedTest = true;
                        var   branch = tree.Children[i];
                        LNode test;
                        if (tree.IsAssertionLevel)
                        {
                            test = GenerateTest(branch.AndPreds, tree.Lookahead, laVar);
                        }
                        else
                        {
                            var set = CGH.Optimize(branch.Set, branch.Covered);
                            test = CGH.GenerateTest(set, laVar);
                        }

                        LNode @if = F.Call(S.If, test, branchCode[i]);
                        if (!ifChain.IsIdWithoutPAttrs(S.Missing))
                        {
                            @if = @if.PlusArg(ifChain);
                        }
                        ifChain = @if;
                    }
                }
                if (!usedTest)
                {
                    laVar = null;                     // unnecessary
                }
                return(ifChain);
            }
Esempio n. 5
0
            public void Generate(Rule rule)
            {
                CGH.BeginRule(rule);
                _currentRule           = rule;
                _target                = new WList <LNode>();
                _laVarsNeeded          = 0;
                _separatedMatchCounter = _stopLabelCounter = 0;
                _recognizerMode        = rule.IsRecognizer;
                _labelsInUse.Clear();

                Visit(rule.Pred);

                if (_laVarsNeeded != 0)
                {
                    LNode laVars = F.Call(S.Var, CGH.LAType());
                    for (int i = 0; _laVarsNeeded != 0; i++, _laVarsNeeded >>= 1)
                    {
                        if ((_laVarsNeeded & 1) != 0)
                        {
                            laVars = laVars.PlusArg(F.Id("la" + i.ToString()));
                        }
                    }
                    _target.Insert(0, laVars);
                }

                LNode method;

                if (rule.TryWrapperName != null)
                {
                    Debug.Assert(rule.IsRecognizer);
                    method = F.OnNewLine(CGH.CreateTryWrapperForRecognizer(rule));
                    _classBody.SpliceAdd(method, S.Splice);
                }

                method = CGH.CreateRuleMethod(rule, _target.ToVList());
                if (!rule.IsRecognizer)
                {
                    method = F.OnNewLine(method);
                }
                _classBody.SpliceAdd(method, S.Splice);
            }
Esempio n. 6
0
		public static LNode Namespace(LNode node, IMacroContext context)
		{
			if (node.ArgCount == 2 && !node.Args.Last.Calls(S.Braces))
			{
				context.DropRemainingNodes = true;
				node = node.PlusArg(F.Braces(context.RemainingNodes).PlusAttr(F.TriviaNewline));
				// avoid artifact: `namespace Xyz` tends to be on one line, but we don't want the output to be.
				return node.SetStyle(node.Style & ~NodeStyle.OneLiner);
			}
			return null;
		}
Esempio n. 7
0
        public static LNode on_return(LNode node, IMacroContext context)
        {
            LNode firstArg, rest, on_handler = ValidateOnStmt(node, context, out rest, out firstArg);

            if (on_handler == null)
            {
                return(null);
            }

            // Get/construct the declaration of the var to return, and get its name
            LNode varDecl = firstArg, varName = firstArg;
            bool  varAssigned = false;

            if (firstArg == null)
            {
                varName = F.Id(__result__);
                varDecl = F.Var(F._Missing, varName);
            }
            else
            {
                if (varDecl.Calls(S.Var, 2))
                {
                    if (varAssigned = (varName = varDecl.Args[1]).Calls(S.Assign, 2))
                    {
                        varName = varName.Args[0];
                    }
                }
                else if (varName.IsId)
                {
                    varDecl = node.With(S.Var, F._Missing, varName);
                }
                else
                {
                    return(Reject(context, firstArg, "The first parameter to on_return must be a simple identifier (the name of a variable to return) or a variable declaration (for a variable to be returned)."));
                }
            }
            var retExpr = F.Call(S.Substitute, F.Id(__retexpr__));

            Pair <LNode, LNode>[] patterns = new Pair <LNode, LNode>[2] {
                // return; => { <on_handler> return; }
                new Pair <LNode, LNode>(F.Call(S.Return), varAssigned
                                                ? on_handler.WithArgs(new RVList <LNode>(varDecl)
                                                                      .AddRange(on_handler.Args)
                                                                      .Add(F.Call(S.Return, varName)))
                                                : on_handler.PlusArg(F.Call(S.Return))),
                // return exp; => { <varDecl = $exp> <on_handler> return <varName>; }
                new Pair <LNode, LNode>(F.Call(S.Return, retExpr),
                                        on_handler.WithArgs(new RVList <LNode>(
                                                                varDecl.WithArgChanged(1, F.Call(S.Assign, varName, retExpr)))
                                                            .AddRange(on_handler.Args)
                                                            .Add(F.Call(S.Return, varName))))
            };
            int            replacementCount = 0;
            RVList <LNode> output           = StandardMacros.Replace(rest.Args, patterns, out replacementCount);

            if (replacementCount == 0)
            {
                context.Write(Severity.Warning, node, "'on_return': no 'return' statements were found below this line, so this macro had no effect.");
            }
            return(output.AsLNode(S.Splice));
        }