Class LexerlessGrammarBuilder

java.lang.Object
org.sonar.sslr.grammar.LexerlessGrammarBuilder

public class LexerlessGrammarBuilder extends Object
A builder for creating Parsing Expression Grammars for lexerless parsing.

Objects of following types can be used as an atomic parsing expressions:

  • GrammarRuleKey
  • String
  • Character
Since:
1.18
See Also:
  • Method Details

    • create

      public static LexerlessGrammarBuilder 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 LexerlessGrammar build()
      Constructs grammar.
      Returns:
      grammar
      Throws:
      GrammarException - if some of rules were used, but not defined
    • regexp

      public Object regexp(String regexp)
      Creates parsing expression based on regular expression.
      Parameters:
      regexp - regular expression
      Throws:
      PatternSyntaxException - if the expression's syntax is invalid
    • endOfInput

      public Object endOfInput()
      Creates parsing expression - "end of input". This expression succeeds only if parser reached end of input.
    • token

      public Object token(TokenType tokenType, Object e)
      Creates parsing expression - "token".
      Parameters:
      e - sub-expression
      Throws:
      IllegalArgumentException - if given argument is not a parsing expression
    • commentTrivia

      public Object commentTrivia(Object e)
      Creates parsing expression - "comment trivia".
      Parameters:
      e - sub-expression
      Throws:
      IllegalArgumentException - if given argument is not a parsing expression
    • skippedTrivia

      public Object skippedTrivia(Object e)
      Creates parsing expression - "skipped trivia".
      Parameters:
      e - sub-expression
      Throws:
      IllegalArgumentException - if given argument 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.