📄 rule.java
字号:
} public Set getTokenRefsInAlt(int altNum) { return altToTokenRefMap[altNum].keySet(); } /** For use with rewrite rules, we must track all tokens matched on the * left-hand-side; so we need Lists. This is a unique list of all * token types for which the rule needs a list of tokens. This * is called from the rule template not directly by the code generator. */ public Set getAllTokenRefsInAltsWithRewrites() { String output = (String)grammar.getOption("output"); Set tokens = new HashSet(); if ( output==null || !output.equals("AST") ) { // return nothing if not generating trees; i.e., don't do for templates return tokens; } for (int i = 1; i <= numberOfAlts; i++) { if ( altsWithRewrites[i] ) { Map m = altToTokenRefMap[i]; Set s = m.keySet(); for (Iterator it = s.iterator(); it.hasNext();) { // convert token name like ID to ID, "void" to 31 String tokenName = (String) it.next(); int ttype = grammar.getTokenType(tokenName); String label = grammar.generator.getTokenTypeAsTargetLabel(ttype); tokens.add(label); } } } return tokens; } public Set getRuleRefsInAlt(int outerAltNum) { return altToRuleRefMap[outerAltNum].keySet(); } /** For use with rewrite rules, we must track all rule AST results on the * left-hand-side; so we need Lists. This is a unique list of all * rule results for which the rule needs a list of results. */ public Set getAllRuleRefsInAltsWithRewrites() { Set rules = new HashSet(); for (int i = 1; i <= numberOfAlts; i++) { if ( altsWithRewrites[i] ) { Map m = altToRuleRefMap[i]; rules.addAll(m.keySet()); } } return rules; } public List<GrammarAST> getInlineActions() { return inlineActions; } public boolean hasRewrite(int i) { return altsWithRewrites[i]; } /** Track which rules have rewrite rules. Pass in the ALT node * for the alt so we can check for problems when output=template, * rewrite=true, and grammar type is tree parser. */ public void trackAltsWithRewrites(GrammarAST altAST, int outerAltNum) { if ( grammar.type==Grammar.TREE_PARSER && grammar.buildTemplate() && grammar.getOption("rewrite")!=null && grammar.getOption("rewrite").equals("true") ) { GrammarAST firstElementAST = (GrammarAST)altAST.getFirstChild(); grammar.sanity.ensureAltIsSimpleNodeOrTree(altAST, firstElementAST, outerAltNum); } altsWithRewrites[outerAltNum] = true; } /** Return the scope containing name */ public AttributeScope getAttributeScope(String name) { AttributeScope scope = getLocalAttributeScope(name); if ( scope!=null ) { return scope; } if ( ruleScope!=null && ruleScope.getAttribute(name)!=null ) { scope = ruleScope; } return scope; } /** Get the arg, return value, or predefined property for this rule */ public AttributeScope getLocalAttributeScope(String name) { AttributeScope scope = null; if ( returnScope!=null && returnScope.getAttribute(name)!=null ) { scope = returnScope; } else if ( parameterScope!=null && parameterScope.getAttribute(name)!=null ) { scope = parameterScope; } else { AttributeScope rulePropertiesScope = RuleLabelScope.grammarTypeToRulePropertiesScope[grammar.type]; if ( rulePropertiesScope.getAttribute(name)!=null ) { scope = rulePropertiesScope; } } return scope; } /** For references to tokens rather than by label such as $ID, we * need to get the existing label for the ID ref or create a new * one. */ public String getElementLabel(String refdSymbol, int outerAltNum, CodeGenerator generator) { GrammarAST uniqueRefAST; if ( grammar.type != Grammar.LEXER && Character.isUpperCase(refdSymbol.charAt(0)) ) { // symbol is a token List tokenRefs = getTokenRefsInAlt(refdSymbol, outerAltNum); uniqueRefAST = (GrammarAST)tokenRefs.get(0); } else { // symbol is a rule List ruleRefs = getRuleRefsInAlt(refdSymbol, outerAltNum); uniqueRefAST = (GrammarAST)ruleRefs.get(0); } if ( uniqueRefAST.code==null ) { // no code? must not have gen'd yet; forward ref return null; } String labelName = null; String existingLabelName = (String)uniqueRefAST.code.getAttribute("label"); // reuse any label or list label if it exists if ( existingLabelName!=null ) { labelName = existingLabelName; } else { // else create new label labelName = generator.createUniqueLabel(refdSymbol); CommonToken label = new CommonToken(ANTLRParser.ID, labelName); if ( grammar.type != Grammar.LEXER && Character.isUpperCase(refdSymbol.charAt(0)) ) { grammar.defineTokenRefLabel(name, label, uniqueRefAST); } else { grammar.defineRuleRefLabel(name, label, uniqueRefAST); } uniqueRefAST.code.setAttribute("label", labelName); } return labelName; } /** If a rule has no user-defined return values and nobody references * it's start/stop (predefined attributes), then there is no need to * define a struct; otherwise for now we assume a struct. A rule also * has multiple return values if you are building trees or templates. */ public boolean getHasMultipleReturnValues() { return referencedPredefinedRuleAttributes || grammar.buildAST() || grammar.buildTemplate() || (returnScope!=null && returnScope.attributes.size()>1); } public boolean getHasSingleReturnValue() { return !(referencedPredefinedRuleAttributes || grammar.buildAST() || grammar.buildTemplate()) && (returnScope!=null && returnScope.attributes.size()==1); } public boolean getHasReturnValue() { return referencedPredefinedRuleAttributes || grammar.buildAST() || grammar.buildTemplate() || (returnScope!=null && returnScope.attributes.size()>0); } public String getSingleValueReturnType() { if ( returnScope!=null && returnScope.attributes.size()==1 ) { Collection retvalAttrs = returnScope.attributes.values(); Object[] javaSucks = retvalAttrs.toArray(); return ((Attribute)javaSucks[0]).type; } return null; } public String getSingleValueReturnName() { if ( returnScope!=null && returnScope.attributes.size()==1 ) { Collection retvalAttrs = returnScope.attributes.values(); Object[] javaSucks = retvalAttrs.toArray(); return ((Attribute)javaSucks[0]).name; } return null; } /** Given @scope::name {action} define it for this grammar. Later, * the code generator will ask for the actions table. */ public void defineNamedAction(GrammarAST ampersandAST, GrammarAST nameAST, GrammarAST actionAST) { //System.out.println("rule @"+nameAST.getText()+"{"+actionAST.getText()+"}"); String actionName = nameAST.getText(); GrammarAST a = (GrammarAST)actions.get(actionName); if ( a!=null ) { ErrorManager.grammarError( ErrorManager.MSG_ACTION_REDEFINITION,grammar, nameAST.getToken(),nameAST.getText()); } else { actions.put(actionName,actionAST); } } public void trackInlineAction(GrammarAST actionAST) { inlineActions.add(actionAST); } public Map<String, GrammarAST> getActions() { return actions; } public void setActions(Map<String, GrammarAST> actions) { this.actions = actions; } /** Save the option key/value pair and process it; return the key * or null if invalid option. */ public String setOption(String key, Object value, antlr.Token optionsStartToken) { if ( !legalOptions.contains(key) ) { ErrorManager.grammarError(ErrorManager.MSG_ILLEGAL_OPTION, grammar, optionsStartToken, key); return null; } if ( options==null ) { options = new HashMap(); } if ( key.equals("k") ) { grammar.numberOfManualLookaheadOptions++; } options.put(key, value); return key; } public void setOptions(Map options, antlr.Token optionsStartToken) { if ( options==null ) { this.options = null; return; } Set keys = options.keySet(); for (Iterator it = keys.iterator(); it.hasNext();) { String optionName = (String) it.next(); Object optionValue = options.get(optionName); String stored=setOption(optionName, optionValue, optionsStartToken); if ( stored==null ) { it.remove(); } } } public String toString() { // used for testing if ( modifier!=null ) { return modifier+" "+name; } return name; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -