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:
  • Method Details

    • 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

      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:
    • 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 expression sequence(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 expression firstOf(t1, rest). Do not overuse this method.
      Parameters:
      t1 - first type of token
      rest - 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, use anyToken() 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 expression sequence(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 expression zeroOrMore(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 expression zeroOrMore(nextNot(firstOf(e, rest)), anyToken()). Do not overuse this method.
      Parameters:
      e1 - first sub-expression
      rest - 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-expression
      e2 - 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". See sequence(Object, Object) for more details.
      Parameters:
      e1 - first sub-expression
      e2 - second sub-expression
      rest - 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-expression
      e2 - 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". See firstOf(Object, Object) for more details.
      Parameters:
      e1 - first sub-expression
      e2 - second sub-expression
      rest - 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 calling optional(sequence(e, rest)).
      Parameters:
      e1 - first sub-expression
      rest - rest of sub-expressions
      Throws:
      IllegalArgumentException - if any of given arguments is not a parsing expression
      See Also:
    • 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
    • oneOrMore

      public final Object oneOrMore(Object e1, Object... rest)
      Creates parsing expression - "one or more". Convenience method equivalent to calling oneOrMore(sequence(e1, rest)).
      Parameters:
      e1 - first sub-expression
      rest - rest of sub-expressions
      Throws:
      IllegalArgumentException - if any of given arguments is not a parsing expression
      See Also:
    • 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
    • zeroOrMore

      public final Object zeroOrMore(Object e1, Object... rest)
      Creates parsing expression - "zero or more". Convenience method equivalent to calling zeroOrMore(sequence(e1, rest)).
      Parameters:
      e1 - sub-expression
      rest - rest of sub-expressions
      Throws:
      IllegalArgumentException - if any of given arguments is not a parsing expression
      See Also:
    • 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 calling next(sequence(e1, rest)).
      Parameters:
      e1 - first sub-expression
      rest - rest of sub-expressions
      Throws:
      IllegalArgumentException - if any of given arguments is not a parsing expression
      See Also:
    • 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 calling nextNot(sequence(e1, rest)).
      Parameters:
      e1 - sub-expression
      rest - rest of sub-expressions
      Throws:
      IllegalArgumentException - if any of given arguments is not a parsing expression
      See Also:
    • nothing

      public final Object nothing()
      Creates parsing expression - "nothing". This expression always fails.