KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antlr > works > grammar > RefactorEngine


1 package org.antlr.works.grammar;
2
3 import org.antlr.works.ate.syntax.misc.ATEScope;
4 import org.antlr.works.ate.syntax.misc.ATEToken;
5 import org.antlr.works.syntax.GrammarSyntaxLexer;
6 import org.antlr.works.syntax.element.*;
7
8 import java.util.List JavaDoc;
9 /*
10
11 [The "BSD licence"]
12 Copyright (c) 2005-2006 Jean Bovet
13 All rights reserved.
14
15 Redistribution and use in source and binary forms, with or without
16 modification, are permitted provided that the following conditions
17 are met:
18
19 1. Redistributions of source code must retain the above copyright
20 notice, this list of conditions and the following disclaimer.
21 2. Redistributions in binary form must reproduce the above copyright
22 notice, this list of conditions and the following disclaimer in the
23 documentation and/or other materials provided with the distribution.
24 3. The name of the author may not be used to endorse or promote products
25 derived from this software without specific prior written permission.
26
27 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
28 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
29 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
30 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
31 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
32 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
36 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37
38 */

39
40 public class RefactorEngine {
41
42     private List JavaDoc<ATEToken> tokens;
43     private RefactorMutator mutator;
44
45     public void setTokens(List JavaDoc<ATEToken> tokens) {
46         this.tokens = tokens;
47     }
48
49     public void setMutator(RefactorMutator mutator) {
50         this.mutator = mutator;
51     }
52
53     public boolean renameToken(ATEToken t, String JavaDoc name) {
54         String JavaDoc attr = t.getAttribute();
55
56         boolean renameRefRule = t.type == GrammarSyntaxLexer.TOKEN_REFERENCE || t.type == GrammarSyntaxLexer.TOKEN_DECL;
57
58         for(int index = tokens.size()-1; index>0; index--) {
59             ATEToken token = tokens.get(index);
60             if(!token.getAttribute().equals(attr)) continue;
61
62             if(token.type == t.type ||
63                     renameRefRule && (token.type == GrammarSyntaxLexer.TOKEN_REFERENCE || token.type == GrammarSyntaxLexer.TOKEN_DECL))
64             {
65                 mutator.replace(token.getStartIndex(), token.getEndIndex(), name);
66             }
67         }
68         return true;
69     }
70
71     /**
72      * Returns true if the scope should be ignored when checking for double-quote literal
73      */

74     public static boolean ignoreScopeForDoubleQuoteLiteral(ATEScope scope) {
75         if(scope == null) return false;
76
77         Class JavaDoc<? extends Object JavaDoc> c = scope.getClass();
78         return c.equals(ElementAction.class) || c.equals(ElementBlock.class) || c.equals(ElementRewriteBlock.class)
79                 || c.equals(ElementRewriteFunction.class) || c.equals(ElementArgumentBlock.class);
80     }
81
82
83 }
84
Popular Tags