Ejemplo n.º 1
0
        private static IEnumerable <Node> ElimQuoteUnfold(
            Node n,
            Stack <Configuration> configStack,
            SuccessToken success,
            List <Flag> flags,
            CancellationToken cancel)
        {
            if (n.NodeKind == NodeKind.Config)
            {
                yield break;
            }

            Cnst          value;
            Configuration conf;

            if (n.TryGetConfiguration(out conf))
            {
                configStack.Push(conf);
            }

            if (n.NodeKind != NodeKind.Quote)
            {
                foreach (var c in n.Children)
                {
                    yield return(c);
                }

                yield break;
            }

            Contract.Assert(configStack.Count > 0);
            conf = configStack.Peek();
            if (!conf.TryGetSetting(Configuration.Parse_ActiveParserSetting, out value))
            {
                var flag = new Flag(
                    SeverityKind.Error,
                    n,
                    Constants.QuotationError.ToString("No active parser configured."),
                    Constants.QuotationError.Code);
                flags.Add(flag);
                success.Failed();
                yield break;
            }

            IQuoteParser parser;

            if (!conf.TryGetParserInstance(value.GetStringValue(), out parser))
            {
                var flag = new Flag(
                    SeverityKind.Error,
                    n,
                    Constants.QuotationError.ToString(string.Format("Cannot find a parser named {0}", value.GetStringValue())),
                    Constants.QuotationError.Code);
                flags.Add(flag);
                success.Failed();
                yield break;
            }

            string     unquotePrefix = "";
            bool       parseSuccess  = true;
            AST <Node> result        = null;

            try
            {
                unquotePrefix = parser.UnquotePrefix;
                List <Flag> parseFlags;
                if (!parser.Parse(
                        conf,
                        new QuoteStream((Quote)n, parser.UnquotePrefix, cancel),
                        new SourcePositioner((Quote)n, parser.UnquotePrefix),
                        out result,
                        out parseFlags))
                {
                    var flag = new Flag(
                        SeverityKind.Error,
                        n,
                        Constants.QuotationError.ToString(string.Empty),
                        Constants.QuotationError.Code);
                    flags.Add(flag);
                    parseSuccess = false;
                }

                if (parseFlags != null)
                {
                    flags.AddRange(parseFlags);
                }
            }
            catch (Exception e)
            {
                var flag = new Flag(
                    SeverityKind.Error,
                    n,
                    Constants.PluginException.ToString(Configuration.ParsersCollectionName, value.GetStringValue(), e.Message),
                    Constants.PluginException.Code);
                flags.Add(flag);
                parseSuccess = false;
            }

            if (cancel.IsCancellationRequested)
            {
                var flag = new Flag(
                    SeverityKind.Error,
                    n,
                    Constants.QuotationError.ToString("Cancelled quotation parse"),
                    Constants.QuotationError.Code);
                flags.Add(flag);
                parseSuccess = false;
            }

            if (parseSuccess && (result == null || result.FindAny(queryQuote, cancel) != null))
            {
                var flag = new Flag(
                    SeverityKind.Error,
                    n,
                    Constants.QuotationError.ToString("Quotation parser did not eliminate quotations."),
                    Constants.QuotationError.Code);
                flags.Add(flag);
                parseSuccess = false;
            }

            if (!parseSuccess)
            {
                success.Failed();
                yield break;
            }

            Quote qn         = (Quote)n;
            int   childId    = 0;
            var   unquoteMap = new Map <string, AST <Node> >(string.CompareOrdinal);

            foreach (var c in qn.Contents)
            {
                if (c.NodeKind != NodeKind.QuoteRun)
                {
                    unquoteMap.Add(string.Format("{0}{1}", unquotePrefix, childId), Factory.Instance.ToAST(c));
                    ++childId;
                }
            }

            if (childId == 0)
            {
                yield return(result.Node);
            }
            else
            {
                yield return(SubstituteEscapes(result, unquoteMap, cancel).Root);
            }
        }
Ejemplo n.º 2
0
        void GenerateLinkerInfo(AST <Program> model)
        {
            var linkerModel = model.FindAny(
                new NodePred[] { NodePredFactory.Instance.MkPredicate(NodeKind.Program), NodePredFactory.Instance.MkPredicate(NodeKind.Model) });

            var factBins = new Dictionary <string, LinkedList <AST <FuncTerm> > >();

            linkerModel.FindAll(
                new NodePred[]
            {
                NodePredFactory.Instance.Star,
                NodePredFactory.Instance.MkPredicate(NodeKind.ModelFact)
            },

                (path, n) =>
            {
                var mf      = (ModelFact)n;
                FuncTerm ft = (FuncTerm)mf.Match;
                GetBin(factBins, ft).AddLast((AST <FuncTerm>)Factory.Instance.ToAST(ft));
            });
            LinkedList <AST <FuncTerm> > terms;


            terms = GetBin(factBins, "CSharpLinkMap");
            foreach (var term in terms)
            {
                using (var it = term.Node.Args.GetEnumerator())
                {
                    it.MoveNext();
                    var name = ((Cnst)it.Current).GetStringValue();
                    it.MoveNext();
                    var currMachineName = ((Cnst)it.Current).GetStringValue();
                    it.MoveNext();
                    var IorMName = ((Cnst)it.Current).GetStringValue();
                    it.MoveNext();
                    var impMachineName = ((Cnst)it.Current).GetStringValue();

                    if (allTests.ContainsKey(name))
                    {
                        if (allTests[name].linkMap.ContainsKey(currMachineName))
                        {
                            allTests[name].linkMap[currMachineName].Add(IorMName, impMachineName);
                        }
                        else
                        {
                            allTests[name].linkMap[currMachineName] = new Dictionary <string, string>();
                            allTests[name].linkMap[currMachineName].Add(IorMName, impMachineName);
                        }
                    }
                    else
                    {
                        allTests[name] = new TestCaseInfo();
                        allTests[name].linkMap[currMachineName] = new Dictionary <string, string>();
                        allTests[name].linkMap[currMachineName].Add(IorMName, impMachineName);
                    }
                }
            }

            terms = GetBin(factBins, "CSharpRenameMap");
            foreach (var term in terms)
            {
                using (var it = term.Node.Args.GetEnumerator())
                {
                    it.MoveNext();
                    var name = ((Cnst)it.Current).GetStringValue();
                    it.MoveNext();
                    var renamedMachineName = ((Cnst)it.Current).GetStringValue();
                    it.MoveNext();
                    var impName = ((Cnst)it.Current).GetStringValue();

                    if (allTests.ContainsKey(name))
                    {
                        if (allTests[name].renameMap.ContainsKey(renamedMachineName))
                        {
                            Console.WriteLine("Internal Error");
                            Environment.Exit(-1);
                        }
                        else
                        {
                            allTests[name].renameMap.Add(renamedMachineName, impName);
                        }
                    }
                    else
                    {
                        allTests[name] = new TestCaseInfo();
                        allTests[name].renameMap.Add(renamedMachineName, impName);
                    }
                }
            }

            terms = GetBin(factBins, "CSharpDependsOn");
            foreach (var term in terms)
            {
                using (var it = term.Node.Args.GetEnumerator())
                {
                    it.MoveNext();
                    var currFileName = ((Cnst)it.Current).GetStringValue();
                    currFileName = Path.GetFileNameWithoutExtension(currFileName);
                    it.MoveNext();

                    if (it.Current.NodeKind == NodeKind.Id)
                    {
                        var name = ((Id)it.Current).Name;
                        if (name == "NIL")
                        {
                            if (!dependsOn.ContainsKey(currFileName))
                            {
                                dependsOn[currFileName] = new List <string>();
                            }
                        }
                    }
                    else
                    {
                        var dOn = ((Cnst)it.Current).GetStringValue();
                        dOn = Path.GetFileNameWithoutExtension(dOn);

                        if (dependsOn.ContainsKey(currFileName))
                        {
                            dependsOn[currFileName].Add(dOn);
                        }
                        else
                        {
                            dependsOn[currFileName] = new List <string>();
                            dependsOn[currFileName].Add(dOn);
                        }
                    }
                }
            }

            terms = GetBin(factBins, "CSharpSafeMap");
            foreach (var term in terms)
            {
                using (var it = term.Node.Args.GetEnumerator())
                {
                    it.MoveNext();
                    var name = ((Cnst)it.Current).GetStringValue();
                    it.MoveNext();
                    var renamedMachineName = ((Cnst)it.Current).GetStringValue();
                    it.MoveNext();
                    var isSafe = (it.Current as Id).Name == PData.Cnst_True.Node.Name;

                    if (allTests.ContainsKey(name))
                    {
                        if (allTests[name].isSafeMap.ContainsKey(renamedMachineName))
                        {
                            Console.WriteLine("Internal Error");
                            Environment.Exit(-1);
                        }
                        else
                        {
                            allTests[name].isSafeMap.Add(renamedMachineName, isSafe);
                        }
                    }
                    else
                    {
                        allTests[name] = new TestCaseInfo();
                        allTests[name].isSafeMap.Add(renamedMachineName, isSafe);
                    }
                }
            }

            terms = GetBin(factBins, "CSharpMonitorMap");
            foreach (var term in terms)
            {
                using (var it = term.Node.Args.GetEnumerator())
                {
                    it.MoveNext();
                    var name = ((Cnst)it.Current).GetStringValue();
                    it.MoveNext();
                    var newSpecMachineName = ((Cnst)it.Current).GetStringValue();
                    it.MoveNext();
                    var impMachine = ((Cnst)it.Current).GetStringValue();

                    if (allTests.ContainsKey(name))
                    {
                        if (allTests[name].specMachineMap.ContainsKey(newSpecMachineName))
                        {
                            allTests[name].specMachineMap[newSpecMachineName].Add(impMachine);
                        }
                        else
                        {
                            allTests[name].specMachineMap[newSpecMachineName] = new List <string>();
                            allTests[name].specMachineMap[newSpecMachineName].Add(impMachine);
                        }
                    }
                    else
                    {
                        allTests[name] = new TestCaseInfo();
                        allTests[name].specMachineMap[newSpecMachineName] = new List <string>();
                        allTests[name].specMachineMap[newSpecMachineName].Add(impMachine);
                    }
                }
            }

            terms = GetBin(factBins, "CSharpInterfaceMap");
            foreach (var term in terms)
            {
                using (var it = term.Node.Args.GetEnumerator())
                {
                    it.MoveNext();
                    var name = ((Cnst)it.Current).GetStringValue();
                    it.MoveNext();
                    var iname = ((Cnst)it.Current).GetStringValue();
                    it.MoveNext();
                    var evname = it.Current is Cnst ? ((Cnst)it.Current).GetStringValue() : "halt";


                    var testInfo = new TestCaseInfo();
                    if (allTests.ContainsKey(name))
                    {
                        if (allTests[name].interfaceMap.ContainsKey(iname))
                        {
                            allTests[name].interfaceMap[iname].Add(evname);
                        }
                        else
                        {
                            allTests[name].interfaceMap[iname] = new List <string>();
                            allTests[name].interfaceMap[iname].Add(evname);
                        }
                    }
                    else
                    {
                        allTests[name] = new TestCaseInfo();
                        allTests[name].interfaceMap[iname] = new List <string>();
                        allTests[name].interfaceMap[iname].Add(evname);
                    }
                }
            }

            terms = GetBin(factBins, "CSharpEventActionsRefinementTest");
            foreach (var term in terms)
            {
                using (var it = term.Node.Args.GetEnumerator())
                {
                    it.MoveNext();
                    var name = ((Cnst)it.Current).GetStringValue();
                    it.MoveNext();
                    var eventName = ((Cnst)it.Current).GetStringValue();

                    var lhsName = string.Format("{0}LHS", name);
                    var rhsName = string.Format("{0}RHS", name);
                    allTests[lhsName].sendActions.Add(eventName);
                    allTests[rhsName].sendActions.Add(eventName);
                }
            }

            terms = GetBin(factBins, "CSharpInterfaceActionsRefinementTest");
            foreach (var term in terms)
            {
                using (var it = term.Node.Args.GetEnumerator())
                {
                    it.MoveNext();
                    var name = ((Cnst)it.Current).GetStringValue();
                    it.MoveNext();
                    var eventName = ((Cnst)it.Current).GetStringValue();

                    var lhsName = string.Format("{0}LHS", name);
                    var rhsName = string.Format("{0}RHS", name);
                    allTests[lhsName].createActions.Add(eventName);
                    allTests[rhsName].createActions.Add(eventName);
                }
            }
        }