@@ -11,7 +11,7 @@ Scala programs are written using the Unicode Basic Multilingual Plane
11
11
presently supported. This chapter defines the two modes of Scala's
12
12
lexical syntax, the Scala mode and the _ XML mode_ . If not
13
13
otherwise mentioned, the following descriptions of Scala tokens refer
14
- to _ Scala mode_ , and literal characters ‘c’ refer to the ASCII fragment
14
+ to _ Scala mode_ , and literal characters ‘c’ refer to the ASCII fragment
15
15
` \u0000 ` – ` \u007F ` .
16
16
17
17
In Scala mode, _ Unicode escapes_ are replaced by the corresponding
@@ -29,7 +29,7 @@ but I can't make it work nor can I imagine how this would make sense,
29
29
so I removed it for now.
30
30
-->
31
31
32
- To construct tokens, characters are distinguished according to the following
32
+ To construct tokens, characters are distinguished according to the following
33
33
classes (Unicode general category given in parentheses):
34
34
35
35
1 . Whitespace characters. ` \u0020 | \u0009 | \u000D | \u000A ` .
@@ -41,13 +41,13 @@ classes (Unicode general category given in parentheses):
41
41
1 . Parentheses ` ‘(’ | ‘)’ | ‘[’ | ‘]’ | ‘{’ | ‘}’ ` .
42
42
1 . Delimiter characters `` ‘`’ | ‘'’ | ‘"’ | ‘.’ | ‘;’ | ‘,’ `` .
43
43
1 . Operator characters. These consist of all printable ASCII characters
44
- ` \u0020 ` - ` \u007F ` which are in none of the sets above, mathematical
44
+ ` \u0020 ` - ` \u007F ` which are in none of the sets above, mathematical
45
45
symbols (` Sm ` ) and other symbols (` So ` ).
46
46
47
47
## Identifiers
48
48
49
49
``` ebnf
50
- op ::= opchar {opchar}
50
+ op ::= opchar {opchar}
51
51
varid ::= lower idrest
52
52
plainid ::= upper idrest
53
53
| varid
@@ -61,14 +61,14 @@ There are three ways to form an identifier. First, an identifier can
61
61
start with a letter which can be followed by an arbitrary sequence of
62
62
letters and digits. This may be followed by underscore ‘_ ’
63
63
characters and another string composed of either letters and digits or
64
- of operator characters. Second, an identifier can start with an operator
64
+ of operator characters. Second, an identifier can start with an operator
65
65
character followed by an arbitrary sequence of operator characters.
66
66
The preceding two forms are called _ plain_ identifiers. Finally,
67
67
an identifier may also be formed by an arbitrary string between
68
68
back-quotes (host systems may impose some restrictions on which
69
69
strings are legal for identifiers). The identifier then is composed
70
70
of all characters excluding the backquotes themselves.
71
-
71
+
72
72
As usual, a longest match rule applies. For instance, the string
73
73
74
74
``` scala
@@ -92,8 +92,8 @@ do else extends false final
92
92
finally for forSome if implicit
93
93
import lazy match new null
94
94
object override package private protected
95
- return sealed super this throw
96
- trait try true type val
95
+ return sealed super this throw
96
+ trait try true type val
97
97
var while with yield
98
98
_ : = => <- <: <% >: # @
99
99
```
@@ -115,7 +115,6 @@ For instance, the statement `Thread.yield()` is illegal, since
115
115
` yield ` is a reserved word in Scala. However, here's a
116
116
work-around: `` Thread.`yield`() ``
117
117
118
-
119
118
## Newline Characters
120
119
121
120
``` ebnf
@@ -134,16 +133,16 @@ The tokens that can terminate a statement are: literals, identifiers
134
133
and the following delimiters and reserved words:
135
134
136
135
``` scala
137
- this null true false return type < xml- start>
136
+ this null true false return type < xml- start>
138
137
_ ) ] }
139
138
```
140
139
141
140
The tokens that can begin a statement are all Scala tokens _ except_
142
141
the following delimiters and reserved words:
143
142
144
143
``` scala
145
- catch else extends finally forSome match
146
- with yield , . ; : = => <- <: <%
144
+ catch else extends finally forSome match
145
+ with yield , . ; : = => <- <: <%
147
146
>: # [ ) ] }
148
147
```
149
148
@@ -169,7 +168,7 @@ Newlines are disabled in:
169
168
1 . Any regions analyzed in [ XML mode] ( #xml-mode ) .
170
169
171
170
Note that the brace characters of ` {...} ` escapes in XML and
172
- string literals are not tokens,
171
+ string literals are not tokens,
173
172
and therefore do not enclose a region where newlines
174
173
are enabled.
175
174
@@ -179,7 +178,7 @@ between the two tokens. However, if two tokens are separated by at
179
178
least one completely blank line (i.e a line which contains no
180
179
printable characters), then two ` nl ` tokens are inserted.
181
180
182
- The Scala grammar (given in full [ here] ( #scala -syntax-summary) )
181
+ The Scala grammar (given in full [ here] ( 13 -syntax-summary.html ) )
183
182
contains productions where optional ` nl ` tokens, but not
184
183
semicolons, are accepted. This has the effect that a newline in one of these
185
184
positions does not terminate an expression or statement. These positions can
@@ -189,21 +188,21 @@ Multiple newline tokens are accepted in the following places (note
189
188
that a semicolon in place of the newline would be illegal in every one
190
189
of these cases):
191
190
192
- - between the condition of a
191
+ - between the condition of a
193
192
[ conditional expression] ( 06-expressions.html#conditional-expressions )
194
193
or [ while loop] ( 06-expressions.html#while-loop-expressions ) and the next
195
194
following expression,
196
- - between the enumerators of a
195
+ - between the enumerators of a
197
196
[ for-comprehension] ( 06-expressions.html#for-comprehensions-and-for-loops )
198
197
and the next following expression, and
199
- - after the initial ` type ` keyword in a
198
+ - after the initial ` type ` keyword in a
200
199
[ type definition or declaration] ( 04-basic-declarations-and-definitions.html#type-declarations-and-type-aliases ) .
201
200
202
201
A single new line token is accepted
203
202
204
203
- in front of an opening brace ‘{’, if that brace is a legal
205
204
continuation of the current statement or expression,
206
- - after an [ infix operator] ( 06-expressions.html#prefix-infix-and-postfix-operations ) ,
205
+ - after an [ infix operator] ( 06-expressions.html#prefix, -infix, -and-postfix-operations ) ,
207
206
if the first token on the next line can start an expression,
208
207
- in front of a [ parameter clause] ( 04-basic-declarations-and-definitions.html#function-declarations-and-definitions ) , and
209
208
- after an [ annotation] ( 11-user-defined-annotations.html#user-defined-annotations ) .
@@ -300,16 +299,15 @@ illegal).
300
299
protected class Data { ... }
301
300
```
302
301
303
-
304
302
## Literals
305
303
306
304
There are literals for integer numbers, floating point numbers,
307
305
characters, booleans, symbols, strings. The syntax of these literals is in
308
306
each case as in Java.
309
307
310
- <!-- TODO
308
+ <!-- TODO
311
309
say that we take values from Java, give examples of some lits in
312
- particular float and double.
310
+ particular float and double.
313
311
-->
314
312
315
313
``` ebnf
@@ -322,11 +320,10 @@ Literal ::= [‘-’] integerLiteral
322
320
| ‘null’
323
321
```
324
322
325
-
326
323
### Integer Literals
327
324
328
325
``` ebnf
329
- integerLiteral ::= (decimalNumeral | hexNumeral | octalNumeral)
326
+ integerLiteral ::= (decimalNumeral | hexNumeral | octalNumeral)
330
327
[‘L’ | ‘l’]
331
328
decimalNumeral ::= ‘0’ | nonZeroDigit {digit}
332
329
hexNumeral ::= ‘0’ ‘x’ hexDigit {hexDigit}
@@ -356,14 +353,12 @@ is _pt_. The numeric ranges given by these types are:
356
353
| ` Short ` | $-2\^ {15}$ to $2\^ {15}-1$|
357
354
| ` Char ` | $0$ to $2\^ {16}-1$ |
358
355
359
-
360
356
### Example
361
357
362
358
``` scala
363
359
0 21 0xFFFFFFFF - 42L
364
360
```
365
361
366
-
367
362
### Floating Point Literals
368
363
369
364
``` ebnf
@@ -410,7 +405,6 @@ booleanLiteral ::= ‘true’ | ‘false’
410
405
The boolean literals ` true ` and ` false ` are
411
406
members of type ` Boolean ` .
412
407
413
-
414
408
### Character Literals
415
409
416
410
``` ebnf
@@ -429,11 +423,10 @@ by an [escape sequence](#escape-sequences).
429
423
430
424
Note that ` '\u000A' ` is _ not_ a valid character literal because
431
425
Unicode conversion is done before literal parsing and the Unicode
432
- character \\ u000A (line feed) is not a printable
426
+ character ` \ u000A` (line feed) is not a printable
433
427
character. One can use instead the escape sequence ` '\n' ` or
434
428
the octal escape ` '\12' ` ([ see here] ( #escape-sequences ) ).
435
429
436
-
437
430
### String Literals
438
431
439
432
``` ebnf
@@ -446,7 +439,7 @@ characters are either printable unicode character or are described by
446
439
[ escape sequences] ( #escape-sequences ) . If the string literal
447
440
contains a double quote character, it must be escaped,
448
441
i.e. ` "\"" ` . The value of a string literal is an instance of
449
- class ` String ` .
442
+ class ` String ` .
450
443
451
444
### Example
452
445
@@ -500,17 +493,16 @@ evaluates to
500
493
501
494
``` scala
502
495
the present string
503
- spans three
496
+ spans three
504
497
lines.
505
498
```
506
499
507
500
Method ` stripMargin ` is defined in class
508
- [ scala.collection.immutable.StringLike] ( http://www.scala-lang.org/api/current/index.html #scala.collection.immutable.StringLike ) .
501
+ [ scala.collection.immutable.StringLike] ( http://www.scala-lang.org/api/current/#scala.collection.immutable.StringLike ) .
509
502
Because there is a predefined
510
503
[ implicit conversion] ( 06-expressions.html#implicit-conversions ) from ` String ` to
511
504
` StringLike ` , the method is applicable to all strings.
512
505
513
-
514
506
### Escape Sequences
515
507
516
508
The following escape sequences are recognized in character and string literals.
@@ -526,15 +518,13 @@ The following escape sequences are recognized in character and string literals.
526
518
| ` ‘\‘ ‘'‘ ` | ` \u0027 ` | single quote | ` ' ` |
527
519
| ` ‘\‘ ‘\‘ ` | ` \u005c ` | backslash | ` \ ` |
528
520
529
-
530
521
A character with Unicode between 0 and 255 may also be represented by
531
- an octal escape, i.e. a backslash ‘\’ followed by a
522
+ an octal escape, i.e. a backslash ` '\' ` followed by a
532
523
sequence of up to three octal characters.
533
524
534
525
It is a compile time error if a backslash character in a character or
535
526
string literal does not start a valid escape sequence.
536
527
537
-
538
528
### Symbol literals
539
529
540
530
``` ebnf
@@ -557,7 +547,6 @@ caches weak references to `Symbol`s, thus ensuring that
557
547
identical symbol literals are equivalent with respect to reference
558
548
equality.
559
549
560
-
561
550
## Whitespace and Comments
562
551
563
552
Tokens may be separated by whitespace characters
@@ -572,7 +561,6 @@ but are required to be properly nested. Therefore, a comment like
572
561
` /* /* */ ` will be rejected as having an unterminated
573
562
comment.
574
563
575
-
576
564
## XML mode
577
565
578
566
In order to allow literal inclusion of XML fragments, lexical analysis
@@ -589,10 +577,10 @@ brace and immediately followed by a character starting an XML name.
589
577
590
578
The scanner switches from XML mode to Scala mode if either
591
579
592
- - the XML expression or the XML pattern started by the initial ‘<’ has been
580
+ - the XML expression or the XML pattern started by the initial ‘<’ has been
593
581
successfully parsed, or if
594
- - the parser encounters an embedded Scala expression or pattern and
595
- forces the Scanner
582
+ - the parser encounters an embedded Scala expression or pattern and
583
+ forces the Scanner
596
584
back to normal mode, until the Scala expression or pattern is
597
585
successfully parsed. In this case, since code and XML fragments can be
598
586
nested, the parser has to maintain a stack that reflects the nesting
0 commit comments