Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 additions & 3 deletions lexer/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type Lexer struct {
nextPos int
line int
column int
command token.Type
}

// New returns a new lexer for tokenizing the input string.
Expand Down Expand Up @@ -84,9 +85,14 @@ func (l *Lexer) NextToken() token.Token {
tok.Literal = l.readString('"')
l.readChar()
case '/':
tok.Type = token.REGEX
tok.Literal = l.readRegex('/')
l.readChar()
if l.command == token.WAIT || l.command == token.SET {
tok.Type = token.REGEX
tok.Literal = l.readRegex('/')
l.readChar()
} else {
tok.Literal = l.readIdentifier()
tok.Type = token.LookupIdentifier(tok.Literal)
}
default:
if isDigit(l.ch) || (isDot(l.ch) && isDigit(l.peekChar())) {
tok.Literal = l.readNumber()
Expand All @@ -99,9 +105,27 @@ func (l *Lexer) NextToken() token.Token {
l.readChar()
}
}

if isCommandToken(tok.Type) {
l.command = tok.Type
}

return tok
}

func isCommandToken(t token.Type) bool {
switch t {
case token.TYPE, token.SLEEP,
token.UP, token.DOWN, token.RIGHT, token.LEFT, token.PAGE_UP, token.PAGE_DOWN, token.SCROLL_UP, token.SCROLL_DOWN,
token.ENTER, token.BACKSPACE, token.DELETE, token.TAB,
token.ESCAPE, token.HOME, token.INSERT, token.END, token.CTRL, token.SOURCE, token.SCREENSHOT, token.COPY, token.PASTE, token.WAIT,
token.SET, token.OUTPUT, token.REQUIRE, token.HIDE, token.SHOW, token.ENV:
return true
default:
return false
}
}

// newToken creates a new token with the given type and literal.
func (l *Lexer) newToken(tokenType token.Type, ch byte) token.Token {
literal := string(ch)
Expand Down Expand Up @@ -227,6 +251,7 @@ func (l *Lexer) skipWhitespace() {
if l.ch == '\n' {
l.line++
l.column = 0
l.command = ""
}
l.readChar()
}
Expand Down
3 changes: 3 additions & 0 deletions lexer/lexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
func TestNextToken(t *testing.T) {
input := `
Output examples/out.gif
Output /tmp/examples/out.gif
Set FontSize 42
Set Padding 5
Set CursorBlink false
Expand Down Expand Up @@ -42,6 +43,8 @@ Wait+Screen@1m /foo\\\/bar/`
}{
{token.OUTPUT, "Output"},
{token.STRING, "examples/out.gif"},
{token.OUTPUT, "Output"},
{token.STRING, "/tmp/examples/out.gif"},
{token.SET, "Set"},
{token.FONT_SIZE, "FontSize"},
{token.NUMBER, "42"},
Expand Down