@@ -368,17 +368,15 @@ public sealed class LuaCharLexer : IDisposable
368
368
private readonly bool leaveOpen ; // do not dispose the text reader at the end
369
369
370
370
private readonly char [ ] lookAheadBuffer ;
371
+ private readonly byte [ ] moveIndex ;
371
372
private int lookAheadOffset ;
372
373
private int lookAheadEof ;
373
374
374
375
private int currentLine ; // Line in the source file
375
376
private int currentColumn ; // Column in the source file
376
377
private readonly int firstColumnIndex ; // Index of the first char in line
377
378
private long currentIndex ; // Index in the source file
378
-
379
- private bool incrementLine = false ;
380
- private bool moveIndex = false ;
381
-
379
+
382
380
private Position startPosition ; // Start of the current token
383
381
private StringBuilder currentStringBuilder = new StringBuilder ( ) ; // Char buffer, for collected chars
384
382
@@ -404,31 +402,40 @@ public LuaCharLexer(string fileName, TextReader tr, int lookAheadLength, bool le
404
402
this . leaveOpen = leaveOpen ;
405
403
406
404
this . lookAheadBuffer = new char [ lookAheadLength ] ;
405
+ this . moveIndex = new byte [ lookAheadLength ] ;
407
406
this . lookAheadOffset = lookAheadLength - 1 ;
408
407
this . lookAheadEof = - 1 ;
409
408
410
409
this . currentLine = currentLine ;
411
410
this . currentColumn = currentColumn - 1 ;
412
411
this . firstColumnIndex = firstColumnIndex ;
413
- this . currentIndex = - 1 ;
412
+ this . currentIndex = 0 ;
414
413
415
414
// inital fill buffer
416
415
for ( var i = 0 ; i < lookAheadLength - 1 ; i ++ )
417
416
{
418
417
if ( lookAheadEof >= 0 )
418
+ {
419
419
lookAheadBuffer [ i ] = '\0 ' ;
420
+ moveIndex [ i ] = 0 ;
421
+ }
420
422
else
421
423
{
422
- var c = ReadCore ( ) ;
423
- if ( c == - 1 )
424
+ var c = ReadCore ( out var len ) ;
425
+ if ( len == 0 )
424
426
{
425
427
lookAheadEof = i ;
426
428
lookAheadBuffer [ i ] = '\0 ' ;
429
+ moveIndex [ i ] = 0 ;
427
430
}
428
431
else
432
+ {
429
433
lookAheadBuffer [ i ] = ( char ) c ;
434
+ moveIndex [ i ] = len ;
435
+ }
430
436
}
431
437
}
438
+ moveIndex [ lookAheadOffset ] = 0 ;
432
439
433
440
// read first char from the buffer
434
441
Next ( ) ;
@@ -460,10 +467,13 @@ public void AppendValue(char value)
460
467
public void AppendValue ( string value )
461
468
=> currentStringBuilder . Append ( Cur ) ;
462
469
463
- private int ReadCore ( )
470
+ private char ReadCore ( out byte len )
464
471
{
465
472
if ( tr == null ) // Source file is readed
466
- return - 1 ;
473
+ {
474
+ len = 0 ;
475
+ return '\0 ' ;
476
+ }
467
477
else
468
478
{
469
479
var i = tr . Read ( ) ;
@@ -472,105 +482,93 @@ private int ReadCore()
472
482
if ( ! leaveOpen )
473
483
tr . Dispose ( ) ;
474
484
tr = null ;
485
+
486
+ len = 0 ;
487
+ return '\0 ' ;
488
+ }
489
+ else if ( i == 10 )
490
+ {
491
+ if ( tr . Peek ( ) == 13 )
492
+ {
493
+ tr . Read ( ) ;
494
+ len = 2 ;
495
+ return ( char ) i ;
496
+ }
497
+ else
498
+ {
499
+ len = 1 ;
500
+ return ( char ) i ;
501
+ }
502
+ }
503
+ else if ( i == 13 )
504
+ {
505
+ if ( tr . Peek ( ) == 10 )
506
+ {
507
+ tr . Read ( ) ;
508
+ len = 2 ;
509
+ return '\n ' ;
510
+ }
511
+ else
512
+ {
513
+ len = 1 ;
514
+ return '\n ' ;
515
+ }
475
516
}
476
- return i ;
517
+
518
+ len = 1 ;
519
+ return ( char ) i ;
477
520
}
478
521
} // func ReadCore
479
522
480
523
private bool ReadCharToBuffer ( )
481
524
{
482
525
if ( lookAheadEof < 0 )
483
526
{
484
- var c = ReadCore ( ) ;
485
- if ( c == - 1 )
527
+ var c = ReadCore ( out var len ) ;
528
+ if ( len == 0 )
486
529
{
487
530
lookAheadEof = lookAheadOffset ;
488
531
lookAheadBuffer [ lookAheadOffset ] = '\0 ' ;
532
+ moveIndex [ lookAheadOffset ] = 0 ;
489
533
}
490
534
else
535
+ {
491
536
lookAheadBuffer [ lookAheadOffset ] = ( char ) c ;
537
+ moveIndex [ lookAheadOffset ] = len ;
538
+ }
492
539
493
540
return true ;
494
541
}
495
542
else if ( lookAheadEof != lookAheadOffset )
496
543
{
497
544
lookAheadBuffer [ lookAheadOffset ] = '\0 ' ;
545
+ moveIndex [ lookAheadOffset ] = 0 ;
498
546
return true ;
499
547
}
500
548
else
501
549
return false ;
502
550
} // func ReadCharToBuffer
503
-
504
- private bool MoveNextCore ( )
505
- {
506
- if ( ReadCharToBuffer ( ) )
507
- {
508
- if ( lookAheadBuffer . Length > 1 )
509
- {
510
- lookAheadOffset ++ ;
511
- if ( lookAheadOffset >= lookAheadBuffer . Length )
512
- lookAheadOffset = 0 ;
513
- }
514
-
515
- return true ;
516
- }
517
- else
518
- return false ;
519
- } // func MoveNextCore
520
551
521
552
/// <summary>Move the char stream and discard the buffer.</summary>
522
553
public void Next ( )
523
554
{
524
- if ( moveIndex )
555
+ currentIndex += moveIndex [ lookAheadOffset ] ;
556
+ if ( lookAheadBuffer [ lookAheadOffset ] == '\n ' )
525
557
{
526
- moveIndex = false ;
527
- currentIndex ++ ;
558
+ currentColumn = firstColumnIndex ;
559
+ currentLine ++ ;
528
560
}
561
+ else
562
+ currentColumn ++ ;
529
563
530
- if ( MoveNextCore ( ) )
564
+ if ( ReadCharToBuffer ( ) )
531
565
{
532
- currentIndex ++ ;
533
-
534
- var c = lookAheadBuffer [ lookAheadOffset ] ;
535
-
536
- // Normalize new line
537
- if ( c == '\n ' )
538
- {
539
- if ( lookAheadBuffer . Length > 1 && IsLookAHead ( '\r ' , 1 ) )
540
- {
541
- moveIndex = MoveNextCore ( ) ;
542
- lookAheadBuffer [ lookAheadOffset ] = '\n ' ; // replace with unique new line
543
- }
544
- else if ( tr != null && tr . Peek ( ) == 13 )
545
- {
546
- moveIndex = MoveNextCore ( ) ;
547
- lookAheadBuffer [ lookAheadOffset ] = '\n ' ; // replace with unique new line
548
- }
549
-
550
- currentColumn ++ ;
551
- incrementLine = true ;
552
- }
553
- else if ( c == '\r ' )
554
- {
555
- if ( lookAheadBuffer . Length > 1 && IsLookAHead ( '\n ' , 1 ) )
556
- moveIndex = MoveNextCore ( ) ;
557
- else if ( tr != null && tr . Peek ( ) == 10 )
558
- moveIndex = MoveNextCore ( ) ;
559
- else
560
- lookAheadBuffer [ lookAheadOffset ] = '\n ' ; // replace with unique new line
561
-
562
- currentColumn ++ ;
563
- incrementLine = true ;
564
- }
565
- else if ( incrementLine )
566
+ if ( lookAheadBuffer . Length > 1 )
566
567
{
567
- currentColumn = firstColumnIndex ;
568
- currentLine ++ ;
569
-
570
- incrementLine = false ;
568
+ lookAheadOffset ++ ;
569
+ if ( lookAheadOffset >= lookAheadBuffer . Length )
570
+ lookAheadOffset = 0 ;
571
571
}
572
- else
573
- currentColumn ++ ;
574
572
}
575
573
} // proc Next
576
574
@@ -589,6 +587,25 @@ public void Eat()
589
587
Next ( ) ;
590
588
} // proc Eat
591
589
590
+ /// <summary>Read a whole line</summary>
591
+ /// <returns></returns>
592
+ public string ReadLine ( )
593
+ {
594
+ while ( ! IsEof && Cur != '\n ' )
595
+ Eat ( ) ;
596
+
597
+ var curValue = CurValue ;
598
+ ResetCurValue ( ) ;
599
+ return curValue ;
600
+ } // func ReadLine
601
+
602
+ /// <summary>Skip white spaces</summary>
603
+ public void SkipWhiteSpaces ( )
604
+ {
605
+ while ( ! IsEof && Cur != '\n ' && Char . IsWhiteSpace ( Cur ) )
606
+ Next ( ) ;
607
+ } // proc SkipWhiteSpaces
608
+
592
609
/// <summary>Create a new token with the current buffer.</summary>
593
610
/// <param name="kind">Token type</param>
594
611
/// <returns>Token</returns>
@@ -661,6 +678,17 @@ public bool IsLookAHead(string value, int offset = 0)
661
678
return true ;
662
679
} // func LookAHead
663
680
681
+ /// <summary>Get look a head.</summary>
682
+ /// <param name="offset"></param>
683
+ /// <returns></returns>
684
+ public char GetLookAHead ( int offset = 0 )
685
+ {
686
+ if ( offset > lookAheadBuffer . Length )
687
+ throw new ArgumentOutOfRangeException ( nameof ( offset ) , lookAheadBuffer . Length , "Look a head buffer is to small." ) ;
688
+
689
+ return lookAheadBuffer [ offset ] ;
690
+ } // func GetLookAHead
691
+
664
692
/// <summary></summary>
665
693
public void ResetCurValue ( )
666
694
{
0 commit comments