Skip to content
Open
Changes from 1 commit
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
28 changes: 17 additions & 11 deletions scanner/lex.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,25 +282,23 @@ func lexStart(l *Lexer) stateFn {

absorbIdentifier(l)

next := l.peek()

// required to avoid the sintaxes below:
// rm -rf $HOME/projects
// rm -rf $GOPATH/test
// for being interpreted as:
// rm -rf $HOME /projects
// rm -rf $GOPATH /test
//
// The list of runes below are the ones allowed to exists close
// to the variable. Eg.:
// Below are the valid suffixes for variables:
// $HOME;
// $HOME[0]
// $HOME()
// $HOME+"a"
next := l.peek()
if next != eof && !isSpace(next) &&
!isEndOfLine(next) && next != ';' &&
next != ')' && next != ',' && next != '+' &&
next != '[' && next != ']' && next != '(' &&
next != '|' {
// $HOME[ # used in: $HOME[0]
// $HOME( # used in: $callback()
// $HOME+ # used in: $HOME+"/src"
// $HOME] # used in: $list[$index]
// $HOME) # used in: call($HOME)
if !isValidVariableSuffix(next) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a nice refactoring. You sir are a gentleman:

gentleman

l.errorf("Unrecognized character in action: %#U", next)
return nil
}
Expand Down Expand Up @@ -564,3 +562,11 @@ func isAlpha(r rune) bool {
func isEndOfLine(r rune) bool {
return r == '\r' || r == '\n'
}

func isValidVariableSuffix(r rune) bool {
return r == eof || isSpace(r) ||
isEndOfLine(r) || r == ';' ||
r == ')' || r == ',' || r == '+' ||
r == '[' || r == ']' || r == '(' ||
r == '|'
}