Skip to content

Commit

Permalink
feat(lexer): exclude special chars from text token
Browse files Browse the repository at this point in the history
  • Loading branch information
xNaCly committed Apr 4, 2023
1 parent e37e776 commit 6248ebe
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
1 change: 0 additions & 1 deletion fleck.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,4 @@ func main() {
}
s := scanner.NewScanner(os.Args[1])
s.Parse()
s.PrintTokens()
}
14 changes: 10 additions & 4 deletions scanner/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import (
"fmt"
"log"
"os"
"unicode"
"strings"
"time"
)

type Scanner struct {
Expand Down Expand Up @@ -106,6 +107,7 @@ func (s *Scanner) advanceLine() {

// parses the file given to the Scanner line by line
func (s *Scanner) Parse() {
startTime := time.Now()
for !s.atEnd() {
switch s.curChar {
case '#':
Expand Down Expand Up @@ -134,18 +136,22 @@ func (s *Scanner) Parse() {
s.addToken(BACKTICK, "")
default:
var res []rune
// TODO: exit loop if #_*\n-[]()`
for !unicode.In(s.curChar) {
// INFO: loop until special char is hit
for !strings.ContainsAny(string(s.curChar), "\n#_*-[]()`>") {
res = append(res, s.curChar)
s.advance()
}

s.addToken(TEXT, string(res))

if s.curChar == '\n' {
s.addToken(NEWLINE, "")
s.advanceLine()
}
s.advanceLine()

continue
}
s.advance()
}
log.Printf("lexed %d token, took %s\n", len(s.tokens), time.Since(startTime).String())
}

0 comments on commit 6248ebe

Please sign in to comment.