Skip to content

Enables | close to variable token #195

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
28 changes: 24 additions & 4 deletions scanner/lex.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,10 +283,22 @@ func lexStart(l *Lexer) stateFn {
absorbIdentifier(l)

next := l.peek()
if next != eof && !isSpace(next) &&
!isEndOfLine(next) && next != ';' &&
next != ')' && next != ',' && next != '+' &&
next != '[' && next != ']' && next != '(' {

// 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
//
// Below are the valid suffixes for variables:
// $HOME;
// $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
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 @@ -550,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 == '|'
}
9 changes: 9 additions & 0 deletions scanner/lex_regression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,4 +207,13 @@ func TestLexerIssue127(t *testing.T) {
}

testTable("test127", `rm -rf $HOME/.vim`, expected, t)

expected = []Token{
{typ: token.Ident, val: "rm"},
{typ: token.Arg, val: "-rf"},
{typ: token.Illegal, val: "test127:1:12: Unrecognized character in action: U+002E '.'"},
{typ: token.EOF},
}

testTable("test127", `rm -rf $HOME.vim`, expected, t)
}
49 changes: 49 additions & 0 deletions scanner/lex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1827,3 +1827,52 @@ func TestLexerLongAssignment(t *testing.T) {
jq ".GroupId" |
xargs echo -n)`, expected, t)
}

func TestLexerVariable(t *testing.T) {
base := []Token{
{typ: token.Variable, val: "$HOME"},
}

for r, tok := range map[rune]token.Token{
';': token.Semicolon,
'+': token.Plus,
'[': token.LBrack,
']': token.RBrack,
'(': token.LParen,
')': token.RParen,
',': token.Comma,
'|': token.Pipe,
} {
expected := append(base, Token{typ: tok, val: string(r)})

if r == ')' {
expected = append(expected, Token{typ: token.Semicolon, val: ";"})
}

expected = append(expected, Token{typ: token.EOF})
testTable("test variable", `$HOME`+string(r), expected, t)
}

// must fail
for _, r := range []rune{
'!', '@', '$', '%', '&', '*', '-', '=', '"', '\'',
'`', '{', '}', '<', '>', '.', ':', '?', '/', '\\',
} {
l := Lex("test", "$GOPATH"+string(r))

var results []Token

for tok := range l.Tokens {
results = append(results, tok)
}

if len(results) != 2 {
t.Fatalf("Expected 2 token, found %d", len(results))
}

if results[0].typ != token.Illegal {
t.Fatalf("Expected illegal token, but found %v", results[0])
}
}

}