예제 #1
0
        private static void TryParseRangeAppend(ref ParserState state, ref RangeSyntaxNode range)
        {
            var snapshot = (state, range);

            var gotSeparator = state.TryConsume(t => t.IsRangeSeparator);

            if (state.TryConsume(t => t.IsRangeComponent))
            {
                var tokens = state.TakeBuffer();

                range = new RangeSyntaxNode(
                    tokens.AddRange(range.Components),
                    range.Fraction,
                    tokens.AddRange(range.Tokens));

                if (range.ToString().Length <= 8)
                {
                    TryParseRangeAppend(ref state, ref range);

                    return;
                }
            }

            (state, range) = snapshot;
        }
예제 #2
0
        // TODO: Discarded leading tokens

        // TODO: Multiple lines

        // TODO: Attention lines

        // TODO: General Delivery

        // TODO: USPS addresses

        // TODO: Military addresses

        // TODO: Puerto Rico addresses

        // TODO: US Virgin Islands addresses

        #region public APIs

        public static bool TryParseRange(string input, out RangeSyntaxNode result)
        {
            var tokens = Tokenizer.Tokenize(input).Reverse().ToImmutableArray();

            var state = new ParserState(tokens);

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

            result = success ? syntax : null;

            return(success);
        }
예제 #3
0
        private static bool TryParseRange(ref ParserState state, out RangeSyntaxNode result)
        {
            result = null;

            var snapshot = state;

            state.ConsumeWhiteSpace();

            var fraction = Token.None;

            if (state.TryConsume(t => t.Kind == TokenKind.Fraction))
            {
                fraction = state.Buffer.Last();

                state.ConsumeWhiteSpace();
            }

            if (state.TryConsume(t => t.IsRangeComponent))
            {
                var tokens = state.TakeBuffer();

                result = new RangeSyntaxNode(
                    tokens.Take(1).ToImmutableArray(),
                    fraction,
                    tokens);

                TryParseRangeAppend(ref state, ref result);

                if (result != null)
                {
                    state.TakeBuffer();
                    return(true);
                }
            }

            state = snapshot;

            return(false);
        }