-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
920 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package lexer | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/rsteube/carapace/third_party/github.com/google/shlex" | ||
) | ||
|
||
type State int | ||
|
||
const ( | ||
UNQUOTED = iota | ||
OPEN_DOUBLE | ||
OPEN_SINGLE | ||
) | ||
|
||
type Tokenset struct { | ||
Tokens []string | ||
Prefix string | ||
State State | ||
} | ||
|
||
func Split(s string) (*Tokenset, error) { | ||
tokenset, err := split(s) | ||
if err != nil && err.Error() == "EOF found when expecting closing quote" { | ||
tokenset, err = split(s + `_"`) | ||
if err == nil { | ||
last := tokenset.Tokens[len(tokenset.Tokens)-1] | ||
tokenset.Tokens[len(tokenset.Tokens)-1] = last[:len(last)-1] | ||
tokenset.Prefix = tokenset.Prefix[:len(tokenset.Prefix)-1] | ||
tokenset.State = OPEN_DOUBLE | ||
} | ||
} | ||
if err != nil && err.Error() == "EOF found when expecting closing quote" { | ||
tokenset, err = split(s + `_'`) | ||
if err == nil { | ||
last := tokenset.Tokens[len(tokenset.Tokens)-1] | ||
tokenset.Tokens[len(tokenset.Tokens)-1] = last[:len(last)-1] | ||
tokenset.Prefix = tokenset.Prefix[:len(tokenset.Prefix)-1] | ||
tokenset.State = OPEN_SINGLE | ||
} | ||
} | ||
return tokenset, err | ||
} | ||
|
||
func split(s string) (*Tokenset, error) { | ||
splitted, err := shlex.Split(s) | ||
if strings.HasSuffix(s, " ") { | ||
splitted = append(splitted, "") | ||
} | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if len(splitted) == 0 { | ||
splitted = []string{""} | ||
} | ||
return &Tokenset{ | ||
Tokens: splitted, | ||
Prefix: s[:strings.LastIndex(s, splitted[len(splitted)-1])], | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package lexer | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/rsteube/carapace/internal/assert" | ||
) | ||
|
||
func TestSplit(t *testing.T) { | ||
_test := func(s string, expected Tokenset) { | ||
t.Run(s, func(t *testing.T) { | ||
tokenset, err := Split(s) | ||
if err != nil { | ||
t.Error(err.Error()) | ||
} | ||
|
||
expected, _ := json.MarshalIndent(expected, "", " ") | ||
actual, _ := json.MarshalIndent(tokenset, "", " ") | ||
assert.Equal(t, string(expected), string(actual)) | ||
}) | ||
} | ||
|
||
_test(``, Tokenset{ | ||
Tokens: []string{""}, | ||
}) | ||
|
||
_test(` `, Tokenset{ | ||
Tokens: []string{""}, | ||
Prefix: ` `, | ||
}) | ||
|
||
_test(`"example`, Tokenset{ | ||
Tokens: []string{"example"}, | ||
State: OPEN_DOUBLE, | ||
}) | ||
|
||
_test(`'example`, Tokenset{ | ||
Tokens: []string{"example"}, | ||
State: OPEN_SINGLE, | ||
}) | ||
|
||
_test(`example a`, Tokenset{ | ||
Tokens: []string{"example", "a"}, | ||
Prefix: `example `, | ||
}) | ||
|
||
_test(`example a`, Tokenset{ | ||
Tokens: []string{"example", "a"}, | ||
Prefix: `example `, | ||
}) | ||
|
||
_test(`example "a`, Tokenset{ | ||
Tokens: []string{"example", "a"}, | ||
Prefix: `example `, | ||
State: OPEN_DOUBLE, | ||
}) | ||
|
||
_test(`example 'a`, Tokenset{ | ||
Tokens: []string{"example", "a"}, | ||
Prefix: `example `, | ||
State: OPEN_SINGLE, | ||
}) | ||
|
||
_test(`example action `, Tokenset{ | ||
Tokens: []string{"example", "action", ""}, | ||
Prefix: `example action `, | ||
}) | ||
|
||
_test(`example action -`, Tokenset{ | ||
Tokens: []string{"example", "action", "-"}, | ||
Prefix: `example action `, | ||
}) | ||
|
||
_test(`example action --`, Tokenset{ | ||
Tokens: []string{"example", "action", "--"}, | ||
Prefix: `example action `, | ||
}) | ||
|
||
_test(`example action - `, Tokenset{ | ||
Tokens: []string{"example", "action", "-", ""}, | ||
Prefix: `example action - `, | ||
}) | ||
|
||
_test(`example action -- `, Tokenset{ | ||
Tokens: []string{"example", "action", "--", ""}, | ||
Prefix: `example action -- `, | ||
}) | ||
|
||
_test(`example "action" -- `, Tokenset{ | ||
Tokens: []string{"example", "action", "--", ""}, | ||
Prefix: `example "action" -- `, | ||
}) | ||
|
||
_test(`example 'action' -- `, Tokenset{ | ||
Tokens: []string{"example", "action", "--", ""}, | ||
Prefix: `example 'action' -- `, | ||
}) | ||
} |
Oops, something went wrong.