Beispiel #1
0
 public Lexer(ICharStream input, TextWriter output, TextWriter errorOutput)
 {
     this._input                  = input;
     this.Output                  = output;
     this.ErrorOutput             = errorOutput;
     this._tokenFactorySourcePair = Tuple.Create((ITokenSource)this, input);
 }
Beispiel #2
0
 /// <summary>
 /// Constructs a new
 /// <see cref="CommonToken"/>
 /// with the specified token type and
 /// text.
 /// </summary>
 /// <param name="type">The token type.</param>
 /// <param name="text">The text of the token.</param>
 public CommonToken(int type, string text)
 {
     this._type    = type;
     this._channel = TokenConstants.DefaultChannel;
     this._text    = text;
     this.source   = EmptySource;
 }
Beispiel #3
0
 private static void GetElementDependencies(ICustomAttributeProvider annotatedElement, IList <Sharpen.Tuple <RuleDependencyAttribute, ICustomAttributeProvider> > result)
 {
     foreach (RuleDependencyAttribute dependency in annotatedElement.GetCustomAttributes(typeof(RuleDependencyAttribute), true))
     {
         result.Add(Tuple.Create(dependency, annotatedElement));
     }
 }
Beispiel #4
0
 /// <summary>Set the char stream and reset the lexer</summary>
 public virtual void SetInputStream(ICharStream input)
 {
     this._input = null;
     this._tokenFactorySourcePair = Tuple.Create((ITokenSource)this, _input);
     Reset();
     this._input = input;
     this._tokenFactorySourcePair = Tuple.Create((ITokenSource)this, _input);
 }
Beispiel #5
0
        /// <summary>Begin parsing at startRuleIndex</summary>
        public virtual ParserRuleContext Parse(int startRuleIndex)
        {
            RuleStartState         startRuleStartState = _atn.ruleToStartState[startRuleIndex];
            InterpreterRuleContext rootContext         = new InterpreterRuleContext(null, ATNState.InvalidStateNumber, startRuleIndex);

            if (startRuleStartState.isPrecedenceRule)
            {
                EnterRecursionRule(rootContext, startRuleStartState.stateNumber, startRuleIndex, 0);
            }
            else
            {
                EnterRule(rootContext, startRuleStartState.stateNumber, startRuleIndex);
            }
            while (true)
            {
                ATNState p = AtnState;
                switch (p.StateType)
                {
                case StateType.RuleStop:
                {
                    // pop; return from rule
                    if (RuleContext.IsEmpty)
                    {
                        if (startRuleStartState.isPrecedenceRule)
                        {
                            ParserRuleContext result = RuleContext;
                            Sharpen.Tuple <ParserRuleContext, int> parentContext = _parentContextStack.Pop();
                            UnrollRecursionContexts(parentContext.Item1);
                            return(result);
                        }
                        else
                        {
                            ExitRule();
                            return(rootContext);
                        }
                    }
                    VisitRuleStopState(p);
                    break;
                }

                default:
                {
                    try
                    {
                        VisitState(p);
                    }
                    catch (RecognitionException e)
                    {
                        State             = _atn.ruleToStopState[p.ruleIndex].stateNumber;
                        Context.exception = e;
                        ErrorHandler.ReportError(this, e);
                        ErrorHandler.Recover(this, e);
                    }
                    break;
                }
                }
            }
        }
Beispiel #6
0
 public CommonToken(Sharpen.Tuple <ITokenSource, ICharStream> source, int type, int channel, int start, int stop)
 {
     this.source   = source;
     this._type    = type;
     this._channel = channel;
     this.start    = start;
     this.stop     = stop;
     if (source.Item1 != null)
     {
         this._line = source.Item1.Line;
         this.charPositionInLine = source.Item1.Column;
     }
 }
Beispiel #7
0
        protected internal virtual void VisitRuleStopState(ATNState p)
        {
            RuleStartState ruleStartState = _atn.ruleToStartState[p.ruleIndex];

            if (ruleStartState.isPrecedenceRule)
            {
                Sharpen.Tuple <ParserRuleContext, int> parentContext = _parentContextStack.Pop();
                UnrollRecursionContexts(parentContext.Item1);
                State = parentContext.Item2;
            }
            else
            {
                ExitRule();
            }
            RuleTransition ruleTransition = (RuleTransition)_atn.states[State].Transition(0);

            State = ruleTransition.followState.stateNumber;
        }
Beispiel #8
0
        public virtual CommonToken Create(Sharpen.Tuple <ITokenSource, ICharStream> source, int type, string text, int channel, int start, int stop, int line, int charPositionInLine)
        {
            CommonToken t = new CommonToken(source, type, channel, start, stop);

            t.Line   = line;
            t.Column = charPositionInLine;
            if (text != null)
            {
                t.Text = text;
            }
            else
            {
                if (copyText && source.Item2 != null)
                {
                    t.Text = source.Item2.GetText(Interval.Of(start, stop));
                }
            }
            return(t);
        }
Beispiel #9
0
 /// <summary>
 /// Constructs a new
 /// <see cref="CommonToken"/>
 /// as a copy of another
 /// <see cref="IToken"/>
 /// .
 /// <p>
 /// If
 /// <paramref name="oldToken"/>
 /// is also a
 /// <see cref="CommonToken"/>
 /// instance, the newly
 /// constructed token will share a reference to the
 /// <see cref="Text()"/>
 /// field and
 /// the
 /// <see cref="Tuple{T1, T2}"/>
 /// stored in
 /// <see cref="source"/>
 /// . Otherwise,
 /// <see cref="Text()"/>
 /// will
 /// be assigned the result of calling
 /// <see cref="Text()"/>
 /// , and
 /// <see cref="source"/>
 /// will be constructed from the result of
 /// <see cref="IToken.TokenSource()"/>
 /// and
 /// <see cref="IToken.InputStream()"/>
 /// .</p>
 /// </summary>
 /// <param name="oldToken">The token to copy.</param>
 public CommonToken(IToken oldToken)
 {
     _type = oldToken.Type;
     _line = oldToken.Line;
     index = oldToken.TokenIndex;
     charPositionInLine = oldToken.Column;
     _channel           = oldToken.Channel;
     start = oldToken.StartIndex;
     stop  = oldToken.StopIndex;
     if (oldToken is Antlr4.Runtime.CommonToken)
     {
         _text  = ((Antlr4.Runtime.CommonToken)oldToken)._text;
         source = ((Antlr4.Runtime.CommonToken)oldToken).source;
     }
     else
     {
         _text  = oldToken.Text;
         source = Sharpen.Tuple.Create(oldToken.TokenSource, oldToken.InputStream);
     }
 }
Beispiel #10
0
 IToken ITokenFactory.Create(Sharpen.Tuple <ITokenSource, ICharStream> source, int type, string text, int channel, int start, int stop, int line, int charPositionInLine)
 {
     return(Create(source, type, text, channel, start, stop, line, charPositionInLine));
 }
Beispiel #11
0
 public override void EnterRecursionRule(ParserRuleContext localctx, int state, int ruleIndex, int precedence)
 {
     _parentContextStack.Push(Tuple.Create(RuleContext, localctx.invokingState));
     base.EnterRecursionRule(localctx, state, ruleIndex, precedence);
 }
Beispiel #12
0
 /// <summary>
 /// Constructs a new
 /// <see cref="CommonToken"/>
 /// with the specified token type.
 /// </summary>
 /// <param name="type">The token type.</param>
 public CommonToken(int type)
 {
     // set to invalid position
     this._type  = type;
     this.source = EmptySource;
 }
Beispiel #13
0
        protected internal virtual void ReadStates(ATN atn)
        {
            //
            // STATES
            //
            IList <Sharpen.Tuple <LoopEndState, int> >    loopBackStateNumbers = new List <Sharpen.Tuple <LoopEndState, int> >();
            IList <Sharpen.Tuple <BlockStartState, int> > endStateNumbers      = new List <Sharpen.Tuple <BlockStartState, int> >();
            int nstates = ReadInt();

            for (int i_1 = 0; i_1 < nstates; i_1++)
            {
                StateType stype = (StateType)ReadInt();
                // ignore bad type of states
                if (stype == StateType.InvalidType)
                {
                    atn.AddState(null);
                    continue;
                }
                int ruleIndex = ReadInt();
                if (ruleIndex == char.MaxValue)
                {
                    ruleIndex = -1;
                }
                ATNState s = StateFactory(stype, ruleIndex);
                if (stype == StateType.LoopEnd)
                {
                    // special case
                    int loopBackStateNumber = ReadInt();
                    loopBackStateNumbers.Add(Tuple.Create((LoopEndState)s, loopBackStateNumber));
                }
                else
                {
                    if (s is BlockStartState)
                    {
                        int endStateNumber = ReadInt();
                        endStateNumbers.Add(Tuple.Create((BlockStartState)s, endStateNumber));
                    }
                }
                atn.AddState(s);
            }
            // delay the assignment of loop back and end states until we know all the state instances have been initialized
            foreach (Sharpen.Tuple <LoopEndState, int> pair in loopBackStateNumbers)
            {
                pair.Item1.loopBackState = atn.states[pair.Item2];
            }
            foreach (Sharpen.Tuple <BlockStartState, int> pair_1 in endStateNumbers)
            {
                pair_1.Item1.endState = (BlockEndState)atn.states[pair_1.Item2];
            }
            int numNonGreedyStates = ReadInt();

            for (int i_2 = 0; i_2 < numNonGreedyStates; i_2++)
            {
                int stateNumber = ReadInt();
                ((DecisionState)atn.states[stateNumber]).nonGreedy = true;
            }
            int numPrecedenceStates = ReadInt();

            for (int i_4 = 0; i_4 < numPrecedenceStates; i_4++)
            {
                int stateNumber = ReadInt();
                ((RuleStartState)atn.states[stateNumber]).isPrecedenceRule = true;
            }
        }
Beispiel #14
0
 public Lexer(ICharStream input)
 {
     this._input = input;
     this._tokenFactorySourcePair = Sharpen.Tuple.Create((ITokenSource)this, input);
 }