001: package fri.patterns.interpreter.parsergenerator.util;
002:
003: import fri.patterns.interpreter.parsergenerator.Token;
004:
005: /**
006: The contained methods are needed to generate nonterminal names for spawned rules,
007: e.g. for a repeatable nullable rule "(a | b | c)" from the rule "e ::= (a | b | c)* d;".
008:
009: @author (c) 2000, Fritz Ritzberger
010: */
011:
012: public abstract class SymbolToName {
013: /**
014: Converts the passed character sequence (symbol) to a name that can be used as identifier (but not as Java identifier).
015: If enclosing quotes are found, they will be substituted by "_".
016: @param symbol character sequence to be converted to identifier
017: */
018: public static String makeIdentifier(String symbol) {
019: return makeIdentifier(symbol, false);
020: }
021:
022: /**
023: Converts the passed character sequence (symbol) to a name that can be used as identifier, optionally as Java identifier.
024: If enclosing quotes are found, they will be substituted by "_".
025: @param symbol character sequence to be converted to identifier
026: @param startIsSignificant when true a Java identifier is produced
027: */
028: public static String makeIdentifier(String symbol,
029: boolean startIsSignificant) {
030: return makeIdentifier(symbol, "_", startIsSignificant);
031: }
032:
033: /**
034: Converts the passed character sequence (symbol) to a name that can be used as identifier (but not as Java identifier).
035: If enclosing quotes are found, they will be substituted by passed substitute string.
036: @param symbol character sequence to be converted to identifier
037: @param enclosingQuoteSubstitute the string to be used for enclosing quotes
038: */
039: public static String makeIdentifier(String symbol,
040: String enclosingQuoteSubstitute) {
041: return makeIdentifier(symbol, enclosingQuoteSubstitute, false);
042: }
043:
044: /**
045: Converts the passed character sequence (symbol) to a name that can be used as identifier, optionally as Java identifier.
046: If enclosing quotes are found, they will be substituted by passed substitute string.
047: @param symbol character sequence to be converted to identifier
048: @param enclosingQuoteSubstitute the string to be used for enclosing quotes
049: @param startIsSignificant when true a Java identifier is produced
050: */
051: public static String makeIdentifier(String symbol,
052: String enclosingQuoteSubstitute, boolean startIsSignificant) {
053: if (symbol.equals(Token.UPTO))
054: return "upto";
055:
056: StringBuffer sb = new StringBuffer();
057: int len = symbol.length();
058:
059: for (int i = 0; i < len; i++) {
060: char c = symbol.charAt(i);
061:
062: if (c == '_'
063: && (len == 1 || len == 3 && i == 1
064: && sb.length() > 0 && sb.charAt(0) == '_')
065: || startIsSignificant
066: && i == 0
067: && (c == '$' || !Character.isJavaIdentifierStart(c))
068: || (c == '$' || !Character.isJavaIdentifierPart(c))) {
069: if ((i == 0 || i == len - 1)
070: && (c == '\'' || c == '"' || c == '`'))
071: sb.append(enclosingQuoteSubstitute);
072: else
073: sb.append(convert(c));
074: } else {
075: sb.append(c);
076: }
077: }
078:
079: return sb.toString();
080: }
081:
082: /** Returns a symbolic name for passed symbol, e.g. "lparen" for "(". */
083: public static String convert(char c) {
084: if (c == ':')
085: return "colon";
086: if (c == ';')
087: return "semicol";
088: if (c == '.')
089: return "dot";
090: if (c == ',')
091: return "comma";
092: if (c == '?')
093: return "quest";
094: if (c == '!')
095: return "call";
096: if (c == '@')
097: return "at";
098: if (c == '_')
099: return "uscore";
100: if (c == '<')
101: return "less";
102: if (c == '=')
103: return "equal";
104: if (c == '>')
105: return "greater";
106: if (c == '-')
107: return "minus";
108: if (c == '+')
109: return "plus";
110: if (c == '/')
111: return "slash";
112: if (c == '*')
113: return "star";
114: if (c == '#')
115: return "nr";
116: if (c == '~')
117: return "tilde";
118: if (c == '$')
119: return "dollar";
120: if (c == '%')
121: return "perct";
122: if (c == '&')
123: return "ampers";
124: if (c == '(')
125: return "lparen";
126: if (c == '{')
127: return "lbrace";
128: if (c == '[')
129: return "lbrack";
130: if (c == ')')
131: return "rparen";
132: if (c == '}')
133: return "rbrace";
134: if (c == ']')
135: return "rbrack";
136: if (c == '\\')
137: return "bslash";
138: if (c == '|')
139: return "or";
140: if (c == '&')
141: return "and";
142: if (c == '^')
143: return "caret";
144: if (c == '"')
145: return "dquote";
146: if (c == '\'')
147: return "quote";
148: if (c == '`')
149: return "bquote";
150: if (c >= '0' && c <= '9')
151: return "" + c;
152: return "_"; // default
153: }
154:
155: private SymbolToName() {
156: } // do not instantiate
157: }
|