Skip to content

Commit

Permalink
Fixed panic when parsing commands, the lexer is a bit too permissive, c…
Browse files Browse the repository at this point in the history
…loses #78

Now:

```
⧐  $1
 parser errors:
	Illegal token '$1'
	no prefix parse function for ILLEGAL found
``
  • Loading branch information
odino committed Dec 28, 2018
1 parent 1080673 commit 5e7d003
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
15 changes: 10 additions & 5 deletions lexer/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (l *Lexer) NextToken() token.Token {
case '/':
if l.peekChar() == '/' {
tok.Type = token.COMMENT
tok.Literal = l.readComment()
tok.Literal = l.readLine()
} else if l.peekChar() == '=' {
tok.Type = token.COMP_SLASH
tok.Literal = "/="
Expand All @@ -84,7 +84,7 @@ func (l *Lexer) NextToken() token.Token {
}
case '#':
tok.Type = token.COMMENT
tok.Literal = l.readComment()
tok.Literal = l.readLine()
case '&':
if l.peekChar() == '&' {
tok.Type = token.AND
Expand Down Expand Up @@ -178,8 +178,13 @@ func (l *Lexer) NextToken() token.Token {
tok.Type = token.STRING
tok.Literal = l.readString()
case '$':
tok.Type = token.COMMAND
tok.Literal = l.readCommand()
if l.peekChar() == '(' {
tok.Type = token.COMMAND
tok.Literal = l.readCommand()
} else {
tok.Type = token.ILLEGAL
tok.Literal = l.readLine()
}
case '[':
tok = newToken(token.LBRACKET, l.ch)
case ']':
Expand Down Expand Up @@ -331,7 +336,7 @@ func (l *Lexer) readString() string {
// Go ahead until you find a new line.
// This makes it so that comments take
// a full line.
func (l *Lexer) readComment() string {
func (l *Lexer) readLine() string {
position := l.position
for {
l.readChar()
Expand Down
2 changes: 2 additions & 0 deletions lexer/lexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ one | two | tree
null
nullo
&^>><<
$111
`

tests := []struct {
Expand Down Expand Up @@ -265,6 +266,7 @@ nullo
{token.BIT_XOR, "^"},
{token.BIT_RSHIFT, ">>"},
{token.BIT_LSHIFT, "<<"},
{token.ILLEGAL, "$111"},
{token.EOF, ""},
}

Expand Down
5 changes: 5 additions & 0 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ func New(l *lexer.Lexer) *Parser {
func (p *Parser) nextToken() {
p.curToken = p.peekToken
p.peekToken = p.l.NextToken()

if p.curTokenIs(token.ILLEGAL) {
msg := fmt.Sprintf(`Illegal token '%s'`, p.curToken.Literal)
p.errors = append(p.errors, msg)
}
}

func (p *Parser) curTokenIs(t token.TokenType) bool {
Expand Down

0 comments on commit 5e7d003

Please sign in to comment.