Ejemplo n.º 1
0
 protected internal virtual void ReadLexerActions(ATN atn)
 {
     //
     // LEXER ACTIONS
     //
     if (atn.grammarType == ATNType.Lexer)
     {
         atn.lexerActions = new ILexerAction[ReadInt()];
         for (int i_10 = 0; i_10 < atn.lexerActions.Length; i_10++)
         {
             LexerActionType actionType = (LexerActionType)ReadInt();
             int             data1      = ReadInt();
             if (data1 == unchecked ((int)(0xFFFF)))
             {
                 data1 = -1;
             }
             int data2 = ReadInt();
             if (data2 == unchecked ((int)(0xFFFF)))
             {
                 data2 = -1;
             }
             ILexerAction lexerAction = LexerActionFactory(actionType, data1, data2);
             atn.lexerActions[i_10] = lexerAction;
         }
     }
 }
Ejemplo n.º 2
0
        public override Handle LexerCommand(GrammarAST ID)
        {
            ILexerAction lexerAction = CreateLexerAction(ID, null);

            if (lexerAction != null)
            {
                return(Action(ID, lexerAction));
            }

            if (codegenTemplates == null)
            {
                // suppress reporting a single missing template when the target couldn't be loaded
                return(Epsilon(ID));
            }

            // fall back to standard action generation for the command
            Template cmdST = codegenTemplates.GetInstanceOf("Lexer" +
                                                            CharSupport.Capitalize(ID.Text) +
                                                            "Command");

            if (cmdST == null)
            {
                g.tool.errMgr.GrammarError(ErrorType.INVALID_LEXER_COMMAND, g.fileName, ID.Token, ID.Text);
                return(Epsilon(ID));
            }

            if (cmdST.impl.FormalArguments != null && cmdST.impl.FormalArguments.Any(x => x.Name == "arg"))
            {
                g.tool.errMgr.GrammarError(ErrorType.MISSING_LEXER_COMMAND_ARGUMENT, g.fileName, ID.Token, ID.Text);
                return(Epsilon(ID));
            }

            return(Action(cmdST.Render()));
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Constructs an executor for a sequence of
 /// <see cref="ILexerAction"/>
 /// actions.
 /// </summary>
 /// <param name="lexerActions">The lexer actions to execute.</param>
 public LexerActionExecutor(ILexerAction[] lexerActions)
 {
     this.lexerActions = lexerActions;
     int hash = MurmurHash.Initialize();
     foreach (ILexerAction lexerAction in lexerActions)
     {
         hash = MurmurHash.Update(hash, lexerAction);
     }
     this.hashCode = MurmurHash.Finish(hash, lexerActions.Length);
 }
Ejemplo n.º 4
0
        protected virtual int GetLexerActionIndex(ILexerAction lexerAction)
        {
            int lexerActionIndex;

            if (!actionToIndexMap.TryGetValue(lexerAction, out lexerActionIndex))
            {
                lexerActionIndex = actionToIndexMap.Count;
                actionToIndexMap[lexerAction]      = lexerActionIndex;
                indexToActionMap[lexerActionIndex] = lexerAction;
            }

            return(lexerActionIndex);
        }
Ejemplo n.º 5
0
        protected virtual Handle Action(GrammarAST node, ILexerAction lexerAction)
        {
            ATNState         left             = NewState(node);
            ATNState         right            = NewState(node);
            bool             isCtxDependent   = false;
            int              lexerActionIndex = GetLexerActionIndex(lexerAction);
            ActionTransition a =
                new ActionTransition(right, currentRule.index, lexerActionIndex, isCtxDependent);

            left.AddTransition(a);
            node.atnState = left;
            Handle h = new Handle(left, right);

            return(h);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Execute the actions encapsulated by this executor within the context of a
        /// particular
        /// <see cref="Antlr4.Runtime.Lexer"/>
        /// .
        /// <p>This method calls
        /// <see cref="Antlr4.Runtime.IIntStream.Seek(int)"/>
        /// to set the position of the
        /// <paramref name="input"/>
        ///
        /// <see cref="Antlr4.Runtime.ICharStream"/>
        /// prior to calling
        /// <see cref="ILexerAction.Execute(Antlr4.Runtime.Lexer)"/>
        /// 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()"/>
        /// for
        /// <paramref name="input"/>
        /// 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)"/>
        /// to set the
        /// <paramref name="input"/>
        /// 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).Offset;
                        input.Seek(startIndex + offset);
                        action       = ((LexerIndexedCustomAction)action).Action;
                        requiresSeek = (startIndex + offset) != stopIndex;
                    }
                    else
                    {
                        if (action.IsPositionDependent)
                        {
                            input.Seek(stopIndex);
                            requiresSeek = false;
                        }
                    }
                    action.Execute(lexer);
                }
            }
            finally
            {
                if (requiresSeek)
                {
                    input.Seek(stopIndex);
                }
            }
        }
Ejemplo n.º 7
0
 protected virtual Handle Action(GrammarAST node, ILexerAction lexerAction)
 {
     ATNState left = NewState(node);
     ATNState right = NewState(node);
     bool isCtxDependent = false;
     int lexerActionIndex = GetLexerActionIndex(lexerAction);
     ActionTransition a =
         new ActionTransition(right, currentRule.index, lexerActionIndex, isCtxDependent);
     left.AddTransition(a);
     node.atnState = left;
     Handle h = new Handle(left, right);
     return h;
 }
Ejemplo n.º 8
0
        protected virtual int GetLexerActionIndex(ILexerAction lexerAction)
        {
            int lexerActionIndex;
            if (!actionToIndexMap.TryGetValue(lexerAction, out lexerActionIndex))
            {
                lexerActionIndex = actionToIndexMap.Count;
                actionToIndexMap[lexerAction] = lexerActionIndex;
                indexToActionMap[lexerActionIndex] = lexerAction;
            }

            return lexerActionIndex;
        }
Ejemplo n.º 9
0
 public static Antlr4.Runtime.Atn.LexerActionExecutor Append(Antlr4.Runtime.Atn.LexerActionExecutor lexerActionExecutor, ILexerAction lexerAction)
 {
     if (lexerActionExecutor == null)
     {
         return new Antlr4.Runtime.Atn.LexerActionExecutor(new ILexerAction[] { lexerAction });
     }
     ILexerAction[] lexerActions = Arrays.CopyOf(lexerActionExecutor.lexerActions, lexerActionExecutor.lexerActions.Length + 1);
     lexerActions[lexerActions.Length - 1] = lexerAction;
     return new Antlr4.Runtime.Atn.LexerActionExecutor(lexerActions);
 }
Ejemplo n.º 10
0
 /// <summary>
 /// Constructs a new indexed custom action by associating a character offset
 /// with a
 /// <see cref="ILexerAction"/>
 /// .
 /// <p>Note: This class is only required for lexer actions for which
 /// <see cref="ILexerAction.IsPositionDependent()"/>
 /// returns
 /// <code>true</code>
 /// .</p>
 /// </summary>
 /// <param name="offset">
 /// The offset into the input
 /// <see cref="Antlr4.Runtime.ICharStream"/>
 /// , relative to
 /// the token start index, at which the specified lexer action should be
 /// executed.
 /// </param>
 /// <param name="action">
 /// The lexer action to execute at a particular offset in the
 /// input
 /// <see cref="Antlr4.Runtime.ICharStream"/>
 /// .
 /// </param>
 public LexerIndexedCustomAction(int offset, ILexerAction action)
 {
     this.offset = offset;
     this.action = action;
 }
Ejemplo n.º 11
0
        public static Antlr4.Runtime.Atn.LexerActionExecutor Append(Antlr4.Runtime.Atn.LexerActionExecutor lexerActionExecutor, ILexerAction lexerAction)
        {
            if (lexerActionExecutor == null)
            {
                return(new Antlr4.Runtime.Atn.LexerActionExecutor(new ILexerAction[] { lexerAction }));
            }
            ILexerAction[] lexerActions = Arrays.CopyOf(lexerActionExecutor.lexerActions, lexerActionExecutor.lexerActions.Length + 1);

            lexerActions[lexerActions.Length - 1] = lexerAction;
            return(new Antlr4.Runtime.Atn.LexerActionExecutor(lexerActions));
        }
 /// <summary>
 /// Constructs a new indexed custom action by associating a character offset
 /// with a
 /// <see cref="ILexerAction"/>
 /// .
 /// <p>Note: This class is only required for lexer actions for which
 /// <see cref="ILexerAction.IsPositionDependent()"/>
 /// returns
 /// <code>true</code>
 /// .</p>
 /// </summary>
 /// <param name="offset">
 /// The offset into the input
 /// <see cref="Antlr4.Runtime.ICharStream"/>
 /// , relative to
 /// the token start index, at which the specified lexer action should be
 /// executed.
 /// </param>
 /// <param name="action">
 /// The lexer action to execute at a particular offset in the
 /// input
 /// <see cref="Antlr4.Runtime.ICharStream"/>
 /// .
 /// </param>
 public LexerIndexedCustomAction(int offset, ILexerAction action)
 {
     this.offset = offset;
     this.action = action;
 }