libexpr/nix2: Make floats require leading digit

Forbid literals like `.3`

Change-Id: Ia05b3729bf9f23d64a2cdb797458aad630d6a639
This commit is contained in:
piegames 2024-08-24 16:50:47 +02:00
parent 909456a8bf
commit c69d2bf34e
3 changed files with 14 additions and 8 deletions

View file

@ -163,18 +163,15 @@ struct identifier : _not_at_any_keyword<
// floats may extend ints, thus these rules are very similar.
struct integer : seq<
sor<
seq<range<'1', '9'>, star<digit>, not_at<one<'.'>>>,
seq<one<'0'>, not_at<one<'.'>, digit>, star<digit>>
>,
plus<digit>,
not_at<one<'.'>>,
not_at<_extend_as_path>
> {};
struct floating : seq<
sor<
seq<range<'1', '9'>, star<digit>, one<'.'>, star<digit>>,
seq<opt<one<'0'>>, one<'.'>, plus<digit>>
>,
plus<digit>,
one<'.'>,
star<digit>,
opt<one<'E', 'e'>, opt<one<'+', '-'>>, plus<digit>>,
not_at<_extend_as_path>
> {};

View file

@ -183,6 +183,10 @@ constexpr std::array<ExperimentalFeatureDetails, numXpFeatures> xpFeatureDetails
Switch the parser and evaluator to a newer, experimental version of the language.
See https://wiki.lix.systems/books/lix-contributors/page/nix-lang-v2
This is the playground for new language improvements.
Notable changes done compared to the stable Nix language:
- Floating point literals must start with a digit, literals like `.3` are forbidden
)",
},
{

View file

@ -62,6 +62,11 @@ namespace nix {
TEST_F(TrivialExpressionTest, pointfloat) {
auto v = eval(".234");
ASSERT_THAT(v, IsFloatEq(0.234));
FeatureSettings mockFeatureSettings;
mockFeatureSettings.set("experimental-features", "nix-lang2");
ASSERT_THROW(eval(".234", true, mockFeatureSettings), Error);
}
TEST_F(TrivialExpressionTest, updateAttrs) {