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); }
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; }
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; } }
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; }
IList<Token> TokenizeInternal(string phrase, Options options) { var tokens = phrase .Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries) .Select(part => new Token(part)) .ToList(); return tokens; }
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); }
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; }
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; }
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; }
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); }
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; }
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; }
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; }
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; }
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; }
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; } }
public IList<Token> Scan(IList<Token> tokens, Options options) { tokens.ForEach(ApplyTags); return tokens; }
public Parser(Options options) { _options = options; _registry.MergeWith(new EndianSpecificRegistry(options.EndianPrecedence)); }
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); }