Class NaftahDateParserHelper
This class provides utility methods to:
- Create a lexer and token stream from a
CharStream - Prepare and configure an
NaftahDateParser - Run the parser on a string input and return an
NaftahTemporalresult
It supports multiple error listeners and debug output for token streams.
- Author:
- Chakib Daii
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprivatePrivate constructor to prevent instantiation. -
Method Summary
Modifier and TypeMethodDescriptionstatic NaftahDatecurrentDate(NaftahDateParserBaseVisitor<?> naftahDateParserBaseVisitor, NaftahDateParser.CalendarSpecifierContext calendarSpecifier, NaftahDateParser.ZoneOrOffsetSpecifierContext zoneOrOffsetSpecifier) Resolves and returns the current date as anNaftahDateinstance.static NaftahTimecurrentTime(NaftahDateParserBaseVisitor<?> naftahDateParserBaseVisitor, NaftahDateParser.ZoneOrOffsetSpecifierContext zoneOrOffsetSpecifier) Resolves and returns the current time as anNaftahTimeinstance.static NaftahTemporalAmountComputes the Arabic temporal amount between twoNaftahTemporalPointinstances.static Pair<NaftahDateLexer,org.antlr.v4.runtime.CommonTokenStream> getCommonTokenStream(org.antlr.v4.runtime.CharStream charStream, List<org.antlr.v4.runtime.ANTLRErrorListener> errorListeners) Creates aCommonTokenStreamand associatedNaftahDateLexerfrom a givenCharStreamand list of ANTLR error listeners.static NaftahDateParsergetParser(org.antlr.v4.runtime.CommonTokenStream commonTokenStream, List<org.antlr.v4.runtime.ANTLRErrorListener> errorListeners) Creates and returns anNaftahDateParserinstance from a givenCommonTokenStreamand list of error listeners.static NaftahDateParserprepareRun(org.antlr.v4.runtime.CharStream input, List<org.antlr.v4.runtime.ANTLRErrorListener> errorListeners) Prepares anNaftahDateParserfor parsing using a list of error listeners.static NaftahDateParserprepareRun(org.antlr.v4.runtime.CharStream input, org.antlr.v4.runtime.ANTLRErrorListener errorListener) Prepares anNaftahDateParserfor parsing using a single error listener.static <T extends NaftahTemporal>
TParses an Arabic date/time expression and returns a typed temporal result.static Objectvisit(NaftahDateParserBaseVisitor<?> naftahDateParserBaseVisitor, org.antlr.v4.runtime.tree.ParseTree tree) Visits the given parse tree using the provided Arabic date parser visitor.
-
Constructor Details
-
NaftahDateParserHelper
private NaftahDateParserHelper()Private constructor to prevent instantiation. Always throws aNaftahBugErrorwhen called.
-
-
Method Details
-
getCommonTokenStream
public static Pair<NaftahDateLexer,org.antlr.v4.runtime.CommonTokenStream> getCommonTokenStream(org.antlr.v4.runtime.CharStream charStream, List<org.antlr.v4.runtime.ANTLRErrorListener> errorListeners) Creates aCommonTokenStreamand associatedNaftahDateLexerfrom a givenCharStreamand list of ANTLR error listeners.- Parameters:
charStream- the input character streamerrorListeners- the list of ANTLR error listeners- Returns:
- a pair containing the lexer and token stream
-
prepareRun
public static NaftahDateParser prepareRun(org.antlr.v4.runtime.CharStream input, org.antlr.v4.runtime.ANTLRErrorListener errorListener) Prepares anNaftahDateParserfor parsing using a single error listener.- Parameters:
input- the input character streamerrorListener- the ANTLR error listener- Returns:
- a configured
NaftahDateParserinstance
-
prepareRun
public static NaftahDateParser prepareRun(org.antlr.v4.runtime.CharStream input, List<org.antlr.v4.runtime.ANTLRErrorListener> errorListeners) Prepares anNaftahDateParserfor parsing using a list of error listeners.Also optionally prints token stream information if debug mode is enabled.
- Parameters:
input- the input character streamerrorListeners- the list of ANTLR error listeners- Returns:
- a configured
NaftahDateParserinstance
-
getParser
public static NaftahDateParser getParser(org.antlr.v4.runtime.CommonTokenStream commonTokenStream, List<org.antlr.v4.runtime.ANTLRErrorListener> errorListeners) Creates and returns anNaftahDateParserinstance from a givenCommonTokenStreamand list of error listeners.- Parameters:
commonTokenStream- the token stream from the lexererrorListeners- the list of ANTLR error listeners- Returns:
- a configured
NaftahDateParserinstance
-
run
Parses an Arabic date/time expression and returns a typed temporal result.This method performs the full parsing pipeline:
- Creates a lexer and parser from the input string
- Registers the default error listener
- Traverses the parse tree using
DefaultNaftahDateParserVisitor - Returns the parsed result cast to the requested temporal type
The returned object may represent:
- A temporal point (date, time, or date-time)
- A temporal amount (duration, period, or a combination)
- Type Parameters:
T- the concreteNaftahTemporalsubtype to return- Parameters:
arabicDate- the Arabic date/time expression to parsetClass- the expected result type- Returns:
- the parsed temporal representation
- Throws:
ClassCastException- if the parsed result cannot be cast totClassRuntimeException- if a parsing or semantic error occurs
-
visit
public static Object visit(NaftahDateParserBaseVisitor<?> naftahDateParserBaseVisitor, org.antlr.v4.runtime.tree.ParseTree tree) Visits the given parse tree using the provided Arabic date parser visitor.This is a convenience method that delegates directly to
AbstractParseTreeVisitor.visit(ParseTree).- Parameters:
naftahDateParserBaseVisitor- the visitor used to traverse the parse treetree- the parse tree to visit- Returns:
- the result produced by the visitor
-
currentTime
public static NaftahTime currentTime(NaftahDateParserBaseVisitor<?> naftahDateParserBaseVisitor, NaftahDateParser.ZoneOrOffsetSpecifierContext zoneOrOffsetSpecifier) Resolves and returns the current time as anNaftahTimeinstance.If a zone or offset specifier is present, it is first visited and resolved using the provided visitor, then applied when computing the current time.
If no zone or offset specifier is provided, the system default is used.
- Parameters:
naftahDateParserBaseVisitor- the visitor used to resolve the zone or offsetzoneOrOffsetSpecifier- the optional zone or offset parse context- Returns:
- an
NaftahTimerepresenting the current time
-
currentDate
public static NaftahDate currentDate(NaftahDateParserBaseVisitor<?> naftahDateParserBaseVisitor, NaftahDateParser.CalendarSpecifierContext calendarSpecifier, NaftahDateParser.ZoneOrOffsetSpecifierContext zoneOrOffsetSpecifier) Resolves and returns the current date as anNaftahDateinstance.The calendar is resolved from the provided calendar specifier if present; otherwise, the default chronology is used.
If a zone or offset specifier is provided, it is applied when determining the current date.
- Parameters:
naftahDateParserBaseVisitor- the visitor used to resolve calendar and zonecalendarSpecifier- the optional calendar parse contextzoneOrOffsetSpecifier- the optional zone or offset parse context- Returns:
- an
NaftahDaterepresenting the current date
-
getArabicTemporalAmountBetween
public static NaftahTemporalAmount getArabicTemporalAmountBetween(NaftahTemporalPoint left, NaftahTemporalPoint right) Computes the Arabic temporal amount between twoNaftahTemporalPointinstances.The result represents the difference between
leftandrightand can be one of the following, depending on the underlyingTemporalobjects:NaftahDuration– if the difference is time-based only (hours, minutes, seconds)NaftahPeriod– if the difference is date-based only (years, months, days)NaftahPeriodWithDuration– if the difference includes both a period and a duration
This method uses
TemporalUtils.between(java.time.temporal.Temporal, java.time.temporal.Temporal)to calculate the rawTemporalAmountand then wraps it in the appropriate Arabic-aware type.- Parameters:
left- the starting temporal pointright- the ending temporal point- Returns:
- an
NaftahTemporalAmountrepresenting the difference betweenleftandright
-