diff options
author | njupt-moon <1015041018@njupt.edu.cn> | 2017-07-31 19:02:36 +0800 |
---|---|---|
committer | Felix Lange <fjl@users.noreply.github.com> | 2017-07-31 19:02:36 +0800 |
commit | 53f3460ab5b94875edf90c8b0f5da46b0c104321 (patch) | |
tree | 87bc935f92353b0a6b60630cb1ffddcb30f0b560 /core | |
parent | bdf98b4fcdb7f7c800568fa32bafe03a070c5802 (diff) | |
download | dexon-53f3460ab5b94875edf90c8b0f5da46b0c104321.tar dexon-53f3460ab5b94875edf90c8b0f5da46b0c104321.tar.gz dexon-53f3460ab5b94875edf90c8b0f5da46b0c104321.tar.bz2 dexon-53f3460ab5b94875edf90c8b0f5da46b0c104321.tar.lz dexon-53f3460ab5b94875edf90c8b0f5da46b0c104321.tar.xz dexon-53f3460ab5b94875edf90c8b0f5da46b0c104321.tar.zst dexon-53f3460ab5b94875edf90c8b0f5da46b0c104321.zip |
core/asm: fix hex number lexing (#14861)
Diffstat (limited to 'core')
-rw-r--r-- | core/asm/lex_test.go | 45 | ||||
-rw-r--r-- | core/asm/lexer.go | 2 |
2 files changed, 41 insertions, 6 deletions
diff --git a/core/asm/lex_test.go b/core/asm/lex_test.go index 36e67bcf7..e6901d4e3 100644 --- a/core/asm/lex_test.go +++ b/core/asm/lex_test.go @@ -16,7 +16,10 @@ package asm -import "testing" +import ( + "reflect" + "testing" +) func lexAll(src string) []token { ch := Lex("test.asm", []byte(src), false) @@ -28,9 +31,41 @@ func lexAll(src string) []token { return tokens } -func TestComment(t *testing.T) { - tokens := lexAll(";; this is a comment") - if len(tokens) != 2 { // {new line, EOF} - t.Error("expected no tokens") +func TestLexer(t *testing.T) { + tests := []struct { + input string + tokens []token + }{ + { + input: ";; this is a comment", + tokens: []token{{typ: lineStart}, {typ: eof}}, + }, + { + input: "0x12345678", + tokens: []token{{typ: lineStart}, {typ: number, text: "0x12345678"}, {typ: eof}}, + }, + { + input: "0x123ggg", + tokens: []token{{typ: lineStart}, {typ: number, text: "0x123"}, {typ: element, text: "ggg"}, {typ: eof}}, + }, + { + input: "12345678", + tokens: []token{{typ: lineStart}, {typ: number, text: "12345678"}, {typ: eof}}, + }, + { + input: "123abc", + tokens: []token{{typ: lineStart}, {typ: number, text: "123"}, {typ: element, text: "abc"}, {typ: eof}}, + }, + { + input: "0123abc", + tokens: []token{{typ: lineStart}, {typ: number, text: "0123"}, {typ: element, text: "abc"}, {typ: eof}}, + }, + } + + for _, test := range tests { + tokens := lexAll(test.input) + if !reflect.DeepEqual(tokens, test.tokens) { + t.Errorf("input %q\ngot: %+v\nwant: %+v", test.input, tokens, test.tokens) + } } } diff --git a/core/asm/lexer.go b/core/asm/lexer.go index 2770bd35f..d784e5d50 100644 --- a/core/asm/lexer.go +++ b/core/asm/lexer.go @@ -254,7 +254,7 @@ func lexInsideString(l *lexer) stateFn { func lexNumber(l *lexer) stateFn { acceptance := Numbers - if l.accept("0") && l.accept("xX") { + if l.accept("0") || l.accept("xX") { acceptance = HexadecimalNumbers } l.acceptRun(acceptance) |