예제 #1
1
        public Span Handle(IList<Token> tokens, Options options)
        {
            RepeaterMonthName month = null;
            int day = 0;
            IList<Token> remainingTokens = null;
            if (tokens.Count > 3)
            {
                month = tokens[2].GetTag<RepeaterMonthName>();
                day = tokens[3].GetTag<ScalarDay>().Value;
                remainingTokens = tokens.Take(2).ToList();
            }
            else
            {
                month = tokens[1].GetTag<RepeaterMonthName>();
                day = tokens[2].GetTag<ScalarDay>().Value;
                remainingTokens = tokens.Take(1).ToList();
            }

            var now = options.Clock();
            if (Time.IsMonthOverflow(now.Year, (int)month.Value, day))
            {
                return null;
            }
            return Utils.HandleMD(month, day, remainingTokens, options);
        }
예제 #2
1
 public IList<Token> Scan(IList<Token> tokens, Options options)
 {
     tokens.ForEach(token => token.Tag(
         new ITag[]
             {
                 ScanOrdinal(token, options),
                 ScanOrdinalDay(token)
             }.Where(
                 x => x != null).ToList()));
     return tokens;
 }
예제 #3
0
        public Span Handle(IList<Token> tokens, Options options)
        {
            var month = (int)tokens[0].GetTag<RepeaterMonthName>().Value;
            var year = (int)tokens[1].GetTag<ScalarYear>().Value;

            int next_month_year = 0;
            int next_month_month = 0;
            if (month == 12)
            {
                next_month_year = year + 1;
                next_month_month = 1;
            }
            else
            {
                next_month_year = year;
                next_month_month = month + 1;
            }

            try
            {
                var endTime = Time.New(next_month_year, next_month_month);
                return new Span(Time.New(year, month), endTime);
            }
            catch (ArgumentException)
            {
                return null;
            }
        }
예제 #4
0
파일: Parser.cs 프로젝트: acsteitz/nChronic
 public Span Parse(string phrase, Options options)
 {
     var taggedTokens = _tokenizer.Tokenize(phrase, options);
     var span = TokensToSpan(taggedTokens, options);
     Logger.Log(() => "=> " + (span != null ? span.ToString() : "<null>"));
     return span;
 }
예제 #5
0
 IList<Token> TokenizeInternal(string phrase, Options options)
 {
     var tokens = phrase
         .Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
         .Select(part => new Token(part))
         .ToList();
     return tokens;
 }
예제 #6
0
 public Span Handle(IList<Token> tokens, Options options)
 {
     var month = (int)tokens[0].GetTag<ScalarMonth>().Value;
     var day = (int)tokens[1].GetTag<ScalarDay>().Value;
     var now = options.Clock();
     var start = Time.New(now.Year, month, day);
     var end = start.AddMonths(1);
     return new Span(start, end);
 }
예제 #7
0
 public static Scalar ScanMonth(Token token, Token nextToken, Options options)
 {
     if (_monthPattern.IsMatch(token.Value))
     {
         var value = int.Parse(token.Value);
         if (value <= 12 && TokenIsAPeriodOfDay(nextToken) == false)
             return new ScalarMonth(value);
     }
     return null;
 }
예제 #8
0
        public Ordinal ScanOrdinal(Token token, Options options)
        {
            var match = Pattern.Match(token.Value);

            if (match.Success)
            {
                return new Ordinal(int.Parse(match.Groups[1].Value));
            }
            return null;
        }
예제 #9
0
        public static Scalar Scan(Token token, Token nextToken, Options options)
        {
            var match = _pattern.Match(token.Value);

            if (match.Success && String.IsNullOrEmpty(token.Value) == false
                && TokenIsAPeriodOfDay(nextToken) == false)
            {
                return new Scalar(int.Parse(match.Groups[0].Value));
            }
            return null;
        }
예제 #10
0
        public Span Handle(IList<Token> tokens, Options options)
        {
            var month = tokens[0].GetTag<RepeaterMonthName>();
            var day = (int)tokens[1].GetTag<OrdinalDay>().Value;

            var now = options.Clock();
            if (Time.IsMonthOverflow(now.Year, (int)month.Value, day))
            {
                return null;
            }
            return Utils.HandleMD(month, day, tokens.Skip(2).ToList(), options);
        }
예제 #11
0
파일: Parser.cs 프로젝트: acsteitz/nChronic
        public Span TokensToSpan(IList<Token> tokens, Options options)
        {
            var handlersToMatch = _registry
                .GetHandlers(HandlerType.Endian)
                .Concat(_registry.GetHandlers(HandlerType.Date));

            foreach (var handler in handlersToMatch)
            {
                if (handler.Match(tokens, _registry))
                {
                    var targetTokens = tokens
                        .Where(x => x.IsNotTaggedAs<Separator>())
                        .ToList();
                    return ExecuteHandler(handler, targetTokens, options);
                }
            }

            foreach (var handler in _registry.GetHandlers(HandlerType.Anchor))
            {
                if (handler.Match(tokens, _registry))
                {
                    var targetTokens = tokens
                        .Where(x => x.IsNotTaggedAs<Separator>())
                        .ToList();
                    return ExecuteHandler(handler, targetTokens, options);
                }
            }

            foreach (var handler in _registry.GetHandlers(HandlerType.Arrow))
            {
                if (handler.Match(tokens, _registry))
                {
                    var targetTokens = tokens
                        .Where(x =>
                            x.IsNotTaggedAs<SeparatorAt>() &&
                            x.IsNotTaggedAs<SeparatorComma>() &&
                            x.IsNotTaggedAs<SeparatorDate>())
                        .ToList();
                    return ExecuteHandler(handler, targetTokens, options);
                }
            }

            foreach (var handler in _registry.GetHandlers(HandlerType.Narrow))
            {
                if (handler.Match(tokens, _registry))
                {
                    return ExecuteHandler(handler, tokens, options);
                }
            }

            return null;
        }
예제 #12
0
 public IList<Token> Scan(IList<Token> tokens, Options options)
 {
     tokens.ForEach((token, nextToken) => token.Tag(
         new ITag[]
             {
                 Scan(token, nextToken, options),
                 ScanDay(token, nextToken, options),
                 ScanMonth(token, nextToken, options),
                 ScanYear(token, nextToken, options),
             }.Where(
                 x => x != null).ToList()));
     return tokens;
 }
예제 #13
0
        public IList<Token> Tokenize(string phrase, Options options)
        {
            options.OriginalPhrase = phrase;
            Logger.Log(() => phrase);

            phrase = Normalize(phrase);
            Logger.Log(() => phrase);

            var tokens = TokenizeInternal(phrase, options);
            _scanners.ForEach(scanner => scanner.Scan(tokens, options));
            var taggedTokens = tokens.Where(token => token.HasTags()).ToList();
            Logger.Log(() => String.Join(",", taggedTokens.Select(t => t.ToString())));

            return taggedTokens;
        }
예제 #14
0
        public Span Handle(IList<Token> tokens, Options options)
        {
            var month = (int)tokens[0].GetTag<RepeaterMonthName>().Value;
            var day = tokens[1].GetTag<ScalarDay>().Value;
            var year = tokens[2].GetTag<ScalarYear>().Value;

            Span span = null;
            try
            {
                var timeTokens = tokens.Skip(3).ToList();
                var dayStart = Time.New(year, month, day);
                span = Utils.DayOrTime(dayStart, timeTokens, options);
            }
            catch (ArgumentException e)
            {
                span = null;
            }
            return span;
        }
예제 #15
0
 public static Scalar ScanYear(Token token, Token nextToken, Options options)
 {
     if (_yearPattern.IsMatch(token.Value))
     {
         var value = int.Parse(token.Value);
         if (TokenIsAPeriodOfDay(nextToken) == false)
         {
             if (value <= 37)
             {
                 value += 2000;
             }
             else if (value <= 137 && value >= 69)
             {
                 value += 1900;
             }
             return new ScalarYear(value);
         }
     }
     return null;
 }
예제 #16
0
        public Span Handle(IList<Token> tokens, Options options)
        {
            var year = tokens[0].GetTag<ScalarYear>().Value;
            var month = tokens[1].GetTag<RepeaterMonthName>();
            var day = tokens[2].GetTag<OrdinalDay>().Value;
            var time_tokens = tokens.Skip(3).ToList();
            if (Time.IsMonthOverflow(year, (int)month.Value, day))
            {
                return null;
            }
            try
            {
                var dayStart = Time.New(year, (int)month.Value, day);
                return Utils.DayOrTime(dayStart, time_tokens, options);

            }
            catch (ArgumentException)
            {
                return null;
            }
        }
예제 #17
0
 public IList<Token> Scan(IList<Token> tokens, Options options)
 {
     tokens.ForEach(ApplyTags);
     return tokens;
 }
예제 #18
0
파일: Parser.cs 프로젝트: acsteitz/nChronic
 public Parser(Options options)
 {
     _options = options;
     _registry.MergeWith(new EndianSpecificRegistry(options.EndianPrecedence));
 }
예제 #19
0
 public IList<Token> Scan(IList<Token> tokens, Options options)
 {
     tokens.ForEach(ApplyTags);
     return tokens;
 }
예제 #20
0
파일: Parser.cs 프로젝트: acsteitz/nChronic
        static Span ExecuteHandler(ComplexHandler handler, IList<Token> tokens, Options options)
        {
            if (handler == null) throw new ArgumentNullException("handler");
            if (tokens == null) throw new ArgumentNullException("tokens");
            if (options == null) throw new ArgumentNullException("options");

            Logger.Log(handler.ToString);
            if (handler.BaseHandler == null)
            {
                throw new InvalidOperationException(String.Format(
                    "No base handler found on complex handler: {0}.",
                    handler));
            }
            return handler.BaseHandler.Handle(tokens, options);
        }