@@ -255,13 +255,15 @@ extension Lexer {
255
255
struct Result {
256
256
let tokenKind : RawTokenKind
257
257
let flags : Lexer . Lexeme . Flags
258
- let error : LexerError ?
258
+ /// The error kind and the cursor pointing to the character at which the
259
+ /// error occurred
260
+ let error : ( kind: LexerError . Kind , position: Lexer . Cursor ) ?
259
261
let stateTransition : StateTransition ?
260
262
261
263
init (
262
264
_ tokenKind: RawTokenKind ,
263
265
flags: Lexer . Lexeme . Flags = [ ] ,
264
- error: LexerError ? = nil ,
266
+ error: ( kind : LexerError . Kind , position : Cursor ) ? = nil ,
265
267
stateTransition: StateTransition ? = nil
266
268
) {
267
269
self . tokenKind = tokenKind
@@ -335,10 +337,14 @@ extension Lexer.Cursor {
335
337
flags. insert ( . isAtStartOfLine)
336
338
}
337
339
340
+ let error = result. error. map { error in
341
+ return LexerError ( error. kind, byteOffset: cursor. distance ( to: error. position) )
342
+ }
343
+
338
344
return . init(
339
345
tokenKind: result. tokenKind,
340
346
flags: flags,
341
- error: result . error,
347
+ error: error,
342
348
start: leadingTriviaStart. pointer,
343
349
leadingTriviaLength: leadingTriviaStart. distance ( to: textStart) ,
344
350
textLength: textStart. distance ( to: trailingTriviaStart) ,
@@ -1194,11 +1200,11 @@ extension Lexer.Cursor {
1194
1200
let oConsumed = self . advance ( matching: " o " ) // Consome 'o'
1195
1201
assert ( zeroConsumed && oConsumed)
1196
1202
if let peeked = self . peek ( ) , peeked < UInt8 ( ascii: " 0 " ) || peeked > UInt8 ( ascii: " 7 " ) {
1197
- let errorOffset = tokenStart . distance ( to : self )
1203
+ let errorPos = self
1198
1204
self . advance ( while: { $0. isValidIdentifierContinuationCodePoint } )
1199
1205
return Lexer . Result (
1200
1206
. integerLiteral,
1201
- error: LexerError ( . invalidOctalDigitInIntegerLiteral, byteOffset : errorOffset )
1207
+ error: ( . invalidOctalDigitInIntegerLiteral, errorPos )
1202
1208
)
1203
1209
}
1204
1210
@@ -1208,11 +1214,11 @@ extension Lexer.Cursor {
1208
1214
1209
1215
let tmp = self
1210
1216
if self . advance ( if: { $0. isValidIdentifierContinuationCodePoint } ) {
1211
- let errorOffset = tokenStart . distance ( to : tmp)
1217
+ let errorPos = tmp
1212
1218
self . advance ( while: { $0. isValidIdentifierContinuationCodePoint } )
1213
1219
return Lexer . Result (
1214
1220
. integerLiteral,
1215
- error: LexerError ( . invalidOctalDigitInIntegerLiteral, byteOffset : errorOffset )
1221
+ error: ( . invalidOctalDigitInIntegerLiteral, errorPos )
1216
1222
)
1217
1223
}
1218
1224
@@ -1225,11 +1231,11 @@ extension Lexer.Cursor {
1225
1231
let bConsumed = self . advance ( matching: " b " ) // Consume 'b'
1226
1232
assert ( zeroConsumed && bConsumed)
1227
1233
if self . is ( notAt: " 0 " , " 1 " ) {
1228
- let errorOffset = tokenStart . distance ( to : self )
1234
+ let errorPos = self
1229
1235
self . advance ( while: { $0. isValidIdentifierContinuationCodePoint } )
1230
1236
return Lexer . Result (
1231
1237
. integerLiteral,
1232
- error: LexerError ( . invalidBinaryDigitInIntegerLiteral, byteOffset : errorOffset )
1238
+ error: ( . invalidBinaryDigitInIntegerLiteral, errorPos )
1233
1239
)
1234
1240
}
1235
1241
@@ -1239,11 +1245,11 @@ extension Lexer.Cursor {
1239
1245
1240
1246
let tmp = self
1241
1247
if self . advance ( if: { $0. isValidIdentifierContinuationCodePoint } ) {
1242
- let errorOffset = tokenStart . distance ( to : tmp)
1248
+ let errorPos = tmp
1243
1249
self . advance ( while: { $0. isValidIdentifierContinuationCodePoint } )
1244
1250
return Lexer . Result (
1245
1251
. integerLiteral,
1246
- error: LexerError ( . invalidBinaryDigitInIntegerLiteral, byteOffset : errorOffset )
1252
+ error: ( . invalidBinaryDigitInIntegerLiteral, errorPos )
1247
1253
)
1248
1254
}
1249
1255
@@ -1268,11 +1274,11 @@ extension Lexer.Cursor {
1268
1274
// something else, then this is the end of the token.
1269
1275
let tmp = self
1270
1276
if self . advance ( if: { $0. isValidIdentifierContinuationCodePoint } ) {
1271
- let errorOffset = tokenStart . distance ( to : tmp)
1277
+ let errorPos = tmp
1272
1278
self . advance ( while: { $0. isValidIdentifierContinuationCodePoint } )
1273
1279
return Lexer . Result (
1274
1280
. integerLiteral,
1275
- error: LexerError ( . invalidDecimalDigitInIntegerLiteral, byteOffset : errorOffset )
1281
+ error: ( . invalidDecimalDigitInIntegerLiteral, errorPos )
1276
1282
)
1277
1283
}
1278
1284
@@ -1305,20 +1311,23 @@ extension Lexer.Cursor {
1305
1311
errorKind = . expectedDigitInFloatLiteral
1306
1312
}
1307
1313
1308
- let errorOffset = tokenStart . distance ( to : tmp)
1314
+ let errorPos = tmp
1309
1315
self . advance ( while: { $0. isValidIdentifierContinuationCodePoint } )
1310
- return Lexer . Result ( . floatingLiteral, error: LexerError ( errorKind, byteOffset: errorOffset) )
1316
+ return Lexer . Result (
1317
+ . floatingLiteral,
1318
+ error: ( errorKind, errorPos)
1319
+ )
1311
1320
}
1312
1321
1313
1322
self . advance ( while: { $0. isDigit || $0 == Unicode . Scalar ( " _ " ) } )
1314
1323
1315
1324
let tmp = self
1316
1325
if self . advance ( if: { $0. isValidIdentifierContinuationCodePoint } ) {
1317
- let errorOffset = tokenStart . distance ( to : tmp)
1326
+ let errorPos = tmp
1318
1327
self . advance ( while: { $0. isValidIdentifierContinuationCodePoint } )
1319
1328
return Lexer . Result (
1320
1329
. floatingLiteral,
1321
- error: LexerError ( . invalidFloatingPointExponentDigit, byteOffset : errorOffset )
1330
+ error: ( . invalidFloatingPointExponentDigit, errorPos )
1322
1331
)
1323
1332
}
1324
1333
}
@@ -1339,11 +1348,11 @@ extension Lexer.Cursor {
1339
1348
return Lexer . Result ( . integerLiteral)
1340
1349
}
1341
1350
guard let peeked = self . peek ( ) , Unicode . Scalar ( peeked) . isHexDigit else {
1342
- let errorOffset = tokStart . distance ( to : self )
1351
+ let errorPos = self
1343
1352
self . advance ( while: { $0. isValidIdentifierContinuationCodePoint } )
1344
1353
return Lexer . Result (
1345
1354
. integerLiteral,
1346
- error: LexerError ( . invalidHexDigitInIntegerLiteral, byteOffset : errorOffset )
1355
+ error: ( . invalidHexDigitInIntegerLiteral, errorPos )
1347
1356
)
1348
1357
}
1349
1358
@@ -1352,11 +1361,11 @@ extension Lexer.Cursor {
1352
1361
if self . isAtEndOfFile || self . is ( notAt: " . " , " p " , " P " ) {
1353
1362
let tmp = self
1354
1363
if self . advance ( if: { $0. isValidIdentifierContinuationCodePoint } ) {
1355
- let errorOffset = tokStart . distance ( to : tmp)
1364
+ let errorPos = tmp
1356
1365
self . advance ( while: { $0. isValidIdentifierContinuationCodePoint } )
1357
1366
return Lexer . Result (
1358
1367
. integerLiteral,
1359
- error: LexerError ( . invalidHexDigitInIntegerLiteral, byteOffset : errorOffset )
1368
+ error: ( . invalidHexDigitInIntegerLiteral, errorPos )
1360
1369
)
1361
1370
} else {
1362
1371
return Lexer . Result ( . integerLiteral)
@@ -1385,7 +1394,7 @@ extension Lexer.Cursor {
1385
1394
}
1386
1395
return Lexer . Result (
1387
1396
. integerLiteral,
1388
- error: LexerError ( . expectedBinaryExponentInHexFloatLiteral, byteOffset : tokStart . distance ( to : self ) )
1397
+ error: ( . expectedBinaryExponentInHexFloatLiteral, self )
1389
1398
)
1390
1399
}
1391
1400
} else {
@@ -1424,20 +1433,23 @@ extension Lexer.Cursor {
1424
1433
} else {
1425
1434
errorKind = . expectedDigitInFloatLiteral
1426
1435
}
1427
- let errorOffset = tokStart . distance ( to : tmp)
1436
+ let errorPos = tmp
1428
1437
self . advance ( while: { $0. isValidIdentifierContinuationCodePoint } )
1429
- return Lexer . Result ( . floatingLiteral, error: LexerError ( errorKind, byteOffset: errorOffset) )
1438
+ return Lexer . Result (
1439
+ . floatingLiteral,
1440
+ error: ( errorKind, errorPos)
1441
+ )
1430
1442
}
1431
1443
1432
1444
self . advance ( while: { $0. isDigit || $0 == Unicode . Scalar ( " _ " ) } )
1433
1445
1434
1446
let tmp = self
1435
1447
if self . advance ( if: { $0. isValidIdentifierContinuationCodePoint } ) {
1436
- let errorOffset = tokStart . distance ( to : tmp)
1448
+ let errorPos = tmp
1437
1449
self . advance ( while: { $0. isValidIdentifierContinuationCodePoint } )
1438
1450
return Lexer . Result (
1439
1451
. floatingLiteral,
1440
- error: LexerError ( . invalidFloatingPointExponentDigit, byteOffset : errorOffset )
1452
+ error: ( . invalidFloatingPointExponentDigit, errorPos )
1441
1453
)
1442
1454
}
1443
1455
return Lexer . Result ( . floatingLiteral)
0 commit comments