1
/* NSC -- new Scala compiler
2
* Copyright 2006-2011 LAMP/EPFL
3
* @author Martin Odersky
6
package scala.tools.nsc
9
import annotation.{ tailrec, switch }
10
import java.lang.{ Character => JCharacter }
12
/** Contains constants and classifier methods for characters */
14
// Be very careful touching these.
15
// Apparently trivial changes to the way you write these constants
16
// will cause Scanners.scala to go from a nice efficient switch to
17
// a ghastly nested if statement which will bring the type checker
18
// to its knees. See ticket #1456
19
// Martin: (this should be verified now that the pattern rules have been redesigned).
20
final val LF = '\u000A'
21
final val FF = '\u000C'
22
final val CR = '\u000D'
23
final val SU = '\u001A'
25
/** Convert a character digit to an Int according to given base,
27
def digit2int(ch: Char, base: Int): Int = {
28
if ('0' <= ch && ch <= '9' && ch < '0' + base)
30
else if ('A' <= ch && ch < 'A' + base - 10)
32
else if ('a' <= ch && ch < 'a' + base - 10)
38
/** Convert a character to a backslash-u escape */
39
def char2uescape(c: Char): String = {
41
val buf = new StringBuilder
43
buf ++= (rest % 16).toHexString
46
"\\u" + buf.toString.reverse
49
/** Is character a line break? */
50
@inline def isLineBreakChar(c: Char) = (c: @switch) match {
51
case LF|FF|CR|SU => true
55
/** Is character a whitespace character (but not a new line)? */
56
def isWhitespace(c: Char) =
57
c == ' ' || c == '\t' || c == CR
59
/** Can character form part of a doc comment variable $xxx? */
60
def isVarPart(c: Char) =
61
'0' <= c && c <= '9' || 'A' <= c && c <= 'Z' || 'a' <= c && c <= 'z'
63
/** Can character start an alphanumeric Scala identifier? */
64
def isIdentifierStart(c: Char): Boolean =
65
(c == '_') || (c == '$') || Character.isUnicodeIdentifierStart(c)
67
/** Can character form part of an alphanumeric Scala identifier? */
68
def isIdentifierPart(c: Char) =
69
(c == '$') || Character.isUnicodeIdentifierPart(c)
71
/** Is character a math or other symbol in Unicode? */
72
def isSpecial(c: Char) = {
73
val chtp = Character.getType(c)
74
chtp == Character.MATH_SYMBOL.toInt || chtp == Character.OTHER_SYMBOL.toInt
77
private final val otherLetters = Set[Char]('\u0024', '\u005F') // '$' and '_'
78
private final val letterGroups = {
80
Set[Byte](LOWERCASE_LETTER, UPPERCASE_LETTER, OTHER_LETTER, TITLECASE_LETTER, LETTER_NUMBER)
82
def isScalaLetter(ch: Char) = letterGroups(JCharacter.getType(ch).toByte) || otherLetters(ch)
84
/** Can character form part of a Scala operator name? */
85
def isOperatorPart(c : Char) : Boolean = (c: @switch) match {
86
case '~' | '!' | '@' | '#' | '%' |
87
'^' | '*' | '+' | '-' | '<' |
88
'>' | '?' | ':' | '=' | '&' |
89
'|' | '/' | '\\' => true
90
case c => isSpecial(c)
94
object Chars extends Chars { }
b'\\ No newline at end of file'