33 Commits

Author SHA1 Message Date
John Bartholomew
22fb64fe50 extract precedence info from internal/parser to a new internal/ast package 2026-02-24 18:01:14 +00:00
John Bartholomew
71c8de23c2 chore: regenerate Bazel files, use go.mod to specify Go SDK version
This brings things more into line with modern conventions for
build target names, and means we have fewer sources of truth for
the minimum Go version that's needed.
2026-02-18 23:06:15 +00:00
John Bartholomew
a52ac8dceb inline processing of number text into lexNumber 2026-01-27 18:43:46 +00:00
John Bartholomew
c9f1b48d2d fix underscore handling in numbers to avoid repeated fractions or exponents
See also the corresponding C++ jsonnet commit:
82ebe7de83

There are some cases which are a little strange but lexically valid.

- `1.2.3.4` lexically this tokenises as `1.2` DOT `3.4`, because a dot
  in the fractional or exponent part of a number is simply treated the
  same as any other possible terminating character (any character that
  isn't part of the valid number lexical syntax)
- `1e2.34` lexically is `1e2` DOT `34` (same as the first case)
- `1e2e34` lexically is `1e2` (number) `e34` (identifier)

These behaviours are basically preserved/extrapolated in the case of
digit separators, so for example `1_2.3_4.5_6` is lexically parsed
as `12.34` DOT `56`. And `1e2_3e4` is lexically parsed as
`1e23` (number), `e4` (identifier). These both look very confusing,
but it probably doesn't matter because those token sequences are,
I think, not valid syntactically so they'll just be rejected by
the parser.

Note that in JSON (and jsonnet), leading zeros are not allowed in
numeric literals. This behaviour is explicitly kept with digit
separators, so `0_5` is explicitly rejected. The alternatives are:

- Treat underscore after an initial zero the same as any terminator
  character, so `0_5` lexes as tokens `0` followed by identifier `_5`.
- Allow underscore, thereby breaking the no-leading-zeros rule, so
  `0_5` tokenises as `05`.

Either option seems confusing, hence it seems better to explicitly
reject an underscore after an initial zero.
2026-01-27 18:43:46 +00:00
David Grant
449c302be9 Simpler to not special-case consecutive _s. 2026-01-27 18:43:46 +00:00
David Grant
f664e6d82f Add a test for _123 lexing as identifier. 2026-01-27 18:43:46 +00:00
David Grant
502cac2427 Just use struct literals. 2026-01-27 18:43:46 +00:00
David Grant
1c972d05a2 Add exceptional test cases. Make case table less crazy. 2026-01-27 18:43:46 +00:00
David Grant
30ab4ea56c Fix the test. 2026-01-27 18:43:46 +00:00
David Grant
c700c000f9 More tests. Some fail. 2026-01-27 18:43:46 +00:00
David Grant
ac85b18155 Initial separator lexing. 2026-01-27 18:43:46 +00:00
John Bartholomew
604f4d7cad chore: bring all the number lex tests into one table driven test 2026-01-26 20:10:40 +00:00
Tim Vergenz
e6f64e89f1
feat: add |||- chomped text block syntax (#773)
Resolves google/jsonnet#289

Companion PR to google/jsonnet#1175
2025-01-20 21:45:15 +00:00
itchyny
e544339880
Remove dependency on gen to generate sets (#746) 2024-06-09 19:24:41 +01:00
Thomas Neidhart
c8d95b9a6f
Gracefully handle encountered regular expression when running jsonnetfmt (#724)
* Gracefully handle encountered regular expression when running jsonnetfmt, adding tests.

* Do not validate verbatim strings.

* Also do not validate string blocks.

* Change golden prefix for formatter tests to fmt.golden to be consistant with cpp version.
2024-06-09 15:32:29 +01:00
Jesse
2655afd2bd
memory align structs (#635)
feat: align most structs and add lint rule to enforce struct alignment
2022-10-19 06:19:15 +01:00
Angus Lees
856bd58872 Add 'importbin' statement
Add `importbin` statement.  Similar to `importstr` but the result is
an array of numbers (all integers 0-255).
2022-03-03 22:49:02 +00:00
Dave Cunningham
5c4247b3f4 Equivalent change to https://github.com/google/jsonnet/pull/898 2021-12-21 14:01:05 +00:00
Stanisław Barzowski
b70cbd441a Anonymous Import Locations
We used to treat dummy paths like <stdin>, <std>, <extvar> as
real import locations, which causes obvious problem for importers.

After this change we consistently pass "" (an empty string) as location
for non-imported paths.

We exposed new functions to properly handle all paths.

The first function family, EvaluateFile* which allow evaluating
a Jsonnet program at a specified importable path. It should
be used in most cases.

The second function family, EvaluateAnonymousSnippet* allows evaluating
a snippet without an importable path. It should be used for situations
like the code passed as a commandline argument, stdin, etc.

The old function family, EvaluateSnippet* is now deprecated, but
works the same as before, i.e. the passed filenames are treated
as imported paths.

Changes are required to custom importers to make sure they satisfy
the refined contract.

Fixes #329.
2020-09-08 17:27:47 +02:00
Stanisław Barzowski
f3873a7495 Fix duplicated fodder (#411).
CommaFodder was not cleared between arguments,
so when there was no commaFodder and the argument
was named a previous one was duplicated.

Fixes #411.
2020-05-22 11:31:30 +02:00
Stanisław Barzowski
3a245f70d4 Preparation for linter.
* Extract some test utilities to a separate package.
* Rename some test utilities.
* Internally expose DirectChildren.
* Add LocationRange to some non-expr AST parts,
  such as local binds, parameters and object fields.
* Add end-of-file-reached testcases.
2020-03-15 14:38:40 +01:00
Dave Cunningham
724650d358
Add native golang formatter (#388)
Add native golang formatter
2020-03-10 22:19:49 +00:00
Stanisław Barzowski
9ada769ce4 Make golangci-lint happy
Some of the suggestions are minor bug fixes (missing error handling).
2020-03-05 14:54:27 +01:00
Dave Cunningham
803ad646cb Refactor parseParameter to return ast.Parameter 2020-03-05 09:33:21 +01:00
Dave Cunningham
5476fefb25 Add ParseParameter. Also fell down the rabbithole improving content and consistency of parse errors 2020-03-05 09:33:21 +01:00
Dave Cunningham
ce07c7fb8b Allow positional params after optionals 2020-03-05 09:33:21 +01:00
Dave Cunningham
0e67cc3c68 Refactor to allow interleaving optional and positional params 2020-03-05 09:33:21 +01:00
Dave Cunningham
8b2b1ba839 Avoid float overflow errors at parse time. 2020-02-27 23:06:53 +00:00
Dave Cunningham
0959f85501 break up parseObjectRemainder 2019-11-06 19:46:26 +00:00
Dave Cunningham
407c0f311a Fix #331 missing comma error 2019-10-16 22:15:28 +02:00
Dave Cunningham
dc5c94ac78
Add fodder to AST nodes (#319)
* Preserve fodder during parsing.
2019-09-24 12:11:22 +01:00
Stanisław Barzowski
ab95594eea Add missing cases in specialChildren
Namely:
1) function body and optional params
2) object locals
2019-09-10 15:41:17 +02:00
Stanisław Barzowski
45f3912215 Make parser package internal
There is no reason for external users to directly depend
on parser. It had a few random things exported as well,
namely errors and "children" functions (helpers for AST
traversal).

It was easy to extract the errors package, but I needed to leave
children in parser for now. The errors package was also
made internal, but it's a candidate for making it public
again potentially (if someone wants to display error messages
just like us). For now it's probably too incomplete anyway.

This change has a potential of breaking the existing users
since we technically remove public APIs. These were not needed
or even helpful for actually running Jsonnet code, but perhaps
someone used them anyway.
2019-09-03 17:28:33 +02:00