TryMatch() public method

public TryMatch ( ParsingContext context, ISourceStream source ) : Token
context ParsingContext
source ISourceStream
return Token
Example #1
0
        private void MatchRegularToken()
        {
            if (_grammar.FlagIsSet(LanguageFlags.EmitLineStartToken))
            {
                if (Context.Source.Location.Line > Context.PreviousLineStart.Line)
                {
                    Context.CurrentToken      = Context.Source.CreateToken(_grammar.LineStartTerminal);
                    Context.PreviousLineStart = Context.Source.Location;
                    return;
                }
            }
            //Find matching terminal
            // First, try terminals with explicit "first-char" prefixes, selected by current char in source
            ComputeCurrentTerminals();
            //If we have more than one candidate; let grammar method select
            if (Context.CurrentTerminals.Count > 1)
            {
                _grammar.OnScannerSelectTerminal(Context);
            }

            MatchTerminals();
            //If we don't have a token from terminals, try Grammar's method
            if (Context.CurrentToken == null)
            {
                Context.CurrentToken = _grammar.TryMatch(Context, Context.SourceStream);
            }
            if (Context.CurrentToken is MultiToken)
            {
                UnpackMultiToken();
            }
        }
Example #2
0
        private bool MatchRegularTerminals()
        {
            //We need to eject LineStart BEFORE we try to produce a real token; this LineStart token should reach
            // the parser, make it change the state and with it to change the set of expected tokens. So when we
            // finally move to scan the real token, the expected terminal set is correct.
            if (NeedLineStartToken(Context.Source.Location))
            {
                Context.CurrentToken      = Context.Source.CreateToken(_grammar.LineStartTerminal);
                Context.PreviousLineStart = Context.Source.Location;
                return(true);
            }
            //Find matching terminal
            // First, try terminals with explicit "first-char" prefixes, selected by current char in source
            ComputeCurrentTerminals();
            //If we have more than one candidate; let grammar method select
            if (Context.CurrentTerminals.Count > 1)
            {
                _grammar.OnScannerSelectTerminal(Context);
            }

            MatchTerminals();
            //If we don't have a token from terminals, try Grammar's method
            if (Context.CurrentToken == null)
            {
                Context.CurrentToken = _grammar.TryMatch(Context, Context.SourceStream);
            }
            if (Context.CurrentToken is MultiToken)
            {
                UnpackMultiToken();
            }
            return(Context.CurrentToken != null);
        }//method