コード例 #1
0
        private bool IsConsistentWithPreviousResult(InterpretTrace trace, bool checkStart, HashSet <IToken> definitions, HashSet <IToken> references)
        {
            Contract.Requires(trace != null);
            Contract.Requires(definitions != null);
            Contract.Requires(references != null);

            InterpretTraceTransition transition = checkStart ? trace.Transitions.First.Value : trace.Transitions.Last.Value;
            IToken token = transition.Token;

            if (definitions.Contains(token) && !references.Contains(token))
            {
                if (transition.Interpreter.Network.StateRules[transition.Transition.SourceState.Id].Name != GoSimplifiedAtnBuilder.RuleNames.SymbolDefinitionIdentifier)
                {
                    return(false);
                }
            }
            else if (references.Contains(token) && !definitions.Contains(token))
            {
                if (transition.Interpreter.Network.StateRules[transition.Transition.SourceState.Id].Name != GoSimplifiedAtnBuilder.RuleNames.SymbolReferenceIdentifier)
                {
                    return(false);
                }
            }

            return(true);
        }
コード例 #2
0
        private List <string> AnalyzeInterpreterTrace(InterpretTrace context, RuleBinding memberSelectRule, out Span span)
        {
            var           transitions     = context.Transitions;
            int           expressionLevel = 0;
            List <string> results         = new List <string>();

            string identifier = null;

            span = new Span();

            foreach (var transition in transitions)
            {
                if (transition.Transition.IsMatch)
                {
                    switch (transition.Symbol.Value)
                    {
                    case AlloyLexer.IDENTIFIER:
                    case AlloyLexer.KW_THIS:
                    case AlloyLexer.KW_UNIV:
                    case AlloyLexer.KW_IDEN:
                    case AlloyLexer.KW_INT2:
                    case AlloyLexer.KW_SEQINT:
                    case AlloyLexer.INTEGER:
                        if (expressionLevel == 0)
                        {
                            IToken token = transition.Token;
                            identifier = token.Text;
                            span       = new Span(token.StartIndex, token.StopIndex - token.StartIndex + 1);
                        }

                        break;

                    case AlloyLexer.DOT:
                        if (expressionLevel == 0)
                        {
                            for (int i = 0; i < results.Count; i++)
                            {
                                results[i] += identifier + ".";
                                identifier  = null;
                            }
                        }

                        break;
                    }
                }
                else if (transition.Transition.IsContext)
                {
                    // only need to handle it if we're entering BinOpExpr18
                    PushContextTransition pushContextTransition = transition.Transition as PushContextTransition;
                    if (pushContextTransition != null && pushContextTransition.TargetState == memberSelectRule.StartState)
                    {
                        expressionLevel++;
                    }

                    PopContextTransition popContextTransition = transition.Transition as PopContextTransition;
                    if (popContextTransition != null && popContextTransition.SourceState == memberSelectRule.EndState)
                    {
                        if (expressionLevel > 0)
                        {
                            expressionLevel--;
                        }
                    }
                }
                else if (transition.Transition.IsEpsilon)
                {
                    if (expressionLevel == 0 && transition.Transition.SourceState == memberSelectRule.StartState)
                    {
                        results.Clear();
                        results.Add("<context>::");
                    }
                }
            }

            if (identifier != null)
            {
                for (int i = 0; i < results.Count; i++)
                {
                    results[i] += "[" + identifier + "]";
                }
            }

            return(results);
        }
コード例 #3
0
        private void StepForward(ICollection<InterpretTrace> result, ICollection<int> states, InterpretTrace context, int symbol, int symbolPosition, PreventContextType preventContextType)
        {
            foreach (var transition in context.EndContext.State.OutgoingTransitions)
            {
                if (transition.IsContext)
                {
                    PopContextTransition popContextTransition = transition as PopContextTransition;
                    if (popContextTransition != null && context.EndContext.Parent != null)
                    {
                        if (popContextTransition.ContextIdentifiers[0] != context.EndContext.Parent.Context)
                            continue;
                    }

                    switch (preventContextType)
                    {
                    case PreventContextType.Pop:
                        if (!transition.IsRecursive && (transition is PopContextTransition))
                            continue;

                        break;

                    case PreventContextType.PopRecursive:
                        if (transition.IsRecursive && (transition is PopContextTransition))
                            continue;

                        break;

                    case PreventContextType.Push:
                        if (!transition.IsRecursive && (transition is PushContextTransition))
                            continue;

                        break;

                    case PreventContextType.PushRecursive:
                        if (transition.IsRecursive && (transition is PushContextTransition))
                            continue;

                        break;

                    default:
                        break;
                    }
                }

                InterpretTrace step;
                if (context.TryStepForward(transition, symbol, symbolPosition, out step))
                {
                    if (transition.IsMatch)
                    {
                        result.Add(step);
                        continue;
                    }

                    bool recursive = transition.TargetState.IsForwardRecursive;
                    bool addRecursive = false;
                    if (recursive)
                    {
                        addRecursive = !states.Contains(transition.TargetState.Id);
                        if (!addRecursive && (!(transition is PopContextTransition) || context.EndContext.Parent == null))
                            continue;
                    }

                    if (addRecursive)
                        states.Add(transition.TargetState.Id);

                    PreventContextType nextPreventContextType = PreventContextType.None;
                    if (context.EndContext.State.IsOptimized)
                    {
                        if (transition is PushContextTransition)
                            nextPreventContextType = transition.IsRecursive ? PreventContextType.PushRecursive : PreventContextType.Push;
                        else if (transition is PopContextTransition)
                            nextPreventContextType = transition.IsRecursive ? PreventContextType.PopRecursive : PreventContextType.Pop;
                    }

                    StepForward(result, states, step, symbol, symbolPosition, nextPreventContextType);

                    if (addRecursive)
                        states.Remove(transition.TargetState.Id);
                }
            }
        }
コード例 #4
0
        private void StepBackward(ICollection<InterpretTrace> result, ICollection<int> states, InterpretTrace context, int symbol, int symbolPosition, PreventContextType preventContextType)
        {
            foreach (var transition in context.StartContext.State.IncomingTransitions)
            {
                if (transition.SourceState.IsOptimized)
                {
                    switch (preventContextType)
                    {
                    case PreventContextType.Pop:
                        if (!transition.IsRecursive && (transition is PopContextTransition))
                            continue;

                        break;

                    case PreventContextType.PopRecursive:
                        if (transition.IsRecursive && (transition is PopContextTransition))
                            continue;

                        break;

                    case PreventContextType.Push:
                        if (!transition.IsRecursive && (transition is PushContextTransition))
                            continue;

                        break;

                    case PreventContextType.PushRecursive:
                        if (transition.IsRecursive && (transition is PushContextTransition))
                            continue;

                        break;

                    default:
                        break;
                    }
                }

                InterpretTrace step;
                if (context.TryStepBackward(transition, symbol, symbolPosition, out step))
                {
                    if (transition.IsMatch)
                    {
                        result.Add(step);
                        continue;
                    }

                    bool recursive = transition.SourceState.IsBackwardRecursive;
                    if (recursive && states.Contains(transition.SourceState.Id))
                    {
                        // TODO: check postfix rule
                        continue;
                    }

                    if (recursive)
                        states.Add(transition.SourceState.Id);

                    PreventContextType nextPreventContextType = PreventContextType.None;
                    if (context.StartContext.State.IsOptimized)
                    {
                        if (transition is PushContextTransition)
                            nextPreventContextType = transition.IsRecursive ? PreventContextType.PushRecursive : PreventContextType.Push;
                        else if (transition is PopContextTransition)
                            nextPreventContextType = transition.IsRecursive ? PreventContextType.PopRecursive : PreventContextType.Pop;
                    }

                    StepBackward(result, states, step, symbol, symbolPosition, nextPreventContextType);

                    if (recursive)
                        states.Remove(transition.SourceState.Id);
                }
            }
        }
コード例 #5
0
        private List<string> AnalyzeInterpreterTrace(InterpretTrace context, RuleBinding memberSelectRule, out Span span)
        {
            var transitions = context.Transitions;
            int expressionLevel = 0;
            List<string> results = new List<string>();

            string identifier = null;
            span = new Span();

            foreach (var transition in transitions)
            {
                if (transition.Transition.IsMatch)
                {
                    switch (transition.Symbol.Value)
                    {
                    case AlloyLexer.IDENTIFIER:
                    case AlloyLexer.KW_THIS:
                    case AlloyLexer.KW_UNIV:
                    case AlloyLexer.KW_IDEN:
                    case AlloyLexer.KW_INT2:
                    case AlloyLexer.KW_SEQINT:
                    case AlloyLexer.INTEGER:
                        if (expressionLevel == 0)
                        {
                            IToken token = transition.Token;
                            identifier = token.Text;
                            span = new Span(token.StartIndex, token.StopIndex - token.StartIndex + 1);
                        }

                        break;

                    case AlloyLexer.DOT:
                        if (expressionLevel == 0)
                        {
                            for (int i = 0; i < results.Count; i++)
                            {
                                results[i] += identifier + ".";
                                identifier = null;
                            }
                        }

                        break;
                    }
                }
                else if (transition.Transition.IsContext)
                {
                    // only need to handle it if we're entering BinOpExpr18
                    PushContextTransition pushContextTransition = transition.Transition as PushContextTransition;
                    if (pushContextTransition != null && pushContextTransition.TargetState == memberSelectRule.StartState)
                    {
                        expressionLevel++;
                    }

                    PopContextTransition popContextTransition = transition.Transition as PopContextTransition;
                    if (popContextTransition != null && popContextTransition.SourceState == memberSelectRule.EndState)
                    {
                        if (expressionLevel > 0)
                            expressionLevel--;
                    }
                }
                else if (transition.Transition.IsEpsilon)
                {
                    if (expressionLevel == 0 && transition.Transition.SourceState == memberSelectRule.StartState)
                    {
                        results.Clear();
                        results.Add("<context>::");
                    }
                }
            }

            if (identifier != null)
            {
                for (int i = 0; i < results.Count; i++)
                {
                    results[i] += "[" + identifier + "]";
                }
            }

            return results;
        }