@@ -64,7 +64,15 @@ func (l *Lexer) NextToken() token.Token {
64
64
65
65
switch l .ch {
66
66
case 0 :
67
- return l .NewToken (token .EOF , "EOF" )
67
+ level := l .whitespaceStack .Find ("" )
68
+ if level > 0 {
69
+ for i := 0 ; i < level ; i ++ {
70
+ l .whitespaceStack .Pop ()
71
+ }
72
+ return l .MakeToken (token .END , fmt .Sprint (level ))
73
+ } else {
74
+ return l .NewToken (token .EOF , "EOF" )
75
+ }
68
76
case '\n' :
69
77
return l .NewToken (token .NEWLINE , ";" )
70
78
case '\\' :
@@ -198,7 +206,7 @@ func (l *Lexer) NextToken() token.Token {
198
206
l .readChar ()
199
207
return l .NewToken (tType , text )
200
208
case token .EMDASH :
201
- return l .MakeToken (tType , l .readSnippet ())
209
+ return l .MakeToken (tType , strings . TrimSpace ( l .readSnippet () ))
202
210
default :
203
211
return l .NewToken (tType , lit )
204
212
}
@@ -393,8 +401,10 @@ func (l *Lexer) readSnippet() string {
393
401
}
394
402
// There are two possibilities. Either we found a non-whitespace character, and the whole snippet is on the same line as the
395
403
// `---` token, or we found a newline and the snipped is an indent on the succeeding lines. Just like with a colon.
396
- if l .peekChar () == '\n' { // --- then we have to mess with whitespace.
397
- l .readChar ()
404
+ if l .peekChar () == '\n' || l .peekChar () == '\r' { // --- then we have to mess with whitespace.
405
+ for l .peekChar () == '\n' || l .peekChar () == '\r' {
406
+ l .readChar ()
407
+ }
398
408
langIndent := ""
399
409
stackTop , ok := l .whitespaceStack .HeadValue ()
400
410
if ! ok {
@@ -417,8 +427,8 @@ func (l *Lexer) readSnippet() string {
417
427
return result
418
428
}
419
429
}
420
- if strings .HasPrefix (stackTop , currentWhitespace ) || currentWhitespace == "\n " { // Then we've unindented. Dobby is free!
421
- if currentWhitespace != "\n " {
430
+ if strings .HasPrefix (stackTop , currentWhitespace ) || currentWhitespace == "\n " || currentWhitespace == " \r " || currentWhitespace == string ( 0 ) { // Then we've unindented. Dobby is free!
431
+ if currentWhitespace != "\n " && currentWhitespace != " \r " && currentWhitespace == string ( 0 ) {
422
432
l .snippetWhitespace = currentWhitespace
423
433
}
424
434
return result
@@ -427,18 +437,19 @@ func (l *Lexer) readSnippet() string {
427
437
l .Throw ("lex/emdash/indent/c" , l .NewToken (token .ILLEGAL , "bad emdash" ))
428
438
return result
429
439
}
430
- for l .peekChar () != '\n' && l .peekChar () != 0 {
440
+ for l .peekChar () != '\n' && l .peekChar () != '\r' && l . peekChar () != 0 {
431
441
l .readChar ()
432
442
result = result + string (l .ch )
433
443
}
434
444
if l .peekChar () == 0 {
445
+ l .readChar ()
435
446
return result
436
447
}
437
448
l .readChar ()
438
449
result = result + "\n "
439
450
}
440
451
} else {
441
- for l .peekChar () != '\n' && l .peekChar () != 0 {
452
+ for l .peekChar () != '\n' && l .peekChar () != '\r' && l . peekChar () != 0 {
442
453
l .readChar ()
443
454
result = result + string (l .ch )
444
455
}
@@ -621,7 +632,7 @@ func isHexDigit(ch rune) bool {
621
632
622
633
func isProtectedPunctuationOrWhitespace (ch rune ) bool {
623
634
return ch == '(' || ch == ')' || ch == '[' || ch == ']' || ch == '{' || ch == '}' || ch == ' ' || ch == ',' ||
624
- ch == ':' || ch == ';' || ch == '\t' || ch == '\n' || ch == 0
635
+ ch == ':' || ch == ';' || ch == '\t' || ch == '\n' || ch == '\r' || ch == 0
625
636
}
626
637
627
638
func isSymbol (ch rune ) bool {
@@ -646,7 +657,7 @@ func (l *Lexer) NewToken(tokenType token.TokenType, st string) token.Token {
646
657
647
658
func (l * Lexer ) MakeToken (tokenType token.TokenType , st string ) token.Token {
648
659
if settings .SHOW_LEXER && ! (settings .IGNORE_BOILERPLATE && settings .ThingsToIgnore .Contains (l .source )) {
649
- fmt .Println (tokenType , st , l . line , l . tstart )
660
+ fmt .Println (tokenType , st )
650
661
}
651
662
return token.Token {Type : tokenType , Literal : st , Source : l .source , Line : l .line , ChStart : l .tstart , ChEnd : l .char }
652
663
}
0 commit comments