001: /*
002: [The "BSD licence"]
003: Copyright (c) 2005-2006 Terence Parr
004: All rights reserved.
005:
006: Redistribution and use in source and binary forms, with or without
007: modification, are permitted provided that the following conditions
008: are met:
009: 1. Redistributions of source code must retain the above copyright
010: notice, this list of conditions and the following disclaimer.
011: 2. Redistributions in binary form must reproduce the above copyright
012: notice, this list of conditions and the following disclaimer in the
013: documentation and/or other materials provided with the distribution.
014: 3. The name of the author may not be used to endorse or promote products
015: derived from this software without specific prior written permission.
016:
017: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
018: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
019: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
020: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
021: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
022: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
023: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
024: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
026: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027: */
028: package org.antlr.tool;
029:
030: import antlr.Token;
031:
032: public class RuleLabelScope extends AttributeScope {
033: /** Rules have a predefined set of attributes as well as
034: * the return values. 'text' needs to be computed though so.
035: */
036: public static AttributeScope predefinedRulePropertiesScope = new AttributeScope(
037: "RulePredefined", null) {
038: {
039: addAttribute("text", null);
040: addAttribute("start", null);
041: addAttribute("stop", null);
042: addAttribute("tree", null);
043: addAttribute("st", null);
044: isPredefinedRuleScope = true;
045: }
046: };
047:
048: public static AttributeScope predefinedTreeRulePropertiesScope = new AttributeScope(
049: "RulePredefined", null) {
050: {
051: addAttribute("text", null);
052: addAttribute("start", null); // note: no stop; not meaningful
053: addAttribute("tree", null);
054: addAttribute("st", null);
055: isPredefinedRuleScope = true;
056: }
057: };
058:
059: public static AttributeScope predefinedLexerRulePropertiesScope = new AttributeScope(
060: "LexerRulePredefined", null) {
061: {
062: addAttribute("text", null);
063: addAttribute("type", null);
064: addAttribute("line", null);
065: addAttribute("index", null);
066: addAttribute("pos", null);
067: addAttribute("channel", null);
068: addAttribute("start", null);
069: addAttribute("stop", null);
070: isPredefinedLexerRuleScope = true;
071: }
072: };
073:
074: public static AttributeScope[] grammarTypeToRulePropertiesScope = {
075: null, predefinedLexerRulePropertiesScope, // LEXER
076: predefinedRulePropertiesScope, // PARSER
077: predefinedTreeRulePropertiesScope, // TREE_PARSER
078: predefinedRulePropertiesScope, // COMBINED
079: };
080:
081: public Rule referencedRule;
082:
083: public RuleLabelScope(Rule referencedRule, Token actionToken) {
084: super ("ref_" + referencedRule.name, actionToken);
085: this .referencedRule = referencedRule;
086: }
087:
088: /** If you label a rule reference, you can access that rule's
089: * return values as well as any predefined attributes.
090: */
091: public Attribute getAttribute(String name) {
092: AttributeScope rulePropertiesScope = RuleLabelScope.grammarTypeToRulePropertiesScope[grammar.type];
093: if (rulePropertiesScope.getAttribute(name) != null) {
094: return rulePropertiesScope.getAttribute(name);
095: }
096:
097: if (referencedRule.returnScope != null) {
098: return referencedRule.returnScope.getAttribute(name);
099: }
100: return null;
101: }
102: }
|