Skip to content

Commit

Permalink
fix(scanner): lexing empty lines
Browse files Browse the repository at this point in the history
  • Loading branch information
xNaCly committed Apr 13, 2023
1 parent 5d79935 commit 88b65ca
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
5 changes: 5 additions & 0 deletions scanner/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package scanner
import (
"bufio"
"fmt"
"html"
"log"
"os"
"strings"
Expand Down Expand Up @@ -45,6 +46,7 @@ func (s *Scanner) addToken(kind uint, value string) {
if len(value) != 0 {
// correct text start position
pos = s.linePos - uint(len(value))
value = html.EscapeString(value)
}
s.tokens = append(s.tokens, Token{
Pos: pos,
Expand Down Expand Up @@ -96,6 +98,7 @@ func (s *Scanner) advanceLine() {
s.line++
s.linePos = 0
for len(s.curLine) == 0 && ok {
s.addToken(EMPTYLINE, "")
ok = s.scan.Scan()
s.curLine = []rune(s.scan.Text())
s.line++
Expand Down Expand Up @@ -168,5 +171,7 @@ func (s *Scanner) Lex() []Token {
s.advance()
}
s.addToken(EOF, "")
// TODO: call this if --verbose was specified
// s.PrintTokens()
return s.tokens
}
3 changes: 3 additions & 0 deletions scanner/tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const (
BACKTICK
GREATERTHAN
BANG
EMPTYLINE
EOF
)

Expand All @@ -38,6 +39,7 @@ var TOKEN_LOOKUP_MAP = map[uint]string{
BACKTICK: "BACKTICK",
TEXT: "TEXT",
BANG: "BANG",
EMPTYLINE: "EMPTYLINE",
EOF: "EOF",
}

Expand All @@ -54,5 +56,6 @@ var TOKEN_SYMBOL_MAP = map[uint]rune{
GREATERTHAN: '>',
BACKTICK: '`',
BANG: '!',
EMPTYLINE: '\n',
EOF: 0,
}

0 comments on commit 88b65ca

Please sign in to comment.