mirror of
https://github.com/jqlang/jq.git
synced 2026-10-12 02:16:58 +00:00
Allow using { $__loc__ } as shorthand for { __loc__: $__loc__ }
n.b.: { $__loc__: "foo" } is still not allowed.
$__loc__ cannot be overwritten, and it is always an object.
Using { $x: "foo" } where $x is not a string is an error, so there is
not much of a point in supporting that.
This commit is contained in:
committed by
Nico Williams
parent
841445eea7
commit
6ca7973d49
Generated
+1043
-1027
File diff suppressed because it is too large
Load Diff
+10
-2
@@ -300,6 +300,11 @@ static block gen_update(block object, block val, int optype) {
|
||||
optype)))));
|
||||
}
|
||||
|
||||
static block gen_loc_object(location *loc, struct locfile *locations) {
|
||||
return gen_const(JV_OBJECT(jv_string("file"), jv_copy(locations->fname),
|
||||
jv_string("line"), jv_number(locfile_get_line(locations, loc->start) + 1)));
|
||||
}
|
||||
|
||||
%}
|
||||
|
||||
%%
|
||||
@@ -780,8 +785,7 @@ BINDING {
|
||||
jv_free($1);
|
||||
} |
|
||||
"$__loc__" {
|
||||
$$ = gen_const(JV_OBJECT(jv_string("file"), jv_copy(locations->fname),
|
||||
jv_string("line"), jv_number(locfile_get_line(locations, @$.start) + 1)));
|
||||
$$ = gen_loc_object(&@$, locations);
|
||||
} |
|
||||
IDENT {
|
||||
const char *s = jv_string_value($1);
|
||||
@@ -981,6 +985,10 @@ IDENT ':' ExpD {
|
||||
$$ = gen_dictpair(gen_const(jv_copy($1)),
|
||||
gen_index(gen_noop(), gen_const($1)));
|
||||
}
|
||||
| "$__loc__" {
|
||||
$$ = gen_dictpair(gen_const(jv_string("__loc__")),
|
||||
gen_loc_object(&@$, locations));
|
||||
}
|
||||
| Keyword {
|
||||
$$ = gen_dictpair(gen_const(jv_copy($1)),
|
||||
gen_index(gen_noop(), gen_const($1)));
|
||||
|
||||
@@ -1792,3 +1792,9 @@ reduce .[] as $then (4 as $else | $else; . as $elif | . + $then * $elif)
|
||||
[ foreach .[] as $try (1 as $catch | $catch - 1; . + $try; .) ]
|
||||
[10,9,8,7]
|
||||
[10,19,27,34]
|
||||
|
||||
|
||||
# { $__loc__ } works
|
||||
{ a, $__loc__, c }
|
||||
{"a":[1,2,3],"b":"foo","c":{"hi":"hey"}}
|
||||
{"a":[1,2,3],"__loc__":{"file":"<top-level>","line":1},"c":{"hi":"hey"}}
|
||||
|
||||
Reference in New Issue
Block a user