@@ -153,8 +153,8 @@ def lex:
153
153
// _re ("^\\ )" ; {rparen : . , string_stack : ($string_stack [0 :- 1 ])})
154
154
// _re ("^\\ [" ; {lsquare : . })
155
155
// _re ("^\\ ]" ; {rsquare : . })
156
- // _re ("^{" ; {lcurly : . })
157
- // _re ("^}" ; {rcurly : . })
156
+ // _re ("^\\ {" ; {lcurly : . })
157
+ // _re ("^\\ }" ; {rcurly : . })
158
158
// _re ("^\\ .\\ ." ; {dotdot : . })
159
159
// _re ("^\\ ." ; {dot : . })
160
160
// _re ("^\\ ?" ; {qmark : . })
@@ -173,7 +173,9 @@ def lex:
173
173
[_lex ];
174
174
175
175
def parse :
176
- def _consume (f ): select (. [0 ] | f ) | . [1 :];
176
+ # TODO: jaq null index
177
+ # TODO: maybe a consume helper that returns first? ala [$rest, $v]?
178
+ def _consume (f ): select (length > 0 and (. [0 ] | f )) | . [1 :];
177
179
def _optional (f ):
178
180
( f
179
181
// [. , null ]
@@ -200,7 +202,9 @@ def parse:
200
202
# filter is used to disable operators, ex in keyval query
201
203
def _op_prec_climb ($p ; filter ):
202
204
def _ops :
203
- if filter then false
205
+ # TODO: jaq: nice way? don't even call _ops for null case?
206
+ if . == null then false
207
+ elif filter then false
204
208
elif .pipe then {prec : 0 , name : "|" , assoc : "right" }
205
209
# TODO: understand why jq has left associativity for "," but right seems to give correct parse tree
206
210
elif .comma then {prec : 1 , name : "," , assoc : "right" }
@@ -956,7 +960,9 @@ def parse:
956
960
957
961
# "abc \(123)"
958
962
def _string_query :
959
- ( . [0 ] as {$string_start }
963
+ # TODO: jaq null index (consume checks length and outoput [value, rest]?)
964
+ ( select (length > 0 )
965
+ | . [0 ] as {$string_start }
960
966
| _consume (.string_start )
961
967
| _repeat (
962
968
( select (length > 0 ) # make sure there is something
@@ -1060,7 +1066,7 @@ def parse:
1060
1066
]
1061
1067
);
1062
1068
1063
- ( . # debug({_p: $type})
1069
+ ( . # debug({_p: $type, dot: . })
1064
1070
| if $type == "query" then
1065
1071
_op_prec_climb (0 ; false )
1066
1072
elif $type == "keyval_query" then
@@ -1267,8 +1273,12 @@ def func_defs_to_env($env):
1267
1273
1268
1274
def eval_ast ($query ; $path ; $env ; undefined_func ):
1269
1275
def _e ($query ; $path ; $env ):
1270
- ( . #| debug({c: ., $query, $path, $env})
1271
- | $query as
1276
+ ( . # debug({c: ., $query, $path, $env})
1277
+ | ( $query
1278
+ # TODO: jaq: destruct null index
1279
+ | if .term == null then .term = {} end
1280
+ | if .term.suffix_list == null then .term.suffix_list = [{}] end
1281
+ ) as
1272
1282
{ term :
1273
1283
{ type : $type
1274
1284
, suffix_list : [{$optional }]
@@ -1417,7 +1427,17 @@ def eval_ast($query; $path; $env; undefined_func):
1417
1427
1418
1428
# .name
1419
1429
def _index :
1420
- _e_index ($query .term.index ; $path ; . ; $query .term.suffix_list [0 ].optional );
1430
+ _e_index (
1431
+ $query .term.index ;
1432
+ $path ;
1433
+ . ;
1434
+ # TODO: jaq null index
1435
+ ( $query .term.suffix_list
1436
+ | if . then . [0 ].optional
1437
+ else false
1438
+ end
1439
+ )
1440
+ );
1421
1441
1422
1442
def _func :
1423
1443
def _fromjson :
@@ -1454,8 +1474,9 @@ def eval_ast($query; $path; $env; undefined_func):
1454
1474
| $query .term.func as {$name , $args }
1455
1475
| func_name ($name ; $args ) as $name
1456
1476
| $query_env [$name ] as $e
1457
- | if $e | has ("value" ) then [[null ], $e .value ]
1458
- elif $e .body then
1477
+ # TODO: jaq null index and null has()
1478
+ | if $e != null and ($e | has ("value" )) then [[null ], $e .value ]
1479
+ elif $e != null and $e .body then
1459
1480
( ($e .args // []) as $func_args
1460
1481
| ($args // []) as $call_args
1461
1482
| ( $func_args
@@ -1519,10 +1540,10 @@ def eval_ast($query; $path; $env; undefined_func):
1519
1540
( a0 as $a0
1520
1541
| [[null ], has ($a0 )]
1521
1542
)
1522
- elif $name == "delpaths/1" then
1523
- ( a0 as $a0
1524
- | [[null ], delpaths ($a0 )]
1525
- )
1543
+ # elif $name == "delpaths/1" then
1544
+ # ( a0 as $a0
1545
+ # | [[null], delpaths($a0)]
1546
+ # )
1526
1547
elif $name == "explode/0" then [[null ], explode ]
1527
1548
elif $name == "implode/0" then [[null ], implode ]
1528
1549
elif $name == "tonumber/0" then [[null ], tonumber ]
@@ -1539,19 +1560,20 @@ def eval_ast($query; $path; $env; undefined_func):
1539
1560
| error ($a0 )
1540
1561
)
1541
1562
elif $name == "halt_error/1" then [[null ], halt_error (a0 )]
1542
- elif $name == "getpath/1" then
1543
- ( a0 as $a0
1544
- | [ $path + $a0
1545
- , getpath ($a0 )
1546
- ]
1547
- )
1548
- elif $name == "setpath/2" then
1549
- ( a0 as $a0
1550
- | a1 as $a1
1551
- | [ []
1552
- , setpath ($a0 ; $a1 )
1553
- ]
1554
- )
1563
+ # TODO: jaq: setpath/getpath missing
1564
+ # elif $name == "getpath/1" then
1565
+ # ( a0 as $a0
1566
+ # | [ $path+$a0
1567
+ # , getpath($a0)
1568
+ # ]
1569
+ # )
1570
+ # elif $name == "setpath/2" then
1571
+ # ( a0 as $a0
1572
+ # | a1 as $a1
1573
+ # | [ []
1574
+ # , setpath($a0; $a1)
1575
+ # ]
1576
+ # )
1555
1577
elif $name == "path/1" then
1556
1578
( _e ($args [0 ]; []; $query_env ) as [$p , $_v ]
1557
1579
# TODO: try/catch error
@@ -1903,7 +1925,7 @@ def eval_ast($query; $path; $env; undefined_func):
1903
1925
# .a.b?? case, just skip extra optional "?"
1904
1926
elif $suffix_list [0 ].optional then _f ($suffix_list [1 :])
1905
1927
else
1906
- ( $suffix_list [1 ].optional as $opt
1928
+ ( ( $suffix_list [1 ] // {}) .optional as $opt # TOOO: jaq null index
1907
1929
| $suffix_list [if $opt then 2 else 1 end :] as $n
1908
1930
| _e_suffix ($suffix_list [0 ]; $path ; $input ; $opt )
1909
1931
| _f ($n )
@@ -2541,10 +2563,12 @@ def eval($expr; $globals; $builtins_env):
2541
2563
# TODO: does not work with jq yet because issue with bind patterns
2542
2564
# $ gojq -cn -L . 'include "jqjq"; {} | {a:1} | eval(".a") += 1'
2543
2565
# {"a":2}
2544
- | if $path | . == [] or . == [null ] then $value
2545
- else getpath ($path )
2546
- end
2547
- );
2566
+ | $value
2567
+ );
2568
+ # | if $path | . == [] or . == [null] then $value
2569
+ # else getpath($path)
2570
+ # end
2571
+ # );
2548
2572
def eval ($expr ):
2549
2573
eval ($expr ; {}; _builtins_env );
2550
2574
@@ -2924,7 +2948,10 @@ def jqjq($args; $env):
2924
2948
)
2925
2949
];
2926
2950
def _f :
2927
- def _to_unslurped : map (tojson ) | join ("," );
2951
+ # TODO: jaq: [] | join("") -> null
2952
+ # TODO: jaq: join works with more than strings
2953
+ def _join ($s ): if . == [] then "" else join ($s ) end ;
2954
+ def _to_unslurped : map (tojson ) | _join ("," );
2928
2955
( _builtins_env as $builtins_env
2929
2956
| _from_jqtest []
2930
2957
| . as $c
0 commit comments