以编程方式检查字符串是否是Scala中的保留字

时间:2014-02-26 09:43:58

标签: scala keyword reserved-words

有没有一种简单的方法来实现以下功能?

即。判断生成代码时是否需要使用反引号引用给定的文本。

def isReservedWord(text: String): Boolean

isReservedWord("type") // true
isReservedWord("foo")  // false

当然,我可以根据语言规范末尾的Scala语法摘要维护一个关键字列表,并检查一下,但是有更好的方法吗?

2 个答案:

答案 0 :(得分:10)

编译器维护一个易于访问的关键字列表:

scala> import scala.tools.nsc._
import scala.tools.nsc._

scala> val compiler = new Global(new Settings)
compiler: scala.tools.nsc.Global = scala.tools.nsc.Global@29935953

scala> compiler.nme.keywords
res0: Set[compiler.TermName] = Set(abstract, >:, true, val, do, throw, <-, package, _, macro, @, object, false, this, if, then, var, trait, ., catch, with, def, else, class, type, #, lazy, null, =, <:, override, protected, =>, private, sealed, finally, new, implicit, extends, final, for, return, case, import, forSome, :, super, while, yield, try, match, <%)

scala> compiler.javanme.keywords
res1: Set[compiler.TermName] = Set(abstract, strictfp, short, int, do, goto, interface, throw, float, package, implements, enum, this, long, if, switch, native, throws, boolean, catch, else, const, class, assert, public, void, instanceof, protected, static, default, private, finally, synchronized, new, char, extends, final, volatile, for, return, continue, case, import, double, super, byte, while, break, try, transient)

幸运的是,Scala已经提供了反射API,它只不过是公共API访问的编译器。将公共类型转换为内部类型时,可以访问包含所有定义的符号表:

scala> val st = scala.reflect.runtime.universe.asInstanceOf[scala.reflect.internal.SymbolTable]
st: scala.reflect.internal.SymbolTable = scala.reflect.runtime.JavaUniverse@472250c4

scala> st.nme.keywords
res10: Set[st.TermName] = Set(abstract, >:, true, val, do, throw, <-, package, _, macro, @, object, false, this, if, then, var, trait, ., catch, with, def, else, class, type, #, lazy, null, =, <:, override, protected, =>, private, sealed, finally, new, implicit, extends, final, for, return, case, import, forSome, :, super, while, yield, try, match, <%)

在REPL内部,您还可以使用:power模式直接访问编译器:

scala> :power
** Power User mode enabled - BEEP WHIR GYVE **
** :phase has been set to 'typer'.          **
** scala.tools.nsc._ has been imported      **
** global._, definitions._ also imported    **
** Try  :help, :vals, power.<tab>           **

scala> nme.keywords
res3: Set[$r.intp.global.TermName] = Set(abstract, >:, true, val, do, throw, <-, package, _, macro, @, object, false, this, if, then, var, trait, ., catch, with, def, else, class, type, #, lazy, null, =, <:, override, protected, =>, private, sealed, finally, new, implicit, extends, final, for, return, case, import, forSome, :, super, while, yield, try, match, <%)

scala> javanme.keywords
res4: Set[$r.intp.global.TermName] = Set(abstract, strictfp, short, int, do, goto, interface, throw, float, package, implements, enum, this, long, if, switch, native, throws, boolean, catch, else, const, class, assert, public, void, instanceof, protected, static, default, private, finally, synchronized, new, char, extends, final, volatile, for, return, continue, case, import, double, super, byte, while, break, try, transient)

答案 1 :(得分:1)

点击编译器源代码,我发现了以下内容:

scala.tools.nsc.doc.html.SyntaxHigh.reserved

这是html的包私有,因此您可能必须编写包装器。可能只是将该数组复制到您自己的源代码更容易。

内部API中有更多内容,例如scala.reflect.internal.StdNames。并且scala.reflect.internal.Printers有一个方法quotedName,但您需要整个蛋糕才能访问这些方法。也许你可以通过官方反思API获得这些?