Package org.sonar.sslr.grammar
Class LexerfulGrammarBuilder
- java.lang.Object
-
- org.sonar.sslr.grammar.LexerfulGrammarBuilder
-
public class LexerfulGrammarBuilder extends Object
A builder for creating Parsing Expression Grammars for lexerful parsing.Lexer
is required for parsers of such grammars.Objects of following types can be used as an atomic parsing expressions:
- GrammarRuleKey
- TokenType
- String
- Since:
- 1.18
- See Also:
LexerlessGrammarBuilder
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description Object
adjacent(Object e)
Creates parsing expression - "adjacent".Object
anyToken()
Creates parsing expression - "any token".Object
anyTokenButNot(Object e)
Creates parsing expression - "any token but not".Object
bridge(TokenType from, TokenType to)
Creates parsing expression - "bridge".Grammar
build()
Constructs grammar.Grammar
buildWithMemoizationOfMatchesForAllRules()
Constructs grammar with memoization of matches for all rules.protected ParsingExpression
convertToExpression(Object e)
static LexerfulGrammarBuilder
create()
Object
everything()
Deprecated.in 1.19, useanyToken()
instead.Object
exclusiveTill(Object e)
Creates parsing expression - "exclusive till".Object
exclusiveTill(Object e1, Object... rest)
Creates parsing expression - "exclusive till".Object
firstOf(Object e1, Object e2)
Creates parsing expression - "first of".Object
firstOf(Object e1, Object e2, Object... rest)
Creates parsing expression - "first of".Object
isOneOfThem(TokenType t1, TokenType... rest)
Creates parsing expression - "is one of them".Object
next(Object e)
Creates parsing expression - "next".Object
next(Object e1, Object... rest)
Creates parsing expression - "next".Object
nextNot(Object e)
Creates parsing expression - "next not".Object
nextNot(Object e1, Object... rest)
Creates parsing expression - "next not".Object
nothing()
Creates parsing expression - "nothing".Object
oneOrMore(Object e)
Creates parsing expression - "one or more".Object
oneOrMore(Object e1, Object... rest)
Creates parsing expression - "one or more".Object
optional(Object e)
Creates parsing expression - "optional".Object
optional(Object e1, Object... rest)
Creates parsing expression - "optional".GrammarRuleBuilder
rule(GrammarRuleKey ruleKey)
Allows to describe rule.Object
sequence(Object e1, Object e2)
Creates parsing expression - "sequence".Object
sequence(Object e1, Object e2, Object... rest)
Creates parsing expression - "sequence".void
setRootRule(GrammarRuleKey ruleKey)
Allows to specify that given rule should be root for grammar.Object
till(Object e)
Creates parsing expression - "till".Object
tillNewLine()
Creates parsing expression - "till new line".Object
zeroOrMore(Object e)
Creates parsing expression - "zero or more".Object
zeroOrMore(Object e1, Object... rest)
Creates parsing expression - "zero or more".
-
-
-
Method Detail
-
create
public static LexerfulGrammarBuilder create()
-
rule
public GrammarRuleBuilder rule(GrammarRuleKey ruleKey)
Allows to describe rule. Result of this method should be used only for execution of methods in it, i.e. you should not save reference on it. No guarantee that this method always returns the same instance for the same key of rule.
-
setRootRule
public void setRootRule(GrammarRuleKey ruleKey)
Allows to specify that given rule should be root for grammar.
-
build
public Grammar build()
Constructs grammar.- Returns:
- grammar
- Throws:
GrammarException
- if some of rules were used, but not defined- See Also:
buildWithMemoizationOfMatchesForAllRules()
-
buildWithMemoizationOfMatchesForAllRules
public Grammar buildWithMemoizationOfMatchesForAllRules()
Constructs grammar with memoization of matches for all rules.- Returns:
- grammar
- Throws:
GrammarException
- if some of rules were used, but not defined- See Also:
build()
-
adjacent
public Object adjacent(Object e)
Creates parsing expression - "adjacent". During execution of this expression parser will execute sub-expression only if there is no space between next and previous tokens.- Parameters:
e
- sub-expression- Throws:
IllegalArgumentException
- if given argument is not a parsing expression
-
anyTokenButNot
public Object anyTokenButNot(Object e)
Creates parsing expression - "any token but not". Equivalent of expressionsequence(nextNot(e), anyToken())
Do not overuse this method.- Parameters:
e
- sub-expression- Throws:
IllegalArgumentException
- if given argument is not a parsing expression
-
isOneOfThem
public Object isOneOfThem(TokenType t1, TokenType... rest)
Creates parsing expression - "is one of them". During execution of this expression parser will consume following token only if its type belongs to the provided list. Equivalent of expressionfirstOf(t1, rest)
. Do not overuse this method.- Parameters:
t1
- first type of tokenrest
- rest of types
-
bridge
public Object bridge(TokenType from, TokenType to)
Creates parsing expression - "bridge". Equivalent of:rule(bridge).is( from, zeroOrMore(firstOf( sequence(nextNot(firstOf(from, to)), anyToken()), bridge )), to ).skip()
Do not overuse this expression.
-
everything
@Deprecated public Object everything()
Deprecated.in 1.19, useanyToken()
instead.
-
anyToken
public Object anyToken()
Creates parsing expression - "any token". During execution of this expression parser will unconditionally consume following token. This expression fails, if end of input reached.
-
tillNewLine
public Object tillNewLine()
Creates parsing expression - "till new line". During execution of this expression parser will consume all following tokens, which are on the current line. This expression always succeeds. Do not overuse this expression.
-
till
public Object till(Object e)
Creates parsing expression - "till". Equivalent of expressionsequence(zeroOrMore(nextNot(e), anyToken()), e)
. Do not overuse this method.- Parameters:
e
- sub-expression- Throws:
IllegalArgumentException
- if given argument is not a parsing expression
-
exclusiveTill
public Object exclusiveTill(Object e)
Creates parsing expression - "exclusive till". Equivalent of expressionzeroOrMore(nextNot(e), anyToken())
. Do not overuse this method.- Parameters:
e
- sub-expression- Throws:
IllegalArgumentException
- if any of given arguments is not a parsing expression
-
exclusiveTill
public Object exclusiveTill(Object e1, Object... rest)
Creates parsing expression - "exclusive till". Equivalent of expressionzeroOrMore(nextNot(firstOf(e, rest)), anyToken())
. Do not overuse this method.- Parameters:
e1
- first sub-expressionrest
- rest of sub-expressions- Throws:
IllegalArgumentException
- if any of given arguments is not a parsing expression
-
convertToExpression
protected ParsingExpression convertToExpression(Object e)
-
sequence
public final Object sequence(Object e1, Object e2)
Creates parsing expression - "sequence". During execution of this expression parser will sequentially execute all sub-expressions. This expression succeeds only if all sub-expressions succeed.- Parameters:
e1
- first sub-expressione2
- second sub-expression- Throws:
IllegalArgumentException
- if any of given arguments is not a parsing expression
-
sequence
public final Object sequence(Object e1, Object e2, Object... rest)
Creates parsing expression - "sequence". Seesequence(Object, Object)
for more details.- Parameters:
e1
- first sub-expressione2
- second sub-expressionrest
- rest of sub-expressions- Throws:
IllegalArgumentException
- if any of given arguments is not a parsing expression
-
firstOf
public final Object firstOf(Object e1, Object e2)
Creates parsing expression - "first of". During the execution of this expression parser execute sub-expressions in order until one succeeds. This expressions succeeds if any sub-expression succeeds.Be aware that in expression
firstOf("foo", sequence("foo", "bar"))
second sub-expression will never be executed.- Parameters:
e1
- first sub-expressione2
- second sub-expression- Throws:
IllegalArgumentException
- if any of given arguments is not a parsing expression
-
firstOf
public final Object firstOf(Object e1, Object e2, Object... rest)
Creates parsing expression - "first of". SeefirstOf(Object, Object)
for more details.- Parameters:
e1
- first sub-expressione2
- second sub-expressionrest
- rest of sub-expressions- Throws:
IllegalArgumentException
- if any of given arguments is not a parsing expression
-
optional
public final Object optional(Object e)
Creates parsing expression - "optional". During execution of this expression parser will execute sub-expression once. This expression always succeeds, with an empty match if sub-expression fails.Be aware that this expression is greedy, i.e. expression
sequence(optional("foo"), "foo")
will never succeed.- Parameters:
e
- sub-expression- Throws:
IllegalArgumentException
- if given argument is not a parsing expression
-
optional
public final Object optional(Object e1, Object... rest)
Creates parsing expression - "optional". Convenience method equivalent to callingoptional(sequence(e, rest))
.- Parameters:
e1
- first sub-expressionrest
- rest of sub-expressions- Throws:
IllegalArgumentException
- if any of given arguments is not a parsing expression- See Also:
optional(Object)
,sequence(Object, Object)
-
oneOrMore
public final Object oneOrMore(Object e)
Creates parsing expression - "one or more". During execution of this expression parser will repeatedly try sub-expression until it fails. This expression succeeds only if sub-expression succeeds at least once.Be aware that:
- This expression is a greedy, i.e. expression
sequence(oneOrMore("foo"), "foo")
will never succeed. - Sub-expression must not allow empty matches, i.e. for expression
oneOrMore(optional("foo"))
parser will report infinite loop.
- Parameters:
e
- sub-expression- Throws:
IllegalArgumentException
- if given argument is not a parsing expression
- This expression is a greedy, i.e. expression
-
oneOrMore
public final Object oneOrMore(Object e1, Object... rest)
Creates parsing expression - "one or more". Convenience method equivalent to callingoneOrMore(sequence(e1, rest))
.- Parameters:
e1
- first sub-expressionrest
- rest of sub-expressions- Throws:
IllegalArgumentException
- if any of given arguments is not a parsing expression- See Also:
oneOrMore(Object)
,sequence(Object, Object)
-
zeroOrMore
public final Object zeroOrMore(Object e)
Creates parsing expression - "zero or more". During execution of this expression parser will repeatedly try sub-expression until it fails. This expression always succeeds, with an empty match if sub-expression fails.Be aware that:
- This expression is greedy, i.e. expression
sequence(zeroOrMore("foo"), "foo")
will never succeed. - Sub-expression must not allow empty matches, i.e. for expression
zeroOrMore(optional("foo"))
parser will report infinite loop.
- Parameters:
e
- sub-expression- Throws:
IllegalArgumentException
- if given argument is not a parsing expression
- This expression is greedy, i.e. expression
-
zeroOrMore
public final Object zeroOrMore(Object e1, Object... rest)
Creates parsing expression - "zero or more". Convenience method equivalent to callingzeroOrMore(sequence(e1, rest))
.- Parameters:
e1
- sub-expressionrest
- rest of sub-expressions- Throws:
IllegalArgumentException
- if any of given arguments is not a parsing expression- See Also:
zeroOrMore(Object)
,sequence(Object, Object)
-
next
public final Object next(Object e)
Creates parsing expression - "next". During execution of this expression parser will execute sub-expression once. This expression succeeds only if sub-expression succeeds, but never consumes any input.- Parameters:
e
- sub-expression- Throws:
IllegalArgumentException
- if given argument is not a parsing expression
-
next
public final Object next(Object e1, Object... rest)
Creates parsing expression - "next". Convenience method equivalent to callingnext(sequence(e1, rest))
.- Parameters:
e1
- first sub-expressionrest
- rest of sub-expressions- Throws:
IllegalArgumentException
- if any of given arguments is not a parsing expression- See Also:
next(Object)
,sequence(Object, Object)
-
nextNot
public final Object nextNot(Object e)
Creates parsing expression - "next not". During execution of this expression parser will execute sub-expression once. This expression succeeds only if sub-expression fails.- Parameters:
e
- sub-expression- Throws:
IllegalArgumentException
- if given argument is not a parsing expression
-
nextNot
public final Object nextNot(Object e1, Object... rest)
Creates parsing expression - "next not". Convenience method equivalent to callingnextNot(sequence(e1, rest))
.- Parameters:
e1
- sub-expressionrest
- rest of sub-expressions- Throws:
IllegalArgumentException
- if any of given arguments is not a parsing expression- See Also:
nextNot(Object)
,sequence(Object, Object)
-
nothing
public final Object nothing()
Creates parsing expression - "nothing". This expression always fails.
-
-