isNumericLiteral function

bool isNumericLiteral(
  1. String token
)

Checks if a token represents a valid numeric literal.

Rejects numbers with leading zeros (except "0" itself or decimals like "0.5").

Implementation

bool isNumericLiteral(String token) {
  if (token.isEmpty) {
    return false;
  }

  // Must not have leading zeros (except for `"0"` itself or decimals like `"0.5"`)
  if (token.length > 1 && token[0] == '0' && token[1] != '.') {
    return false;
  }

  // Check if it's a valid number
  final num? number = num.tryParse(token);
  return number != null && number.isFinite;
}