Cleaner initialization of predefined sets and maps

This commit is contained in:
Sam Harwell 2012-12-14 09:38:40 -06:00
parent ac55cf3990
commit 227214765d
5 changed files with 93 additions and 83 deletions

View File

@ -79,29 +79,35 @@ import java.util.Map;
/** */ /** */
public class ActionTranslator implements ActionSplitterListener { public class ActionTranslator implements ActionSplitterListener {
public static final Map<String, Class<? extends RulePropertyRef>> thisRulePropToModelMap = new HashMap<String, Class<? extends RulePropertyRef>>() {{ public static final Map<String, Class<? extends RulePropertyRef>> thisRulePropToModelMap =
put("start", ThisRulePropertyRef_start.class); new HashMap<String, Class<? extends RulePropertyRef>>();
put("stop", ThisRulePropertyRef_stop.class); static {
put("text", ThisRulePropertyRef_text.class); thisRulePropToModelMap.put("start", ThisRulePropertyRef_start.class);
put("ctx", ThisRulePropertyRef_ctx.class); thisRulePropToModelMap.put("stop", ThisRulePropertyRef_stop.class);
}}; thisRulePropToModelMap.put("text", ThisRulePropertyRef_text.class);
thisRulePropToModelMap.put("ctx", ThisRulePropertyRef_ctx.class);
}
public static final Map<String, Class<? extends RulePropertyRef>> rulePropToModelMap = new HashMap<String, Class<? extends RulePropertyRef>>() {{ public static final Map<String, Class<? extends RulePropertyRef>> rulePropToModelMap =
put("start", RulePropertyRef_start.class); new HashMap<String, Class<? extends RulePropertyRef>>();
put("stop", RulePropertyRef_stop.class); static {
put("text", RulePropertyRef_text.class); rulePropToModelMap.put("start", RulePropertyRef_start.class);
put("ctx", RulePropertyRef_ctx.class); rulePropToModelMap.put("stop", RulePropertyRef_stop.class);
}}; rulePropToModelMap.put("text", RulePropertyRef_text.class);
rulePropToModelMap.put("ctx", RulePropertyRef_ctx.class);
}
public static final Map<String, Class<? extends TokenPropertyRef>> tokenPropToModelMap = new HashMap<String, Class<? extends TokenPropertyRef>>() {{ public static final Map<String, Class<? extends TokenPropertyRef>> tokenPropToModelMap =
put("text", TokenPropertyRef_text.class); new HashMap<String, Class<? extends TokenPropertyRef>>();
put("type", TokenPropertyRef_type.class); static {
put("line", TokenPropertyRef_line.class); tokenPropToModelMap.put("text", TokenPropertyRef_text.class);
put("index", TokenPropertyRef_index.class); tokenPropToModelMap.put("type", TokenPropertyRef_type.class);
put("pos", TokenPropertyRef_pos.class); tokenPropToModelMap.put("line", TokenPropertyRef_line.class);
put("channel", TokenPropertyRef_channel.class); tokenPropToModelMap.put("index", TokenPropertyRef_index.class);
put("int", TokenPropertyRef_int.class); tokenPropToModelMap.put("pos", TokenPropertyRef_pos.class);
}}; tokenPropToModelMap.put("channel", TokenPropertyRef_channel.class);
tokenPropToModelMap.put("int", TokenPropertyRef_int.class);
}
CodeGenerator gen; CodeGenerator gen;
ActionAST node; ActionAST node;

View File

@ -52,15 +52,16 @@ public class AttributeDict {
* of predefined attributes. I keep this out of the runtime.Token * of predefined attributes. I keep this out of the runtime.Token
* object to avoid a runtime type leakage. * object to avoid a runtime type leakage.
*/ */
public static AttributeDict predefinedTokenDict = new AttributeDict(DictType.TOKEN) {{ public static final AttributeDict predefinedTokenDict = new AttributeDict(DictType.TOKEN);
add(new Attribute("text")); static {
add(new Attribute("type")); predefinedTokenDict.add(new Attribute("text"));
add(new Attribute("line")); predefinedTokenDict.add(new Attribute("type"));
add(new Attribute("index")); predefinedTokenDict.add(new Attribute("line"));
add(new Attribute("pos")); predefinedTokenDict.add(new Attribute("index"));
add(new Attribute("channel")); predefinedTokenDict.add(new Attribute("pos"));
add(new Attribute("int")); predefinedTokenDict.add(new Attribute("channel"));
}}; predefinedTokenDict.add(new Attribute("int"));
}
public static enum DictType { public static enum DictType {
ARG, RET, LOCAL, TOKEN, ARG, RET, LOCAL, TOKEN,

View File

@ -72,50 +72,50 @@ import java.util.Set;
public class Grammar implements AttributeResolver { public class Grammar implements AttributeResolver {
public static final String GRAMMAR_FROM_STRING_NAME = "<string>"; public static final String GRAMMAR_FROM_STRING_NAME = "<string>";
public static final Set<String> parserOptions = new HashSet<String>() {{ public static final Set<String> parserOptions = new HashSet<String>();
add("superClass"); static {
add("TokenLabelType"); parserOptions.add("superClass");
add("tokenVocab"); parserOptions.add("TokenLabelType");
add("language"); parserOptions.add("tokenVocab");
}}; parserOptions.add("language");
}
public static final Set<String> lexerOptions = parserOptions; public static final Set<String> lexerOptions = parserOptions;
public static final Set<String> ruleOptions = new HashSet<String>() {{ public static final Set<String> ruleOptions = new HashSet<String>();
}};
public static final Set<String> ParserBlockOptions = new HashSet<String>() {{ public static final Set<String> ParserBlockOptions = new HashSet<String>();
}};
public static final Set<String> LexerBlockOptions = new HashSet<String>() {{ public static final Set<String> LexerBlockOptions = new HashSet<String>();
}};
/** Legal options for terminal refs like ID<assoc=right> */ /** Legal options for terminal refs like ID<assoc=right> */
public static final Set<String> tokenOptions = new HashSet<String>() {{ public static final Set<String> tokenOptions = new HashSet<String>();
add("assoc"); static {
}}; tokenOptions.add("assoc");
}
public static final Set<String> actionOptions = new HashSet<String>() {{ public static final Set<String> actionOptions = new HashSet<String>();
}};
public static final Set<String> semPredOptions = new HashSet<String>() {{ public static final Set<String> semPredOptions = new HashSet<String>();
add("fail"); static {
}}; semPredOptions.add("fail");
}
public static final Set doNotCopyOptionsToLexer = public static final Set<String> doNotCopyOptionsToLexer = new HashSet<String>();
new HashSet() {{ static {
add("superClass"); doNotCopyOptionsToLexer.add("superClass");
add("TokenLabelType"); doNotCopyOptionsToLexer.add("TokenLabelType");
add("tokenVocab"); doNotCopyOptionsToLexer.add("tokenVocab");
}}; }
public static Map<String, AttributeDict> grammarAndLabelRefTypeToScope = public static final Map<String, AttributeDict> grammarAndLabelRefTypeToScope =
new HashMap<String, AttributeDict>() {{ new HashMap<String, AttributeDict>();
put("parser:RULE_LABEL", Rule.predefinedRulePropertiesDict); static {
put("parser:TOKEN_LABEL", AttributeDict.predefinedTokenDict); grammarAndLabelRefTypeToScope.put("parser:RULE_LABEL", Rule.predefinedRulePropertiesDict);
put("combined:RULE_LABEL", Rule.predefinedRulePropertiesDict); grammarAndLabelRefTypeToScope.put("parser:TOKEN_LABEL", AttributeDict.predefinedTokenDict);
put("combined:TOKEN_LABEL", AttributeDict.predefinedTokenDict); grammarAndLabelRefTypeToScope.put("combined:RULE_LABEL", Rule.predefinedRulePropertiesDict);
}}; grammarAndLabelRefTypeToScope.put("combined:TOKEN_LABEL", AttributeDict.predefinedTokenDict);
}
public String name; public String name;
public GrammarRootAST ast; public GrammarRootAST ast;

View File

@ -51,26 +51,28 @@ public class Rule implements AttributeResolver {
* *
* These must be consistent with ActionTranslator.rulePropToModelMap, ... * These must be consistent with ActionTranslator.rulePropToModelMap, ...
*/ */
public static AttributeDict predefinedRulePropertiesDict = public static final AttributeDict predefinedRulePropertiesDict =
new AttributeDict(AttributeDict.DictType.PREDEFINED_RULE) {{ new AttributeDict(AttributeDict.DictType.PREDEFINED_RULE);
add(new Attribute("text")); static {
add(new Attribute("start")); predefinedRulePropertiesDict.add(new Attribute("text"));
add(new Attribute("stop")); predefinedRulePropertiesDict.add(new Attribute("start"));
add(new Attribute("ctx")); predefinedRulePropertiesDict.add(new Attribute("stop"));
}}; predefinedRulePropertiesDict.add(new Attribute("ctx"));
}
public static Set<String> validLexerCommands = new HashSet<String>() {{ public static final Set<String> validLexerCommands = new HashSet<String>();
static {
// CALLS // CALLS
add("mode"); validLexerCommands.add("mode");
add("pushMode"); validLexerCommands.add("pushMode");
add("type"); validLexerCommands.add("type");
add("channel"); validLexerCommands.add("channel");
// ACTIONS // ACTIONS
add("popMode"); validLexerCommands.add("popMode");
add("skip"); validLexerCommands.add("skip");
add("more"); validLexerCommands.add("more");
}}; }
public String name; public String name;
public List<GrammarAST> modifiers; public List<GrammarAST> modifiers;

View File

@ -38,10 +38,11 @@ import java.util.HashMap;
import java.util.Map; import java.util.Map;
public class GrammarRootAST extends GrammarASTWithOptions { public class GrammarRootAST extends GrammarASTWithOptions {
public static final Map<String, String> defaultOptions = public static final Map<String, String> defaultOptions = new HashMap<String, String>();
new HashMap<String, String>() {{ static {
put("language","Java"); defaultOptions.put("language","Java");
}}; }
public int grammarType; // LEXER, PARSER, GRAMMAR (combined) public int grammarType; // LEXER, PARSER, GRAMMAR (combined)
public boolean hasErrors; public boolean hasErrors;
/** Track stream used to create this tree */ /** Track stream used to create this tree */