KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fri > patterns > interpreter > parsergenerator > util > SymbolToName


1 package fri.patterns.interpreter.parsergenerator.util;
2
3 import fri.patterns.interpreter.parsergenerator.Token;
4
5 /**
6     The contained methods are needed to generate nonterminal names for spawned rules,
7     e.g. for a repeatable nullable rule "(a | b | c)" from the rule "e ::= (a | b | c)* d;".
8
9     @author (c) 2000, Fritz Ritzberger
10 */

11
12 public abstract class SymbolToName
13 {
14     /**
15         Converts the passed character sequence (symbol) to a name that can be used as identifier (but not as Java identifier).
16         If enclosing quotes are found, they will be substituted by "_".
17         @param symbol character sequence to be converted to identifier
18     */

19     public static String JavaDoc makeIdentifier(String JavaDoc symbol) {
20         return makeIdentifier(symbol, false);
21     }
22     
23     /**
24         Converts the passed character sequence (symbol) to a name that can be used as identifier, optionally as Java identifier.
25         If enclosing quotes are found, they will be substituted by "_".
26         @param symbol character sequence to be converted to identifier
27         @param startIsSignificant when true a Java identifier is produced
28     */

29     public static String JavaDoc makeIdentifier(String JavaDoc symbol, boolean startIsSignificant) {
30         return makeIdentifier(symbol, "_", startIsSignificant);
31     }
32     
33     /**
34         Converts the passed character sequence (symbol) to a name that can be used as identifier (but not as Java identifier).
35         If enclosing quotes are found, they will be substituted by passed substitute string.
36         @param symbol character sequence to be converted to identifier
37         @param enclosingQuoteSubstitute the string to be used for enclosing quotes
38     */

39     public static String JavaDoc makeIdentifier(String JavaDoc symbol, String JavaDoc enclosingQuoteSubstitute) {
40         return makeIdentifier(symbol, enclosingQuoteSubstitute, false);
41     }
42     
43     /**
44         Converts the passed character sequence (symbol) to a name that can be used as identifier, optionally as Java identifier.
45         If enclosing quotes are found, they will be substituted by passed substitute string.
46         @param symbol character sequence to be converted to identifier
47         @param enclosingQuoteSubstitute the string to be used for enclosing quotes
48         @param startIsSignificant when true a Java identifier is produced
49     */

50     public static String JavaDoc makeIdentifier(String JavaDoc symbol, String JavaDoc enclosingQuoteSubstitute, boolean startIsSignificant) {
51         if (symbol.equals(Token.UPTO))
52             return "upto";
53             
54         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
55         int len = symbol.length();
56         
57         for (int i = 0; i < len; i++) {
58             char c = symbol.charAt(i);
59
60             if (c == '_' && (len == 1 || len == 3 && i == 1 && sb.length() > 0 && sb.charAt(0) == '_') ||
61                     startIsSignificant && i == 0 && (c == '$' || !Character.isJavaIdentifierStart(c)) ||
62                     (c == '$' || !Character.isJavaIdentifierPart(c)))
63             {
64                 if ((i == 0 || i == len - 1) && (c == '\'' || c == '"' || c == '`'))
65                     sb.append(enclosingQuoteSubstitute);
66                 else
67                     sb.append(convert(c));
68             }
69             else {
70                 sb.append(c);
71             }
72         }
73         
74         return sb.toString();
75     }
76     
77
78     /** Returns a symbolic name for passed symbol, e.g. "lparen" for "(". */
79     public static String JavaDoc convert(char c) {
80         if (c == ':')
81             return "colon";
82         if (c == ';')
83             return "semicol";
84         if (c == '.')
85             return "dot";
86         if (c == ',')
87             return "comma";
88         if (c == '?')
89             return "quest";
90         if (c == '!')
91             return "call";
92         if (c == '@')
93             return "at";
94         if (c == '_')
95             return "uscore";
96         if (c == '<')
97             return "less";
98         if (c == '=')
99             return "equal";
100         if (c == '>')
101             return "greater";
102         if (c == '-')
103             return "minus";
104         if (c == '+')
105             return "plus";
106         if (c == '/')
107             return "slash";
108         if (c == '*')
109             return "star";
110         if (c == '#')
111             return "nr";
112         if (c == '~')
113             return "tilde";
114         if (c == '$')
115             return "dollar";
116         if (c == '%')
117             return "perct";
118         if (c == '&')
119             return "ampers";
120         if (c == '(')
121             return "lparen";
122         if (c == '{')
123             return "lbrace";
124         if (c == '[')
125             return "lbrack";
126         if (c == ')')
127             return "rparen";
128         if (c == '}')
129             return "rbrace";
130         if (c == ']')
131             return "rbrack";
132         if (c == '\\')
133             return "bslash";
134         if (c == '|')
135             return "or";
136         if (c == '&')
137             return "and";
138         if (c == '^')
139             return "caret";
140         if (c == '"')
141             return "dquote";
142         if (c == '\'')
143             return "quote";
144         if (c == '`')
145             return "bquote";
146         if (c >= '0' && c <= '9')
147             return ""+c;
148         return "_"; // default
149
}
150     
151     
152     private SymbolToName() {} // do not instantiate
153
}
154     
155     
Popular Tags