| 1 | module lexer |
| 2 | |
| 3 | fn assert_numeric_token(source string) { |
| 4 | tokens := tokenize(source) or { panic('${source}: ${err}') } |
| 5 | assert tokens.map(it.kind) == [.number, .eof], source |
| 6 | assert tokens.map(it.lexeme) == [source, ''], source |
| 7 | assert tokens[0].start == 0 |
| 8 | assert tokens[0].end == source.len |
| 9 | } |
| 10 | |
| 11 | fn assert_invalid_number(source string) { |
| 12 | if tokens := tokenize(source) { |
| 13 | assert false, 'expected a lexical error for ${source}, got ${tokens}' |
| 14 | } else { |
| 15 | assert err is LexError |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | fn test_decimal_integers() { |
| 20 | for text in ['0', '1', '42', '+42', '-42', '00042', '-0', '+0', '9223372036854775808', |
| 21 | '-9223372036854775809', '123456789012345678901234567890'] { |
| 22 | assert_numeric_token(text) |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | fn test_decimal_fractions_and_exponents() { |
| 27 | for text in ['.5', '+.5', '-.5', '1.', '1.0', '-12.34', '0.0001', '1e3', '1E3', '1e+3', '1e-3', |
| 28 | '1.e2', '.5e2', '-.5E-2', '12s3', '12f3', '12d3', '12l3', '12S+3', '12F-3', '12D3', '12L3'] { |
| 29 | assert_numeric_token(text) |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | fn test_rationals() { |
| 34 | for text in ['1/2', '-3/4', '+3/4', '0/1', '1/0', '12/34', '#b101/11', '#o17/10', '#d10/3', |
| 35 | '#xA/F', '#e1/3', '#i1/3'] { |
| 36 | assert_numeric_token(text) |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | fn test_numeric_prefixes_in_both_orders() { |
| 41 | for radix in ['#b', '#B', '#o', '#O', '#d', '#D', '#x', '#X'] { |
| 42 | for exactness in ['', '#e', '#E', '#i', '#I'] { |
| 43 | assert_numeric_token(radix + exactness + '10') |
| 44 | if exactness != '' { |
| 45 | assert_numeric_token(exactness + radix + '-10') |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | for text in ['#e1.2', '#i.5', '#d1e2', '#xdeadBEEF', '#x1e2', '#x+f', '#x-ff'] { |
| 50 | assert_numeric_token(text) |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | fn test_complex_numbers() { |
| 55 | for text in ['+i', '-i', '+2i', '-2i', '+.5i', '-1/2i', '1+i', '1-i', '1+2i', '1-2i', '-1-2i', |
| 56 | '+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', |
| 57 | '1+2I', '#xA+Bi', '#x-i', '#b1+10i', '#o7-2i', '#i#d+2i', '0+0i', '#e1.0+2.0i'] { |
| 58 | assert_numeric_token(text) |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | fn test_polar_numbers() { |
| 63 | 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', |
| 64 | '#e1@2'] { |
| 65 | assert_numeric_token(text) |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | fn test_unknown_digits() { |
| 70 | for text in ['1#', '12##', '1#/2#', '1#.##', '1##.', '1.2##', '1.##', '.12##', '1##e2', '1#.##e-2', |
| 71 | '+1#i', '1#+2#i', '1#@2#', '#b1#', '#o7##', '#xA##', '#e1#'] { |
| 72 | assert_numeric_token(text) |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | fn test_invalid_numeric_prefixes() { |
| 77 | for text in ['#b', '#o', '#d', '#x', '#e', '#i', '#q1', '#b2', '#o8', '#xg', '#b#b1', '#b#o1', |
| 78 | '#d#x1', '#e#e1', '#e#i1', '#i#e1', '#x#e#i1', '#e#d#d1', '#x#', '#e#x', '#b1.0', '#o1.0', |
| 79 | '#x1.0', '#b1e2', '#b1/2'] { |
| 80 | assert_invalid_number(text) |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | fn test_invalid_numeric_bodies() { |
| 85 | for text in ['1abc', '123xyz', '+.', '-.', '1..2', '1e', '1e+', '1e-', '1e2e3', '1e1.5', '1e#', |
| 86 | '1/2/3', '1/', '1/-2', '1/+2', '1.2/3', '1/2.3', '1/2e3', '2i', '1+-2i', '1--2i', '1+2', |
| 87 | '1+2ii', '1+i2', '1+2i3', '1@', '1@@2', '1@2@3', '1@2i', '1@+i', '1#2', '1#.2', '1.2#3', |
| 88 | '.1#2', '#xA#B'] { |
| 89 | assert_invalid_number(text) |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | fn test_numeric_case_is_preserved() { |
| 94 | assert_numeric_token('#E#XA+BI') |
| 95 | } |
| 96 | |
| 97 | fn test_each_digit_in_each_radix() { |
| 98 | for prefix, radix in { |
| 99 | '#b': 2 |
| 100 | '#o': 8 |
| 101 | '#d': 10 |
| 102 | '#x': 16 |
| 103 | } { |
| 104 | for digit, c in '0123456789abcdef' { |
| 105 | if digit < radix { |
| 106 | assert_numeric_token(prefix + c.ascii_str()) |
| 107 | assert_numeric_token(prefix + c.ascii_str().to_upper()) |
| 108 | } else { |
| 109 | assert_invalid_number(prefix + c.ascii_str()) |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | fn test_rectangular_numbers_from_real_components() { |
| 116 | for real_part in ['0', '12', '-3', '+4', '1/2', '-.25', '1e-3', '1##'] { |
| 117 | for sign in ['+', '-'] { |
| 118 | for imaginary_part in ['', '0', '12', '1/2', '.25', '1e+3', '1##'] { |
| 119 | assert_numeric_token(real_part + sign + imaginary_part + 'i') |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | fn test_polar_numbers_from_real_components() { |
| 126 | for magnitude in ['0', '12', '-3', '+4', '1/2', '-.25', '1e-3', '1##'] { |
| 127 | for angle in ['0', '12', '-3', '+4', '1/2', '-.25', '1e-3', '1##'] { |
| 128 | assert_numeric_token(magnitude + '@' + angle) |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | fn test_long_signed_numbers_and_imaginary_magnitudes() { |
| 134 | digits := '9'.repeat(262144) |
| 135 | for text in ['-' + digits, '+' + digits, '-' + digits + 'i', '+' + digits + 'I', |
| 136 | '#E#X-' + 'ABCDEF'.repeat(32768) + 'I'] { |
| 137 | assert_numeric_token(text) |
| 138 | } |
| 139 | } |
| 140 | |