Пример #1
0
        public override object VisitStateDecl(PParser.StateDeclContext context)
        {
            string symbolName = context.name.GetText();

            AST.States.State decl = CurrentScope.Put(symbolName, context);
            nodesToDeclarations.Put(context, decl);
            return(null);
        }
Пример #2
0
        public State Put(string name, PParser.StateDeclContext tree)
        {
            State state = new State(tree, name);

            CheckConflicts(state, Namespace(states));
            states.Add(name, state);
            return(state);
        }
Пример #3
0
        public override object VisitStateDecl(PParser.StateDeclContext context)
        {
            // STATE name=iden
            var state = (State)nodesToDeclarations.Get(context);

            // START?
            state.IsStart = context.START() != null;

            // temperature=(HOT | COLD)?
            state.Temperature = context.temperature == null
                                    ? StateTemperature.WARM
                                    : context.temperature.Text.Equals("HOT")
                                        ? StateTemperature.HOT
                                        : StateTemperature.COLD;

            // annotationSet?
            if (context.annotationSet() != null)
            {
                throw new NotImplementedException("state annotations");
            }

            // LBRACE stateBodyItem* RBRACE ;
            foreach (var stateBodyItemContext in context.stateBodyItem())
            {
                switch (Visit(stateBodyItemContext))
                {
                case IStateAction[] actions:
                    foreach (var action in actions)
                    {
                        if (state.HasHandler(action.Trigger))
                        {
                            // TODO: get the exact EventIdContext instead of stateBodyItemContext
                            throw Handler.DuplicateEventAction(action.SourceLocation, state[action.Trigger], state);
                        }
                        state[action.Trigger] = action;
                    }
                    break;

                case Tuple <string, Function> entryOrExit:
                    if (entryOrExit.Item1.Equals("ENTRY"))
                    {
                        if (state.Entry != null)
                        {
                            throw Handler.DuplicateStateEntry(stateBodyItemContext, state.Entry, state);
                        }
                        state.Entry = entryOrExit.Item2;
                    }
                    else
                    {
                        if (state.Exit != null)
                        {
                            throw Handler.DuplicateStateExitHandler(stateBodyItemContext, state.Exit, state);
                        }
                        state.Exit = entryOrExit.Item2;
                    }
                    break;

                default:
                    throw Handler.InternalError(stateBodyItemContext, "state body item not recognized");
                }
            }

            if (state.IsStart)
            {
                if (CurrentMachine.StartState != null)
                {
                    throw Handler.DuplicateDeclaration(context, state, CurrentMachine.StartState);
                }
                CurrentMachine.StartState  = state;
                CurrentMachine.PayloadType = state.Entry?.Signature.Parameters.ElementAtOrDefault(0)?.Type ?? PrimitiveType.Null;
            }

            return(state);
        }
Пример #4
0
        public override object VisitStateDecl(PParser.StateDeclContext context)
        {
            // STATE name=iden
            var state = (State)nodesToDeclarations.Get(context);

            state.OwningMachine = CurrentMachine;

            // START?
            state.IsStart = context.START() != null;

            // temperature=(HOT | COLD)?
            state.Temperature = context.temperature == null
                ? StateTemperature.Warm
                : context.HOT() != null
                    ? StateTemperature.Hot
                    : StateTemperature.Cold;

            // LBRACE stateBodyItem* RBRACE ;
            foreach (var stateBodyItemContext in context.stateBodyItem())
            {
                switch (Visit(stateBodyItemContext))
                {
                case IStateAction[] actions:
                    foreach (var action in actions)
                    {
                        if (state.HasHandler(action.Trigger))
                        {
                            throw Handler.DuplicateEventAction(action.SourceLocation, state[action.Trigger], state);
                        }

                        if (action.Trigger.Name.Equals("null") && CurrentMachine.IsSpec)
                        {
                            throw Handler.NullTransitionInMonitor(action.SourceLocation, CurrentMachine);
                        }
                        state[action.Trigger] = action;
                    }

                    break;

                case Tuple <string, Function> entryOrExit:
                    if (entryOrExit.Item1.Equals("ENTRY"))
                    {
                        if (state.Entry != null)
                        {
                            throw Handler.DuplicateStateEntry(stateBodyItemContext, state.Entry, state);
                        }
                        state.Entry = entryOrExit.Item2;
                    }
                    else
                    {
                        if (state.Exit != null)
                        {
                            throw Handler.DuplicateStateExitHandler(stateBodyItemContext, state.Exit, state);
                        }
                        state.Exit = entryOrExit.Item2;
                    }

                    break;

                default:
                    throw Handler.InternalError(stateBodyItemContext,
                                                new ArgumentOutOfRangeException(nameof(context)));
                }
            }

            if (state.IsStart)
            {
                if (CurrentMachine.StartState != null)
                {
                    throw Handler.DuplicateStartState(context, state, CurrentMachine.StartState, CurrentMachine);
                }
                CurrentMachine.StartState  = state;
                CurrentMachine.PayloadType =
                    state.Entry?.Signature.Parameters.ElementAtOrDefault(0)?.Type ?? PrimitiveType.Null;
            }

            return(state);
        }