示例#1
0
        public AbstractImageSpec WitnessCompose_TopParam(GrammarRule rule, AbstractImageSpec spec)
        {
            Program.DEBUG("Entered WitnessCompose_TopParam");
            var result = new Dictionary <State, object>();

            foreach (var example in spec.AbstractImageExamples)
            {
                State inputState = example.Key;
                var   output     = example.Value as AbstractImage;
                // TODO: Determine if this is a problem
                if (output.isEmptySet())
                {
                    return(null);
                }

                AbstractImage preimage = new AbstractImage(output.x, output.y, output.w, output.h);
                for (int ay = preimage.y; ay < preimage.y + preimage.h; ay++)
                {
                    for (int ax = preimage.x; ax < preimage.x + preimage.w; ax++)
                    {
                        AbstractValue od = output.getAbstractValueAtPixel(ax, ay);
                        preimage.setAbstractValueAtPixel(ax, ay, new AbstractValue(od.d).UnionWith(new AbstractValue(AbstractConstants.ZERO)));
                    }
                }
                result[inputState] = preimage;
            }
            return(new AbstractImageSpec(result));
        }
示例#2
0
        public AbstractImageSpec WitnessRecolor_SingleParam(GrammarRule rule, AbstractImageSpec spec, ExampleSpec colorSpec)
        {
            var result = new Dictionary <State, object>();

            foreach (var example in spec.AbstractImageExamples)
            {
                State inputState = example.Key;
                var   output     = example.Value as AbstractImage;
                int   color      = (int)colorSpec.Examples[inputState];
                // create blank preimage
                AbstractImage preimage = new AbstractImage(output.x, output.y, output.w, output.h);
                // loop through all pixels of output image
                for (int i = 0; i < output.abstract_data.Length; i++)
                {
                    ISet <int> colorSet = output.abstract_data[i].ToSet();

                    if (colorSet.Contains(0))
                    {
                        preimage.abstract_data[i].UnionWith(new AbstractValue(new List <int> {
                            0
                        }));
                    }
                    if (colorSet.Contains(color))
                    {
                        preimage.abstract_data[i].UnionWith(new AbstractValue(AbstractConstants.NONZERO));
                    }
                    if (preimage.abstract_data[i].IsEmpty()) // empty set (output not 0 or color)
                    {
                        return(null);
                    }
                }
                result[inputState] = preimage;
            }
            return(new AbstractImageSpec(result));
        }
    string Iterate(string axiom)
    {
        string result = "";

        for (int i = 0; i < axiom.Length; ++i)
        {
            GrammarRule rule = ruleDict[axiom[i].ToString()];
            float       prob = Random.Range(0.0f, 1.0f);

            int index = 0;

            for (int j = 0; j < rule.probabilities.Count; ++j)
            {
                if (prob <= rule.probabilities[j])
                {
                    index = j;
                    break;
                }
            }

            result += rule.transforms[index];
        }

        return(result);
    }
示例#4
0
        public DisjunctiveExamplesSpec WitnessRegexPair(GrammarRule rule, DisjunctiveExamplesSpec spec)
        {
            var result = new Dictionary <State, IEnumerable <object> >();

            foreach (KeyValuePair <State, IEnumerable <object> > example in spec.DisjunctiveExamples)
            {
                State inputState = example.Key;
                var   input      = inputState[rule.Body[0]] as string;

                var regexes = new List <Tuple <Regex, Regex> >();
                foreach (int output in example.Value)
                {
                    List <Regex>[] leftMatches, rightMatches;
                    BuildStringMatches(input, out leftMatches, out rightMatches);


                    List <Regex> leftRegex  = leftMatches[output];
                    List <Regex> rightRegex = rightMatches[output];
                    if (leftRegex.Count == 0 || rightRegex.Count == 0)
                    {
                        return(null);
                    }
                    regexes.AddRange(from l in leftRegex
                                     from r in rightRegex
                                     select Tuple.Create(l, r));
                }
                if (regexes.Count == 0)
                {
                    return(null);
                }
                result[inputState] = regexes;
            }
            return(DisjunctiveExamplesSpec.From(result));
        }
        public DisjunctiveExamplesSpec WitnessFirstElementM(GrammarRule rule, ExampleSpec spec)
        {
            var result = new Dictionary <State, IEnumerable <object> >();

            foreach (KeyValuePair <State, object> example in spec.Examples)
            {
                State inputState = example.Key;
                var   input      = (int[])inputState[rule.Body[0]];
                var   output     = (int)example.Value;
                if (output <= 1)
                {
                    continue;
                }
                var occurences = new List <int>();
                int i          = 0;
                while (i < input.Length)
                {
                    if (input[i] >= output || input[i] == 1)
                    {
                        i += 1;
                        continue;
                    }
                    occurences.Add(input[i]);
                    i += 1;
                }
                result[inputState] = occurences.Cast <object>();
            }
            return(new DisjunctiveExamplesSpec(result));
        }
示例#6
0
        public DisjunctiveExamplesSpec WitnessTwoAddendOne(GrammarRule rule,
                                                           DisjunctiveExamplesSpec spec)
        {
            var result = new Dictionary <State, IEnumerable <object> >();

            foreach (var example in spec.DisjunctiveExamples)
            {
                State inputState = example.Key;

                var outputs = example.Value;

                var possibleAddends = new List <int>();

                foreach (int output in outputs)
                {
                    int outputNumber = output;

                    for (int i = 1; i < outputNumber; i++)
                    {
                        possibleAddends.Add(i);
                    }
                }
                result[inputState] = possibleAddends.Cast <object>();
            }
            return(new DisjunctiveExamplesSpec(result));
        }
示例#7
0
        public ExampleSpec WitnessStartPosition(GrammarRule rule, ExampleSpec spec)
        {
            //a result of a witness function is a refined example-based specification
            //Each example is a map from an input state (State) to an output value (object)
            var result = new Dictionary <State, object>();

            //iterate over the input - output examples for the Substring operator
            foreach (KeyValuePair <State, object> example in spec.Examples)
            {
                //get the input state of the current example
                State inputState = example.Key;
                // the first parameter of Substring is the input variable 'v'
                // we extract its current bound value from the given input state
                // (e.g., "(19-Feb-1960)")
                var input = inputState[rule.Body[0]] as string;
                //Get the output value (e.g., "Feb")
                var output = example.Value as string;
                //now we deduce a spec on the first position of the substring
                ////////////////////////////////////////////////////////////////////////////////
                //TODO uncomment the following code fragment to complete this witness function//
                ////////////////////////////////////////////////////////////////////////////////
                //var refinedExample = input.IndexOf(output);
                //result[inputState] = refinedExample;
            }
            return(new ExampleSpec(result));
        }
示例#8
0
        public static DisjunctiveExamplesSpec WitnessRelativeToken(GrammarRule rule, int parameter, ExampleSpec spec)
        {
            var result = new Dictionary <State, IEnumerable <object> >();

            foreach (var input in spec.ProvidedInputs)
            {
                var example = spec.DisjunctiveExamples[input].First() as Tuple <PythonNode, PythonNode>;
                if (example != null)
                {
                    //if the anchor node is the target node, we don't generate relative paths
                    //just the root path "0"
                    if (AnchorAndTargetAreEqual(example))
                    {
                        return(null);
                    }
                    var node         = example.Item1;
                    var treeTemplate = new TreeTemplate(node.GetType().Name);
                    if (node.Value != null)
                    {
                        treeTemplate.Value = node.Value;
                    }

                    result[input] = new List <TreeTemplate>()
                    {
                        treeTemplate, new Wildcard(node.GetType().Name)
                    };
                }
                else
                {
                    return(null);
                }
            }
            return(DisjunctiveExamplesSpec.From(result));
        }
示例#9
0
        DisjunctiveExamplesSpec WitnessMult1(GrammarRule rule, DisjunctiveExamplesSpec spec)
        {
            if (bound == 0)
            {
                return(null);
            }
            var result      = new Dictionary <State, IEnumerable <object> >();
            var occurrences = new List <double[]>();

            foreach (var example in spec.DisjunctiveExamples)
            {
                State inputState = example.Key;
                // the first parameter of Substring is the variable symbol 'x'
                // we extract its current bound value from the given input state
                double[] M1 = (double[])inputState[rule.Body[1]];
                foreach (double[] M2 in example.Value)
                {
                    occurrences.Add(matMulInv(M2, M1));
                }
                result[inputState] = occurrences;
            }
            bound--;
            //Console.WriteLine(bound);
            return(new DisjunctiveExamplesSpec(result));
        }
        public DisjunctiveExamplesSpec WitnessFilterByProcessProcess(GrammarRule rule, ExampleSpec spec)
        {
            var examples = new Dictionary <State, IEnumerable <object> >();

            foreach (var input in spec.ProvidedInputs)
            {
                var before   = (Node)input[rule.Body[0]];
                var after    = (List <int>)spec.Examples[input];
                var children = before.GetProcesses();
                foreach (var child in children)
                {
                    if (Semantics.FilterByProcess(before, child).OrderBy(i => i)
                        .SequenceEqual(after.OrderBy(i => i)))
                    {
                        if (!examples.ContainsKey(input))
                        {
                            examples[input] = new List <string>();
                        }
                        ((List <string>)examples[input]).Add(child);
                    }
                }
            }

            return(new DisjunctiveExamplesSpec(examples));
        }
示例#11
0
        public DisjunctiveExamplesSpec WitnessStartPosition(GrammarRule rule, ExampleSpec spec)
        {
            var result = new Dictionary <State, IEnumerable <object> >();

            foreach (var example in spec.Examples)
            {
                State inputState  = example.Key;
                var   input       = inputState[rule.Body[0]] as string;
                var   output      = example.Value as string;
                var   occurrences = new List <int>();

                occurrences.Add(input.IndexOf(output));
                for (int i = input.IndexOf(output); i >= 0; i = input.IndexOf(output, i + 1))
                {
                    occurrences.Add(i);
                }

                if (occurrences.Count == 0)
                {
                    return(null);
                }
                result[inputState] = occurrences.Cast <object>();
            }
            return(new DisjunctiveExamplesSpec(result));
        }
示例#12
0
        public static DisjunctiveExamplesSpec WitnessRegexCount(GrammarRule rule, int parameter,
                                                                DisjunctiveExamplesSpec spec,
                                                                ExampleSpec regexBinding)
        {
            var kExamples = new Dictionary <State, IEnumerable <object> >();

            foreach (State input in spec.ProvidedInputs)
            {
                var v  = (StringRegion)input[rule.Body[0]];
                var rr = (Tuple <RegularExpression, RegularExpression>)regexBinding.Examples[input];
                var ks = new List <object>();
                foreach (uint pos in spec.DisjunctiveExamples[input])
                {
                    var ms    = rr.Item1.Run(v).Where(m => rr.Item2.MatchesAt(v, m.Right)).ToArray();
                    int index = ms.BinarySearchBy(m => m.Right.CompareTo(pos));
                    if (index < 0)
                    {
                        return(null);
                    }
                    ks.Add(index + 1);
                    ks.Add(index - ms.Length);
                }
                kExamples[input] = ks;
            }
            return(DisjunctiveExamplesSpec.From(kExamples));
        }
        public DisjunctiveExamplesSpec WitnessMultiplyStartPosition(GrammarRule rule, ExampleSpec spec)
        {
            var result = new Dictionary <State, IEnumerable <object> >();

            foreach (KeyValuePair <State, object> example in spec.Examples)
            {
                State inputState  = example.Key;
                var   input       = (Dictionary <uint?, uint?>)inputState[rule.Body[0]];
                var   output      = example.Value as uint?;
                var   occurrences = new List <uint?>();
                foreach (uint?i in input.Keys)
                {
                    if (i < output)
                    {
                        occurrences.Add(i);
                    }
                }
                if (occurrences.Count == 0)
                {
                    return(null);
                }
                result[inputState] = occurrences.Cast <object>();
            }
            return(new DisjunctiveExamplesSpec(result));
        }
示例#14
0
        public DisjunctiveExamplesSpec WitnessK(GrammarRule rule, DisjunctiveExamplesSpec spec)
        {
            var kExamples = new Dictionary <State, IEnumerable <object> >();

            foreach (var example in spec.DisjunctiveExamples)
            {
                State inputState = example.Key;
                var   v          = inputState[rule.Body[0]] as string;

                var positions = new List <int>();
                foreach (int pos in example.Value)
                {
                    //the positive spec for k
                    positions.Add(pos + 1);
                    //TODO add the negative spec for k
                    //uncomment the next statement and replace X by the expression that should return the negative spec
                    //positions.Add(X);
                }
                if (positions.Count == 0)
                {
                    return(null);
                }
                kExamples[inputState] = positions.Cast <object>();
            }
            return(DisjunctiveExamplesSpec.From(kExamples));
        }
示例#15
0
        public DisjunctiveExamplesSpec WitnessStartPosition(GrammarRule rule, ExampleSpec spec)
        {
            //the spec on the first position for each input state will have type IEnumerable<object> since we may have
            //more than one possible output
            var result = new Dictionary <State, IEnumerable <object> >();

            foreach (var example in spec.Examples)
            {
                State inputState  = example.Key;
                var   input       = inputState[rule.Body[0]] as string;
                var   output      = example.Value as string;
                var   occurrences = new List <int>();

                ///////////////////////////////////////////////////////////////////////
                //TODO replace the following line by the commented out for-loop bellow where we identify all start positions
                //and add each one to the occurrences list.
                ///////////////////////////////////////////////////////////////////////
                occurrences.Add(input.IndexOf(output));
                //for (int i = input.IndexOf(output); i >= 0; i = input.IndexOf(output, i + 1)) {
                //    occurrences.Add(i);
                //}

                if (occurrences.Count == 0)
                {
                    return(null);
                }
                result[inputState] = occurrences.Cast <object>();
            }
            return(new DisjunctiveExamplesSpec(result));
        }
        public DisjunctiveExamplesSpec WitnessK(GrammarRule rule, DisjunctiveExamplesSpec spec)
        {
            var result = new Dictionary <State, IEnumerable <object> >();

            foreach (var example in spec.DisjunctiveExamples)
            {
                State inputState = example.Key;
                var   v          = inputState[rule.Body[0]] as string;

                var positions = new List <int>();

                foreach (int pos in example.Value)
                {
                    positions.Add(pos + 1);
                    positions.Add(pos - v.Length - 1);
                }

                if (positions.IsEmpty())
                {
                    return(null);
                }

                result[inputState] = positions.Cast <object>();
            }

            return(new DisjunctiveExamplesSpec(result));
        }
示例#17
0
        [WitnessFunction(nameof(Semantics.Substring), parameterSymbolName: "startPos", DependsOnParameters = new[] { 0, 2 })]// <- name nach .grammar-Datei erwartet. So richtig!
        DisjunctiveExamplesSpec WitnessSubstring_startPos(GrammarRule rule, ExampleSpec spec, ExampleSpec spec0, ExampleSpec spec2)
        {
            var innerSpec = new Dictionary <State, IEnumerable <object> >();

            foreach (var example in spec.Examples)
            {
                //Get the output of this operator
                var operatorOutput = (string)example.Value;
                var operatorInputs = example.Key;
                var completeString = (string)operatorInputs[rule.Body[0]]; //gets the first inputparameter, here string sr

                //Find all possible start-positions of operatoroutput in completestring
                var possibleStartPositions = new List <int?>();
                for (int i = completeString.IndexOf(operatorOutput); i >= 0; i = completeString.IndexOf(operatorOutput, i + 1))
                {
                    possibleStartPositions.Add(i);
                }
                if (possibleStartPositions.Count == 0)
                {
                    return(null);
                }
                innerSpec[operatorInputs] = possibleStartPositions.Cast <object>();
            }
            return(new DisjunctiveExamplesSpec(innerSpec));
        }
示例#18
0
        public static PrefixSpec WitnessLinesMap(GrammarRule rule, int parameter,
                                                 PrefixSpec spec)
        {
            var linesExamples = new Dictionary <State, IEnumerable <object> >();

            foreach (State input in spec.ProvidedInputs)
            {
                var document        = ((StringRegion)input[rule.Grammar.InputSymbol]);
                var selectionPrefix = spec.Examples[input].Cast <StringRegion>();

                var linesContainingSelection = new List <StringRegion>();
                foreach (StringRegion example in selectionPrefix)
                {
                    var startLine = GetLine(document, example.Start);
                    var endLine   = GetLine(document, example.End);
                    if (startLine == null || endLine == null || startLine != endLine)
                    {
                        return(null);
                    }
                    linesContainingSelection.Add(startLine);
                }

                linesExamples[input] = linesContainingSelection;
            }
            return(new PrefixSpec(linesExamples));
        }
示例#19
0
        DisjunctiveExamplesSpec WitnessRegexFunction_iTH(GrammarRule rule, ExampleSpec spec, ExampleSpec regexSpec)
        {
            var result = new Dictionary <State, IEnumerable <object> >();

            // Die erste Spec ist die "äußere spec", die die beispiele enthält
            foreach (var example in spec.Examples)
            {
                var functionInput  = (string)example.Key[rule.Body[2]]; //TODO funktioniert das so? 0 -> der index zeigt die position in der ganzen grammatik an, und 0 ist der input. oder muss hier eine 2 hin? -> index zeigt die position in der parameterliste an. TESTEN!
                var regex          = new Regex("");                     // (Regex)regexSpec.Examples[example.Key];
                var completeString = "";
                var programOutput  = (string)example.Value;             // TODO ?

                //TODO match regex to input and write back, which match-indices have a match, where the output starts, too
                var possibleNs = new HashSet <int>();
                var matches    = regex.Matches(functionInput);

                var possibleStartPositions = new List <int?>();
                for (int i = functionInput.IndexOf(programOutput); i >= 0; i = completeString.IndexOf(programOutput, i + 1))
                {
                    possibleStartPositions.Add(i);
                }


                if (possibleNs.Count == 0)
                {
                    return(null);
                }
                result[example.Key] = possibleNs.Cast <object>();
            }
            return(new DisjunctiveExamplesSpec(result));
        }
示例#20
0
        public static ExampleSpec WitnessContextPath(GrammarRule rule, int parameter,
                                                     DisjunctiveExamplesSpec spec, ExampleSpec dSpec)
        {
            var result = new Dictionary <State, object>();

            foreach (var input in spec.ProvidedInputs)
            {
                var outerSpec     = spec.DisjunctiveExamples[input] as IEnumerable <Tuple <int, PythonNode> >;
                var dependentSpec = (int)dSpec.Examples[input];
                if (outerSpec != null)
                {
                    if (dependentSpec == 0)
                    {
                        result[input] = 0;
                    }
                    else
                    {
                        var node = outerSpec.Where(e => e.Item1 == 0).First().Item2;
                        result[input] = node.Parent.Children.IndexOf(node) + 1;
                    }
                }
                else
                {
                    return(null);
                }
            }
            return(new ExampleSpec(result));
        }
        public ExampleSpec WitnessFlipComparisonSymbol(GrammarRule rule, ExampleSpec spec)
        {
            var examples = new Dictionary <State, object>();

            foreach (var input in spec.ProvidedInputs)
            {
                var before = (Node)input[rule.Body[0]];
                var after  = (Node)spec.Examples[input];

                var flippable = new List <string>()
                {
                    "<",
                    ">",
                    "<=",
                    ">="
                };

                foreach (var symbol in flippable)
                {
                    if (Semantics.FlipComparison(before, symbol, true).IsEqualTo(after))
                    {
                        examples[input] = symbol;
                    }
                }
            }

            return(new ExampleSpec(examples));
        }
示例#22
0
        DisjunctiveExamplesSpec WitnessCdr(GrammarRule rule, DisjunctiveExamplesSpec spec)
        {
            var result = new Dictionary <State, IEnumerable <object> >();

            foreach (var example in spec.DisjunctiveExamples)
            {
                State inputState = example.Key;
                Cons  input      = inputState[rule.Grammar.Symbol("x")] as Cons;
                if (input == null)
                {
                    return(null);
                }
                var l = new List <object>();
                foreach (Semantics.SExp output in example.Value)
                {
                    l.AddRange(findAll(null, input, output, Semantics.Semantics.Cdr));
                }
                if (l.Count == 0)
                {
                    return(null);
                }
                result[inputState] = l;
            }
            return(new DisjunctiveExamplesSpec(result));
        }
示例#23
0
        public static ExampleSpec WitnessPatch(GrammarRule rule, int parameter,
                                               ExampleSpec spec)
        {
            var examples = new Dictionary <State, object>();

            foreach (State input in spec.ProvidedInputs)
            {
                var before = (PythonNode)input[rule.Body[0]];
                var after  = (PythonNode)spec.Examples[input];

                var zss          = new PythonZss(before, after);
                var editDistance = zss.Compute();

                var rootAndNonRootEdits = SplitEditsByRootsAndNonRoots(editDistance);
                var edits = ExtractPrimaryEdits(rootAndNonRootEdits, editDistance);

                //todo fix some big examples that are not working
                //if (edits.Count > 18)
                //    return null;

                var patch = new Patch();
                edits.ForEach(e => patch.EditSets.Add(new List <Edit>()
                {
                    e
                }));
                examples[input] = patch;
            }
            return(new ExampleSpec(examples));
        }
        internal DisjunctiveExamplesSpec WitnessCheck(GrammarRule rule, DisjunctiveExamplesSpec spec)
        {
            var result = new Dictionary <State, IEnumerable <object> >();

            foreach (KeyValuePair <State, IEnumerable <object> > example in spec.DisjunctiveExamples)
            {
                State        inputState   = example.Key;
                List <int[]> predicateInt = new List <int[]>();
                var          duplicate    = example.Key[rule.Body[0]] as List <IReadOnlyList <Node> >;
                List <int>   temp         = new List <int>();
                for (int i = 0; i < duplicate.Count; i++)
                {
                    if (duplicate[i].Count > 0)
                    {
                        temp.Add(i);
                    }
                }
                int[] tempArr  = new int[temp.Count];
                int   countInt = 0;
                foreach (int a in temp)
                {
                    tempArr[countInt] = a;
                    countInt++;
                }
                predicateInt.Add(tempArr);
                result[inputState] = predicateInt;
            }
            return(DisjunctiveExamplesSpec.From(result));
        }
示例#25
0
        public DisjunctiveExamplesSpec WitnessAddendTwo(GrammarRule rule,
                                                        DisjunctiveExamplesSpec spec, ExampleSpec ySpec)
        {
            var result = new Dictionary <State, IEnumerable <object> >();

            foreach (var examples in spec.DisjunctiveExamples)
            {
                State inputState = examples.Key;

                var outputs = examples.Value;

                var possibleAddends = new List <int>();

                foreach (int output in outputs)
                {
                    int outputNumber = output;

                    var yExamples = ySpec.DisjunctiveExamples.Values;

                    foreach (var example in yExamples)
                    {
                        foreach (int paramOne in example)
                        {
                            int addend = output - paramOne;

                            possibleAddends.Add(addend);
                        }
                    }
                }
                result[inputState] = possibleAddends.Cast <object>();
            }
            return(new DisjunctiveExamplesSpec(result));
        }
示例#26
0
        internal DisjunctiveExamplesSpec WitnessSelectWithWhereCondition(GrammarRule rule, ExampleSpec spec)
        {
            var ppExamples = new Dictionary <State, IEnumerable <object> >();

            foreach (State input in spec.ProvidedInputs)
            {
                var        inputTable           = ((DataTable[])input[rule.Grammar.InputSymbol])[0];
                var        outputTable          = (DataTable)spec.Examples[input];
                var        allPossibleSolutions = new List <object>();
                var        selectQuery          = outputTable.PrimaryKey.Select(c => c.ColumnName).ToArray();
                var        completeRowsList     = new List <DataRow[]>();
                DataView   view            = new DataView(outputTable);
                List <int> outputCountList = new List <int>();
                DataTable  distinctValues  = view.ToTable(true, selectQuery);
                foreach (DataRow row in distinctValues.Rows)
                {
                    var query = string.Join(" AND ", selectQuery.Select(c => (inputTable.Columns[c].DataType == typeof(string))? $"{c}='{row[c]}'":$"{c}={row[c]}"));
                    completeRowsList.Add(inputTable.Select(query));
                    outputCountList.Add(outputTable.Select(query).Count());
                }
                var conditionTables = Utils.Utils.GetAllPossibleTables(completeRowsList, outputCountList, inputTable);
                allPossibleSolutions.AddRange(conditionTables);
                ppExamples[input] = allPossibleSolutions;
            }
            //Complete the rows
            return(DisjunctiveExamplesSpec.From(ppExamples));
        }
示例#27
0
        internal DisjunctiveExamplesSpec WitnessRegexPair(GrammarRule rule, DisjunctiveExamplesSpec spec)
        {
            var rrExamples = new Dictionary <State, IEnumerable <object> >();

            foreach (State input in spec.ProvidedInputs)
            {
                var v       = (StringRegion)input[rule.Body[0]];
                var regexes = new List <object>();
                foreach (uint pos in spec.DisjunctiveExamples[input])
                {
                    UnboundedMultiValueCache <Token, TokenMatch> rightMatches;
                    if (!v.Cache.TryGetAllMatchesStartingAt(pos, out rightMatches))
                    {
                        continue;
                    }
                    UnboundedMultiValueCache <Token, TokenMatch> leftMatches;
                    if (!v.Cache.TryGetAllMatchesEndingAt(pos, out leftMatches))
                    {
                        continue;
                    }
                    var leftRegexes  = RegularExpression.LearnLeftMatches(v, pos, RegularExpression.DefaultTokenCount);
                    var rightRegexes = RegularExpression.LearnRightMatches(v, pos, RegularExpression.DefaultTokenCount);
                    var regexPairs   =
                        from l in leftRegexes from r in rightRegexes select(object) ValueTuple.Create(l, r);

                    regexes.AddRange(regexPairs);
                }
                rrExamples[input] = regexes;
            }
            return(DisjunctiveExamplesSpec.From(rrExamples));
        }
示例#28
0
        public static DisjunctiveExamplesSpec WitnessContext(GrammarRule rule, int parameter, ExampleSpec spec)
        {
            var templateExamples = new Dictionary <State, IEnumerable <object> >();

            foreach (var input in spec.ProvidedInputs)
            {
                var node = spec.Examples[input] as PythonNode;
                var ast  = (PythonNode)input[rule.Body[0]];
                if (ast.ContainsByBinding(node))
                {
                    node = ast.GetCorrespondingNodeByBinding(node);
                }
                else
                {
                    return(null);
                }
                var innerSpec = new List <Tuple <int, PythonNode> >()
                {
                    Tuple.Create(0, node)
                };
                if (node.Parent != null)
                {
                    innerSpec.Add(Tuple.Create(1, node));
                }
                templateExamples[input] = innerSpec;
            }
            return(new DisjunctiveExamplesSpec(templateExamples));
        }
示例#29
0
        public static DisjunctiveExamplesSpec WitnessRegexPair(GrammarRule rule, int parameter,
                                                               DisjunctiveExamplesSpec spec)
        {
            var rrExamples = new Dictionary <State, IEnumerable <object> >();

            foreach (State input in spec.ProvidedInputs)
            {
                var v       = (StringRegion)input[rule.Body[0]];
                var regexes = new List <Tuple <RegularExpression, RegularExpression> >();
                foreach (uint pos in spec.DisjunctiveExamples[input])
                {
                    Dictionary <Token, TokenMatch> rightMatches;
                    if (!v.Cache.TryGetAllMatchesStartingAt(pos, out rightMatches))
                    {
                        continue;
                    }
                    Dictionary <Token, TokenMatch> leftMatches;
                    if (!v.Cache.TryGetAllMatchesEndingAt(pos, out leftMatches))
                    {
                        continue;
                    }
                    var leftRegexes  = leftMatches.Keys.Select(RegularExpression.Create).Append(Epsilon);
                    var rightRegexes = rightMatches.Keys.Select(RegularExpression.Create).Append(Epsilon);
                    var regexPairs   = from l in leftRegexes from r in rightRegexes select Tuple.Create(l, r);

                    regexes.AddRange(regexPairs);
                }
                rrExamples[input] = regexes;
            }
            return(DisjunctiveExamplesSpec.From(rrExamples));
        }
        public static DisjunctiveExamplesSpec WitnessRegexPair(GrammarRule rule, int parameter, ExampleSpec spec)
        {
            var result = new Dictionary <State, IEnumerable <object> >();

            foreach (var example in spec.Examples)
            {
                State inputState = example.Key;
                var   input      = inputState[rule.Body[0]] as string;
                var   output     = (int)example.Value;

                List <Tuple <Match, Regex> >[] leftMatches, rightMatches;
                BuildStringMatches(input, out leftMatches, out rightMatches);

                var leftRegex  = leftMatches[output];
                var rightRegex = rightMatches[output];
                if (leftRegex.Count == 0 || rightRegex.Count == 0)
                {
                    return(null);
                }
                var regexes = new List <Tuple <Regex, Regex> >();
                regexes.AddRange(from l in leftRegex
                                 from r in rightRegex
                                 select Tuple.Create(l.Item2, r.Item2));
                result[inputState] = regexes;
            }
            return(DisjunctiveExamplesSpec.From(result));
        }
示例#31
0
        public void HasAnOptionallyProvidedName()
        {
            var unnamed = new GrammarRule<string>();
            var named = new GrammarRule<string>("Named");

            unnamed.Name.ShouldBeNull();
            named.Name.ShouldEqual("Named");
        }
示例#32
0
        public void ProvidesAdviceWhenRuleIsUsedBeforeBeingInitialized()
        {
            var tokens = new CharLexer().Tokenize("123").ToArray();
            var numeric = new GrammarRule<string>();
            var alpha = new GrammarRule<string>("Alpha");

            numeric.FailsToParse(tokens).WithMessage("(1, 1): An anonymous GrammarRule has not been initialized.  Try setting the Rule property.");
            alpha.FailsToParse(tokens).WithMessage("(1, 1): GrammarRule 'Alpha' has not been initialized.  Try setting the Rule property.");
        }
示例#33
0
        public void CanDefineMutuallyRecursiveRules()
        {
            var tokens = new CharLexer().Tokenize("(A)");
            var expression = new GrammarRule<string>();
            var alpha = new GrammarRule<string>();
            var parenthesizedExpresion = new GrammarRule<string>();

            expression.Rule = Choice(alpha, parenthesizedExpresion);
            alpha.Rule = from a in Token("A") select a.Literal;
            parenthesizedExpresion.Rule = Between(Token("("), expression, Token(")"));

            expression.Parses(tokens).WithValue("A");
        }
示例#34
0
 public static DisjunctiveExamplesSpec WitnessK(GrammarRule rule, int parameter,
                                                DisjunctiveExamplesSpec spec)
 {
     var kExamples = new Dictionary<State, IEnumerable<object>>();
     foreach (State input in spec.ProvidedInputs)
     {
         var v = (StringRegion) input[rule.Body[0]];
         var positions = new List<object>();
         foreach (uint pos in spec.DisjunctiveExamples[input])
         {
             positions.Add((int) pos + 1 - (int) v.Start);
             positions.Add((int) pos - (int) v.End - 1);
         }
         kExamples[input] = positions;
     }
     return DisjunctiveExamplesSpec.From(kExamples);
 }
 static DisjunctiveExamplesSpec WitnessPositionPair(GrammarRule rule, int parameter, ExampleSpec spec)
 {
     var result = new Dictionary<State, IEnumerable<object>>();
     foreach (var example in spec.Examples)
     {
         State inputState = example.Key;
         var inp = (string)inputState[rule.Body[0]];
         var substring = (string)example.Value;
         var occurrences = new List<Tuple<int?, int?>>();
         for (int i = inp.IndexOf(substring); i >= 0; i = inp.IndexOf(substring, i + 1))
         {
             occurrences.Add(Tuple.Create((int?)i, (int?)i + substring.Length));
         }
         if (occurrences.Count == 0) return null;
         result[inputState] = occurrences;
     }
     return new DisjunctiveExamplesSpec(result);
 }
 static DisjunctiveExamplesSpec WitnessK(GrammarRule rule, int parameter, DisjunctiveExamplesSpec spec)
 {
     var result = new Dictionary<State, IEnumerable<object>>();
     foreach (var example in spec.DisjunctiveExamples)
     {
         State inputState = example.Key;
         var ks = new HashSet<int?>();
         var inp = (string)inputState[rule.Body[0]];
         foreach (int? pos in example.Value)
         {
             ks.Add(pos);
             ks.Add(pos - inp.Length - 1);
         }
         if (ks.Count == 0) return null;
         result[inputState] = ks.Cast<object>();
     }
     return new DisjunctiveExamplesSpec(result);
 }
示例#37
0
 public static DisjunctiveExamplesSpec WitnessPositionPair(GrammarRule rule, int parameter,
                                                           ExampleSpec spec)
 {
     var ppExamples = new Dictionary<State, IEnumerable<object>>();
     foreach (State input in spec.ProvidedInputs)
     {
         var v = (StringRegion) input[rule.Body[0]];
         var desiredOutput = (StringRegion) spec.Examples[input];
         var occurrences = new List<object>();
         for (int i = v.Value.IndexOf(desiredOutput.Value, StringComparison.Ordinal);
              i >= 0;
              i = v.Value.IndexOf(desiredOutput.Value, i + 1, StringComparison.Ordinal))
         {
             occurrences.Add(Tuple.Create(v.Start + (uint?) i, v.Start + (uint?) i + desiredOutput.Length));
         }
         ppExamples[input] = occurrences;
     }
     return DisjunctiveExamplesSpec.From(ppExamples);
 }
 static DisjunctiveExamplesSpec WitnessRegexPair(GrammarRule rule, int parameter,  DisjunctiveExamplesSpec spec)
 {
     var result = new Dictionary<State, IEnumerable<object>>();
     foreach (var example in spec.DisjunctiveExamples)
     {
         State inputState = example.Key;
         var inp = (string)inputState[rule.Body[0]];
         List<Tuple<Match, Regex>>[] leftMatches, rightMatches;
         BuildStringMatches(inp, out leftMatches, out rightMatches);
         var regexes = new List<Tuple<Regex, Regex>>();
         foreach (int? pos in example.Value)
         {
             regexes.AddRange(from l in leftMatches[pos.Value]
                              from r in rightMatches[pos.Value]
                              select Tuple.Create(l.Item2, r.Item2));
         }
         if (regexes.Count == 0) return null;
         result[inputState] = regexes;
     }
     return new DisjunctiveExamplesSpec(result);
 }
示例#39
0
 public static DisjunctiveExamplesSpec WitnessRegexCount(GrammarRule rule, int parameter,
                                                         DisjunctiveExamplesSpec spec,
                                                         ExampleSpec regexBinding)
 {
     var kExamples = new Dictionary<State, IEnumerable<object>>();
     foreach (State input in spec.ProvidedInputs)
     {
         var v = (StringRegion) input[rule.Body[0]];
         var rr = (Tuple<RegularExpression, RegularExpression>) regexBinding.Examples[input];
         var ks = new List<object>();
         foreach (uint pos in spec.DisjunctiveExamples[input])
         {
             var ms = rr.Item1.Run(v).Where(m => rr.Item2.MatchesAt(v, m.Right)).ToArray();
             int index = ms.BinarySearchBy(m => m.Right.CompareTo(pos));
             if (index < 0) return null;
             ks.Add(index + 1);
             ks.Add(index - ms.Length);
         }
         kExamples[input] = ks;
     }
     return DisjunctiveExamplesSpec.From(kExamples);
 }
示例#40
0
 public static DisjunctiveExamplesSpec WitnessRegexPair(GrammarRule rule, int parameter,
                                                        DisjunctiveExamplesSpec spec)
 {
     var rrExamples = new Dictionary<State, IEnumerable<object>>();
     foreach (State input in spec.ProvidedInputs)
     {
         var v = (StringRegion) input[rule.Body[0]];
         var regexes = new List<Tuple<RegularExpression, RegularExpression>>();
         foreach (uint pos in spec.DisjunctiveExamples[input])
         {
             Dictionary<Token, TokenMatch> rightMatches;
             if (!v.Cache.TryGetAllMatchesStartingAt(pos, out rightMatches)) continue;
             Dictionary<Token, TokenMatch> leftMatches;
             if (!v.Cache.TryGetAllMatchesEndingAt(pos, out leftMatches)) continue;
             var leftRegexes = leftMatches.Keys.Select(RegularExpression.Create).Append(Epsilon);
             var rightRegexes = rightMatches.Keys.Select(RegularExpression.Create).Append(Epsilon);
             var regexPairs = from l in leftRegexes from r in rightRegexes select Tuple.Create(l, r);
             regexes.AddRange(regexPairs);
         }
         rrExamples[input] = regexes;
     }
     return DisjunctiveExamplesSpec.From(rrExamples);
 }
示例#41
0
        public static PrefixSpec WitnessLinesMap(GrammarRule rule, int parameter,
                                                 PrefixSpec spec)
        {
            var linesExamples = new Dictionary<State, IEnumerable<object>>();
            foreach (State input in spec.ProvidedInputs)
            {
                var document = ((StringRegion) input[rule.Grammar.InputSymbol]);
                var selectionPrefix = spec.Examples[input].Cast<StringRegion>();

                var linesContainingSelection = new List<StringRegion>();
                foreach (StringRegion example in selectionPrefix)
                {
                    var startLine = GetLine(document, example.Start);
                    var endLine = GetLine(document, example.End);
                    if (startLine == null || endLine == null || startLine != endLine)
                        return null;
                    linesContainingSelection.Add(startLine);
                }

                linesExamples[input] = linesContainingSelection;
            }
            return new PrefixSpec(linesExamples);
        }
示例#42
0
文件: Item.cs 项目: paulochang/LR0
 public Item(GrammarRule theRule, int thePosition)
 {
     Rule = theRule;
     Position = thePosition;
 }
示例#43
0
文件: Item.cs 项目: paulochang/LR0
 public Item(GrammarRule theRule)
 {
     Rule = theRule;
     Position = 0;
 }
示例#44
0
        public GrammarRuleNameInferenceTests()
        {
            AlreadyNamedRule = new GrammarRule<int>("This name is not inferred.");
            PublicStaticRule = new GrammarRule<object>();
            PrivateStaticRule = new GrammarRule<string>();
            PublicInstanceRule = new GrammarRule<int>();
            PrivateInstanceRule = new GrammarRule<int>();
            NullRule = null;

            InferGrammarRuleNames();
        }
 static DisjunctiveExamplesSpec WitnessKForRegexPair(GrammarRule rule, int parameter,  DisjunctiveExamplesSpec spec, ExampleSpec rrSpec)
 {
     var result = new Dictionary<State, IEnumerable<object>>();
     foreach (var example in spec.DisjunctiveExamples)
     {
         State inputState = example.Key;
         var inp = (string)inputState[rule.Body[0]];
         var regexPair = (Tuple<Regex, Regex>)rrSpec.Examples[inputState];
         Regex left = regexPair.Item1;
         Regex right = regexPair.Item2;
         var rightMatches = right.Matches(inp).Cast<Match>().ToDictionary(m => m.Index);
         var matchPositions = new List<int>();
         foreach (Match m in left.Matches(inp))
         {
             if (rightMatches.ContainsKey(m.Index + m.Length))
                 matchPositions.Add(m.Index + m.Length);
         }
         var ks = new HashSet<int?>();
         foreach (int? pos in example.Value)
         {
             int occurrence = matchPositions.BinarySearch(pos.Value);
             if (occurrence < 0) continue;
             ks.Add(occurrence);
             ks.Add(occurrence - matchPositions.Count);
         }
         if (ks.Count == 0) return null;
         result[inputState] = ks.Cast<object>();
     }
     return new DisjunctiveExamplesSpec(result);
 }
示例#46
0
        private IEnumerable<ILexer> GetMatches(ILexer lexer, GrammarRule rule, int startElement = 0)
        {
            if (lexer.Current == null)
                yield break;

            var element = rule.Elements[startElement];

            if (rule.Elements.Length - 1 == startElement)
            {
                // Last element
                foreach (var match in element.Match(this, lexer))
                    yield return match;
            }
            else
            {
                // Not last element
                foreach (var match in element.Match(this, lexer))
                {
                    if (match.Current == null)
                        continue;

                    foreach (var match2 in GetMatches(match, rule, startElement + 1))
                        yield return match2;
                }
            }
        }