well those tests were broken

master
Jordan Orelli 10 years ago
parent 3b84cb18cd
commit c3ecc23b7c

@ -15,6 +15,7 @@ const (
t_string // a string literal t_string // a string literal
t_name // a name t_name // a name
t_type // a type t_type // a type
t_equals // equals sign
) )
type stateFn func(*lexer) (stateFn, error) type stateFn func(*lexer) (stateFn, error)
@ -106,6 +107,10 @@ func lexRoot(l *lexer) (stateFn, error) {
return nil, err return nil, err
} }
switch { switch {
case r == '=':
l.keep(r)
l.emit(t_equals)
return lexRoot, nil
case r == '"', r == '`': case r == '"', r == '`':
return lexStringLiteral(r), nil return lexStringLiteral(r), nil
case unicode.IsSpace(r): case unicode.IsSpace(r):
@ -147,7 +152,12 @@ func lexStringLiteral(delim rune) stateFn {
func lexName(l *lexer) (stateFn, error) { func lexName(l *lexer) (stateFn, error) {
r, err := l.next() r, err := l.next()
if err != nil { switch err {
case io.EOF:
l.emit(t_name)
return nil, io.EOF
case nil:
default:
return nil, err return nil, err
} }
switch { switch {
@ -163,7 +173,12 @@ func lexName(l *lexer) (stateFn, error) {
func lexType(l *lexer) (stateFn, error) { func lexType(l *lexer) (stateFn, error) {
r, err := l.next() r, err := l.next()
if err != nil { switch err {
case io.EOF:
l.emit(t_type)
return nil, io.EOF
case nil:
default:
return nil, err return nil, err
} }
switch { switch {

@ -6,22 +6,24 @@ import (
var primitivesTests = []struct { var primitivesTests = []struct {
in string in string
out token out []token
}{ }{
{`"x"`, token{t_string, "x"}}, {`"x"`, []token{{t_string, "x"}}},
{`"yes"`, token{t_string, "yes"}}, {`"yes"`, []token{{t_string, "yes"}}},
{`"this one has spaces"`, token{t_string, "this one has spaces"}}, {`"this one has spaces"`, []token{{t_string, "this one has spaces"}}},
{`"this one has \"quotes\" in it"`, token{t_string, `this one has "quotes" in it`}}, {`"this one has \"quotes\" in it"`, []token{{t_string, `this one has "quotes" in it`}}},
{"`this one is delimited by backticks`", token{t_string, "this one is delimited by backticks"}}, {"`this one is delimited by backticks`", []token{{t_string, "this one is delimited by backticks"}}},
{` "this one has white space on either end" `, token{t_string, "this one has white space on either end"}}, {` "this one has white space on either end" `, []token{{t_string, "this one has white space on either end"}}},
{`name`, token{t_name, "name"}}, {`name`, []token{{t_name, "name"}}},
{`name_with_underscore`, token{t_name, "name_with_underscore"}}, {`name_with_underscore`, []token{{t_name, "name_with_underscore"}}},
{` name_surrounded_by_whitespace `, token{t_name, "name_surrounded_by_whitespace"}}, {` name_surrounded_by_whitespace `, []token{{t_name, "name_surrounded_by_whitespace"}}},
{`name1`, token{t_name, "name1"}}, {`name1`, []token{{t_name, "name1"}}},
{`camelName`, token{t_name, "camelName"}}, {`camelName`, []token{{t_name, "camelName"}}},
{`Type`, token{t_type, "Type"}}, {`Type`, []token{{t_type, "Type"}}},
{`CamelType`, token{t_type, "CamelType"}}, {`CamelType`, []token{{t_type, "CamelType"}}},
{`Type_1_2`, token{t_type, "Type_1_1"}}, {`Type_1_2`, []token{{t_type, "Type_1_2"}}},
{`=`, []token{{t_equals, "="}}},
{` = `, []token{{t_equals, "="}}},
} }
func TestLexPrimities(t *testing.T) { func TestLexPrimities(t *testing.T) {
@ -31,10 +33,15 @@ func TestLexPrimities(t *testing.T) {
t.Error(err) t.Error(err)
continue continue
} }
if len(tokens) > 1 { if len(tokens) != len(test.out) {
t.Errorf("expected 1 token, saw %d: %v", len(tokens), tokens) t.Errorf("expected %d token, saw %d: %v", len(test.out), len(tokens), tokens)
continue continue
} }
for i := range tokens {
if tokens[i] != test.out[i] {
t.Errorf("token %d is %v, expected %v", i, tokens[i], test.out[i])
}
}
t.Logf("OK: %s", test.in) t.Logf("OK: %s", test.in)
} }
} }

Loading…
Cancel
Save