From 62444043447991b987f8698987dcde2fa9b6b8b6 Mon Sep 17 00:00:00 2001 From: Stepan Koltsov Date: Mon, 28 May 2012 20:30:23 +0400 Subject: [PATCH] JetTypeName utility --- .../jet/lang/types/ref/JetTypeName.java | 66 +++++++++++++ .../jet/lang/types/ref/JetTypeNameParser.java | 74 +++++++++++++++ .../jet/lang/types/ref/SimpleParser.java | 93 +++++++++++++++++++ 3 files changed, 233 insertions(+) create mode 100644 compiler/frontend/src/org/jetbrains/jet/lang/types/ref/JetTypeName.java create mode 100644 compiler/frontend/src/org/jetbrains/jet/lang/types/ref/JetTypeNameParser.java create mode 100644 compiler/frontend/src/org/jetbrains/jet/lang/types/ref/SimpleParser.java diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/types/ref/JetTypeName.java b/compiler/frontend/src/org/jetbrains/jet/lang/types/ref/JetTypeName.java new file mode 100644 index 00000000000..77c69dd301f --- /dev/null +++ b/compiler/frontend/src/org/jetbrains/jet/lang/types/ref/JetTypeName.java @@ -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 arguments; + + public JetTypeName(@NotNull FqName className, @NotNull List arguments) { + this.className = className; + this.arguments = arguments; + } + + @NotNull + public FqName getClassName() { + return className; + } + + @NotNull + public List 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.emptyList()); + } + + @NotNull + public static JetTypeName parse(@NotNull String value) { + return JetTypeNameParser.parse(value); + } +} diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/types/ref/JetTypeNameParser.java b/compiler/frontend/src/org/jetbrains/jet/lang/types/ref/JetTypeNameParser.java new file mode 100644 index 00000000000..e1fc5ee026e --- /dev/null +++ b/compiler/frontend/src/org/jetbrains/jet/lang/types/ref/JetTypeNameParser.java @@ -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 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; + } + +} diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/types/ref/SimpleParser.java b/compiler/frontend/src/org/jetbrains/jet/lang/types/ref/SimpleParser.java new file mode 100644 index 00000000000..6e20eb28b99 --- /dev/null +++ b/compiler/frontend/src/org/jetbrains/jet/lang/types/ref/SimpleParser.java @@ -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; + } +}