Chromium Code Reviews| Index: compiler/java/com/google/dart/compiler/SystemLibrariesReader.java |
| =================================================================== |
| --- compiler/java/com/google/dart/compiler/SystemLibrariesReader.java (revision 0) |
| +++ compiler/java/com/google/dart/compiler/SystemLibrariesReader.java (revision 0) |
| @@ -0,0 +1,183 @@ |
| +/* |
| + * Copyright (c) 2012, the Dart project authors. |
| + * |
| + * Licensed under the Eclipse Public License v1.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.eclipse.org/legal/epl-v10.html |
| + * |
| + * 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 com.google.dart.compiler; |
| + |
| +import com.google.common.io.CharStreams; |
| +import com.google.common.io.Closeables; |
| +import com.google.dart.compiler.CompilerConfiguration.ErrorFormat; |
| +import com.google.dart.compiler.ast.ASTVisitor; |
| +import com.google.dart.compiler.ast.DartBooleanLiteral; |
| +import com.google.dart.compiler.ast.DartExpression; |
| +import com.google.dart.compiler.ast.DartFieldDefinition; |
| +import com.google.dart.compiler.ast.DartIdentifier; |
| +import com.google.dart.compiler.ast.DartIntegerLiteral; |
| +import com.google.dart.compiler.ast.DartMapLiteralEntry; |
| +import com.google.dart.compiler.ast.DartNamedExpression; |
| +import com.google.dart.compiler.ast.DartNewExpression; |
| +import com.google.dart.compiler.ast.DartNode; |
| +import com.google.dart.compiler.ast.DartStringLiteral; |
| +import com.google.dart.compiler.ast.DartUnit; |
| +import com.google.dart.compiler.metrics.CompilerMetrics; |
| +import com.google.dart.compiler.parser.DartParser; |
| +import com.google.dart.compiler.util.DartSourceString; |
| + |
| +import java.io.File; |
| +import java.io.FileInputStream; |
| +import java.io.IOException; |
| +import java.io.InputStreamReader; |
| +import java.io.Reader; |
| +import java.nio.charset.Charset; |
| +import java.util.HashMap; |
| +import java.util.List; |
| +import java.util.Map; |
| + |
| +/** |
| + * A reader that parses and reads the libraries dart-sdk/lib/_internal/libraries.dart file |
| + * for system library information |
| + * |
| + * library information is in the format |
| + * |
| + * |
| + * final Map<String, LibraryInfo> LIBRARIES = const <LibraryInfo> { |
| + * |
| + * // Used by VM applications |
| + * "builtin": const LibraryInfo( |
| + * "builtin/builtin_runtime.dart", |
| + * category: "Server", |
| + * platforms: VM_PLATFORM), |
| + * |
| + * "compiler": const LibraryInfo( |
| + * "compiler/compiler.dart", |
| + * category: "Tools", |
| + * platforms: 0), |
| + * |
| + * }; |
| + * |
| + */ |
| +public class SystemLibrariesReader { |
| + |
| + |
| + class LibrariesFileAstVistor extends ASTVisitor<Void>{ |
| + |
| + @Override |
| + public Void visitMapLiteralEntry(DartMapLiteralEntry node) { |
| + |
| + Map<String, String> map = new HashMap<String, String>(); |
| + String keyString = null; |
| + DartExpression key = node.getKey(); |
| + if (key instanceof DartStringLiteral){ |
| + keyString = "dart:" + ((DartStringLiteral) key).getValue(); |
| + } |
| + DartExpression value = node.getValue(); |
| + if (value instanceof DartNewExpression){ |
| + List<DartExpression> args = ((DartNewExpression) value).getArguments(); |
| + for (DartExpression arg : args){ |
| + if (arg instanceof DartStringLiteral){ |
| + map.put(KEY_PATH, ((DartStringLiteral) arg).getValue()); |
| + } |
| + if (arg instanceof DartNamedExpression){ |
| + DartExpression expression = ((DartNamedExpression) arg).getExpression(); |
| + if (expression instanceof DartBooleanLiteral || expression instanceof DartIntegerLiteral){ |
| + map.put(((DartNamedExpression) arg).getName().getName(), expression.toString()); |
| + } else if (expression instanceof DartStringLiteral){ |
| + map.put(((DartNamedExpression) arg).getName().getName(), ((DartStringLiteral) expression).getValue()); |
| + } else if (expression instanceof DartIdentifier){ |
| + map.put(((DartNamedExpression)arg).getName().getName(), ((DartIdentifier)expression).getName()); |
| + } |
| + } |
| + } |
| + } |
| + librariesMap.put(keyString,map); |
| + return null; |
| + } |
| + |
| + } |
| + |
| + |
| + public static final String LIBRARIES_FILE = "libraries.dart"; |
| + public static final String INTERNAL_DIR = "_internal"; |
| + private static final String KEY_PATH = "path"; |
| + |
| + private final File sdkLibPath; |
| + private final Map<String,Map<String, String>> librariesMap = new HashMap<String,Map<String, String>>(); |
| + |
| + |
| + public SystemLibrariesReader(File sdkLibPath) { |
| + this.sdkLibPath =sdkLibPath; |
| + loadLibraryInfo(); |
| + } |
| + |
| + public Map<String, Map<String, String>> getLibrariesMap() { |
| + return librariesMap; |
| + } |
|
danrubel
2012/08/28 00:25:45
Better would a a method that returns Map<String, D
keertip
2012/08/28 20:41:57
Done.
|
| + |
| + private void loadLibraryInfo(){ |
| + File librariesFile = getLibrariesFile(); |
| + |
| + DartUnit unit = getDartUnit(librariesFile); |
| + |
| + List<DartNode> nodes = unit.getTopLevelNodes(); |
| + for (DartNode node : nodes){ |
| + if (node instanceof DartFieldDefinition){ |
| + node.accept(new LibrariesFileAstVistor()); |
| + } |
| + } |
| + } |
| + |
| + |
| + /** |
| + * Parse the given dart file and return the AST |
| + * @param librariesFile the dart file to parse |
| + * @return the parsed AST |
| + */ |
| + private DartUnit getDartUnit(File librariesFile) { |
| + |
| + String srcCode = getSource(librariesFile); |
| + DartSourceString dartSource = new DartSourceString(LIBRARIES_FILE, srcCode); |
| + DefaultDartCompilerListener listener = new DefaultDartCompilerListener(ErrorFormat.NORMAL); |
| + DartParser parser = new DartParser(dartSource, srcCode, false, null, listener, new CompilerMetrics()); |
| + return parser.parseUnit(); |
| + } |
| + |
| + |
| + private String getSource(File file) { |
| + String srcCode = null; |
| + Reader r = null; |
| + boolean failed = true; |
| + try { |
| + r = new InputStreamReader(new FileInputStream(file), Charset.forName("UTF8")); |
| + srcCode = CharStreams.toString(r); |
| + failed = false; |
| + } catch (IOException e) { |
| + e.printStackTrace(); |
| + } finally { |
| + try { |
| + Closeables.close(r, failed); |
| + } catch (IOException e) { |
| + e.printStackTrace(); |
| + } |
| + } |
| + return srcCode; |
| + } |
| + |
| + private File getLibrariesFile() { |
| + File file = new File(new File(sdkLibPath, INTERNAL_DIR), LIBRARIES_FILE); |
| + if (!file.exists()) { |
| + throw new InternalCompilerException("Failed to find " + file.toString() |
| + + ". Is dart-sdk path correct?"); |
| + } |
| + return file; |
| + } |
| + |
| +} |