master
Jordan Orelli 10 years ago
parent c7255523ef
commit 3b84cb18cd

@ -14,6 +14,7 @@ const (
t_error tokenType = iota // a stored lex error t_error tokenType = iota // a stored lex error
t_string // a string literal t_string // a string literal
t_name // a name t_name // a name
t_type // a type
) )
type stateFn func(*lexer) (stateFn, error) type stateFn func(*lexer) (stateFn, error)
@ -112,6 +113,9 @@ func lexRoot(l *lexer) (stateFn, error) {
case unicode.IsLower(r): case unicode.IsLower(r):
l.keep(r) l.keep(r)
return lexName, nil return lexName, nil
case unicode.IsUpper(r):
l.keep(r)
return lexType, nil
default: default:
return nil, fmt.Errorf("unexpected rune in lexRoot: %c", r) return nil, fmt.Errorf("unexpected rune in lexRoot: %c", r)
} }
@ -146,11 +150,29 @@ func lexName(l *lexer) (stateFn, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
if r == '_' || unicode.IsLower(r) { switch {
case unicode.IsLetter(r), unicode.IsDigit(r), r == '_':
l.keep(r) l.keep(r)
return lexName, nil return lexName, nil
default:
l.emit(t_name)
l.unread(r)
return lexRoot, nil
}
}
func lexType(l *lexer) (stateFn, error) {
r, err := l.next()
if err != nil {
return nil, err
}
switch {
case unicode.IsLetter(r), unicode.IsDigit(r), r == '_':
l.keep(r)
return lexType, nil
default:
l.emit(t_type)
l.unread(r)
return lexRoot, nil
} }
l.emit(t_name)
l.unread(r)
return lexRoot, nil
} }

@ -17,6 +17,11 @@ var primitivesTests = []struct {
{`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"}},
{`camelName`, token{t_name, "camelName"}},
{`Type`, token{t_type, "Type"}},
{`CamelType`, token{t_type, "CamelType"}},
{`Type_1_2`, token{t_type, "Type_1_1"}},
} }
func TestLexPrimities(t *testing.T) { func TestLexPrimities(t *testing.T) {

Loading…
Cancel
Save