static string _ParseIdentifier(ParseContext pc) { pc.TrySkipCCommentsAndWhiteSpace(); if (-1 == pc.Current) { pc.Expecting(); return(null); } var l = pc.CaptureBuffer.Length; if ('_' != pc.Current && !char.IsLetter((char)pc.Current)) { pc.Expecting("ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz".ToCharArray().Convert <int>().ToArray()); } pc.CaptureCurrent(); while (-1 != pc.Advance() && ('_' == pc.Current || '-' == pc.Current || char.IsLetterOrDigit((char)pc.Current))) { pc.CaptureCurrent(); } pc.TrySkipCCommentsAndWhiteSpace(); return(pc.GetCapture(l)); }
static bool _DoMatchDfa(CharDfaEntry[] dfaTable, ParseContext context) { // track our current state var state = 0; // prepare the parse context context.EnsureStarted(); while (true) { // if no more input if (-1 == context.Current) { // if we accept, return that return(-1 != dfaTable[state].AcceptSymbolId); } // move by current character var newState = MoveDfa(dfaTable, state, (char)context.Current); // we couldn't match anything if (-1 == newState) { // if we accept, return that if (-1 != dfaTable[state].AcceptSymbolId) { return(true); } // otherwise error // advance the input context.Advance(); return(false); } // store the current character context.CaptureCurrent(); // advance the input context.Advance(); // iterate to our next states state = newState; } }
bool _DoMatchDfa(ParseContext context) { // track the current state var state = this; while (true) { // if no more input if (-1 == context.Current) { // if we accept, return that return(state.IsAccepting); } // move by current character var newState = state.MoveDfa((char)context.Current); // we couldn't match anything if (null == newState) { // if we accept, return that if (state.IsAccepting) { return(true); } // otherwise error // advance the input context.Advance(); return(false); } // store the current character context.CaptureCurrent(); // advance the input context.Advance(); // iterate to our next state state = newState; } }
// almost the same as our lex methods bool _DoMatch(ParseContext context) { // get the initial states var states = FillEpsilonClosure(); while (true) { // if no more input if (-1 == context.Current) { // if we accept, return that return(IsAnyAccepting(states)); } // move by current character var newStates = FillMove(states, (char)context.Current); // we couldn't match anything if (0 == newStates.Count) { // if we accept, return that if (IsAnyAccepting(states)) { return(true); } // otherwise error // advance the input context.Advance(); return(false); } // store the current character context.CaptureCurrent(); // advance the input context.Advance(); // iterate to our next states states = newStates; } }