class ParseException(Exception):
"""An exception thrown during parsing because of unrecognized input."""
def __init__(self, message: str) -> None:
- logger.error(message)
self.message = message
def syntaxError(
self, recognizer, offendingSymbol, line, column, msg, e
):
- logger.error(msg)
raise ParseException(msg)
def reportAmbiguity(
This is the main entrypoint to this class for caller code.
"""
+ date_string = date_string.strip()
+ date_string = re.sub('\s+', ' ', date_string)
self._reset()
listener = RaisingErrorListener()
input_stream = antlr4.InputStream(date_string)
return TimeUnit.MONTHS
txt = orig.lower()[:3]
if txt in self.day_name_to_number:
- return(self.day_name_to_number[txt])
+ return(TimeUnit(self.day_name_to_number[txt]))
elif txt in self.delta_unit_to_constant:
- return(self.delta_unit_to_constant[txt])
+ return(TimeUnit(self.delta_unit_to_constant[txt]))
raise ParseException(f'Invalid date unit: {orig}')
def _figure_out_time_unit(self, orig: str) -> int:
unit = self.context['delta_unit']
dt = n_timeunits_from_base(
count,
- unit,
+ TimeUnit(unit),
date_to_datetime(self.date)
)
self.date = datetime_to_date(dt)
unit = self._figure_out_date_unit(unit)
d = n_timeunits_from_base(
count,
- unit,
+ TimeUnit(unit),
d)
self.context['year'] = d.year
self.context['month'] = d.month
unit = self._figure_out_date_unit(unit)
d = n_timeunits_from_base(
count,
- unit,
+ TimeUnit(unit),
d)
self.context['year'] = d.year
self.context['month'] = d.month