Example #1
0
        /// <summary>
        /// Execute the actions encapsulated by this executor within the context of a
        /// particular
        /// <see cref="Antlr4.Runtime.Lexer">Antlr4.Runtime.Lexer</see>
        /// .
        /// <p>This method calls
        /// <see cref="Antlr4.Runtime.IIntStream.Seek(int)">Antlr4.Runtime.IIntStream.Seek(int)</see>
        /// to set the position of the
        /// <code>input</code>
        ///
        /// <see cref="Antlr4.Runtime.ICharStream">Antlr4.Runtime.ICharStream</see>
        /// prior to calling
        /// <see cref="ILexerAction.Execute(Antlr4.Runtime.Lexer)">ILexerAction.Execute(Antlr4.Runtime.Lexer)</see>
        /// on a position-dependent action. Before the
        /// method returns, the input position will be restored to the same position
        /// it was in when the method was invoked.</p>
        /// </summary>
        /// <param name="lexer">The lexer instance.</param>
        /// <param name="input">
        /// The input stream which is the source for the current token.
        /// When this method is called, the current
        /// <see cref="Antlr4.Runtime.IIntStream.Index()">Antlr4.Runtime.IIntStream.Index()</see>
        /// for
        /// <code>input</code>
        /// should be the start of the following token, i.e. 1
        /// character past the end of the current token.
        /// </param>
        /// <param name="startIndex">
        /// The token start index. This value may be passed to
        /// <see cref="Antlr4.Runtime.IIntStream.Seek(int)">Antlr4.Runtime.IIntStream.Seek(int)</see>
        /// to set the
        /// <code>input</code>
        /// position to the beginning
        /// of the token.
        /// </param>
        public virtual void Execute(Lexer lexer, ICharStream input, int startIndex)
        {
            bool requiresSeek = false;
            int  stopIndex    = input.Index;

            try
            {
                foreach (ILexerAction lexerAction in lexerActions)
                {
                    ILexerAction action = lexerAction;
                    if (action is LexerIndexedCustomAction)
                    {
                        int offset = ((LexerIndexedCustomAction)action).GetOffset();
                        input.Seek(startIndex + offset);
                        action       = ((LexerIndexedCustomAction)action).GetAction();
                        requiresSeek = (startIndex + offset) != stopIndex;
                    }
                    else
                    {
                        if (action.IsPositionDependent())
                        {
                            input.Seek(stopIndex);
                            requiresSeek = false;
                        }
                    }
                    action.Execute(lexer);
                }
            }
            finally
            {
                if (requiresSeek)
                {
                    input.Seek(stopIndex);
                }
            }
        }