Skip to content

Instantly share code, notes, and snippets.

@fge
Created May 4, 2015 07:54
Show Gist options
  • Save fge/45c7e4cce487f55c62cf to your computer and use it in GitHub Desktop.
Save fge/45c7e4cce487f55c62cf to your computer and use it in GitHub Desktop.
package es.litesolutions.sonar.objectscript.ast.grammars;
import es.litesolutions.sonar.objectscript.ast.tokens.flowctl.FlowCtl;
import es.litesolutions.sonar.objectscript.ast.tokens.identifiers.Identifiers;
import es.litesolutions.sonar.objectscript.ast.tokens.operators.Symbols;
import org.sonar.sslr.grammar.GrammarRuleKey;
import org.sonar.sslr.grammar.LexerfulGrammarBuilder;
public enum FlowctlGrammar
implements GrammarRuleKey
{
/*
* IF/ELSEIF/ELSE
*/
IF_STATEMENT,
ELSEIF,
ELSE,
/*
* WHILE
*/
WHILE_STATEMENT,
/*
* TRY/CATCH
*/
TRYCATCH_STATEMENT,
TRYBLOCK,
CATCHARG,
CATCHBLOCK,
/*
* Flow control in general
*/
FLOWCTL,
;
public static void injectInto(final LexerfulGrammarBuilder builder)
{
builder.rule(ELSE).is(
FlowCtl.ELSE,
ObjectScriptGrammar.CODE_BLOCK
).skip();
builder.rule(ELSEIF).is(
FlowCtl.ELSEIF,
ObjectScriptGrammar.BOOLEAN_EXPRESSION,
ObjectScriptGrammar.CODE_BLOCK
).skip();
builder.rule(IF_STATEMENT).is(FlowCtl.IF,
ObjectScriptGrammar.BOOLEAN_EXPRESSION,
ObjectScriptGrammar.CODE_BLOCK, builder.zeroOrMore(ELSEIF),
builder.optional(ELSE));
builder.rule(WHILE_STATEMENT).is(
FlowCtl.WHILE,
ObjectScriptGrammar.BOOLEAN_EXPRESSION,
ObjectScriptGrammar.CODE_BLOCK
);
builder.rule(TRYBLOCK).is(FlowCtl.TRY, ObjectScriptGrammar.CODE_BLOCK);
builder.rule(CATCHARG).is(
builder.firstOf(
builder.sequence(
Symbols.LPAREN,
Identifiers.LOCAL,
Symbols.RPAREN
),
Identifiers.LOCAL
)
);
builder.rule(CATCHBLOCK).is(
FlowCtl.CATCH,
CATCHARG,
ObjectScriptGrammar.CODE_BLOCK
);
builder.rule(TRYCATCH_STATEMENT).is(TRYBLOCK, CATCHBLOCK);
builder.rule(FLOWCTL).is(
builder.firstOf(
IF_STATEMENT,
WHILE_STATEMENT,
TRYCATCH_STATEMENT
)
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment