module lexer fn assert_numeric_token(source string) { tokens := tokenize(source) or { panic('${source}: ${err}') } assert tokens.map(it.kind) == [.number, .eof], source assert tokens.map(it.lexeme) == [source, ''], source assert tokens[0].start == 0 assert tokens[0].end == source.len } fn assert_invalid_number(source string) { if tokens := tokenize(source) { assert false, 'expected a lexical error for ${source}, got ${tokens}' } else { assert err is LexError } } fn test_decimal_integers() { for text in ['0', '1', '42', '+42', '-42', '00042', '-0', '+0', '9223372036854775808', '-9223372036854775809', '123456789012345678901234567890'] { assert_numeric_token(text) } } fn test_decimal_fractions_and_exponents() { for text in ['.5', '+.5', '-.5', '1.', '1.0', '-12.34', '0.0001', '1e3', '1E3', '1e+3', '1e-3', '1.e2', '.5e2', '-.5E-2', '12s3', '12f3', '12d3', '12l3', '12S+3', '12F-3', '12D3', '12L3'] { assert_numeric_token(text) } } fn test_rationals() { for text in ['1/2', '-3/4', '+3/4', '0/1', '1/0', '12/34', '#b101/11', '#o17/10', '#d10/3', '#xA/F', '#e1/3', '#i1/3'] { assert_numeric_token(text) } } fn test_numeric_prefixes_in_both_orders() { for radix in ['#b', '#B', '#o', '#O', '#d', '#D', '#x', '#X'] { for exactness in ['', '#e', '#E', '#i', '#I'] { assert_numeric_token(radix + exactness + '10') if exactness != '' { assert_numeric_token(exactness + radix + '-10') } } } for text in ['#e1.2', '#i.5', '#d1e2', '#xdeadBEEF', '#x1e2', '#x+f', '#x-ff'] { assert_numeric_token(text) } } fn test_complex_numbers() { for text in ['+i', '-i', '+2i', '-2i', '+.5i', '-1/2i', '1+i', '1-i', '1+2i', '1-2i', '-1-2i', '+inf.0', '-nan.0', '1+inf.0i', '+1+2i', '1/2+3/4i', '1.5-2.5i', '1e-2+3e+4i', '1s+2-3f-4i', '1+2I', '#xA+Bi', '#x-i', '#b1+10i', '#o7-2i', '#i#d+2i', '0+0i', '#e1.0+2.0i'] { assert_numeric_token(text) } } fn test_polar_numbers() { for text in ['1@2', '-1@2', '1@-2', '+1@+2', '1/2@3/4', '1.5@-.5', '1e2@3e-2', '#xA@-B', '#b10@+1', '#e1@2'] { assert_numeric_token(text) } } fn test_unknown_digits() { for text in ['1#', '12##', '1#/2#', '1#.##', '1##.', '1.2##', '1.##', '.12##', '1##e2', '1#.##e-2', '+1#i', '1#+2#i', '1#@2#', '#b1#', '#o7##', '#xA##', '#e1#'] { assert_numeric_token(text) } } fn test_invalid_numeric_prefixes() { for text in ['#b', '#o', '#d', '#x', '#e', '#i', '#q1', '#b2', '#o8', '#xg', '#b#b1', '#b#o1', '#d#x1', '#e#e1', '#e#i1', '#i#e1', '#x#e#i1', '#e#d#d1', '#x#', '#e#x', '#b1.0', '#o1.0', '#x1.0', '#b1e2', '#b1/2'] { assert_invalid_number(text) } } fn test_invalid_numeric_bodies() { for text in ['1abc', '123xyz', '+.', '-.', '1..2', '1e', '1e+', '1e-', '1e2e3', '1e1.5', '1e#', '1/2/3', '1/', '1/-2', '1/+2', '1.2/3', '1/2.3', '1/2e3', '2i', '1+-2i', '1--2i', '1+2', '1+2ii', '1+i2', '1+2i3', '1@', '1@@2', '1@2@3', '1@2i', '1@+i', '1#2', '1#.2', '1.2#3', '.1#2', '#xA#B'] { assert_invalid_number(text) } } fn test_numeric_case_is_preserved() { assert_numeric_token('#E#XA+BI') } fn test_each_digit_in_each_radix() { for prefix, radix in { '#b': 2 '#o': 8 '#d': 10 '#x': 16 } { for digit, c in '0123456789abcdef' { if digit < radix { assert_numeric_token(prefix + c.ascii_str()) assert_numeric_token(prefix + c.ascii_str().to_upper()) } else { assert_invalid_number(prefix + c.ascii_str()) } } } } fn test_rectangular_numbers_from_real_components() { for real_part in ['0', '12', '-3', '+4', '1/2', '-.25', '1e-3', '1##'] { for sign in ['+', '-'] { for imaginary_part in ['', '0', '12', '1/2', '.25', '1e+3', '1##'] { assert_numeric_token(real_part + sign + imaginary_part + 'i') } } } } fn test_polar_numbers_from_real_components() { for magnitude in ['0', '12', '-3', '+4', '1/2', '-.25', '1e-3', '1##'] { for angle in ['0', '12', '-3', '+4', '1/2', '-.25', '1e-3', '1##'] { assert_numeric_token(magnitude + '@' + angle) } } } fn test_long_signed_numbers_and_imaginary_magnitudes() { digits := '9'.repeat(262144) for text in ['-' + digits, '+' + digits, '-' + digits + 'i', '+' + digits + 'I', '#E#X-' + 'ABCDEF'.repeat(32768) + 'I'] { assert_numeric_token(text) } }