Package com.gengoai.parsing
Class Grammar
- java.lang.Object
-
- com.gengoai.parsing.Grammar
-
- All Implemented Interfaces:
Serializable
public class Grammar extends Object implements Serializable
A grammar representing the rules for parsing. Rules are defined using
ParserHandler
s, which are associated with individualTag
s. There are two main types of handlers, prefix and postfix. ThePrefixHandler
takes care of prefix operators and thePostfixHandler
handles infix and postfix operators.By default a grammar will throw a
ParseException
when it encounters a token type that it does not know how to handle. Grammars can be set to instead ignore these tokens.- Author:
- David B. Bracewell
- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description Grammar()
Grammar(@NonNull GrammarRegistrable... registrables)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Optional<PostfixHandler>
getPostfixHandler(ParserToken token)
Gets the postfix handler associated with theTag
of the givenParserToken
Optional<PostfixHandler>
getPostfixHandler(Tag tag)
Gets the postfix handler associated with the givenTag
.Optional<PrefixHandler>
getPrefixHandler(ParserToken token)
Gets the prefix handler associated with theTag
of the givenParserToken
Optional<PrefixHandler>
getPrefixHandler(Tag tag)
Gets the prefix handler associated with the givenTag
.boolean
isIgnored(ParserToken token)
Checks if the given token should be ignored during parsingboolean
isIgnored(Tag tag)
Checks if the given tag should be ignored during parsingGrammar
postfix(Tag tag, PostfixHandler handler)
Grammar
postfix(Tag tag, PostfixHandler handler, int precedence)
Registers aPostfixHandler
with the given precedence for the givenTag
<E extends Expression>
Grammarpostfix(Tag tag, PostfixHandler handler, int precedence, SerializablePredicate<E> validator)
Registers aPostfixHandler
with the given precedence for the givenTag
int
precedenceOf(ParserToken token)
Determines the precedence of theTag
of the givenParserToken
int
precedenceOf(Tag tag)
Determines the precedence of the given tagGrammar
prefix(Tag tag, PrefixHandler handler)
Registers aPrefixHandler
for the givenTag
<E extends Expression>
Grammarprefix(Tag tag, PrefixHandler handler, SerializablePredicate<E> validator)
Registers aPrefixHandler
for the givenTag
Grammar
skip(Tag tag)
Registers the givenTag
as one which should be skipped during parsing.void
validatePostfix(Expression expression)
Validates the given expression as if it were generated from a postfix handler.void
validatePrefix(Expression expression)
Validates the given expression as if it were generated from a prefix handler.
-
-
-
Constructor Detail
-
Grammar
public Grammar()
-
Grammar
public Grammar(@NonNull @NonNull GrammarRegistrable... registrables)
-
-
Method Detail
-
getPostfixHandler
public Optional<PostfixHandler> getPostfixHandler(ParserToken token)
Gets the postfix handler associated with theTag
of the givenParserToken
- Parameters:
token
- the token to use to determine the correctPostfixHandler
to retrieve- Returns:
- An
Optional
containing the postfix handler if available or empty if none are available
-
getPostfixHandler
public Optional<PostfixHandler> getPostfixHandler(Tag tag)
Gets the postfix handler associated with the givenTag
.- Parameters:
tag
- the tag representing the operator / value for which we want to retrieve aPostfixHandler
- Returns:
- An
Optional
containing the postfix handler if available or empty if none are available
-
getPrefixHandler
public Optional<PrefixHandler> getPrefixHandler(ParserToken token)
Gets the prefix handler associated with theTag
of the givenParserToken
- Parameters:
token
- the token to use to determine the correctPrefixHandler
to retrieve- Returns:
- An
Optional
containing the prefix handler if available or empty if none are available
-
getPrefixHandler
public Optional<PrefixHandler> getPrefixHandler(Tag tag)
Gets the prefix handler associated with the givenTag
.- Parameters:
tag
- the tag representing the operator / value for which we want to retrieve aPrefixHandler
- Returns:
- An
Optional
containing the postfix handler if available or empty if none are available
-
isIgnored
public boolean isIgnored(ParserToken token)
Checks if the given token should be ignored during parsing- Parameters:
token
- the token- Returns:
- True - if should be ignored, False otherwise
-
isIgnored
public boolean isIgnored(Tag tag)
Checks if the given tag should be ignored during parsing- Parameters:
tag
- the tag- Returns:
- True - if should be ignored, False otherwise
-
postfix
public Grammar postfix(Tag tag, PostfixHandler handler)
- Parameters:
tag
- the tag for which the handler will be registeredhandler
- the handler to associate with the given tag- Returns:
- the grammar
-
postfix
public Grammar postfix(Tag tag, PostfixHandler handler, int precedence)
Registers aPostfixHandler
with the given precedence for the givenTag
- Parameters:
tag
- the tag for which the handler will be registeredhandler
- the handler to associate with the given tagprecedence
- the precedence of the tag (operator). Note that precedence will be the given value or 1 if the given value is less than 1.- Returns:
- the grammar
-
postfix
public <E extends Expression> Grammar postfix(Tag tag, PostfixHandler handler, int precedence, SerializablePredicate<E> validator)
Registers aPostfixHandler
with the given precedence for the givenTag
- Type Parameters:
E
- the type parameter- Parameters:
tag
- the tag for which the handler will be registeredhandler
- the handler to associate with the given tagprecedence
- the precedence of the tag (operator). Note that precedence will be the given value or 1 if the given value is less than 1.validator
- the validator to use validate the generated expression- Returns:
- the grammar
-
precedenceOf
public int precedenceOf(Tag tag)
Determines the precedence of the given tag- Parameters:
tag
- the tag- Returns:
- the precedence
-
precedenceOf
public int precedenceOf(ParserToken token)
Determines the precedence of theTag
of the givenParserToken
- Parameters:
token
- the token for which the precedence will bec calculated.- Returns:
- the precedence
-
prefix
public Grammar prefix(Tag tag, PrefixHandler handler)
Registers aPrefixHandler
for the givenTag
- Parameters:
tag
- the tag for which the handler will be registeredhandler
- the handler to associate with the given tag- Returns:
- the grammar
-
prefix
public <E extends Expression> Grammar prefix(Tag tag, PrefixHandler handler, SerializablePredicate<E> validator)
Registers aPrefixHandler
for the givenTag
- Parameters:
tag
- the tag for which the handler will be registeredhandler
- the handler to associate with the given tagvalidator
- the validator to use validate the generated expression- Returns:
- the grammar
-
skip
public Grammar skip(Tag tag)
Registers the givenTag
as one which should be skipped during parsing.- Parameters:
tag
- the tag which will be skipped.- Returns:
- the grammar
-
validatePostfix
public void validatePostfix(Expression expression) throws ParseException
Validates the given expression as if it were generated from a postfix handler.- Parameters:
expression
- the expression to validate- Throws:
ParseException
- the exception if the expression is invalid
-
validatePrefix
public void validatePrefix(Expression expression) throws ParseException
Validates the given expression as if it were generated from a prefix handler.- Parameters:
expression
- the expression to validate- Throws:
ParseException
- the exception if the expression is invalid
-
-