Skip to content

Generated Dart code for Flutter web contains integer literals that exceed JavaScript's safe integer limit (2^53) #4880

@limexp

Description

@limexp

Description

The generated Dart code for ANTLR grammars, when targeting web platforms, can produce integer literals that exceed the maximum safe integer limit in JavaScript ($2^{53}-1$). This leads to runtime errors because JavaScript cannot represent these large integers exactly.

This issue specifically affects web builds and is a direct result of the difference between 64-bit integer representation in most native environments and the 53-bit safe integer limit in JavaScript's number type.


Error Message

Error: The integer literal 1295065285207669298 can't be represented exactly in JavaScript.
Try changing the literal to something that can be represented in JavaScript. In JavaScript 1295065285207669248 is the nearest value that can be represented exactly.


Affected Grammar

This issue is consistently reproducible with the objc grammar from the antlr/grammars-v4 repository.


Steps to Reproduce

To reproduce this issue, run the following commands (just generate dart-parser and import it or add checkVersion call to prevent unused import warning):

# Clone the repository with the grammars
git clone https://github.com/antlr/grammars-v4.git

# Create a new Flutter project for web
flutter create --platforms web flutter_web_error
cd flutter_web_error/

# Add the antlr4 package
flutter pub add antlr4

# Generate Dart code from the objc grammar
antlr4 -visitor -o lib/antlr4 -Xexact-output-dir -Dlanguage=Dart ../grammars-v4/objc/ObjectiveCParser.g4 ../grammars-v4/objc/ObjectiveCLexer.g4

# Add ANTLR generated parser import to main.dart to trigger the error on run
printf "import 'package:flutter_web_error/antlr4/ObjectiveCParser.dart';\n$(cat lib/main.dart)" > lib/main.dart

# Run the web application
flutter run

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions