Beispiel #1
0
        public void Visit(Statement s)
        {
            string statementtypename = s.GetType().Name;

            string funcname = "Visit" + statementtypename;

            MethodInfo mi = this.GetType().GetMethod(funcname, new [] { s.GetType() });

            if (mi != null)
            {
                try
                {
                    mi.Invoke(this, new[] { s });
                }
                catch (TargetInvocationException e)
                {
                    throw e.InnerException;
                }
            }
        }
Beispiel #2
0
        public FSM(FSMFile _file)
        {
            file = _file;
            InitTokensDictionary( _file.config );

            StreamReader stream = new StreamReader(file.SourceFile);

            try
            {

                while (!stream.EndOfStream)
                {
                    currentLine++;
                    rawtokens = new Queue<string>(stream.ReadLine().Split(null));

                    if (IsComment(rawtokens.Peek()))
                        continue;

                    while (rawtokens.Count > 0)
                    {
                        string token = rawtokens.Dequeue();

                        if (token == "")
                            continue;

                        if (!IsToken(token))
                            throw new MalformedFSMException("Unexpected identifier " + token + ", expected keyword.", currentLine);

                        Type statementtype = Tokens[token];

                        Statement statement = (Statement)Activator.CreateInstance(statementtype);
                        statement.owner = this;
                        statement.line = currentLine;

                        System.Diagnostics.Debug.Assert(statement != null);

                        statement.Consume(rawtokens);
                        if (statement.ShouldPush())
                        {
                            //we need to add it to the current state, if it exists.
                            //it may not exist if this is the first statement (Global)
                            if (statements.Count > 0)
                                statements.Peek().Statements().Add(statement);
                            statements.Push(statement);
                        }
                        else if (statement.ShouldPop())
                        {
                            lastpopped = statements.Pop();

                            if (statements.Count > 0)
                                statements.Peek().Statements().Add(statement);
                        }
                        else //add to current statement
                        {
                            statements.Peek().Statements().Add(statement);
                        }
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                stream.Close();
            }

            if (statements.Count > 0) //we have terminated without closing the FSM.
            {
                throw new MalformedFSMException("Unexpected EOF: Did you forget an endstate/endfsm?", currentLine);
            }
        }