public static bool TryParseRuralRouteBox(string input, out RuralRouteBoxSyntaxNode result) { var tokens = Tokenizer.Tokenize(input).Reverse().ToImmutableArray(); var state = new ParserState(tokens); var success = TryParseRuralRouteBox(ref state, out var syntax) && state.Current.IsNone; result = success ? syntax : null; return(success); }
private static bool TryParseRuralRouteBox(ref ParserState state, out RuralRouteBoxSyntaxNode result) { result = null; var snapshot = state; RangeSyntaxNode routeNumber; ImmutableArray <Token> boxKeywordTokens; if (TryParseRange(ref state, out var boxNumber)) { state.ConsumeWhiteSpace(); if (state.TryConsume(t => t.IsOctothorpe)) { state.ConsumeWhiteSpace(); } if (state.TryConsume("box")) { state.ConsumeWhiteSpace(); if (state.TryConsume(t => t.IsCommaOrPeriod)) { state.ConsumeWhiteSpace(); } boxKeywordTokens = state.TakeBuffer(); if (TryParseRange(ref state, out routeNumber)) { state.ConsumeWhiteSpace(); if (state.TryConsume(t => t.IsOctothorpe)) { state.ConsumeWhiteSpace(); } if (state.TryConsume(t => t.IsCommaOrPeriod)) { state.ConsumeWhiteSpace(); } if (state.TryConsume(t => t.Matches("rr") || t.Matches("rd") || t.Matches("rfd"))) { goto Parsed; } else if (state.TryConsume("r")) { state.ConsumeWhiteSpace(); if (state.TryConsume(t => t.IsCommaOrPeriod)) { state.ConsumeWhiteSpace(); } if (state.TryConsume("r")) { goto Parsed; } } else if (state.TryConsume(t => t.Matches("route") || t.Matches("rt"))) { state.ConsumeWhiteSpace(); state.TryConsume("rural"); goto Parsed; } } } } state = snapshot; return(false); Parsed: result = new RuralRouteBoxSyntaxNode( state.TakeBuffer(), routeNumber, boxKeywordTokens, boxNumber); return(true); }