treebrowsers.scala

来自「JAVA 语言的函数式编程扩展」· SCALA 代码 · 共 694 行 · 第 1/2 页

SCALA
694
字号
        ("Ident", name)      case Literal(value) =>        ("Literal", EMPTY)      case TypeTree() =>        ("TypeTree", EMPTY)      case Annotated(annot, arg) =>        ("Annotated", EMPTY)              case Annotation(constr, elements) =>        ("Annotation", EMPTY)              case SingletonTypeTree(ref) =>        ("SingletonType", EMPTY)      case SelectFromTypeTree(qualifier, selector) =>        ("SelectFromType", selector)      case CompoundTypeTree(template) =>        ("CompoundType", EMPTY)      case AppliedTypeTree(tpe, args) =>        ("AppliedType", EMPTY)      case TypeBoundsTree(lo, hi) =>        ("TypeBoundsTree", EMPTY)            case ExistentialTypeTree(tpt, whereClauses) =>        ("ExistentialTypeTree", EMPTY)              case Try(block, catcher, finalizer) =>        ("Try", EMPTY)      case EmptyTree =>        ("Empty", EMPTY)      case ArrayValue(elemtpt, trees) =>        ("ArrayValue", EMPTY)      case Star(t) =>        ("Star", EMPTY)    }    /** Return a list of children for the given tree node */    def children(t: Tree): List[Tree] = t match {      case ProgramTree(units) =>        units      case UnitTree(unit) =>        List(unit.body)      case DocDef(comment, definition) =>        List(definition)      case ClassDef(mods, name, tparams, impl) => {        var children: List[Tree] = List()        children = tparams ::: children        impl :: children      }      case PackageDef(name, stats) =>        stats      case ModuleDef(mods, name, impl) =>        List(impl)      case ValDef(mods, name, tpe, rhs) =>        List(tpe, rhs)      case DefDef(mods, name, tparams, vparams, tpe, rhs) => {        var children: List[Tree] = List()        children = tparams ::: children        children = List.flatten(vparams) ::: children        tpe :: rhs :: children      }      case TypeDef(mods, name, tparams, rhs) =>        rhs :: tparams // @M: was List(rhs, lobound)      case Import(expr, selectors) => {        var children: List[Tree] = List(expr)        children      }      case CaseDef(pat, guard, body) =>        List(pat, guard, body)      case Template(parents, self, body) =>        parents ::: List(self) ::: body      case LabelDef(name, params, rhs) =>        params ::: List(rhs)      case Block(stats, expr) =>        stats ::: List(expr)      case Sequence(trees) =>        trees      case Alternative(trees) =>        trees      case Bind(name, rhs) =>        List(rhs)              case UnApply(fun, args) =>        fun :: args              case Match(selector, cases) =>        selector :: cases      case Function(vparams, body) =>        vparams ::: List(body)      case Assign(lhs, rhs) =>        List(lhs, rhs)      case If(cond, thenp, elsep) =>        List(cond, thenp, elsep)      case Return(expr) =>        List(expr)      case Throw(expr) =>        List(expr)      case New(init) =>        List(init)      case Typed(expr, tpe) =>        List(expr, tpe)      case TypeApply(fun, args) =>        List(fun) ::: args      case Apply(fun, args) =>        List(fun) ::: args      case ApplyDynamic(qual, args) =>        List(qual) ::: args      case Super(qualif, mix) =>        Nil      case This(qualif) =>        Nil      case Select(qualif, selector) =>        List(qualif)      case Ident(name) =>        Nil      case Literal(value) =>        Nil      case TypeTree() =>        Nil      case Annotated(annot, arg) =>        annot.constr :: annot.elements ::: List(arg)            case Annotation(constr, elements) =>        constr :: elements              case SingletonTypeTree(ref) =>        List(ref)      case SelectFromTypeTree(qualif, selector) =>        List(qualif)      case CompoundTypeTree(templ) =>        List(templ)      case AppliedTypeTree(tpe, args) =>        tpe :: args      case TypeBoundsTree(lo, hi) =>        List(lo, hi)      case ExistentialTypeTree(tpt, whereClauses) =>        tpt :: whereClauses              case Try(block, catches, finalizer) =>        block :: catches ::: List(finalizer)      case ArrayValue(elemtpt, elems) =>        elemtpt :: elems      case EmptyTree =>        Nil      case Star(t) =>        List(t)    }    /** Return a textual representation of this t's symbol */    def symbolText(t: Tree): String = {      var prefix = ""      if (t.hasSymbol)        prefix = "[has] "      if (t.isDef)        prefix = "[defines] "      prefix + t.symbol    }    /** Return t's symbol type  */    def symbolTypeDoc(t: Tree): Document = {      val s = t.symbol      if (s ne null)        TypePrinter.toDocument(s.info)      else        DocNil    }    /** Return a textual representation of (some of) the symbol's     * attributes */    def symbolAttributes(t: Tree): String = {      val s = t.symbol      var att = ""      if (s ne null) {        var str = flagsToString(s.flags)        if (s.hasFlag(STATIC) || s.hasFlag(STATICMEMBER))          str = str + " isStatic ";        str      }      else ""    }  }  object TypePrinter {    ///////////////// Document pretty printer ////////////////    implicit def view(n: String): Document = DocText(n)    def toDocument(sym: Symbol): Document =      toDocument(sym.info)    def symsToDocument(syms: List[Symbol]): Document = syms match {      case Nil => DocNil      case s :: Nil => Document.group(toDocument(s))      case _ =>        Document.group(          syms.tail.foldLeft (toDocument(syms.head) :: ", ") (             (d: Document, s2: Symbol) => toDocument(s2) :: ", " :/: d) )    }    def toDocument(ts: List[Type]): Document = ts match {      case Nil => DocNil      case t :: Nil => Document.group(toDocument(t))      case _ =>        Document.group(          ts.tail.foldLeft (toDocument(ts.head) :: ", ") (             (d: Document, t2: Type) => toDocument(t2) :: ", " :/: d) )    }    def toDocument(t: Type): Document = t match {      case ErrorType => "ErrorType()"      case WildcardType => "WildcardType()"      case NoType => "NoType()"      case NoPrefix => "NoPrefix()"      case ThisType(s) => "ThisType(" + s.name + ")"      case SingleType(pre, sym) =>        Document.group(          Document.nest(4, "SingleType(" :/:                        toDocument(pre) :: ", " :/: sym.name.toString() :: ")")        )      case ConstantType(value) =>         "ConstantType(" + value + ")"      case TypeRef(pre, sym, args) =>        Document.group(          Document.nest(4, "TypeRef(" :/:                         toDocument(pre) :: ", " :/:                        sym.name.toString() :: ", " :/:                        "[ " :: toDocument(args) ::"]" :: ")")        )      case TypeBounds(lo, hi) =>        Document.group(          Document.nest(4, "TypeBounds(" :/:                         toDocument(lo) :: ", " :/:                        toDocument(hi) :: ")")        )       case RefinedType(parents, defs) =>        Document.group(          Document.nest(4, "RefinedType(" :/:                        toDocument(parents) :: ")")        )      case ClassInfoType(parents, defs, clazz) =>        Document.group(          Document.nest(4,"ClassInfoType(" :/:                        toDocument(parents) :: ", " :/:                        clazz.name.toString() :: ")")        )      case MethodType(paramtypes, result) =>        Document.group(          Document.nest(4, "MethodType(" :/:                        Document.group("(" :/:                                       toDocument(paramtypes) :/:                                       "), ") :/:                        toDocument(result) :: ")")        )      case PolyType(tparams, result) =>        Document.group(          Document.nest(4,"PolyType(" :/:                        Document.group("(" :/:                                        symsToDocument(tparams) :/:                                       "), ") :/:                        toDocument(result) :: ")")        )              case AnnotatedType(attribs, tp, _) =>        Document.group(          Document.nest(4, "AnnotatedType(" :/:                        attribs.mkString("[", ",", "]") :/:                        "," :/: toDocument(tp) :: ")")        )              case ExistentialType(tparams, result) =>        Document.group(            Document.nest(4, "ExistentialType(" :/:                Document.group("(" :/: symsToDocument(tparams) :/: "), ") :/:                toDocument(result) :: ")"))      case global.analyzer.ImportType(expr) =>        "ImportType(" + expr.toString + ")"      case _ =>        throw new Error("Unknown case: " + t.toString())    }  }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?