01: /*
02: * xtc - The eXTensible Compiler
03: * Copyright (C) 2005-2007 Robert Grimm
04: *
05: * This program is free software; you can redistribute it and/or
06: * modify it under the terms of the GNU General Public License
07: * version 2 as published by the Free Software Foundation.
08: *
09: * This program is distributed in the hope that it will be useful,
10: * but WITHOUT ANY WARRANTY; without even the implied warranty of
11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12: * GNU General Public License for more details.
13: *
14: * You should have received a copy of the GNU General Public License
15: * along with this program; if not, write to the Free Software
16: * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
17: * USA.
18: */
19: package xtc.parser;
20:
21: import java.util.ArrayList;
22:
23: import xtc.Constants;
24:
25: import xtc.tree.Attribute;
26:
27: import xtc.util.Runtime;
28:
29: /**
30: * Visitor to detect productions that can be treated as transient.
31: *
32: * <p />Note that this visitor requires that a grammar's productions
33: * have been {@link ReferenceCounter reference counted}. Further note
34: * that this visitor assumes that the entire grammar is contained in a
35: * single module.
36: *
37: * @author Robert Grimm
38: * @version $Revision: 1.22 $
39: */
40: public class TransientMarker extends GrammarVisitor {
41:
42: /**
43: * Create a new transient marker.
44: *
45: * @param runtime The runtime.
46: * @param analyzer The analyzer utility.
47: */
48: public TransientMarker(Runtime runtime, Analyzer analyzer) {
49: super (runtime, analyzer);
50: }
51:
52: /**
53: * Visit the specified grammar.
54: *
55: * @param m The grammar module.
56: * @return <code>Boolean.TRUE</code> if the grammar has been modified,
57: * otherwise <code>Boolean.FALSE</code>.
58: */
59: public Object visit(Module m) {
60: // Initialize the per-grammar state.
61: analyzer.register(this );
62: analyzer.init(m);
63:
64: // Process the productions.
65: boolean changed = false;
66: for (Production p : m.productions) {
67: MetaData md = (MetaData) p
68: .getProperty(Properties.META_DATA);
69:
70: if ((1 >= md.usageCount)
71: && (!p.hasAttribute(Constants.ATT_TRANSIENT))
72: && (!p.hasAttribute(Constants.ATT_INLINE))
73: && (!p.hasAttribute(Constants.ATT_MEMOIZED))) {
74: if (runtime.test("optionVerbose")) {
75: System.err.println("[Marking " + p.qName
76: + " as transient]");
77: }
78:
79: if (null == p.attributes) {
80: p.attributes = new ArrayList<Attribute>(1);
81: }
82: p.attributes.add(Constants.ATT_TRANSIENT);
83: changed = true;
84: }
85: }
86:
87: // Done.
88: return Boolean.valueOf(changed);
89: }
90:
91: }
|