org.sonar.sslr.grammar
Class LexerfulGrammarBuilder

java.lang.Object
  extended by 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:

Since:
1.18
See Also:
LexerlessGrammarBuilder

Method Summary
 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)
           
protected  ParsingExpression[] convertToExpressions(List<Object> expressions)
           
static LexerfulGrammarBuilder create()
           
static LexerfulGrammarBuilder createBasedOn(LexerfulGrammarBuilder... base)
           
 Object everything()
          Deprecated. in 1.19, use anyToken() 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".
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

create

public static LexerfulGrammarBuilder create()

createBasedOn

public static LexerfulGrammarBuilder createBasedOn(LexerfulGrammarBuilder... base)

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 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:
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:

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:
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:

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:
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 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:
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 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:
nextNot(Object), sequence(Object, Object)

nothing

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


convertToExpressions

protected final ParsingExpression[] convertToExpressions(List<Object> expressions)


Copyright © 2009-2014 SonarSource. All Rights Reserved.