1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
|
/*
* Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package propertiesparser.parser;
/**
* Common interface to all kinds of diagnostic argument types.
*/
public interface MessageType {
/**
* Visitor method.
*/
<R, A> R accept(Visitor<R, A> v, A a);
/**
* The type as mentioned in the resource file.
*/
String kindName();
/**
* A custom type is a type for which a predefined alternative does not exist. As such, it is an
* handy option when prototyping - but usages of custom types should be avoided in product-quality
* resource file comments.
*
* Example: 'com.sun.tools.javac.code.Flags.Flag'
*/
public static class CustomType implements MessageType {
/** The string-based representation of this type. */
public String typeString;
public CustomType(String typeString) {
this.typeString = typeString;
}
@Override
public String kindName() {
return typeString;
}
@Override
public <R, A> R accept(Visitor<R, A> v, A a) {
return v.visitCustomType(this, a);
}
}
/**
* A predefined type. All common types mentioned in the resource file comments are meant to
* be included here.
*/
public enum SimpleType implements MessageType {
BOOLEAN("boolean", "boolean", null),
FRAGMENT("fragment", "Fragment", null),
DIAGNOSTIC("diagnostic", "JCDiagnostic", "com.sun.tools.javac.util"),
MODIFIER("modifier", "Modifier", "javax.lang.model.element"),
FILE("file", "File", "java.io"),
FILE_OBJECT("file object", "JavaFileObject", "javax.tools"),
PATH("path", "Path", "java.nio.file"),
NAME("name", "Name", "com.sun.tools.javac.util"),
NUMBER("number", "int", null),
OPTION_NAME("option name", "Option", "com.sun.tools.javac.main"),
SOURCE_VERSION("source version", "Source", "com.sun.tools.javac.code"),
STRING("string", "String", null),
SYMBOL("symbol", "Symbol", "com.sun.tools.javac.code"),
SYMBOL_KIND("symbol kind", "Kind", "com.sun.tools.javac.code.Kinds"),
KIND_NAME("kind name", "KindName", "com.sun.tools.javac.code.Kinds"),
TOKEN("token", "TokenKind", "com.sun.tools.javac.parser.Tokens"),
TYPE("type", "Type", "com.sun.tools.javac.code"),
SET("set", "Set", "java.util"),
LIST("list", "List", "java.util"),
OBJECT("object", "Object", null),
UNUSED("unused", "Void", null),
UNKNOWN("<unknown>", "UnknownType", null);
/** name of the predefined type as mentioned in the resource file. */
public final String kindName;
/** string-based representation of the type */
public final String clazz;
/** type qualifier (might be null) */
public final String qualifier;
SimpleType(String kindName, String clazz, String qualifier) {
this.kindName = kindName;
this.clazz = clazz;
this.qualifier = qualifier;
}
@Override
public String kindName() {
return kindName;
}
@Override
public <R, A> R accept(Visitor<R, A> v, A a) {
return v.visitSimpleType(this, a);
}
}
/**
* A compound type is a collection of some element type.
*
* Example: list of string
*/
public static class CompoundType implements MessageType {
/**
* Compound type kind.
*/
public enum Kind {
LIST("list of", SimpleType.LIST),
SET("set of", SimpleType.SET);
public final String kindName;
public final SimpleType clazz;
Kind(String kindName, SimpleType clazz) {
this.kindName = kindName;
this.clazz = clazz;
}
}
/** The compound type kind. */
public final Kind kind;
/** The element type. */
public final MessageType elemtype;
public CompoundType(Kind kind, MessageType elemtype) {
this.kind = kind;
this.elemtype = elemtype;
}
@Override
public String kindName() {
return kind.kindName;
}
@Override
public <R, A> R accept(Visitor<R, A> v, A a) {
return v.visitCompoundType(this, a);
}
}
/**
* A union type represents an alternative between two (or more) types. It can be useful to
* define the type of an argument which can assume multiple (unrelated) values; union types
* are only meant to be used in cases where the alternative comes up frequently enough in the
* resource file comments - in order to avoid cluttered comments.
*
* Example: message segment
*/
public static class UnionType implements MessageType {
/**
* Union type kind.
*/
public enum Kind {
MESSAGE_SEGMENT("message segment", SimpleType.DIAGNOSTIC, SimpleType.FRAGMENT),
FILE_NAME("file name", SimpleType.FILE, SimpleType.FILE_OBJECT);
final String kindName;
final SimpleType[] choices;
Kind(String kindName, SimpleType... choices) {
this.kindName = kindName;
this.choices = choices;
}
}
/** The union type kind. */
public final Kind kind;
/** The union type alternatives. */
public final MessageType[] choices;
UnionType(Kind kind) {
this(kind, kind.choices);
}
protected UnionType(Kind kind, MessageType[] choices) {
this.choices = choices;
this.kind = kind;
}
@Override
public String kindName() {
return kind.kindName;
}
@Override
public <R, A> R accept(Visitor<R, A> v, A a) {
return v.visitUnionType(this, a);
}
}
/**
* A subclass of union type representing 'explicit' alternatives in the resource file comments.
* Note: as the token 'or' is parsed with lowest priority, it is not possible, for instance,
* to form a compound type out of an 'or' type. In such cases a plain union type should be used
* instead.
*
* Examples: symbol or type
*/
public static class OrType extends UnionType {
public static final String OR_NAME = "or";
@Override
public String kindName() {
return OR_NAME;
}
public OrType(MessageType... choices) {
super(null, choices);
}
}
/**
* Visitor class.
*/
public static abstract class Visitor<R, A> {
public abstract R visitCustomType(CustomType t, A a);
public abstract R visitSimpleType(SimpleType t, A a);
public abstract R visitCompoundType(CompoundType t, A a);
public abstract R visitUnionType(UnionType t, A a);
}
}
|