|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.spark.repl |
| 19 | + |
| 20 | +import scala.tools.nsc.Settings |
| 21 | +import scala.tools.nsc.interpreter._ |
| 22 | + |
| 23 | +class SparkILoopInterpreter(settings: Settings, out: JPrintWriter) extends IMain(settings, out) { |
| 24 | + self => |
| 25 | + |
| 26 | + override lazy val memberHandlers = new { |
| 27 | + val intp: self.type = self |
| 28 | + } with MemberHandlers { |
| 29 | + import intp.global._ |
| 30 | + |
| 31 | + override def chooseHandler(member: intp.global.Tree): MemberHandler = member match { |
| 32 | + case member: Import => new SparkImportHandler(member) |
| 33 | + case _ => super.chooseHandler (member) |
| 34 | + } |
| 35 | + |
| 36 | + class SparkImportHandler(imp: Import) extends ImportHandler(imp: Import) { |
| 37 | + |
| 38 | + override def targetType: Type = intp.global.rootMirror.getModuleIfDefined("" + expr) match { |
| 39 | + case NoSymbol => intp.typeOfExpression("" + expr) |
| 40 | + case sym => sym.tpe |
| 41 | + } |
| 42 | + |
| 43 | + private def safeIndexOf(name: Name, s: String): Int = fixIndexOf(name, pos(name, s)) |
| 44 | + private def fixIndexOf(name: Name, idx: Int): Int = if (idx == name.length) -1 else idx |
| 45 | + private def pos(name: Name, s: String): Int = { |
| 46 | + var i = name.pos(s.charAt(0), 0) |
| 47 | + val sLen = s.length() |
| 48 | + if (sLen == 1) return i |
| 49 | + while (i + sLen <= name.length) { |
| 50 | + var j = 1 |
| 51 | + while (s.charAt(j) == name.charAt(i + j)) { |
| 52 | + j += 1 |
| 53 | + if (j == sLen) return i |
| 54 | + } |
| 55 | + i = name.pos(s.charAt(0), i + 1) |
| 56 | + } |
| 57 | + name.length |
| 58 | + } |
| 59 | + |
| 60 | + private def isFlattenedSymbol(sym: Symbol): Boolean = |
| 61 | + sym.owner.isPackageClass && |
| 62 | + sym.name.containsName(nme.NAME_JOIN_STRING) && |
| 63 | + sym.owner.info.member(sym.name.take( |
| 64 | + safeIndexOf(sym.name, nme.NAME_JOIN_STRING))) != NoSymbol |
| 65 | + |
| 66 | + private def importableTargetMembers = |
| 67 | + importableMembers(exitingTyper(targetType)).filterNot(isFlattenedSymbol).toList |
| 68 | + |
| 69 | + def isIndividualImport(s: ImportSelector): Boolean = |
| 70 | + s.name != nme.WILDCARD && s.rename != nme.WILDCARD |
| 71 | + def isWildcardImport(s: ImportSelector): Boolean = |
| 72 | + s.name == nme.WILDCARD |
| 73 | + |
| 74 | + // non-wildcard imports |
| 75 | + private def individualSelectors = selectors filter isIndividualImport |
| 76 | + |
| 77 | + override val importsWildcard: Boolean = selectors exists isWildcardImport |
| 78 | + |
| 79 | + lazy val importableSymbolsWithRenames: List[(Symbol, Name)] = { |
| 80 | + val selectorRenameMap = |
| 81 | + individualSelectors.flatMap(x => x.name.bothNames zip x.rename.bothNames).toMap |
| 82 | + importableTargetMembers flatMap (m => selectorRenameMap.get(m.name) map (m -> _)) |
| 83 | + } |
| 84 | + |
| 85 | + override lazy val individualSymbols: List[Symbol] = importableSymbolsWithRenames map (_._1) |
| 86 | + override lazy val wildcardSymbols: List[Symbol] = |
| 87 | + if (importsWildcard) importableTargetMembers else Nil |
| 88 | + |
| 89 | + } |
| 90 | + |
| 91 | + } |
| 92 | + |
| 93 | + object expressionTyper extends { |
| 94 | + val repl: SparkILoopInterpreter.this.type = self |
| 95 | + } with SparkExprTyper { } |
| 96 | + |
| 97 | + override def symbolOfLine(code: String): global.Symbol = |
| 98 | + expressionTyper.symbolOfLine(code) |
| 99 | + |
| 100 | + override def typeOfExpression(expr: String, silent: Boolean): global.Type = |
| 101 | + expressionTyper.typeOfExpression(expr, silent) |
| 102 | + |
| 103 | +} |
0 commit comments