Skip to content

Commit b498f45

Browse files
authored
Merge pull request #24 from wader/at-format
Implement @Format similar to jaq
2 parents 8f822b4 + 3ac4cb3 commit b498f45

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

jqjq.jq

+15-12
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ def lex:
9797
| _re("^\\s+"; {whitespace: .})
9898
// _re("^#[^\n]*"; {comment: .})
9999
// _re("^\\.[_a-zA-Z][_a-zA-Z0-9]*"; {index: .[1:]})
100-
// _re("^[_a-zA-Z][_a-zA-Z0-9]*"; {ident: .})
101100
// _re("^@[_a-zA-Z][_a-zA-Z0-9]*"; {at_ident: .})
101+
// _re("^[_a-zA-Z][_a-zA-Z0-9]*"; {ident: .})
102102
// _re("^\\$[_a-zA-Z][_a-zA-Z0-9]*"; {binding: .})
103103
# 1.23, .123, 123e2, 1.23e2, 123E2, 1.23e+2, 1.23E-2 or 123
104104
// _re("^(?:[0-9]+\\.[0-9]+|[0-9]+)(?:[eE][-\\+]?[0-9]+)?"; {number: .})
@@ -705,12 +705,15 @@ def parse:
705705
);
706706

707707
# def a: ...;
708-
# def a(f) ...;
709-
# def a(f; $v) ...;
708+
# def a(f): ...;
709+
# def a(f; $v): ...;
710710
def _func_defs:
711711
_repeat(
712712
( _keyword("def")
713-
| _consume(.ident) as [$rest, $ident]
713+
| ( _consume(.at_ident)
714+
// _consume(.ident)
715+
) as [$rest, $tok]
716+
| ($tok.ident // $tok.at_ident) as $name
714717
| $rest
715718
| ( ( _consume(.lparen)[0]
716719
| _repeat(
@@ -736,7 +739,7 @@ def parse:
736739
| $rest
737740
| _consume(.semicolon) as [$rest, $_]
738741
| [ $rest
739-
, { name: $ident.ident
742+
, { name: $name
740743
, args: $args
741744
, body: $body
742745
}
@@ -748,7 +751,7 @@ def parse:
748751
| $rest
749752
| _consume(.semicolon) as [$rest, $_]
750753
| [ $rest
751-
, { name: $ident.ident
754+
, { name: $name
752755
, body: $body
753756
}
754757
]
@@ -1414,11 +1417,11 @@ def eval_ast($query; $path; $env; undefined_func):
14141417
end;
14151418

14161419
def _string:
1417-
_string($query; "_format_text");
1420+
_string($query; "tostring");
14181421

14191422
def _format:
1420-
# @name -> _format_name
1421-
_string($query.term.str; "_format_\($query.term.format[1:])");
1423+
# @name "abc \(.def)" -> "abc \(.def | @name)"
1424+
_string($query.term.str; $query.term.format);
14221425

14231426
# .
14241427
def _identity:
@@ -2500,8 +2503,8 @@ def del(p): delpaths([path(p)]);
25002503
25012504
def paths: path(..) | select(. != []);
25022505
2503-
def _format_text: tostring;
2504-
def _format_json: tojson;
2506+
def @text: tostring;
2507+
def @json: tojson;
25052508
25062509
def _utf8_bytes:
25072510
[ explode[]
@@ -2538,7 +2541,7 @@ def _utf8_bytes:
25382541
end
25392542
];
25402543
2541-
def _format_uri:
2544+
def @uri:
25422545
gsub(\"(?<c>[^A-Za-z0-9-_\\\\.~])\";
25432546
( # A (65) - 10 = 55
25442547
def _hex: . + if . < 10 then 48 else 55 end;

0 commit comments

Comments
 (0)