tidied up names, added package doc, removed unnecessary error returns
diff --git a/shlex.go b/shlex.go
index 986adf8..c897fc4 100644
--- a/shlex.go
+++ b/shlex.go
@@ -20,9 +20,16 @@
Package shlex implements a simple lexer which splits input in to tokens using
shell-style rules for quoting and commenting.
-TODO: document examples:
+The basic use case uses the default ASCII lexer to split a string into sub-strings:
-Alternative classifiers.
+shlex.Split("one \"two three\" four") -> []string{"one", "two three", "four"}
+
+To process a stream of tokens:
+
+l := NewLexer(os.Stdin)
+for token, err := l.Next(); err != nil {
+ // process token
+}
*/
import (
"bufio"
@@ -138,16 +145,16 @@
}
// NewLexer creates a new lexer from an input stream.
-func NewLexer(r io.Reader) (*Lexer, error) {
+func NewLexer(r io.Reader) *Lexer {
tokenizer := NewTokenizer(r)
lexer := &Lexer{tokenizer: tokenizer}
- return lexer, nil
+ return lexer
}
-// NextWords returns the next word, or an error. If there are no more words,
+// Next returns the next word, or an error. If there are no more words,
// the error will be io.EOF.
-func (l *Lexer) NextWord() (string, error) {
+func (l *Lexer) Next() (string, error) {
var token *Token
var err error
for {
@@ -420,15 +427,11 @@
}
// Split partitions a string into a slice of strings.
-
func Split(s string) ([]string, error) {
- l, err := NewLexer(strings.NewReader(s))
- if err != nil {
- return nil, err
- }
+ l := NewLexer(strings.NewReader(s))
subStrings := make([]string, 0)
for {
- word, err := l.NextWord()
+ word, err := l.Next()
if err != nil {
if err == io.EOF {
return subStrings, nil
diff --git a/shlex_test.go b/shlex_test.go
index 824f399..d729597 100644
--- a/shlex_test.go
+++ b/shlex_test.go
@@ -82,9 +82,8 @@
func TestLexer(t *testing.T) {
testInput := strings.NewReader("one")
expectedWord := "one"
- lexer, err := NewLexer(testInput)
- checkError(err, t)
- foundWord, err := lexer.NextWord()
+ lexer := NewLexer(testInput)
+ foundWord, err := lexer.Next()
checkError(err, t)
if expectedWord != foundWord {
t.Error("Expected word:", expectedWord, ". Found:", foundWord)