From 227214765dfdacc67a4642b6430cf9e558a2aded Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Fri, 14 Dec 2012 09:38:40 -0600 Subject: [PATCH] Cleaner initialization of predefined sets and maps --- .../antlr/v4/codegen/ActionTranslator.java | 48 ++++++++------ tool/src/org/antlr/v4/tool/AttributeDict.java | 19 +++--- tool/src/org/antlr/v4/tool/Grammar.java | 66 +++++++++---------- tool/src/org/antlr/v4/tool/Rule.java | 34 +++++----- .../org/antlr/v4/tool/ast/GrammarRootAST.java | 9 +-- 5 files changed, 93 insertions(+), 83 deletions(-) diff --git a/tool/src/org/antlr/v4/codegen/ActionTranslator.java b/tool/src/org/antlr/v4/codegen/ActionTranslator.java index 1cb0564f5..95c8d0b5f 100644 --- a/tool/src/org/antlr/v4/codegen/ActionTranslator.java +++ b/tool/src/org/antlr/v4/codegen/ActionTranslator.java @@ -79,29 +79,35 @@ import java.util.Map; /** */ public class ActionTranslator implements ActionSplitterListener { - public static final Map> thisRulePropToModelMap = new HashMap>() {{ - put("start", ThisRulePropertyRef_start.class); - put("stop", ThisRulePropertyRef_stop.class); - put("text", ThisRulePropertyRef_text.class); - put("ctx", ThisRulePropertyRef_ctx.class); - }}; + public static final Map> thisRulePropToModelMap = + new HashMap>(); + static { + thisRulePropToModelMap.put("start", ThisRulePropertyRef_start.class); + thisRulePropToModelMap.put("stop", ThisRulePropertyRef_stop.class); + thisRulePropToModelMap.put("text", ThisRulePropertyRef_text.class); + thisRulePropToModelMap.put("ctx", ThisRulePropertyRef_ctx.class); + } - public static final Map> rulePropToModelMap = new HashMap>() {{ - put("start", RulePropertyRef_start.class); - put("stop", RulePropertyRef_stop.class); - put("text", RulePropertyRef_text.class); - put("ctx", RulePropertyRef_ctx.class); - }}; + public static final Map> rulePropToModelMap = + new HashMap>(); + static { + rulePropToModelMap.put("start", RulePropertyRef_start.class); + rulePropToModelMap.put("stop", RulePropertyRef_stop.class); + rulePropToModelMap.put("text", RulePropertyRef_text.class); + rulePropToModelMap.put("ctx", RulePropertyRef_ctx.class); + } - public static final Map> tokenPropToModelMap = new HashMap>() {{ - put("text", TokenPropertyRef_text.class); - put("type", TokenPropertyRef_type.class); - put("line", TokenPropertyRef_line.class); - put("index", TokenPropertyRef_index.class); - put("pos", TokenPropertyRef_pos.class); - put("channel", TokenPropertyRef_channel.class); - put("int", TokenPropertyRef_int.class); - }}; + public static final Map> tokenPropToModelMap = + new HashMap>(); + static { + tokenPropToModelMap.put("text", TokenPropertyRef_text.class); + tokenPropToModelMap.put("type", TokenPropertyRef_type.class); + tokenPropToModelMap.put("line", TokenPropertyRef_line.class); + tokenPropToModelMap.put("index", TokenPropertyRef_index.class); + tokenPropToModelMap.put("pos", TokenPropertyRef_pos.class); + tokenPropToModelMap.put("channel", TokenPropertyRef_channel.class); + tokenPropToModelMap.put("int", TokenPropertyRef_int.class); + } CodeGenerator gen; ActionAST node; diff --git a/tool/src/org/antlr/v4/tool/AttributeDict.java b/tool/src/org/antlr/v4/tool/AttributeDict.java index 8e34d9120..dac4fcecb 100644 --- a/tool/src/org/antlr/v4/tool/AttributeDict.java +++ b/tool/src/org/antlr/v4/tool/AttributeDict.java @@ -52,15 +52,16 @@ public class AttributeDict { * of predefined attributes. I keep this out of the runtime.Token * object to avoid a runtime type leakage. */ - public static AttributeDict predefinedTokenDict = new AttributeDict(DictType.TOKEN) {{ - add(new Attribute("text")); - add(new Attribute("type")); - add(new Attribute("line")); - add(new Attribute("index")); - add(new Attribute("pos")); - add(new Attribute("channel")); - add(new Attribute("int")); - }}; + public static final AttributeDict predefinedTokenDict = new AttributeDict(DictType.TOKEN); + static { + predefinedTokenDict.add(new Attribute("text")); + predefinedTokenDict.add(new Attribute("type")); + predefinedTokenDict.add(new Attribute("line")); + predefinedTokenDict.add(new Attribute("index")); + predefinedTokenDict.add(new Attribute("pos")); + predefinedTokenDict.add(new Attribute("channel")); + predefinedTokenDict.add(new Attribute("int")); + } public static enum DictType { ARG, RET, LOCAL, TOKEN, diff --git a/tool/src/org/antlr/v4/tool/Grammar.java b/tool/src/org/antlr/v4/tool/Grammar.java index 4363c8c71..42ad3578a 100644 --- a/tool/src/org/antlr/v4/tool/Grammar.java +++ b/tool/src/org/antlr/v4/tool/Grammar.java @@ -72,50 +72,50 @@ import java.util.Set; public class Grammar implements AttributeResolver { public static final String GRAMMAR_FROM_STRING_NAME = ""; - public static final Set parserOptions = new HashSet() {{ - add("superClass"); - add("TokenLabelType"); - add("tokenVocab"); - add("language"); - }}; + public static final Set parserOptions = new HashSet(); + static { + parserOptions.add("superClass"); + parserOptions.add("TokenLabelType"); + parserOptions.add("tokenVocab"); + parserOptions.add("language"); + } public static final Set lexerOptions = parserOptions; - public static final Set ruleOptions = new HashSet() {{ - }}; + public static final Set ruleOptions = new HashSet(); - public static final Set ParserBlockOptions = new HashSet() {{ - }}; + public static final Set ParserBlockOptions = new HashSet(); - public static final Set LexerBlockOptions = new HashSet() {{ - }}; + public static final Set LexerBlockOptions = new HashSet(); /** Legal options for terminal refs like ID */ - public static final Set tokenOptions = new HashSet() {{ - add("assoc"); - }}; + public static final Set tokenOptions = new HashSet(); + static { + tokenOptions.add("assoc"); + } - public static final Set actionOptions = new HashSet() {{ - }}; + public static final Set actionOptions = new HashSet(); - public static final Set semPredOptions = new HashSet() {{ - add("fail"); - }}; + public static final Set semPredOptions = new HashSet(); + static { + semPredOptions.add("fail"); + } - public static final Set doNotCopyOptionsToLexer = - new HashSet() {{ - add("superClass"); - add("TokenLabelType"); - add("tokenVocab"); - }}; + public static final Set doNotCopyOptionsToLexer = new HashSet(); + static { + doNotCopyOptionsToLexer.add("superClass"); + doNotCopyOptionsToLexer.add("TokenLabelType"); + doNotCopyOptionsToLexer.add("tokenVocab"); + } - public static Map grammarAndLabelRefTypeToScope = - new HashMap() {{ - put("parser:RULE_LABEL", Rule.predefinedRulePropertiesDict); - put("parser:TOKEN_LABEL", AttributeDict.predefinedTokenDict); - put("combined:RULE_LABEL", Rule.predefinedRulePropertiesDict); - put("combined:TOKEN_LABEL", AttributeDict.predefinedTokenDict); - }}; + public static final Map grammarAndLabelRefTypeToScope = + new HashMap(); + static { + grammarAndLabelRefTypeToScope.put("parser:RULE_LABEL", Rule.predefinedRulePropertiesDict); + grammarAndLabelRefTypeToScope.put("parser:TOKEN_LABEL", AttributeDict.predefinedTokenDict); + grammarAndLabelRefTypeToScope.put("combined:RULE_LABEL", Rule.predefinedRulePropertiesDict); + grammarAndLabelRefTypeToScope.put("combined:TOKEN_LABEL", AttributeDict.predefinedTokenDict); + } public String name; public GrammarRootAST ast; diff --git a/tool/src/org/antlr/v4/tool/Rule.java b/tool/src/org/antlr/v4/tool/Rule.java index c22143830..db8faef22 100644 --- a/tool/src/org/antlr/v4/tool/Rule.java +++ b/tool/src/org/antlr/v4/tool/Rule.java @@ -51,26 +51,28 @@ public class Rule implements AttributeResolver { * * These must be consistent with ActionTranslator.rulePropToModelMap, ... */ - public static AttributeDict predefinedRulePropertiesDict = - new AttributeDict(AttributeDict.DictType.PREDEFINED_RULE) {{ - add(new Attribute("text")); - add(new Attribute("start")); - add(new Attribute("stop")); - add(new Attribute("ctx")); - }}; + public static final AttributeDict predefinedRulePropertiesDict = + new AttributeDict(AttributeDict.DictType.PREDEFINED_RULE); + static { + predefinedRulePropertiesDict.add(new Attribute("text")); + predefinedRulePropertiesDict.add(new Attribute("start")); + predefinedRulePropertiesDict.add(new Attribute("stop")); + predefinedRulePropertiesDict.add(new Attribute("ctx")); + } - public static Set validLexerCommands = new HashSet() {{ + public static final Set validLexerCommands = new HashSet(); + static { // CALLS - add("mode"); - add("pushMode"); - add("type"); - add("channel"); + validLexerCommands.add("mode"); + validLexerCommands.add("pushMode"); + validLexerCommands.add("type"); + validLexerCommands.add("channel"); // ACTIONS - add("popMode"); - add("skip"); - add("more"); - }}; + validLexerCommands.add("popMode"); + validLexerCommands.add("skip"); + validLexerCommands.add("more"); + } public String name; public List modifiers; diff --git a/tool/src/org/antlr/v4/tool/ast/GrammarRootAST.java b/tool/src/org/antlr/v4/tool/ast/GrammarRootAST.java index 9bacc52a5..ad8f28aaa 100644 --- a/tool/src/org/antlr/v4/tool/ast/GrammarRootAST.java +++ b/tool/src/org/antlr/v4/tool/ast/GrammarRootAST.java @@ -38,10 +38,11 @@ import java.util.HashMap; import java.util.Map; public class GrammarRootAST extends GrammarASTWithOptions { - public static final Map defaultOptions = - new HashMap() {{ - put("language","Java"); - }}; + public static final Map defaultOptions = new HashMap(); + static { + defaultOptions.put("language","Java"); + } + public int grammarType; // LEXER, PARSER, GRAMMAR (combined) public boolean hasErrors; /** Track stream used to create this tree */