Reduced token size
This commit is contained in:
parent
7bfd0e731d
commit
1e3705df55
47 changed files with 543 additions and 764 deletions
|
@ -9,394 +9,243 @@ import (
|
|||
|
||||
func TestFunction(t *testing.T) {
|
||||
tokens := token.Tokenize([]byte("main(){}"))
|
||||
assert.DeepEqual(t, tokens, token.List{
|
||||
{
|
||||
Kind: token.Identifier,
|
||||
Bytes: []byte("main"),
|
||||
Position: 0,
|
||||
},
|
||||
{
|
||||
Kind: token.GroupStart,
|
||||
Bytes: []byte("("),
|
||||
Position: 4,
|
||||
},
|
||||
{
|
||||
Kind: token.GroupEnd,
|
||||
Bytes: []byte(")"),
|
||||
Position: 5,
|
||||
},
|
||||
{
|
||||
Kind: token.BlockStart,
|
||||
Bytes: []byte("{"),
|
||||
Position: 6,
|
||||
},
|
||||
{
|
||||
Kind: token.BlockEnd,
|
||||
Bytes: []byte("}"),
|
||||
Position: 7,
|
||||
},
|
||||
{
|
||||
Kind: token.EOF,
|
||||
Bytes: nil,
|
||||
Position: 8,
|
||||
},
|
||||
})
|
||||
|
||||
expected := []token.Kind{
|
||||
token.Identifier,
|
||||
token.GroupStart,
|
||||
token.GroupEnd,
|
||||
token.BlockStart,
|
||||
token.BlockEnd,
|
||||
token.EOF,
|
||||
}
|
||||
|
||||
for i, kind := range expected {
|
||||
assert.Equal(t, tokens[i].Kind, kind)
|
||||
}
|
||||
}
|
||||
|
||||
func TestKeyword(t *testing.T) {
|
||||
tokens := token.Tokenize([]byte("return x"))
|
||||
assert.DeepEqual(t, tokens, token.List{
|
||||
{
|
||||
Kind: token.Keyword,
|
||||
Bytes: []byte("return"),
|
||||
Position: 0,
|
||||
},
|
||||
{
|
||||
Kind: token.Identifier,
|
||||
Bytes: []byte("x"),
|
||||
Position: 7,
|
||||
},
|
||||
{
|
||||
Kind: token.EOF,
|
||||
Bytes: nil,
|
||||
Position: 8,
|
||||
},
|
||||
})
|
||||
|
||||
expected := []token.Kind{
|
||||
token.Return,
|
||||
token.Identifier,
|
||||
token.EOF,
|
||||
}
|
||||
|
||||
for i, kind := range expected {
|
||||
assert.Equal(t, tokens[i].Kind, kind)
|
||||
}
|
||||
}
|
||||
|
||||
func TestArray(t *testing.T) {
|
||||
tokens := token.Tokenize([]byte("array[i]"))
|
||||
assert.DeepEqual(t, tokens, token.List{
|
||||
{
|
||||
Kind: token.Identifier,
|
||||
Bytes: []byte("array"),
|
||||
Position: 0,
|
||||
},
|
||||
{
|
||||
Kind: token.ArrayStart,
|
||||
Bytes: []byte("["),
|
||||
Position: 5,
|
||||
},
|
||||
{
|
||||
Kind: token.Identifier,
|
||||
Bytes: []byte("i"),
|
||||
Position: 6,
|
||||
},
|
||||
{
|
||||
Kind: token.ArrayEnd,
|
||||
Bytes: []byte("]"),
|
||||
Position: 7,
|
||||
},
|
||||
{
|
||||
Kind: token.EOF,
|
||||
Bytes: nil,
|
||||
Position: 8,
|
||||
},
|
||||
})
|
||||
|
||||
expected := []token.Kind{
|
||||
token.Identifier,
|
||||
token.ArrayStart,
|
||||
token.Identifier,
|
||||
token.ArrayEnd,
|
||||
token.EOF,
|
||||
}
|
||||
|
||||
for i, kind := range expected {
|
||||
assert.Equal(t, tokens[i].Kind, kind)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewline(t *testing.T) {
|
||||
tokens := token.Tokenize([]byte("\n\n"))
|
||||
assert.DeepEqual(t, tokens, token.List{
|
||||
{
|
||||
Kind: token.NewLine,
|
||||
Bytes: []byte("\n"),
|
||||
Position: 0,
|
||||
},
|
||||
{
|
||||
Kind: token.NewLine,
|
||||
Bytes: []byte("\n"),
|
||||
Position: 1,
|
||||
},
|
||||
{
|
||||
Kind: token.EOF,
|
||||
Bytes: nil,
|
||||
Position: 2,
|
||||
},
|
||||
})
|
||||
|
||||
expected := []token.Kind{
|
||||
token.NewLine,
|
||||
token.NewLine,
|
||||
token.EOF,
|
||||
}
|
||||
|
||||
for i, kind := range expected {
|
||||
assert.Equal(t, tokens[i].Kind, kind)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNumber(t *testing.T) {
|
||||
tokens := token.Tokenize([]byte(`123 456`))
|
||||
assert.DeepEqual(t, tokens, token.List{
|
||||
{
|
||||
Kind: token.Number,
|
||||
Bytes: []byte("123"),
|
||||
Position: 0,
|
||||
},
|
||||
{
|
||||
Kind: token.Number,
|
||||
Bytes: []byte("456"),
|
||||
Position: 4,
|
||||
},
|
||||
{
|
||||
Kind: token.EOF,
|
||||
Bytes: nil,
|
||||
Position: 7,
|
||||
},
|
||||
})
|
||||
|
||||
expected := []token.Kind{
|
||||
token.Number,
|
||||
token.Number,
|
||||
token.EOF,
|
||||
}
|
||||
|
||||
for i, kind := range expected {
|
||||
assert.Equal(t, tokens[i].Kind, kind)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOperator(t *testing.T) {
|
||||
tokens := token.Tokenize([]byte(`+ - * /`))
|
||||
assert.DeepEqual(t, tokens, token.List{
|
||||
{
|
||||
Kind: token.Operator,
|
||||
Bytes: []byte("+"),
|
||||
Position: 0,
|
||||
},
|
||||
{
|
||||
Kind: token.Operator,
|
||||
Bytes: []byte("-"),
|
||||
Position: 2,
|
||||
},
|
||||
{
|
||||
Kind: token.Operator,
|
||||
Bytes: []byte("*"),
|
||||
Position: 4,
|
||||
},
|
||||
{
|
||||
Kind: token.Operator,
|
||||
Bytes: []byte("/"),
|
||||
Position: 6,
|
||||
},
|
||||
{
|
||||
Kind: token.EOF,
|
||||
Bytes: nil,
|
||||
Position: 7,
|
||||
},
|
||||
})
|
||||
|
||||
expected := []token.Kind{
|
||||
token.Add,
|
||||
token.Sub,
|
||||
token.Mul,
|
||||
token.Div,
|
||||
token.EOF,
|
||||
}
|
||||
|
||||
for i, kind := range expected {
|
||||
assert.Equal(t, tokens[i].Kind, kind)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOperatorAssign(t *testing.T) {
|
||||
tokens := token.Tokenize([]byte(`+= -= *= /= ==`))
|
||||
assert.DeepEqual(t, tokens, token.List{
|
||||
{
|
||||
Kind: token.Operator,
|
||||
Bytes: []byte("+="),
|
||||
Position: 0,
|
||||
},
|
||||
{
|
||||
Kind: token.Operator,
|
||||
Bytes: []byte("-="),
|
||||
Position: 3,
|
||||
},
|
||||
{
|
||||
Kind: token.Operator,
|
||||
Bytes: []byte("*="),
|
||||
Position: 6,
|
||||
},
|
||||
{
|
||||
Kind: token.Operator,
|
||||
Bytes: []byte("/="),
|
||||
Position: 9,
|
||||
},
|
||||
{
|
||||
Kind: token.Operator,
|
||||
Bytes: []byte("=="),
|
||||
Position: 12,
|
||||
},
|
||||
{
|
||||
Kind: token.EOF,
|
||||
Bytes: nil,
|
||||
Position: 14,
|
||||
},
|
||||
})
|
||||
tokens := token.Tokenize([]byte(`+= -= *= /= &= |= ^= <<= >>=`))
|
||||
|
||||
expected := []token.Kind{
|
||||
token.AddAssign,
|
||||
token.SubAssign,
|
||||
token.MulAssign,
|
||||
token.DivAssign,
|
||||
token.AndAssign,
|
||||
token.OrAssign,
|
||||
token.XorAssign,
|
||||
token.ShlAssign,
|
||||
token.ShrAssign,
|
||||
token.EOF,
|
||||
}
|
||||
|
||||
for i, kind := range expected {
|
||||
assert.Equal(t, tokens[i].Kind, kind)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSeparator(t *testing.T) {
|
||||
tokens := token.Tokenize([]byte("a,b,c"))
|
||||
assert.DeepEqual(t, tokens, token.List{
|
||||
{
|
||||
Kind: token.Identifier,
|
||||
Bytes: []byte("a"),
|
||||
Position: 0,
|
||||
},
|
||||
{
|
||||
Kind: token.Separator,
|
||||
Bytes: []byte(","),
|
||||
Position: 1,
|
||||
},
|
||||
{
|
||||
Kind: token.Identifier,
|
||||
Bytes: []byte("b"),
|
||||
Position: 2,
|
||||
},
|
||||
{
|
||||
Kind: token.Separator,
|
||||
Bytes: []byte(","),
|
||||
Position: 3,
|
||||
},
|
||||
{
|
||||
Kind: token.Identifier,
|
||||
Bytes: []byte("c"),
|
||||
Position: 4,
|
||||
},
|
||||
{
|
||||
Kind: token.EOF,
|
||||
Bytes: nil,
|
||||
Position: 5,
|
||||
},
|
||||
})
|
||||
|
||||
expected := []token.Kind{
|
||||
token.Identifier,
|
||||
token.Separator,
|
||||
token.Identifier,
|
||||
token.Separator,
|
||||
token.Identifier,
|
||||
token.EOF,
|
||||
}
|
||||
|
||||
for i, kind := range expected {
|
||||
assert.Equal(t, tokens[i].Kind, kind)
|
||||
}
|
||||
}
|
||||
|
||||
func TestComment(t *testing.T) {
|
||||
tokens := token.Tokenize([]byte("// Hello\n// World"))
|
||||
assert.DeepEqual(t, tokens, token.List{
|
||||
{
|
||||
Kind: token.Comment,
|
||||
Bytes: []byte(`// Hello`),
|
||||
Position: 0,
|
||||
},
|
||||
{
|
||||
Kind: token.NewLine,
|
||||
Bytes: []byte("\n"),
|
||||
Position: 8,
|
||||
},
|
||||
{
|
||||
Kind: token.Comment,
|
||||
Bytes: []byte(`// World`),
|
||||
Position: 9,
|
||||
},
|
||||
{
|
||||
Kind: token.EOF,
|
||||
Bytes: nil,
|
||||
Position: 17,
|
||||
},
|
||||
})
|
||||
|
||||
expected := []token.Kind{
|
||||
token.Comment,
|
||||
token.NewLine,
|
||||
token.Comment,
|
||||
token.EOF,
|
||||
}
|
||||
|
||||
for i, kind := range expected {
|
||||
assert.Equal(t, tokens[i].Kind, kind)
|
||||
}
|
||||
|
||||
tokens = token.Tokenize([]byte("// Hello\n"))
|
||||
assert.DeepEqual(t, tokens, token.List{
|
||||
{
|
||||
Kind: token.Comment,
|
||||
Bytes: []byte(`// Hello`),
|
||||
Position: 0,
|
||||
},
|
||||
{
|
||||
Kind: token.NewLine,
|
||||
Bytes: []byte("\n"),
|
||||
Position: 8,
|
||||
},
|
||||
{
|
||||
Kind: token.EOF,
|
||||
Bytes: nil,
|
||||
Position: 9,
|
||||
},
|
||||
})
|
||||
|
||||
expected = []token.Kind{
|
||||
token.Comment,
|
||||
token.NewLine,
|
||||
token.EOF,
|
||||
}
|
||||
|
||||
for i, kind := range expected {
|
||||
assert.Equal(t, tokens[i].Kind, kind)
|
||||
}
|
||||
|
||||
tokens = token.Tokenize([]byte(`// Hello`))
|
||||
assert.DeepEqual(t, tokens, token.List{
|
||||
{
|
||||
Kind: token.Comment,
|
||||
Bytes: []byte(`// Hello`),
|
||||
Position: 0,
|
||||
},
|
||||
{
|
||||
Kind: token.EOF,
|
||||
Bytes: nil,
|
||||
Position: 8,
|
||||
},
|
||||
})
|
||||
|
||||
expected = []token.Kind{
|
||||
token.Comment,
|
||||
token.EOF,
|
||||
}
|
||||
|
||||
for i, kind := range expected {
|
||||
assert.Equal(t, tokens[i].Kind, kind)
|
||||
}
|
||||
|
||||
tokens = token.Tokenize([]byte(`//`))
|
||||
assert.DeepEqual(t, tokens, token.List{
|
||||
{
|
||||
Kind: token.Comment,
|
||||
Bytes: []byte(`//`),
|
||||
Position: 0,
|
||||
},
|
||||
{
|
||||
Kind: token.EOF,
|
||||
Bytes: nil,
|
||||
Position: 2,
|
||||
},
|
||||
})
|
||||
|
||||
expected = []token.Kind{
|
||||
token.Comment,
|
||||
token.EOF,
|
||||
}
|
||||
|
||||
for i, kind := range expected {
|
||||
assert.Equal(t, tokens[i].Kind, kind)
|
||||
}
|
||||
|
||||
tokens = token.Tokenize([]byte(`/`))
|
||||
assert.DeepEqual(t, tokens, token.List{
|
||||
{
|
||||
Kind: token.Operator,
|
||||
Bytes: []byte(`/`),
|
||||
Position: 0,
|
||||
},
|
||||
{
|
||||
Kind: token.EOF,
|
||||
Bytes: nil,
|
||||
Position: 1,
|
||||
},
|
||||
})
|
||||
|
||||
expected = []token.Kind{
|
||||
token.Div,
|
||||
token.EOF,
|
||||
}
|
||||
|
||||
for i, kind := range expected {
|
||||
assert.Equal(t, tokens[i].Kind, kind)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInvalid(t *testing.T) {
|
||||
tokens := token.Tokenize([]byte(`@#`))
|
||||
assert.DeepEqual(t, tokens, token.List{
|
||||
{
|
||||
Kind: token.Invalid,
|
||||
Bytes: []byte(`@`),
|
||||
Position: 0,
|
||||
},
|
||||
{
|
||||
Kind: token.Invalid,
|
||||
Bytes: []byte(`#`),
|
||||
Position: 1,
|
||||
},
|
||||
{
|
||||
Kind: token.EOF,
|
||||
Bytes: nil,
|
||||
Position: 2,
|
||||
},
|
||||
})
|
||||
tokens := token.Tokenize([]byte(`##`))
|
||||
|
||||
expected := []token.Kind{
|
||||
token.Invalid,
|
||||
token.Invalid,
|
||||
token.EOF,
|
||||
}
|
||||
|
||||
for i, kind := range expected {
|
||||
assert.Equal(t, tokens[i].Kind, kind)
|
||||
}
|
||||
}
|
||||
|
||||
func TestString(t *testing.T) {
|
||||
tokens := token.Tokenize([]byte(`"Hello" "World"`))
|
||||
assert.DeepEqual(t, tokens, token.List{
|
||||
{
|
||||
Kind: token.String,
|
||||
Bytes: []byte(`"Hello"`),
|
||||
Position: 0,
|
||||
},
|
||||
{
|
||||
Kind: token.String,
|
||||
Bytes: []byte(`"World"`),
|
||||
Position: 8,
|
||||
},
|
||||
{
|
||||
Kind: token.EOF,
|
||||
Bytes: nil,
|
||||
Position: 15,
|
||||
},
|
||||
})
|
||||
|
||||
expected := []token.Kind{
|
||||
token.String,
|
||||
token.String,
|
||||
token.EOF,
|
||||
}
|
||||
|
||||
for i, kind := range expected {
|
||||
assert.Equal(t, tokens[i].Kind, kind)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStringMultiline(t *testing.T) {
|
||||
tokens := token.Tokenize([]byte("\"Hello\nWorld\""))
|
||||
assert.DeepEqual(t, tokens, token.List{
|
||||
{
|
||||
Kind: token.String,
|
||||
Bytes: []byte("\"Hello\nWorld\""),
|
||||
Position: 0,
|
||||
},
|
||||
{
|
||||
Kind: token.EOF,
|
||||
Bytes: nil,
|
||||
Position: 13,
|
||||
},
|
||||
})
|
||||
|
||||
expected := []token.Kind{
|
||||
token.String,
|
||||
token.EOF,
|
||||
}
|
||||
|
||||
for i, kind := range expected {
|
||||
assert.Equal(t, tokens[i].Kind, kind)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStringEOF(t *testing.T) {
|
||||
tokens := token.Tokenize([]byte(`"EOF`))
|
||||
assert.DeepEqual(t, tokens, token.List{
|
||||
{
|
||||
Kind: token.String,
|
||||
Bytes: []byte(`"EOF`),
|
||||
Position: 0,
|
||||
},
|
||||
{
|
||||
Kind: token.EOF,
|
||||
Bytes: nil,
|
||||
Position: 4,
|
||||
},
|
||||
})
|
||||
|
||||
expected := []token.Kind{
|
||||
token.String,
|
||||
token.EOF,
|
||||
}
|
||||
|
||||
for i, kind := range expected {
|
||||
assert.Equal(t, tokens[i].Kind, kind)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue