Exemplo n.º 1
0
        public static bool TryParsePostOfficeBox(string input, out PostOfficeBoxSyntaxNode result)
        {
            var tokens = Tokenizer.Tokenize(input).Reverse().ToImmutableArray();

            var state = new ParserState(tokens);

            var success = TryParsePostOfficeBox(ref state, out var syntax) && state.Current.IsNone;

            result = success ? syntax : null;

            return(success);
        }
Exemplo n.º 2
0
        private static bool TryParsePostOfficeBox(ref ParserState state, out PostOfficeBoxSyntaxNode result)
        {
            result = null;

            var snapshot = state;

            if (TryParseRange(ref state, out var boxNumber))
            {
                state.ConsumeWhiteSpace();

                if (state.TryConsume(t => t.IsOctothorpe))
                {
                    state.ConsumeWhiteSpace();
                }

                if (state.TryConsume("caller"))
                {
                    state.ConsumeWhiteSpace();

                    state.TryConsume("firm");
                }
                else if (state.TryConsume(t => t.Matches("box") || t.Matches("drawer") || t.Matches("bin") || t.Matches("drawer") || t.Matches("lockbox")))
                {
                    // no-op
                }
                else
                {
                    goto Failure;
                }

                var boxKeywordTokens = state.TakeBuffer();

                if (TryParsePostOfficeBoxKeywords(ref state, out var additionalTokens))
                {
                    boxKeywordTokens = additionalTokens.AddRange(boxKeywordTokens);
                }

                result = new PostOfficeBoxSyntaxNode(boxKeywordTokens, boxNumber);

                return(true);
            }

Failure:
            state = snapshot;
            return(false);
        }