scnaner: implement scanning hexadecimal numbers
This commit is contained in:
parent
5d75c5a143
commit
0255587e95
@ -132,8 +132,13 @@ func (s *Scanner) Scan() (tok token.Token) {
|
|||||||
func (s *Scanner) scanNumber(ch rune) token.Token {
|
func (s *Scanner) scanNumber(ch rune) token.Token {
|
||||||
if ch == '0' {
|
if ch == '0' {
|
||||||
// check hexadecimal or float
|
// check hexadecimal or float
|
||||||
// ch = s.next()
|
ch = s.next()
|
||||||
// return token.ILLEGAL
|
if ch == 'x' || ch == 'X' {
|
||||||
|
ch = s.next()
|
||||||
|
s.scanHexadecimal(ch)
|
||||||
|
return token.NUMBER
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
s.scanMantissa(ch)
|
s.scanMantissa(ch)
|
||||||
@ -147,6 +152,23 @@ func (s *Scanner) scanMantissa(ch rune) {
|
|||||||
s.unread()
|
s.unread()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Scanner) scanHexadecimal(ch rune) {
|
||||||
|
found := false
|
||||||
|
|
||||||
|
// after "0x" or "0X"
|
||||||
|
for isHexadecimal(ch) {
|
||||||
|
ch = s.next()
|
||||||
|
found = true
|
||||||
|
}
|
||||||
|
|
||||||
|
if !found {
|
||||||
|
// only scanned "0x" or "0X"
|
||||||
|
s.err("illegal hexadecimal number")
|
||||||
|
}
|
||||||
|
|
||||||
|
s.unread()
|
||||||
|
}
|
||||||
|
|
||||||
// scanString scans a quoted string
|
// scanString scans a quoted string
|
||||||
func (s *Scanner) scanString() {
|
func (s *Scanner) scanString() {
|
||||||
for {
|
for {
|
||||||
@ -268,6 +290,10 @@ func isDecimal(ch rune) bool {
|
|||||||
return '0' <= ch && ch <= '9'
|
return '0' <= ch && ch <= '9'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func isHexadecimal(ch rune) bool {
|
||||||
|
return '0' <= ch && ch <= '9' || 'a' <= ch && ch <= 'f' || 'A' <= ch && ch <= 'F'
|
||||||
|
}
|
||||||
|
|
||||||
// isWhitespace returns true if the rune is a space, tab, newline or carriage return
|
// isWhitespace returns true if the rune is a space, tab, newline or carriage return
|
||||||
func isWhitespace(ch rune) bool {
|
func isWhitespace(ch rune) bool {
|
||||||
return ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r'
|
return ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r'
|
||||||
|
@ -111,18 +111,18 @@ func TestNumber(t *testing.T) {
|
|||||||
{token.NUMBER, "07"},
|
{token.NUMBER, "07"},
|
||||||
{token.NUMBER, "042"},
|
{token.NUMBER, "042"},
|
||||||
{token.NUMBER, "01234567"},
|
{token.NUMBER, "01234567"},
|
||||||
// {token.NUMBER, "0x0"},
|
{token.NUMBER, "0x0"},
|
||||||
// {token.NUMBER, "0x1"},
|
{token.NUMBER, "0x1"},
|
||||||
// {token.NUMBER, "0xf"},
|
{token.NUMBER, "0xf"},
|
||||||
// {token.NUMBER, "0x42"},
|
{token.NUMBER, "0x42"},
|
||||||
// {token.NUMBER, "0x123456789abcDEF"},
|
{token.NUMBER, "0x123456789abcDEF"},
|
||||||
// {token.NUMBER, "0x" + f100},
|
{token.NUMBER, "0x" + f100},
|
||||||
// {token.NUMBER, "0X0"},
|
{token.NUMBER, "0X0"},
|
||||||
// {token.NUMBER, "0X1"},
|
{token.NUMBER, "0X1"},
|
||||||
// {token.NUMBER, "0XF"},
|
{token.NUMBER, "0XF"},
|
||||||
// {token.NUMBER, "0X42"},
|
{token.NUMBER, "0X42"},
|
||||||
// {token.NUMBER, "0X123456789abcDEF"},
|
{token.NUMBER, "0X123456789abcDEF"},
|
||||||
// {token.NUMBER, "0X" + f100},
|
{token.NUMBER, "0X" + f100},
|
||||||
// {token.FLOAT, "0."},
|
// {token.FLOAT, "0."},
|
||||||
// {token.FLOAT, "1."},
|
// {token.FLOAT, "1."},
|
||||||
// {token.FLOAT, "42."},
|
// {token.FLOAT, "42."},
|
||||||
|
Loading…
Reference in New Issue
Block a user