scanner: enable 0oxx to handle octals

pull/3826/head
yuyi 2020-02-23 19:33:07 +08:00 committed by GitHub
parent 26fa833984
commit adb1d3f8c9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 20 deletions

View File

@ -113,3 +113,10 @@ fn test_hex() {
x := u64(10)
assert x.hex() == '0xa'
}
fn test_oct() {
x1 := 0o12
assert x1 == 10
x2 := 012
assert x2 == 10
}

View File

@ -122,7 +122,7 @@ fn filter_num_sep(txt byteptr, start int, end int) string {
mut i := start
mut i1 := 0
for i < end {
if txt[i] != num_sep {
if txt[i] != num_sep && txt[i] != `o` {
b[i1]=txt[i]
i1++
}
@ -172,17 +172,13 @@ fn (s mut Scanner) ident_hex_number() string {
fn (s mut Scanner) ident_oct_number() string {
start_pos := s.pos
s.pos += 2 // skip '0o'
for {
if s.pos >= s.text.len {
break
}
c := s.text[s.pos]
if c.is_digit() {
if !c.is_oct_digit() && c != num_sep {
s.error('malformed octal constant')
}
}
else {
if !c.is_oct_digit() && c != num_sep {
break
}
s.pos++
@ -253,12 +249,12 @@ fn (s mut Scanner) ident_number() string {
if s.expect('0x', s.pos) {
return s.ident_hex_number()
}
if s.expect('0o', s.pos) {
return s.ident_oct_number()
}
if s.expect('0.', s.pos) || s.expect('0e', s.pos) {
return s.ident_dec_number()
}
if s.text[s.pos] == `0` {
return s.ident_oct_number()
}
return s.ident_dec_number()
}

View File

@ -116,7 +116,7 @@ fn filter_num_sep(txt byteptr, start int, end int) string {
mut i := start
mut i1 := 0
for i < end {
if txt[i] != num_sep {
if txt[i] != num_sep && txt[i] != `o` {
b[i1] = txt[i]
i1++
}
@ -168,17 +168,13 @@ fn (s mut Scanner) ident_hex_number() string {
fn (s mut Scanner) ident_oct_number() string {
start_pos := s.pos
s.pos += 2 // skip '0o'
for {
if s.pos >= s.text.len {
break
}
c := s.text[s.pos]
if c.is_digit() {
if !c.is_oct_digit() && c != num_sep {
s.error('malformed octal constant')
}
}
else {
if !c.is_oct_digit() && c != num_sep {
break
}
s.pos++
@ -247,12 +243,12 @@ fn (s mut Scanner) ident_number() string {
if s.expect('0x', s.pos) {
return s.ident_hex_number()
}
if s.expect('0o', s.pos) {
return s.ident_oct_number()
}
if s.expect('0.', s.pos) || s.expect('0e', s.pos) {
return s.ident_dec_number()
}
if s.text[s.pos] == `0` {
return s.ident_oct_number()
}
return s.ident_dec_number()
}