Skip to content

Commit 9b0c01c

Browse files
authored
Merge pull request #101 from google/fix_conformance_tests
parser: support hex integer notation
2 parents c804f26 + d943190 commit 9b0c01c

File tree

3 files changed

+14
-5
lines changed

3 files changed

+14
-5
lines changed

conformance/BUILD

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,6 @@ cc_binary(
136136
"--skip_test=fields/qualified_identifier_resolution/map_field_select",
137137
"--skip_test=fields/qualified_identifier_resolution/ident_with_longest_prefix_check",
138138
# New conformance tests awaiting synchronization.
139-
"--skip_test=basic/self_eval_nonzeroish/self_eval_int_hex",
140-
"--skip_test=basic/self_eval_nonzeroish/self_eval_int_hex_negative",
141-
"--skip_test=basic/self_eval_nonzeroish/self_eval_uint_hex",
142139
"--skip_test=basic/functions/unbound",
143140
"--skip_test=basic/functions/unbound_is_runtime_error",
144141
"--skip_test=comparisons/eq_literal/not_eq_list_false_vs_types",

parser/parser_test.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,9 @@ std::vector<TestInfo> test_cases = {
138138
{"0u", "0u^#1:uint64#"},
139139
{"23u", "23u^#1:uint64#"},
140140
{"24u", "24u^#1:uint64#"},
141+
{"0xAu", "10u^#1:uint64#"},
142+
{"-0xA", "-10^#1:int64#"},
143+
{"0xA", "10^#1:int64#"},
141144
{"-1", "-1^#1:int64#"},
142145
{"4--4",
143146
"_-_(\n"

parser/visitor.cc

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "absl/strings/numbers.h"
88
#include "absl/strings/str_format.h"
99
#include "absl/strings/str_join.h"
10+
#include "absl/strings/match.h"
1011
#include "common/escaping.h"
1112
#include "common/operators.h"
1213
#include "parser/balancer.h"
@@ -400,9 +401,13 @@ antlrcpp::Any ParserVisitor::visitInt(CelParser::IntContext* ctx) {
400401
if (ctx->sign) {
401402
value = ctx->sign->getText();
402403
}
404+
int base = 10;
405+
if (absl::StartsWith(ctx->tok->getText(), "0x")) {
406+
base = 16;
407+
}
403408
value += ctx->tok->getText();
404409
int64_t int_value;
405-
if (absl::SimpleAtoi(value, &int_value)) {
410+
if (absl::numbers_internal::safe_strto64_base(value, &int_value, base)) {
406411
return sf_->newLiteralInt(ctx, int_value);
407412
} else {
408413
return sf_->reportError(ctx, "invalid int literal");
@@ -415,8 +420,12 @@ antlrcpp::Any ParserVisitor::visitUint(CelParser::UintContext* ctx) {
415420
if (!value.empty()) {
416421
value.resize(value.size() - 1);
417422
}
423+
int base = 10;
424+
if (absl::StartsWith(value, "0x")) {
425+
base = 16;
426+
}
418427
uint64_t uint_value;
419-
if (absl::SimpleAtoi(value, &uint_value)) {
428+
if (absl::numbers_internal::safe_strtou64_base(value, &uint_value, base)) {
420429
return sf_->newLiteralUint(ctx, uint_value);
421430
} else {
422431
return sf_->reportError(ctx, "invalid uint literal");

0 commit comments

Comments
 (0)