JetTypeName utility

This commit is contained in:
Stepan Koltsov
2012-05-28 20:30:23 +04:00
parent f353f84db8
commit 6244404344
3 changed files with 233 additions and 0 deletions
@@ -0,0 +1,66 @@
/*
* Copyright 2010-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.lang.types.ref;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.resolve.name.FqName;
import java.util.Collections;
import java.util.List;
/**
* @author Stepan Koltsov
*/
public class JetTypeName {
// generic types one day
private final FqName className;
private final List<JetTypeName> arguments;
public JetTypeName(@NotNull FqName className, @NotNull List<JetTypeName> arguments) {
this.className = className;
this.arguments = arguments;
}
@NotNull
public FqName getClassName() {
return className;
}
@NotNull
public List<JetTypeName> getArguments() {
return arguments;
}
@NotNull
public static JetTypeName fromJavaClass(@NotNull Class<?> clazz) {
if (clazz.getTypeParameters().length != 0) {
throw new IllegalArgumentException("cannot create type reference: actual type parameters unknown: " + clazz);
}
FqName fqName = new FqName(clazz.getName());
return withoutParameters(fqName);
}
@NotNull
public static JetTypeName withoutParameters(@NotNull FqName fqName) {
return new JetTypeName(fqName, Collections.<JetTypeName>emptyList());
}
@NotNull
public static JetTypeName parse(@NotNull String value) {
return JetTypeNameParser.parse(value);
}
}
@@ -0,0 +1,74 @@
/*
* Copyright 2010-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.lang.types.ref;
import com.google.common.collect.Lists;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.resolve.name.FqName;
import org.jetbrains.jet.lang.resolve.name.Name;
import java.util.List;
import java.util.regex.Pattern;
/**
* @author Stepan Koltsov
*/
class JetTypeNameParser extends SimpleParser {
public JetTypeNameParser(String input) {
super(input);
}
private static final Pattern namePattern = Pattern.compile("(?i)[a-z][a-z0-9]*");
@NotNull
public Name parseName() {
return Name.identifier(consume(namePattern));
}
@NotNull
public FqName parseFqName() {
FqName fqName = FqName.topLevel(parseName());
while (consumeIfLookingAt('.')) {
fqName = fqName.child(parseName());
}
return fqName;
}
@NotNull
public JetTypeName parse() {
FqName fqName = parseFqName();
List<JetTypeName> typeArguments = Lists.newArrayList();
if (consumeIfLookingAt('<')) {
typeArguments.add(parse());
while (consumeIfLookingAt(',')) {
typeArguments.add(parse());
}
consume('>');
}
return new JetTypeName(fqName, typeArguments);
}
@NotNull
public static JetTypeName parse(@NotNull String string) {
JetTypeNameParser parser = new JetTypeNameParser(string);
JetTypeName typeName = parser.parse();
parser.checkEof();
return typeName;
}
}
@@ -0,0 +1,93 @@
/*
* Copyright 2010-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.lang.types.ref;
import org.jetbrains.annotations.NotNull;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author Stepan Koltsov
*/
class SimpleParser {
private final String input;
private int position = 0;
public SimpleParser(@NotNull String input) {
this.input = input;
}
public void checkEof() {
if (!lookingAtEof()) {
throw new IllegalStateException("expecting EOF");
}
}
public void checkNotEof() {
if (lookingAtEof()) {
throw new IllegalStateException("unexpected EOF");
}
}
public boolean lookingAtEof() {
return position == input.length();
}
public char peek() {
checkNotEof();
return input.charAt(position);
}
public boolean lookingAt(char c) {
return !lookingAtEof() && peek() == c;
}
public char next() {
checkNotEof();
return input.charAt(position++);
}
public void consume(char c) {
char next = next();
if (next != c) {
throw new IllegalStateException("invalid next, expecting " + c + ", actual " + next);
}
}
public boolean consumeIfLookingAt(char c) {
if (lookingAt(c)) {
consume(c);
return true;
}
else {
return false;
}
}
@NotNull
public String consume(@NotNull Pattern pattern) {
Matcher matcher = pattern.matcher(input.substring(position));
if (!matcher.lookingAt()) {
throw new IllegalStateException("expecting " + pattern + " at pos " + position);
}
String r = matcher.group();
position += r.length();
return r;
}
}