dymokomi/luce-browser-engine
Source of release 0.6.0
0.6.0 / src / web / html / numbers.lucb
#==============================================================================================
#
# numbers - HTML::Numbers: the common microsyntaxes for integers and floating-point numbers
#
# DESCRIPTION:
# Port of LibWeb/HTML/Numbers.cpp (lines 1-368): the rules for parsing integers,
# non-negative integers and floating-point number values, the valid floating-point number
# check, and the non-negative integer reflection helper. The C++ `goto fraction`,
# `goto fraction_exit` and `goto conversion` of parse_floating_point_number are labeled
# loops that run once: `break conversion` and `break fraction` leave them where C++ jumps.
#
#==============================================================================================
# mark: Integers ================================================================================
## Ported from Numbers.cpp:17 Web::HTML::parse_integer_digits.
# https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#rules-for-parsing-integers
pub func html_parse_integer_digits(string: ak.StringView) -> ak.StringView?:
let is_ascii_whitespace = ak.CharPredicateFunction(function = web_infra.is_ascii_whitespace)
let is_ascii_digit = ak.CharPredicateFunction(function = ak.is_ascii_digit)
# 1. Let input be the string being parsed.
# 2. Let position be a pointer into input, initially pointing at the start of the string.
var lexer = ak.generic_lexer_new(string)
# 3. Let sign have the value "positive".
# NOTE: Skipped, see comment on step 6.
# 4. Skip ASCII whitespace within input given position.
ak.generic_lexer_ignore_while(&lexer, &is_ascii_whitespace)
# 5. If position is past the end of input, return an error.
if ak.generic_lexer_is_eof(&lexer):
return none
# 6. If the character indicated by position (the first character) is a U+002D HYPHEN-MINUS character (-):
#
# If we parse a signed integer, then we include the sign character (if present) in the collect step
# (step 8) and lean on `AK::StringUtils::convert_to_int` to handle it for us.
let start_index = ak.generic_lexer_tell(&lexer)
if ak.generic_lexer_peek(&lexer) == '-' or ak.generic_lexer_peek(&lexer) == '+':
_ = ak.generic_lexer_consume(&lexer)
# 7. If the character indicated by position is not an ASCII digit, then return an error.
if not ak.generic_lexer_next_is_tt_predicate(&lexer, &is_ascii_digit):
return none
# 8. Collect a sequence of code points that are ASCII digits from input given position, and interpret the resulting sequence as a base-ten integer. Let value be that integer.
# NOTE: Integer conversion is performed by the caller.
_ = ak.generic_lexer_consume_while(&lexer, &is_ascii_digit)
let end_index = ak.generic_lexer_tell(&lexer)
let input = ak.generic_lexer_input(&lexer)
let digits = ak.string_view_substring_view_usize(&input, start_index, end_index - start_index)
# 9. If sign is "positive", return value, otherwise return the result of subtracting value from zero.
# NOTE: Skipped, see comment on step 6.
return digits
## Ported from Numbers.cpp:60 Web::HTML::parse_integer.
# https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#rules-for-parsing-integers
pub func html_parse_integer(string: ak.StringView) -> i32?:
let digits = html_parse_integer_digits(string) else return none
return ak.string_view_to_number_i32(&digits, ak.TrimWhitespace.no)
## Ported from Numbers.cpp:70 Web::HTML::parse_non_negative_integer_digits.
# https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#rules-for-parsing-non-negative-integers
pub func html_parse_non_negative_integer_digits(string: ak.StringView) -> ak.StringView?:
# 1. Let input be the string being parsed.
# 2. Let value be the result of parsing input using the rules for parsing integers.
#
# NOTE: Because we call `parse_integer`, we parse all integers as signed. If we need the extra
# size that an unsigned integer offers, then this would need to be improved. That said,
# I don't think we need to support such large integers at the moment.
let optional_integer_digits = html_parse_integer_digits(string)
# 3. If value is an error, return an error.
let integer_digits = optional_integer_digits else return none
# 4. If value is less than zero, return an error.
if ak.string_view_length(&integer_digits) > 1 and ak.string_view_starts_with_u8(&integer_digits, '-') and ak.string_view_at(&integer_digits, 1) != '0':
return none
# 5. Return value.
# NOTE: Integer conversion is performed by the caller.
return integer_digits
## Ported from Numbers.cpp:92 Web::HTML::parse_non_negative_integer.
# https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#rules-for-parsing-non-negative-integers
pub func html_parse_non_negative_integer(string: ak.StringView) -> u32?:
let digits = html_parse_non_negative_integer_digits(string) else return none
let value = ak.string_view_to_number_i64(&digits, ak.TrimWhitespace.no) else return none
if value > (i64)ak.u32_max:
return none
return (u32)value
# mark: Floating-point numbers ==================================================================
## Ported from Numbers.cpp:106 Web::HTML::parse_floating_point_number(StringView).
# https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#rules-for-parsing-floating-point-number-values
pub func html_parse_floating_point_number_string_view(string: ak.StringView) -> f64?:
let is_ascii_whitespace = ak.CharPredicateFunction(function = web_infra.is_ascii_whitespace)
let is_ascii_digit = ak.CharPredicateFunction(function = ak.is_ascii_digit)
# 1. Let input be the string being parsed.
# 2. Let position be a pointer into input, initially pointing at the start of the string.
var lexer = ak.generic_lexer_new(string)
# 3. Let value have the value 1.
var value: f64 = 1.0
# 4. Let divisor have the value 1.
var divisor: f64 = 1.0
# 5. Let exponent have the value 1.
var exponent: i16 = 1
# 6. Skip ASCII whitespace within input given position.
ak.generic_lexer_ignore_while(&lexer, &is_ascii_whitespace)
# 7. If position is past the end of input, return an error.
if ak.generic_lexer_is_eof(&lexer):
return none
# 8. If the character indicated by position is a U+002D HYPHEN-MINUS character (-):
if ak.generic_lexer_next_is_t_char_type(&lexer, '-'):
# 8.1. Change value and divisor to −1.
value = -1.0
divisor = -1.0
# 8.2. Advance position to the next character.
_ = ak.generic_lexer_consume(&lexer)
# 8.3. If position is past the end of input, return an error.
if ak.generic_lexer_is_eof(&lexer):
return none
# Otherwise, if the character indicated by position (the first character) is a U+002B PLUS SIGN character (+):
elif ak.generic_lexer_next_is_t_char_type(&lexer, '+'):
# 8.1. Advance position to the next character. (The "+" is ignored, but it is not conforming.)
_ = ak.generic_lexer_consume(&lexer)
# 8.2. If position is past the end of input, return an error.
if ak.generic_lexer_is_eof(&lexer):
return none
# 9. If the character indicated by position is a U+002E FULL STOP (.),
# and that is not the last character in input,
# and the character after the character indicated by position is an ASCII digit,
# then set value to zero and jump to the step labeled fraction.
var jump_to_fraction = false
if ak.generic_lexer_next_is_t_char_type(&lexer, '.') and ak.generic_lexer_tell_remaining(&lexer) > 1 and ak.is_ascii_digit(ak.generic_lexer_peek(&lexer, 1)):
value = 0.0
jump_to_fraction = true
# The step labeled conversion follows this loop; `break conversion` is C++'s `goto conversion`.
conversion: while true:
if not jump_to_fraction:
# 10. If the character indicated by position is not an ASCII digit, then return an error.
if not ak.generic_lexer_next_is_tt_predicate(&lexer, &is_ascii_digit):
return none
# 11. Collect a sequence of code points that are ASCII digits from input given position, and interpret the resulting sequence as a base-ten integer.
# Multiply value by that integer.
let start_index = ak.generic_lexer_tell(&lexer)
_ = ak.generic_lexer_consume_while(&lexer, &is_ascii_digit)
let end_index = ak.generic_lexer_tell(&lexer)
let input = ak.generic_lexer_input(&lexer)
let digits = ak.string_view_substring_view_usize(&input, start_index, end_index - start_index)
let optional_value = ak.string_view_to_number_f64(&digits, ak.TrimWhitespace.no)
value *= optional_value else trap("VERIFY: Optional::value() of the collected digits")
# 12. If position is past the end of input, jump to the step labeled conversion.
if ak.generic_lexer_is_eof(&lexer):
break conversion
# fraction: (`break fraction` is C++'s `goto fraction_exit`)
fraction: while true:
# 13. Fraction: If the character indicated by position is a U+002E FULL STOP (.), run these substeps:
if ak.generic_lexer_next_is_t_char_type(&lexer, '.'):
# 13.1. Advance position to the next character.
_ = ak.generic_lexer_consume(&lexer)
# 13.2. If position is past the end of input,
# or if the character indicated by position is not an ASCII digit,
# U+0065 LATIN SMALL LETTER E (e), or U+0045 LATIN CAPITAL LETTER E (E),
# then jump to the step labeled conversion.
if ak.generic_lexer_is_eof(&lexer) or (not ak.generic_lexer_next_is_tt_predicate(&lexer, &is_ascii_digit) and not ak.generic_lexer_next_is_t_char_type(&lexer, 'e') and not ak.generic_lexer_next_is_t_char_type(&lexer, 'E')):
break conversion
# 13.3. If the character indicated by position is a U+0065 LATIN SMALL LETTER E character (e) or a U+0045 LATIN CAPITAL LETTER E character (E),
# skip the remainder of these substeps.
if ak.generic_lexer_next_is_t_char_type(&lexer, 'e') or ak.generic_lexer_next_is_t_char_type(&lexer, 'E'):
break fraction
# fraction_loop:
while true:
# 13.4. Fraction loop: Multiply divisor by ten.
divisor *= 10.0
# 13.5. Add the value of the character indicated by position, interpreted as a base-ten digit (0..9) and divided by divisor, to value.
value += f64((i32)ak.generic_lexer_peek(&lexer) - (i32)'0') / divisor
# 13.6. Advance position to the next character.
_ = ak.generic_lexer_consume(&lexer)
# 13.7. If position is past the end of input, then jump to the step labeled conversion.
if ak.generic_lexer_is_eof(&lexer):
break conversion
# 13.8. If the character indicated by position is an ASCII digit, jump back to the step labeled fraction loop in these substeps.
if not ak.generic_lexer_next_is_tt_predicate(&lexer, &is_ascii_digit):
break
break fraction
# fraction_exit:
# 14. If the character indicated by position is U+0065 (e) or a U+0045 (E), then:
if ak.generic_lexer_next_is_t_char_type(&lexer, 'e') or ak.generic_lexer_next_is_t_char_type(&lexer, 'E'):
# 14.1. Advance position to the next character.
_ = ak.generic_lexer_consume(&lexer)
# 14.2. If position is past the end of input, then jump to the step labeled conversion.
if ak.generic_lexer_is_eof(&lexer):
break conversion
# 14.3. If the character indicated by position is a U+002D HYPHEN-MINUS character (-):
if ak.generic_lexer_next_is_t_char_type(&lexer, '-'):
# 14.3.1. Change exponent to −1.
exponent = -1
# 14.3.2. Advance position to the next character.
_ = ak.generic_lexer_consume(&lexer)
# 14.3.3. If position is past the end of input, then jump to the step labeled conversion.
if ak.generic_lexer_is_eof(&lexer):
break conversion
# Otherwise, if the character indicated by position is a U+002B PLUS SIGN character (+):
elif ak.generic_lexer_next_is_t_char_type(&lexer, '+'):
# 14.3.1. Advance position to the next character.
_ = ak.generic_lexer_consume(&lexer)
# 14.3.2. If position is past the end of input, then jump to the step labeled conversion.
if ak.generic_lexer_is_eof(&lexer):
break conversion
# 14.4. If the character indicated by position is not an ASCII digit, then jump to the step labeled conversion.
if not ak.generic_lexer_next_is_tt_predicate(&lexer, &is_ascii_digit):
break conversion
# 14.5. Collect a sequence of code points that are ASCII digits from input given position, and interpret the resulting sequence as a base-ten integer.
# Multiply exponent by that integer.
let start_index = ak.generic_lexer_tell(&lexer)
_ = ak.generic_lexer_consume_while(&lexer, &is_ascii_digit)
let end_index = ak.generic_lexer_tell(&lexer)
let input = ak.generic_lexer_input(&lexer)
let digits = ak.string_view_substring_view_usize(&input, start_index, end_index - start_index)
let optional_value = ak.string_view_to_number_i32(&digits)
# C++: `exponent *= value` computes in int and stores the low 16 bits into the i16.
exponent = (i16)((i32)exponent *% (optional_value else trap("VERIFY: Optional::value() of the exponent digits")))
# 14.6. Multiply value by ten raised to the exponentth power.
value *= math.pow(10.0, f64(exponent))
break conversion
# conversion:
# 15. Conversion: Let S be the set of finite IEEE 754 double-precision floating-point values except −0,
# but with two special values added: 2^1024 and −2^1024.
if not math.is_finite(value):
return none
if value == 0.0 and math.signbit(value):
return 0.0
# 16. Let rounded-value be the number in S that is closest to value, selecting the number with an even significand if there are two equally close values.
# (The two special values 2^1024 and −2^1024 are considered to have even significands for this purpose.)
let rounded_value = value
# 17. If rounded-value is 2^1024 or −2^1024, return an error.
if math.abs(rounded_value) >= math.pow(2.0, 1024.0):
return none
# 18. Return rounded-value.
return rounded_value
## Ported from Numbers.cpp:290 Web::HTML::parse_floating_point_number(Utf16String const&).
# https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#rules-for-parsing-floating-point-number-values
pub func html_parse_floating_point_number_utf16_string(string: ak.Utf16String) -> f64?:
# FIXME: Implement a UTF-16 GenericLexer.
if not ak.utf16_string_base_has_ascii_storage(&string.utf16_string_base):
return none
return html_parse_floating_point_number_string_view(ak.utf16_string_base_ascii_view(&string.utf16_string_base))
## Ported from Numbers.cpp:299 Web::HTML::is_valid_floating_point_number(StringView).
# https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#valid-floating-point-number
pub func html_is_valid_floating_point_number_string_view(string: ak.StringView) -> bool:
let is_ascii_digit = ak.CharPredicateFunction(function = ak.is_ascii_digit)
var lexer = ak.generic_lexer_new(string)
# 1. Optionally, a U+002D HYPHEN-MINUS character (-).
_ = ak.generic_lexer_consume_specific_t_char_type(&lexer, '-')
# 2. One or both of the following, in the given order:
# 2.1. A series of one or more ASCII digits.
let leading_digits = ak.generic_lexer_consume_while(&lexer, &is_ascii_digit)
let has_leading_digits = not ak.string_view_is_empty(&leading_digits)
# 2.2. Both of the following, in the given order:
# 2.2.1. A single U+002E FULL STOP character (.).
if ak.generic_lexer_consume_specific_t_char_type(&lexer, '.'):
# 2.2.2. A series of one or more ASCII digits.
let fraction_digits = ak.generic_lexer_consume_while(&lexer, &is_ascii_digit)
if ak.string_view_is_empty(&fraction_digits):
return false
elif not has_leading_digits:
# Doesn’t begin with digits, doesn’t begin with a full stop followed by digits.
return false
# 3. Optionally:
# 3.1. Either a U+0065 LATIN SMALL LETTER E character (e) or a U+0045 LATIN CAPITAL
# LETTER E character (E).
if ak.generic_lexer_consume_specific_t_char_type(&lexer, 'e') or ak.generic_lexer_consume_specific_t_char_type(&lexer, 'E'):
# 3.2. Optionally, a U+002D HYPHEN-MINUS character (-) or U+002B PLUS SIGN
# character (+).
if not ak.generic_lexer_consume_specific_t_char_type(&lexer, '-'):
_ = ak.generic_lexer_consume_specific_t_char_type(&lexer, '+')
# 3.3. A series of one or more ASCII digits.
let exponent_digits = ak.generic_lexer_consume_while(&lexer, &is_ascii_digit)
if ak.string_view_is_empty(&exponent_digits):
return false
return ak.generic_lexer_tell_remaining(&lexer) == 0
## Ported from Numbers.cpp:332 Web::HTML::is_valid_floating_point_number(Utf16String const&).
# https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#valid-floating-point-number
pub func html_is_valid_floating_point_number_utf16_string(string: ak.Utf16String) -> bool:
# FIXME: Implement a UTF-16 GenericLexer.
if not ak.utf16_string_base_has_ascii_storage(&string.utf16_string_base):
return false
return html_is_valid_floating_point_number_string_view(ak.utf16_string_base_ascii_view(&string.utf16_string_base))
# mark: Reflection ==============================================================================
## Ported from Numbers.cpp:340 Web::HTML::convert_non_negative_integer_to_string.
pub func html_convert_non_negative_integer_to_string(realm: Realm*, value: i32) -> ak.String!:
if value < 0:
try idl_throw(.idl_dom_exception(idl_index_size_error_create(realm, ak.utf16_string_from_str("The attribute is limited to only non-negative numbers"))))
return ak.string_number_i64(value)