Package org.sonar.sslr.grammar
Class LexerfulGrammarBuilder
java.lang.Object
org.sonar.sslr.grammar.LexerfulGrammarBuilder
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:
-
Method Summary
Modifier and TypeMethodDescriptionCreates parsing expression - "adjacent".anyToken()Creates parsing expression - "any token".Creates parsing expression - "any token but not".Creates parsing expression - "bridge".build()Constructs grammar.Constructs grammar with memoization of matches for all rules.protected ParsingExpressionstatic LexerfulGrammarBuildercreate()Deprecated.Creates parsing expression - "exclusive till".exclusiveTill(Object e1, Object... rest) Creates parsing expression - "exclusive till".final ObjectCreates parsing expression - "first of".final ObjectCreates parsing expression - "first of".isOneOfThem(TokenType t1, TokenType... rest) Creates parsing expression - "is one of them".final ObjectCreates parsing expression - "next".final ObjectCreates parsing expression - "next".final ObjectCreates parsing expression - "next not".final ObjectCreates parsing expression - "next not".final Objectnothing()Creates parsing expression - "nothing".final ObjectCreates parsing expression - "one or more".final ObjectCreates parsing expression - "one or more".final ObjectCreates parsing expression - "optional".final ObjectCreates parsing expression - "optional".rule(GrammarRuleKey ruleKey) Allows to describe rule.final ObjectCreates parsing expression - "sequence".final ObjectCreates parsing expression - "sequence".voidsetRootRule(GrammarRuleKey ruleKey) Allows to specify that given rule should be root for grammar.Creates parsing expression - "till".Creates parsing expression - "till new line".final ObjectzeroOrMore(Object e) Creates parsing expression - "zero or more".final ObjectzeroOrMore(Object e1, Object... rest) Creates parsing expression - "zero or more".
-
Method Details
-
create
-
rule
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
Allows to specify that given rule should be root for grammar. -
build
Constructs grammar.- Returns:
- grammar
- Throws:
GrammarException- if some of rules were used, but not defined- See Also:
-
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:
-
adjacent
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
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
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
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.in 1.19, useanyToken()instead. -
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
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
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
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
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
-
sequence
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
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
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
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
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
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:
-
oneOrMore
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
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:
-
zeroOrMore
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
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:
-
next
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
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:
-
nextNot
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
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:
-
nothing
Creates parsing expression - "nothing". This expression always fails.
-
anyToken()instead.