Skip to content

Instantly share code, notes, and snippets.

@166MMX
Last active August 29, 2015 14:05
Show Gist options
  • Save 166MMX/3c51992164f001db652a to your computer and use it in GitHub Desktop.
Save 166MMX/3c51992164f001db652a to your computer and use it in GitHub Desktop.
ArrayIndexOutOfBoundsException with imported Lexers https://github.com/antlr/antlr3/issues/161
8:57:35 PM: Executing external task 'generateGrammarSource'...
error(10): internal error: jconfig/src/main/antlr3/com/initvoid/jconfig/zconf/ZConfParser.g : java.lang.ArrayIndexOutOfBoundsException: 2
org.antlr.tool.Rule.trackRuleReferenceInAlt(Rule.java:294)
org.antlr.tool.Grammar.altReferencesRule(Grammar.java:2011)
org.antlr.grammar.v3.DefineGrammarItemsWalker.atom(DefineGrammarItemsWalker.java:3197)
org.antlr.grammar.v3.DefineGrammarItemsWalker.element(DefineGrammarItemsWalker.java:2089)
org.antlr.grammar.v3.DefineGrammarItemsWalker.alternative(DefineGrammarItemsWalker.java:1738)
org.antlr.grammar.v3.DefineGrammarItemsWalker.block(DefineGrammarItemsWalker.java:1639)
org.antlr.grammar.v3.DefineGrammarItemsWalker.rule(DefineGrammarItemsWalker.java:1254)
org.antlr.grammar.v3.DefineGrammarItemsWalker.rules(DefineGrammarItemsWalker.java:981)
org.antlr.grammar.v3.DefineGrammarItemsWalker.grammarSpec(DefineGrammarItemsWalker.java:572)
org.antlr.grammar.v3.DefineGrammarItemsWalker.grammar_(DefineGrammarItemsWalker.java:271)
org.antlr.tool.Grammar.defineGrammarSymbols(Grammar.java:791)
org.antlr.tool.CompositeGrammar.defineGrammarSymbols(CompositeGrammar.java:379)
org.antlr.Tool.process(Tool.java:484)
org.antlr.Tool.main(Tool.java:98)
:generateGrammarSource FAILED
lexer grammar ZConfHelpLexer;
options {
language=Java;
}
@lexer::header {package com.initvoid.jconfig.zconf;}
//input : lines EOF ;
//
//lines : line+ content? ;
//
//line : content? HELP_EOL ;
//
//content : HELP_LEADING_WS HELP_TEXT ;
HELP_LEADING_WS
@init {
int spaces = 0;
StringBuilder sb = new StringBuilder();
}
@after {
while (spaces > 0)
{
if (spaces > 8)
{ sb.append(" "); spaces -= 8; }
else
{ sb.append(" "); spaces--; }
}
emit(new ClassicToken(HELP_LEADING_WS, sb.toString()));
}
: { getCharPositionInLine() == 0 }?
( SP { spaces++; }
| HT { spaces += 8; spaces -= (spaces \% 8); }
)+
;
HELP_TEXT
: ( options { greedy = false; }:
~( WS | CRLF | LF )
)
( options { greedy = true; }:
~( CRLF | LF )
)*
;
HELP_EOL
: CRLF | LF
;
fragment
WS : SP | HT ;
fragment
CRLF : '\r\n' ;
fragment
CR : '\r' ;
fragment
LF : '\n' ;
fragment
HT : '\t' ;
fragment
SP : ' ' ;
lexer grammar ZConfMainLexer;
options {
language=Java;
}
@lexer::header {package com.initvoid.jconfig.zconf;}
COMMENT
: '#' ~('\r'|'\n')* {$channel=HIDDEN;}
;
WS : (' '|'\t') {$channel=HIDDEN;}
;
DASHES
: '---' {$channel=HIDDEN;}
;
T_FOLD
: '\\' T_EOL {$channel=HIDDEN;}
;
T_EOL
: '\r'? '\n'
;
T_HELP_TEXT
: '\u001F' ~('\u001F')* '\u001F'
;
T_WORD_QUOTE
: '"' ( ESC_SEQ | ~('\\'| '"') )* '"'
| '\'' ( ESC_SEQ | ~('\\'|'\'') )* '\''
;
T_WORD
: ('a'..'z'|'A'..'Z'|'0'..'9'|'_'|'-'|'/'|'.')+
;
fragment
HEX_DIGIT
: ('0'..'9'|'a'..'f'|'A'..'F')
;
fragment
ESC_SEQ
: '\\' ('b'|'t'|'n'|'f'|'r'|'\"'|'\''|'\\')
| UNICODE_ESC
| HEX_ESC
| OCTAL_ESC
;
fragment
OCTAL_ESC
: '\\' ('0'..'3') ('0'..'7') ('0'..'7')
| '\\' ('0'..'7') ('0'..'7')
| '\\' ('0'..'7')
;
fragment
HEX_ESC
: '\\' 'x' HEX_DIGIT HEX_DIGIT
;
fragment
UNICODE_ESC
: '\\' 'u' HEX_DIGIT HEX_DIGIT HEX_DIGIT HEX_DIGIT
;
parser grammar ZConfParser;
options {
language=Java;
}
import ZConfMainLexer, ZConfHelpLexer;
tokens {
T_MAINMENU ;
T_MENU ;
T_ENDMENU ;
T_SOURCE ;
T_CHOICE ;
T_ENDCHOICE ;
T_COMMENT ;
T_CONFIG ;
T_MENUCONFIG ;
T_HELP ;
T_IF ;
T_ENDIF ;
T_DEPENDS ;
T_OPTIONAL ;
T_DEFAULT ;
T_PROMPT ;
T_TYPE_TRISTATE ;
T_DEFAULT_TRISTATE ;
T_TYPE_BOOL ;
T_TYPE_BOOLEAN ;
T_DEFAULT_BOOL ;
T_TYPE_INT ;
T_TYPE_HEX ;
T_TYPE_STRING ;
T_SELECT ;
T_RANGE ;
T_VISIBLE ;
T_OPTION ;
T_ON ;
T_OPT_MODULES ;
T_OPT_DEFCONFIG_LIST ;
T_OPT_ENV ;
T_OPT_ALLNOCONFIG_Y ;
HELP_TEXT;
HELP_LEADING_WS;
HELP_EOL;
T_AND ;
T_OR ;
T_OPEN_PAREN ;
T_CLOSE_PAREN ;
T_NOT ;
T_EQUAL ;
T_UNEQUAL ;
T_DQUOT ;
T_SQUOT ;
T_FOLD;
T_EOL;
T_WORD;
T_WORD_QUOTE;
}
@parser::header {package com.initvoid.jconfig.zconf;
import com.initvoid.jconfig.zconf.Input;
import com.initvoid.jconfig.zconf.expr.model.Expression;
import com.initvoid.jconfig.zconf.expr.model.ExpressionImpl;
import com.initvoid.jconfig.zconf.expr.model.Symbol;
import com.initvoid.jconfig.zconf.property.BooleanTypeProperty;
import com.initvoid.jconfig.zconf.property.DefaultBooleanProperty;
import com.initvoid.jconfig.zconf.property.DefaultProperty;
import com.initvoid.jconfig.zconf.property.DefaultTriStateProperty;
import com.initvoid.jconfig.zconf.property.DependsProperty;
import com.initvoid.jconfig.zconf.property.HelpProperty;
import com.initvoid.jconfig.zconf.property.HexTypeProperty;
import com.initvoid.jconfig.zconf.property.IntTypeProperty;
import com.initvoid.jconfig.zconf.property.OptionProperty;
import com.initvoid.jconfig.zconf.property.OptionalProperty;
import com.initvoid.jconfig.zconf.property.PromptProperty;
import com.initvoid.jconfig.zconf.property.Property;
import com.initvoid.jconfig.zconf.property.RangeProperty;
import com.initvoid.jconfig.zconf.property.SelectProperty;
import com.initvoid.jconfig.zconf.property.StringTypeProperty;
import com.initvoid.jconfig.zconf.property.TriStateTypeProperty;
import com.initvoid.jconfig.zconf.property.TypeProperty;
import com.initvoid.jconfig.zconf.property.VisibleProperty;
import com.initvoid.jconfig.zconf.statement.ChoiceStatement;
import com.initvoid.jconfig.zconf.statement.CommentStatement;
import com.initvoid.jconfig.zconf.statement.ConfigStatement;
import com.initvoid.jconfig.zconf.statement.IfStatement;
import com.initvoid.jconfig.zconf.statement.MainMenuStatement;
import com.initvoid.jconfig.zconf.statement.MenuConfigStatement;
import com.initvoid.jconfig.zconf.statement.MenuStatement;
import com.initvoid.jconfig.zconf.statement.SourceStatement;
import com.initvoid.jconfig.zconf.statement.Statement;
import java.util.LinkedList;}
input
returns [ Input result ]
: { result = new Input(); }
( options{ greedy = true; }:
T_EOL
)*
sub_stmt_list=input_sub_stmt_list { result.setStatementList($sub_stmt_list.result); }
;
input_sub_stmt_list
returns [ List<Statement> result ]
: { result = new LinkedList<>(); }
main_menu_stmt? { result.add($main_menu_stmt.result); }
( options{ greedy = true; }:
common_stmt { result.add($common_stmt.result); }
| choice_stmt { result.add($choice_stmt.result); }
| menu_stmt { result.add($menu_stmt.result); }
| T_EOL
)*
;
common_stmt
returns [ Statement result ]
: if_stmt { result = $if_stmt.result; }
| comment_stmt { result = $comment_stmt.result; }
| config_stmt { result = $config_stmt.result; }
| menu_config_stmt { result = $menu_config_stmt.result; }
| source_stmt { result = $source_stmt.result; }
;
config_stmt
returns [ ConfigStatement result ]
: start=config_start { result = $start.result; }
option_list=config_option_list { result.setPropertyList($option_list.result); }
;
config_start
returns [ ConfigStatement result ]
: T_CONFIG { result = new ConfigStatement(); }
T_WORD { result.setSymbolName($T_WORD.text); }
T_EOL
;
config_option_list
returns [ List<Property> result ]
: { result = new LinkedList<>(); }
( options{ greedy = true; }:
config_option { result.add($config_option.result); }
| symbol_option { result.add($symbol_option.result); }
| depends { result.add($depends.result); }
| help { result.add($help.result); }
| T_EOL
)*
;
menu_config_stmt
returns [ MenuConfigStatement result ]
: start=menu_config_start { result = $start.result; }
option_list=config_option_list { result.setPropertyList($option_list.result); }
;
menu_config_start
returns [ MenuConfigStatement result ]
: T_MENUCONFIG { result = new MenuConfigStatement(); }
T_WORD { result.setSymbolName($T_WORD.text); }
T_EOL
;
choice_stmt
returns [ ChoiceStatement result ]
: start=choice_start { result = $start.result; }
option_list=choice_option_list { result.setPropertyList($option_list.result); }
sub_stmt_list=choice_sub_stmt_list { result.setStatementList($sub_stmt_list.result); }
choice_end
;
choice_start
returns [ ChoiceStatement result ]
: T_CHOICE { result = new ChoiceStatement(); }
T_WORD? { result.setSymbolName($T_WORD.text); }
T_EOL
;
choice_option_list
returns [ List<Property> result ]
: { result = new LinkedList<>(); }
( options{ greedy = true; }:
choice_option { result.add($choice_option.result); }
| depends { result.add($depends.result); }
| help { result.add($help.result); }
| T_EOL
)*
;
choice_sub_stmt_list
returns [ List<Statement> result ]
: { result = new LinkedList<>(); }
( options{ greedy = true; }:
common_stmt { result.add($common_stmt.result); }
| T_EOL
)*
;
choice_end
: T_ENDCHOICE
T_EOL
;
comment_stmt
returns [ CommentStatement result ]
: start=comment_start { result = $start.result; }
option_list=comment_option_list { result.setPropertyList($option_list.result); }
;
comment_start
returns [ CommentStatement result ]
: T_COMMENT { result = new CommentStatement(); }
prompt_value { result.setPrompt($prompt_value.result); }
T_EOL
;
comment_option_list
returns [ List<Property> result ]
: { result = new LinkedList<>(); }
( options{ greedy = true; }:
depends { result.add($depends.result); }
| T_EOL
)*
;
menu_stmt
returns [ MenuStatement result ]
: start=menu_start { result = $start.result; }
option_list=menu_option_list { result.setPropertyList($option_list.result); }
sub_stmt_list=menu_sub_stmt_list { result.setStatementList($sub_stmt_list.result); }
menu_end
;
menu_start
returns [ MenuStatement result ]
: T_MENU { result = new MenuStatement(); }
prompt_value { result.setPrompt($prompt_value.result); }
T_EOL
;
menu_option_list
returns [ List<Property> result ]
: { result = new LinkedList<>(); }
( options{ greedy = true; }:
visible { result.add($visible.result); }
| depends { result.add($depends.result); }
| T_EOL
)*
;
menu_sub_stmt_list
returns [ List<Statement> result ]
: { result = new LinkedList<>(); }
( options{ greedy = true; }:
common_stmt { result.add($common_stmt.result); }
| menu_stmt { result.add($menu_stmt.result); }
| choice_stmt { result.add($choice_stmt.result); }
| T_EOL
)*
;
menu_end
: T_ENDMENU
T_EOL
;
if_stmt
returns [ IfStatement result ]
: start=if_start { result = $start.result; }
sub_stmt_list=if_sub_stmt_list { result.setStatementList($sub_stmt_list.result); }
if_end
;
if_start
returns [ IfStatement result ]
: T_IF { result = new IfStatement(); }
expr { result.setCondition($expr.result); }
T_EOL
;
if_sub_stmt_list
returns [ List<Statement> result ]
: { result = new LinkedList<>(); }
( options{ greedy = true; }:
common_stmt { result.add($common_stmt.result); }
| menu_stmt { result.add($menu_stmt.result); }
| choice_stmt { result.add($choice_stmt.result); }
| T_EOL
)*
;
if_end
: T_ENDIF
T_EOL
;
source_stmt
returns [ SourceStatement result ]
: start=source_start { result = $start.result; }
;
source_start
returns [ SourceStatement result ]
: T_SOURCE { result = new SourceStatement(); }
prompt_value { result.setPath($prompt_value.result); }
T_EOL
;
main_menu_stmt
returns [ MainMenuStatement result ]
: start=main_menu_start { result = $start.result; }
;
main_menu_start
returns [ MainMenuStatement result ]
: T_MAINMENU { result = new MainMenuStatement(); }
prompt_value { result.setPrompt($prompt_value.result); }
T_EOL
;
// ================================================================ dd
config_option
returns [ Property result ]
: type_option { result = $type_option.result; }
| prompt_option { result = $prompt_option.result; }
| default_config_option { result = $default_config_option.result; }
| select_option { result = $select_option.result; }
| range_option { result = $range_option.result; }
;
choice_option
returns [ Property result ]
: type_option { result = $type_option.result; }
| prompt_option { result = $prompt_option.result; }
| default_choice_option { result = $default_choice_option.result; }
| optional_option { result = $optional_option.result; }
;
symbol_option
returns [ Property result ]
: option_option { result = $option_option.result; }
;
type_option
returns [ TypeProperty result ]
:
( T_TYPE_BOOL { result = new BooleanTypeProperty(); }
| T_TYPE_BOOLEAN { result = new BooleanTypeProperty(); }
| T_TYPE_TRISTATE { result = new TriStateTypeProperty(); }
| T_TYPE_STRING { result = new StringTypeProperty(); }
| T_TYPE_HEX { result = new HexTypeProperty(); }
| T_TYPE_INT { result = new IntTypeProperty(); }
) prompt_value? { result.setPrompt($prompt_value.result); }
if_option_frag? { result.setCondition($if_option_frag.result); }
T_EOL
;
prompt_option
returns [ PromptProperty result ]
: T_PROMPT { result = new PromptProperty(); }
prompt_value { result.setPrompt($prompt_value.result); }
if_option_frag? { result.setCondition($if_option_frag.result); }
T_EOL
;
default_config_option
returns [ DefaultProperty result ]
:
( T_DEFAULT { result = new DefaultProperty(); }
| T_DEFAULT_BOOL { result = new DefaultBooleanProperty(); }
| T_DEFAULT_TRISTATE { result = new DefaultTriStateProperty(); }
) expr { result.setConfigExpression($expr.result); }
if_option_frag? { result.setCondition($if_option_frag.result); }
T_EOL
;
default_choice_option
returns [ DefaultProperty result ]
:
( T_DEFAULT { result = new DefaultProperty(); }
| T_DEFAULT_BOOL { result = new DefaultBooleanProperty(); }
| T_DEFAULT_TRISTATE { result = new DefaultTriStateProperty(); }
) T_WORD { result.setChoiceValue($T_WORD.text); }
if_option_frag? { result.setCondition($if_option_frag.result); }
T_EOL
;
select_option
returns [ SelectProperty result ]
: T_SELECT { result = new SelectProperty(); }
T_WORD { result.setSymbol($T_WORD.text); }
if_option_frag? { result.setCondition($if_option_frag.result); }
T_EOL
;
range_option
returns [ RangeProperty result ]
: T_RANGE { result = new RangeProperty(); }
from=symbol { result.setFrom($from.result); }
to=symbol { result.setTo($to.result); }
if_option_frag? { result.setCondition($if_option_frag.result); }
T_EOL
;
option_option
returns [ OptionProperty result ]
: T_OPTION { result = new OptionProperty(); }
param_list=option_param_list { result.setParameterList($param_list.result); }
T_EOL
;
optional_option
returns [ OptionalProperty result ]
: T_OPTIONAL { result = new OptionalProperty(); }
T_EOL
;
if_option_frag
returns [ Expression result ]
: T_IF
expr { result = $expr.result; }
;
depends
returns [ DependsProperty result ]
: T_DEPENDS T_ON { result = new DependsProperty(); }
expr { result.setExpression($expr.result); }
T_EOL
;
visible
returns [ VisibleProperty result ]
: T_VISIBLE { result = new VisibleProperty(); }
if_option_frag? { result.setCondition($if_option_frag.result); }
T_EOL
;
help
returns [ HelpProperty result ]
: start=help_start { result = $start.result; }
text=help_text? { result.setText($text.result); }
;
help_start
returns [ HelpProperty result ]
: T_HELP { result = new HelpProperty(); ((TokenStreamSelector)input).push("help"); }
T_EOL
;
help_text
returns [ String result ]
@init { int firstHelpLineIndentLength = -1; StringBuilder sb = new StringBuilder(); }
@after { result = sb.toString(); ((TokenStreamSelector)input).pop(); }
: ( ( ws=HELP_LEADING_WS {
int currentHelpLineIndentLength = $ws.text.length();
if (firstHelpLineIndentLength == -1)
firstHelpLineIndentLength = currentHelpLineIndentLength;
else if (currentHelpLineIndentLength < firstHelpLineIndentLength)
break;
}
text=HELP_TEXT { sb.append($ws.text).append($text.text); }
)?
HELP_EOL { sb.append("\n"); }
)+
( last_ws=HELP_LEADING_WS {
int currentHelpLineIndentLength = $last_ws.text.length();
if (firstHelpLineIndentLength == -1)
firstHelpLineIndentLength = currentHelpLineIndentLength;
else if (currentHelpLineIndentLength < firstHelpLineIndentLength)
break;
}
last_text=HELP_TEXT { sb.append($last_ws.text).append($last_text.text); }
)?
;
// try { HexDump.dump((T_HELP_TEXT59!=null?T_HELP_TEXT59.getText():null).getBytes(), 0, System.out, 0); } catch (IOException e) { e.printStackTrace(); }
option_param_list
returns [ List result ]
: { result = new LinkedList(); }
( options{ greedy = true; }:
modules_option_param
| def_config_list_option_param
| env_option_param
| all_no_config_y_option_param
)*
;
modules_option_param
: T_OPT_MODULES
;
def_config_list_option_param
: T_OPT_DEFCONFIG_LIST
;
env_option_param
: T_OPT_ENV T_EQUAL prompt_value
;
all_no_config_y_option_param
: T_OPT_ALLNOCONFIG_Y
;
prompt_value
returns [ String result ]
: T_WORD { result = $T_WORD.text; }
| T_WORD_QUOTE { result = getQuotedStringValue($T_WORD_QUOTE.text); }
;
// ================================================================
// ================================================================
// T_LPAREN > T_NOT > T_EQUAL, T_UNEQUAL > T_AND > T_OR
// T_OR < T_AND < T_EQUAL, T_UNEQUAL < T_NOT < T_LPAREN
expr
returns [ Expression result ]
: left=or_expr { result = $left.result; }
| T_FOLD left=or_expr { result = $left.result; }
;
or_expr
returns [ Expression result ]
: left=and_expr { result = $left.result; }
( T_OR right=or_expr { result = ExpressionImpl.createOrExpression( $result, $right.result); }
)?
;
and_expr
returns [ Expression result ]
: left=comp_expr { result = $left.result; }
( T_AND right=and_expr { result = ExpressionImpl.createAndExpression( $result, $right.result); }
)?
;
comp_expr
returns [ Expression result ]
: left=not_expr { result = $left.result; }
( T_EQUAL right=not_expr { result = ExpressionImpl.createEqualExpression( $left.result, $right.result); }
| T_UNEQUAL right=not_expr { result = ExpressionImpl.createUnequalExpression( $left.result, $right.result); }
)?
;
not_expr
returns [ Expression result ]
: left=list_expr { result = $left.result; }
| T_NOT right=not_expr { result = ExpressionImpl.createNotExpression( $right.result); }
;
list_expr
returns [ Expression result ]
: left=symbol { result = $left.result; }
| T_OPEN_PAREN right=or_expr T_CLOSE_PAREN
{ result = ExpressionImpl.createListExpression( $right.result); }
;
symbol
returns [ Symbol result ]
: T_WORD { result = new Symbol($T_WORD.text); }
| T_WORD_QUOTE { result = new Symbol(getQuotedStringValue($T_WORD_QUOTE.text)); }
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment