Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7f251344b7 | ||
|
|
21309bc6d7 | ||
|
|
8295baf284 | ||
|
|
ec84f28630 | ||
|
|
701a47c82e | ||
|
|
616f365e5a | ||
|
|
ceb2684f2f | ||
|
|
b9c340a211 | ||
|
|
3a964e2025 | ||
|
|
3d49fc59dd | ||
|
|
16e8d0b1ab | ||
|
|
5856a2d2c0 | ||
|
|
227f62e013 | ||
|
|
89244cc3ea | ||
|
|
3a43e2ff07 | ||
|
|
81f328f1aa | ||
|
|
f5a7bba986 | ||
|
|
66ef8e2c24 | ||
|
|
1628bbf95f | ||
|
|
812703139c | ||
|
|
54add5ac9a | ||
|
|
f183b57ed5 | ||
|
|
dc4dfdff17 | ||
|
|
f55034d314 | ||
|
|
930899e12d | ||
|
|
579518c78d | ||
|
|
fa45a5121c | ||
|
|
862566a48f | ||
|
|
f9f4d2d1e5 | ||
|
|
b74379532d | ||
|
|
1e5e0c9fe2 | ||
|
|
05899f8b3e | ||
|
|
a2a6f2d896 | ||
|
|
1c350c67d7 | ||
|
|
2348b7245e | ||
|
|
6e27de4f74 | ||
|
|
e5dafaea44 | ||
|
|
fd4f4f2087 | ||
|
|
6c89186437 | ||
|
|
32c4759e6b | ||
|
|
6366cfd7a4 | ||
|
|
9985c01975 | ||
|
|
e6371712ff | ||
|
|
d7e35101c5 | ||
|
|
1defaa7b00 | ||
|
|
815b4c9772 | ||
|
|
aaf305868c | ||
|
|
8cc31f0f49 | ||
|
|
a011ac6392 | ||
|
|
505e23124f | ||
|
|
e7f6d1e6d0 | ||
|
|
a220482de6 | ||
|
|
e16bd17571 | ||
|
|
25d47ca08e | ||
|
|
5108a451ca | ||
|
|
ec7647c4ab | ||
|
|
8aecf82bfe | ||
|
|
10cbb9b580 | ||
|
|
4a316fbb5a | ||
|
|
7811ef1e17 | ||
|
|
b04cd6aada | ||
|
|
1146b8b84a | ||
|
|
292f2d3208 | ||
|
|
3cbefde376 | ||
|
|
0b42457929 | ||
|
|
51a81c96f1 | ||
|
|
6083581fea | ||
|
|
164b877bfa | ||
|
|
b9c2a326ba | ||
|
|
bdc1feb50e | ||
|
|
4ef04e5b5c | ||
|
|
0f5345e515 | ||
|
|
c95b4a8ab4 | ||
|
|
520b429ca0 | ||
|
|
d3343d5113 | ||
|
|
2e96fb2c9b | ||
|
|
24a5e5b1b1 | ||
|
|
74ecb88935 | ||
|
|
33874eb513 | ||
|
|
1de56bc5df | ||
|
|
8a79f1d0a3 | ||
|
|
f4f5d8882d | ||
|
|
4451c08007 | ||
|
|
89c57f7936 | ||
|
|
6af7857ccd | ||
|
|
dff96aebcf | ||
|
|
8af52aaf52 | ||
|
|
d66e434fb4 | ||
|
|
3700b457aa | ||
|
|
11f084f9a7 | ||
|
|
454a9dc042 | ||
|
|
ff458803ce | ||
|
|
110e009996 | ||
|
|
7b6a018dff | ||
|
|
a50e548cc5 | ||
|
|
4b258f7d31 | ||
|
|
d1cb8ee0ad | ||
|
|
c7f063188f | ||
|
|
a6460f4fe2 | ||
|
|
2e257563a6 | ||
|
|
ad0090964b | ||
|
|
cbdaeb4062 | ||
|
|
cf1306f469 | ||
|
|
7675f7434a | ||
|
|
821d6404b6 | ||
|
|
e3cb1f76cd | ||
|
|
ef12efbf8c | ||
|
|
adb9b9b8a7 | ||
|
|
07f94f82a2 | ||
|
|
215b4c0fe1 | ||
|
|
89963e1370 | ||
|
|
5d40f3a7c0 | ||
|
|
5e0e7a7ebd | ||
|
|
dad6e42934 | ||
|
|
7d938487dd | ||
|
|
fcfacb932d | ||
|
|
6906d291c5 | ||
|
|
943090846f | ||
|
|
2dc8621d05 | ||
|
|
b6cc00fa71 | ||
|
|
9e2d9b6419 | ||
|
|
3210b29bba | ||
|
|
ddad9618dc | ||
|
|
24005287f4 | ||
|
|
1dcfc2f547 | ||
|
|
7d6d4066dd | ||
|
|
4c22bda09b | ||
|
|
9d62c6fd0c | ||
|
|
5e0db6dd65 | ||
|
|
551d875be1 | ||
|
|
4f8567476d | ||
|
|
ffa2832e33 | ||
|
|
0f7759f831 | ||
|
|
ccfba00178 | ||
|
|
3e8183fcd5 | ||
|
|
0df387f6b5 | ||
|
|
4104c4fa1c | ||
|
|
33a85679b9 | ||
|
|
a4b9552c82 | ||
|
|
b82c231900 | ||
|
|
3d2ab93b11 | ||
|
|
c86ef36769 | ||
|
|
3b8d08177f | ||
|
|
0164ff0bbc | ||
|
|
a4baac2256 | ||
|
|
1e5e9f3ef8 | ||
|
|
d58d85af9e | ||
|
|
10e8d34fd3 | ||
|
|
ebc090ad15 | ||
|
|
8cef5a37ab | ||
|
|
8afdeee785 | ||
|
|
a83a9e0bc2 | ||
|
|
0d414471bb | ||
|
|
91fb3df495 | ||
|
|
373ef71feb | ||
|
|
ca78a746e6 | ||
|
|
1e13e1e06e | ||
|
|
42ff8a6959 | ||
|
|
2e92c3e568 | ||
|
|
5389fdb651 | ||
|
|
7a295b30e0 | ||
|
|
cca3e92854 | ||
|
|
4d05dc55a3 | ||
|
|
aeb52e29d6 | ||
|
|
97602456e3 | ||
|
|
8b5ff40402 | ||
|
|
d630597e87 | ||
|
|
60d1ecb58a | ||
|
|
d927a2c185 | ||
|
|
6e7cf81d74 | ||
|
|
736f1751ee | ||
|
|
28aab9ca79 | ||
|
|
972c048149 | ||
|
|
902aa39fce | ||
|
|
22f55aa585 | ||
|
|
157c95b988 | ||
|
|
426913f127 | ||
|
|
c4cc62a053 | ||
|
|
862696a778 | ||
|
|
86bc662bd2 | ||
|
|
ae7f8d6ab9 | ||
|
|
7dc34b92af | ||
|
|
cbfc0d6130 | ||
|
|
c308b2881f | ||
|
|
c959d1fca1 | ||
|
|
63511a874f | ||
|
|
90c705f138 | ||
|
|
b67bad82cb | ||
|
|
6981b46338 | ||
|
|
752e00bc66 | ||
|
|
00f018a054 | ||
|
|
56344670f4 | ||
|
|
5df20f4954 | ||
|
|
a9c613e87d | ||
|
|
7538ef34be | ||
|
|
5c62a4b805 | ||
|
|
559863ca68 | ||
|
|
845ad5e969 | ||
|
|
243b1f8e5e | ||
|
|
5bfb9781f7 | ||
|
|
906d2537b9 | ||
|
|
bbb287f54b | ||
|
|
e51e916e31 | ||
|
|
46e3125e26 | ||
|
|
c02fcc8fef | ||
|
|
25d4cbd1b3 | ||
|
|
3234a7681e | ||
|
|
36077da66a | ||
|
|
9e1fd8cf4d | ||
|
|
a55085b662 | ||
|
|
e0f9b6a5cd | ||
|
|
0053aa868c | ||
|
|
fab2048648 | ||
|
|
56ae88d9d5 | ||
|
|
1cdcff1c01 | ||
|
|
8ca07a08c3 | ||
|
|
2775b90b20 | ||
|
|
ae312bd7fe | ||
|
|
9017b83d4e | ||
|
|
02cf1831e9 | ||
|
|
d237de7b30 | ||
|
|
84d3203dd1 | ||
|
|
a3234034b5 | ||
|
|
b748eae035 | ||
|
|
a6656edc7c | ||
|
|
b4a9ea56ae | ||
|
|
fdbc91e3c4 | ||
|
|
bd67bb8273 | ||
|
|
54a0470c42 | ||
|
|
3dd790c408 | ||
|
|
be11b2768f | ||
|
|
2075bec60f | ||
|
|
44c2382402 | ||
|
|
fd041c2064 | ||
|
|
c6495f6632 | ||
|
|
7243989c52 | ||
|
|
46613af64c | ||
|
|
79000644c6 | ||
|
|
b349e7d9a8 | ||
|
|
6c8882caf3 | ||
|
|
30e00820a7 | ||
|
|
0f81e1bcd0 | ||
|
|
cb376b27f8 | ||
|
|
dc5e77ef8e | ||
|
|
72735167f9 | ||
|
|
89791a000b | ||
|
|
3411167c03 | ||
|
|
85f0e30c81 | ||
|
|
1796a716ea | ||
|
|
07d8d5c5ef | ||
|
|
0b29b44cab | ||
|
|
a0a350f468 | ||
|
|
4a57b84db0 | ||
|
|
f7a2af7052 | ||
|
|
456bafa82f | ||
|
|
7a8e3c759e | ||
|
|
b70bea8d34 | ||
|
|
d177944b75 | ||
|
|
9bd55487ea | ||
|
|
23e2e2eab3 | ||
|
|
8d2d5e37e5 | ||
|
|
287e2c96ed | ||
|
|
f8d9478517 | ||
|
|
1ba8c2cfa6 | ||
|
|
d654724fef | ||
|
|
3375089013 | ||
|
|
9f13afa20f | ||
|
|
908c10928b | ||
|
|
c321c3b86b | ||
|
|
f87521183f | ||
|
|
8a561120c4 | ||
|
|
20ca21cb0d | ||
|
|
2d1a75f550 | ||
|
|
8cddb7c681 | ||
|
|
4249bbf0d5 | ||
|
|
8a57c27e92 | ||
|
|
8706b6b16a | ||
|
|
32c08d48f3 | ||
|
|
fc52e78ed4 | ||
|
|
eb12f0358e | ||
|
|
a696c6b551 | ||
|
|
0d437e25de | ||
|
|
6744664ff4 | ||
|
|
ef5bd5a6de | ||
|
|
2159f9f5e0 | ||
|
|
2518b3a34f | ||
|
|
c3d6d5fd37 | ||
|
|
a25950a58c | ||
|
|
f8fed0f0bc | ||
|
|
89545ea9d0 | ||
|
|
a2f99d43c3 | ||
|
|
a7878a8d1a | ||
|
|
c73ad74be0 | ||
|
|
53993a9942 | ||
|
|
e1b20b48f0 | ||
|
|
14bc1f6526 | ||
|
|
2e2538ccb8 | ||
|
|
ae27178352 | ||
|
|
0f89270d08 | ||
|
|
10f5a3448d | ||
|
|
50b85c2911 | ||
|
|
38b939688a | ||
|
|
01fc8168e9 | ||
|
|
3362fb3406 | ||
|
|
8c3d503d54 | ||
|
|
2bb9fc5dda | ||
|
|
ea3e947b50 | ||
|
|
538e012e85 | ||
|
|
918ada06dc | ||
|
|
cb2737a433 | ||
|
|
c38e5b1b2c | ||
|
|
089041b090 | ||
|
|
cadf8c3cb0 | ||
|
|
6b6e3f4262 | ||
|
|
6e88d419ba | ||
|
|
ab4bf05300 | ||
|
|
ecac7bd3a1 | ||
|
|
1d620c0de3 | ||
|
|
f68815d5ad | ||
|
|
666aeda82c | ||
|
|
5be80b81eb | ||
|
|
10477fc2d4 | ||
|
|
d8672e1b2a | ||
|
|
a68958e5dc | ||
|
|
824f7df404 | ||
|
|
4bcff40828 | ||
|
|
80a9937e48 | ||
|
|
b963ebaf1d | ||
|
|
eb45372414 | ||
|
|
539dccae90 | ||
|
|
6458910ba3 | ||
|
|
ef9b591215 | ||
|
|
f845808813 | ||
|
|
25719b5596 | ||
|
|
8780bc0b8e | ||
|
|
821cd31e67 | ||
|
|
5a863bf010 | ||
|
|
d0ca11d665 | ||
|
|
7d3a44a1e2 | ||
|
|
7fce34292e | ||
|
|
9113282c27 | ||
|
|
78a8419428 | ||
|
|
ff9a61ead2 | ||
|
|
3a647d3e47 | ||
|
|
1f17ef3623 | ||
|
|
a6030ab374 | ||
|
|
9f4d7e2fb8 | ||
|
|
ba1acbe961 | ||
|
|
9deee38dc8 | ||
|
|
436941d48b | ||
|
|
e1c5a2f575 | ||
|
|
a5e9086676 | ||
|
|
1204e328df | ||
|
|
3e1baf5916 | ||
|
|
beb0d081bb | ||
|
|
4b7c701fdd | ||
|
|
233e32208c | ||
|
|
63e31c2a35 | ||
|
|
f9349becab | ||
|
|
ff5cf0abc7 | ||
|
|
8ff935c01a | ||
|
|
5d9d1b1020 | ||
|
|
8725d9fa3e | ||
|
|
9ae2d66d21 | ||
|
|
0c762925b2 | ||
|
|
ad52026550 | ||
|
|
cf7895876b | ||
|
|
f902d5fae6 | ||
|
|
98e3ebc378 | ||
|
|
dde43f796e | ||
|
|
7ee3b724d1 | ||
|
|
e151a300d0 | ||
|
|
f58f771660 | ||
|
|
ea63d5d3c2 | ||
|
|
ea0ffe24f8 | ||
|
|
061a604a2a | ||
|
|
f741074267 | ||
|
|
de5952d67e | ||
|
|
a03f4802ef | ||
|
|
778ba9f7ab | ||
|
|
676e0ce38e | ||
|
|
088a42fc53 | ||
|
|
63dbac28ad | ||
|
|
e9a1de47b4 | ||
|
|
d14fca0cc6 | ||
|
|
bb83813f51 | ||
|
|
8225d4e0c5 | ||
|
|
d45195ab5c | ||
|
|
73bd347fcf | ||
|
|
c9dbe090c5 | ||
|
|
61cc0d4fb1 | ||
|
|
15c4a7f95f | ||
|
|
20e629284f | ||
|
|
e73951f3d1 | ||
|
|
fdf843c08b | ||
|
|
cf145ec65e | ||
|
|
4889844d0c | ||
|
|
dc7857c585 | ||
|
|
d65dbf89c9 | ||
|
|
ed2b3e26fb | ||
|
|
46436bd870 | ||
|
|
1dbe9317bc | ||
|
|
ef4f3a54fe | ||
|
|
9af03c2562 | ||
|
|
c10e5f4e73 | ||
|
|
01f676041d | ||
|
|
6856ab8a69 | ||
|
|
8212750fe9 | ||
|
|
35b6b949e3 | ||
|
|
d1ea3ab89d | ||
|
|
ae625d0de7 | ||
|
|
5fb82d1018 | ||
|
|
f9d1b94496 | ||
|
|
854ff5643a | ||
|
|
072ebb911b | ||
|
|
d46f5d29fa | ||
|
|
69aa0003af | ||
|
|
41a093c1f2 | ||
|
|
022a418894 | ||
|
|
d056668f4f | ||
|
|
917334d18a | ||
|
|
7a83f1c416 | ||
|
|
6f0a9521e1 | ||
|
|
aee5c3ecb0 | ||
|
|
da0d575649 | ||
|
|
74d2216da2 | ||
|
|
769314a8a2 | ||
|
|
831be04027 | ||
|
|
1a1adf73e8 | ||
|
|
68f41097b9 | ||
|
|
4402c6d806 | ||
|
|
2aa8a43c5b | ||
|
|
219bb9ecb7 | ||
|
|
88fd2ee4f2 | ||
|
|
6c3683dfe9 | ||
|
|
36e495da1e | ||
|
|
15faf9dc3b | ||
|
|
59302fd208 | ||
|
|
24202d1ecf | ||
|
|
336f4e27e6 | ||
|
|
fe29d3d3fa | ||
|
|
3d33412e9a | ||
|
|
fb9196021e | ||
|
|
b23fae9ee3 | ||
|
|
8b41415b07 | ||
|
|
1fa55a3fae | ||
|
|
a45c937f80 | ||
|
|
6ed3adb091 | ||
|
|
106fdf5114 | ||
|
|
99f170adc3 | ||
|
|
f60d83c15f | ||
|
|
36a47bae56 | ||
|
|
dc8528af0f | ||
|
|
e12d0ffd1e | ||
|
|
a71138a43a | ||
|
|
cb976b9a50 | ||
|
|
060eb39bf9 | ||
|
|
ab363e9e50 | ||
|
|
41a8ae7557 | ||
|
|
1956abbedf | ||
|
|
2003a0440f | ||
|
|
a0274cc46e | ||
|
|
3b384740b9 | ||
|
|
4d40a47eab | ||
|
|
18e828f17c | ||
|
|
0966230ba8 | ||
|
|
669f4ab4c3 | ||
|
|
3455714410 | ||
|
|
32ea50d336 | ||
|
|
5ac6a460fa | ||
|
|
f147b7fae5 | ||
|
|
474386a1d9 | ||
|
|
7fd2c2d08b | ||
|
|
5412f3f1c1 | ||
|
|
014b45b4a9 | ||
|
|
30ea390b77 | ||
|
|
00f244385b | ||
|
|
7b671b95d2 | ||
|
|
1535f23455 | ||
|
|
4ba0c8baef | ||
|
|
a7dd3ab793 | ||
|
|
f6f7d4d973 | ||
|
|
f4ae5c883b | ||
|
|
74a14f5de7 | ||
|
|
fee21c959a | ||
|
|
4b48054f41 | ||
|
|
17a319d120 | ||
|
|
3a1dab5396 | ||
|
|
dac081e183 | ||
|
|
489fcdc089 | ||
|
|
3ba7c97b2a | ||
|
|
54635000c5 | ||
|
|
426edff09c | ||
|
|
b37ce20c2a | ||
|
|
cac14a531d | ||
|
|
ae7a042876 | ||
|
|
eb165459aa | ||
|
|
04bc2ef7cf | ||
|
|
09104ce531 | ||
|
|
9fa2173825 | ||
|
|
346da34432 | ||
|
|
fa316ac430 | ||
|
|
77936a594d | ||
|
|
4e07eac356 | ||
|
|
d018e43ddc | ||
|
|
8cee66e609 | ||
|
|
3c4248192d | ||
|
|
255f8ef5be | ||
|
|
20b32db5af | ||
|
|
b634722c8a | ||
|
|
c9e8581f45 | ||
|
|
da129f6664 | ||
|
|
884e6c7d8b | ||
|
|
5aadaa79eb | ||
|
|
cf562961b7 | ||
|
|
63bed9bdf1 | ||
|
|
5989dbdfcf | ||
|
|
db19a11399 | ||
|
|
6c734cb609 | ||
|
|
ef41d1cb57 | ||
|
|
08b4a34a8e | ||
|
|
1fc5656762 | ||
|
|
91cfc34ae4 | ||
|
|
a0b942a7f5 | ||
|
|
6a401c8262 | ||
|
|
d235f32bc9 | ||
|
|
bdbfa7e32c | ||
|
|
78976b3504 | ||
|
|
8d4748c8df | ||
|
|
da723d3e11 | ||
|
|
0feeb7d0d4 | ||
|
|
9e53f36a74 | ||
|
|
7e7c08da47 | ||
|
|
2f6b89ef70 | ||
|
|
4e3024cb61 | ||
|
|
c427bf6a94 | ||
|
|
dae2422fd1 | ||
|
|
f98e3e6c26 | ||
|
|
fe99532f8b | ||
|
|
41a4968c90 | ||
|
|
1cdaabf2dd | ||
|
|
82d8253c19 | ||
|
|
c1717d6e12 | ||
|
|
f0d1c39b5b | ||
|
|
111aea3f0a | ||
|
|
d253414783 | ||
|
|
8b1b503609 | ||
|
|
ff48bd6ec5 | ||
|
|
54b9c9bdb2 | ||
|
|
3dcdc582ea | ||
|
|
7c4171d414 | ||
|
|
1d839ff9df | ||
|
|
3403d07912 |
No files matched your search
+12
-6
@@ -10,17 +10,12 @@
|
||||
tags
|
||||
|
||||
jq
|
||||
!tests/modules/lib/jq/
|
||||
jq.1
|
||||
|
||||
# Something delightfully recursive happens otherwise
|
||||
docs/content/2.download/source/*
|
||||
|
||||
# Autogenerated
|
||||
lexer.c
|
||||
lexer.h
|
||||
parser.c
|
||||
parser.h
|
||||
|
||||
# Autotools junk
|
||||
.libs
|
||||
.deps
|
||||
@@ -39,3 +34,14 @@ Makefile.in
|
||||
version.h
|
||||
.remake-version-h
|
||||
config.cache
|
||||
*.rpm
|
||||
m4/libtool.m4
|
||||
m4/ltoptions.m4
|
||||
m4/ltsugar.m4
|
||||
m4/ltversion.m4
|
||||
m4/lt~obsolete.m4
|
||||
tests/*.trs
|
||||
|
||||
cscope.in.out
|
||||
cscope.out
|
||||
cscope.po.out
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
sudo: false
|
||||
|
||||
language: c
|
||||
|
||||
compiler:
|
||||
- gcc
|
||||
- clang
|
||||
|
||||
matrix:
|
||||
include:
|
||||
- compiler: gcc
|
||||
env: COVERAGE="--disable-valgrind --enable-gcov"
|
||||
|
||||
addons:
|
||||
apt:
|
||||
packages:
|
||||
- libonig-dev
|
||||
- valgrind
|
||||
|
||||
before_install:
|
||||
- rm lexer.h lexer.c parser.h parser.c
|
||||
|
||||
install:
|
||||
- bundle install --gemfile=docs/Gemfile
|
||||
|
||||
- wget http://ftp.debian.org/debian/pool/main/b/bison/bison_3.0.2.dfsg-2_amd64.deb
|
||||
- ar p bison_3.0.2.dfsg-2_amd64.deb data.tar.xz | tar xJ
|
||||
|
||||
- if [ -n "$COVERAGE" ]; then pip install --user cpp-coveralls; fi
|
||||
|
||||
before_script:
|
||||
- autoreconf -i
|
||||
- ./configure
|
||||
YACC="usr/bin/bison -y"
|
||||
$COVERAGE
|
||||
|
||||
script:
|
||||
- make -j4 BISON_PKGDATADIR=$(pwd)/usr/share/bison
|
||||
- make check -j4
|
||||
|
||||
after_script:
|
||||
- |
|
||||
if [ -n "$COVERAGE" ]; then
|
||||
rm -rf .libs usr # don't care about coverage for libjq and bison
|
||||
coveralls -e lexer.c -e parser.c -e jv_dtoa.c --gcov-options '\-lp'
|
||||
fi
|
||||
|
||||
after_failure:
|
||||
- cat test-suite.log
|
||||
- cat tests/*.log
|
||||
|
||||
notifications:
|
||||
email: false
|
||||
@@ -1,6 +1,73 @@
|
||||
Created By:
|
||||
Stephen Dolan <mu@netsoc.tcd.ie>
|
||||
|
||||
Contributions by
|
||||
Anthony Shortland <anthony.shortland@me.com> - rpmbuild target
|
||||
Lee Thompson <stagr.lee@gmail.com> - autoconf stuff
|
||||
Maintained by:
|
||||
Nicolas Williams <nico@cryptonector.com>
|
||||
William Langford <wlangfor@gmail.com>
|
||||
David Tolnay <dtolnay@gmail.com>
|
||||
|
||||
Contributions by:
|
||||
Andrew Rodland <andrew@cleverdomain.org> - bug fixes
|
||||
Ankur <ankz.kothari@gmail.com> - bug fixes
|
||||
Anthony Shortland <anthony.shortland@me.com> - rpmbuild target
|
||||
Brendan Macmillan <melbourne.research@gmail.com> - bug fixes, load library from ~/.jq
|
||||
David R. MacIver <david@drmaciver.com> - bug fixes
|
||||
David Tolnay <dtolnay@gmail.com> - destructuring, build improvements
|
||||
Filippo Giunchedi <fgiunchedi@gmail.com> - bug fixes
|
||||
Filippo Valsorda <filippo.valsorda@gmail.com> - recursive object merge (`*`)
|
||||
Jack Pearkes <jackpearkes@gmail.com> - update tutorial
|
||||
Jingwen Owen Ou <jingweno@gmail.com> - jqplay.org and link to it
|
||||
Joe Littlejohn <joe.littlejohn@nokia.com> - bug fixes
|
||||
Juan Guerrero <juan.guerrero.lozano@gmail.com> - bug fixes
|
||||
Kenny Shen <kenny.shen@zalora.com> - doc fixes
|
||||
Lee Thompson <stagr.lee@gmail.com> - autoconf stuff
|
||||
Markus Lanthaler <mark_lanthaler@gmx.net> - doc fixes
|
||||
Michael Daines <michael@mdaines.com> - add sqrt; doc fixes
|
||||
Mike McCabe <mccabe@archive.org> - math (libm) functions
|
||||
Nicolas Pouillard <nicolas.pouillard@gmail.com> - add tests
|
||||
Nicolas Williams <nico@cryptonector.com> - library-fication, autoconf stuff, exception handling, various
|
||||
Peter van Dijk <peter@7bits.nl> - doc fixes
|
||||
Philipp Hagemeister <phihag@phihag.de> - doc fixes
|
||||
polyester <paul@cleanclothes.org> - automake version update
|
||||
Rémy Léone <remy.leone@gmail.com> - add .travis.yml
|
||||
Ryoichi KATO <ryo1kato@gmail.com> - doc fixes
|
||||
Santiago Lapresta <santiago.lapresta@gmail.com> - join, arrays, all, any, other filters
|
||||
pkoppstein <pkoppstein@gmail.com> - various builtins, improvements
|
||||
13ren <melbourne.research@gmail.com>
|
||||
Aaron Peschel <apeschel@zendesk.com>
|
||||
Adam Lindberg <hello@alind.io>
|
||||
Alex Chamberlain <alex@alexchamberlain.co.uk>
|
||||
Andrew O'Brien <obrien.andrew@gmail.com> - docs build
|
||||
Assaf Gordon <assafgordon@gmail.com> - error handling
|
||||
cdnbacon <mike@busbud.com>
|
||||
Charles Merriam <charles.merriam@gmail.com>
|
||||
Colin von Heuring <colin@janrain.com>
|
||||
Damian Gryski <damian@gryski.com>
|
||||
David Haguenauer <ml@kurokatta.org>
|
||||
Doug Luce <doug@github.con.com> - build
|
||||
Eiichi Sato <sato.eiichi@gmail.com>
|
||||
Hanfei Shen <qqshfox@gmail.com>
|
||||
Ian Miell <ian.miell@gmail.com>
|
||||
i <isomorphisms@sdf.org>
|
||||
James Andariese <james.andariese@locationlabs.com> - Dockerfile
|
||||
jkleint <jkleint@gmail.com>
|
||||
Joel Purra <code+github@joelpurra.com>
|
||||
Kim De Mey <kim.demey@gmail.com> - build
|
||||
Kim Toms <kim.toms@bplglobal.net>
|
||||
LCD 47 <lcd047@gmail.com>
|
||||
Lee Thompson <thompson@dtosolutions.com> - rpm
|
||||
Marc Abramowitz <marc@marc-abramowitz.com>
|
||||
Marc Bruggmann <marcbr@spotify.com>
|
||||
Maxime Biais <maxime.biais@gmail.com> - build
|
||||
Mike Fletcher <john.m.fletcher@bankofamerica.com>
|
||||
Sebastian Freundt <freundt@ga-group.nl> - build
|
||||
Shay Elkin <shay@everything.me>
|
||||
Simon Elsbrock <simon@iodev.org> - Debian
|
||||
Stefan Seemayer <stefan@seemayer.de>
|
||||
Stephen Roantree <stroantree@gmail.com>
|
||||
Stephen Shaw <stesh@netsoc.tcd.ie>
|
||||
Steven Maude <StevenMaude@users.noreply.github.com>
|
||||
Steven Penny <svnpenn@gmail.com> - Windows bug fixes
|
||||
tal@whatexit.org <tal@whatexit.org>
|
||||
Travis Gockel <travis@gockelhut.com>
|
||||
Zhiming Wang <zmwangx@gmail.com>
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
FROM debian:8
|
||||
|
||||
COPY . /app
|
||||
|
||||
# get dependencies, build, and remove anything we don't need for running jq.
|
||||
# valgrind seems to have trouble with pthreads TLS so it's off.
|
||||
|
||||
RUN apt-get update && \
|
||||
apt-get install -y \
|
||||
build-essential \
|
||||
autoconf \
|
||||
libtool \
|
||||
git \
|
||||
bison \
|
||||
flex \
|
||||
ruby \
|
||||
wget \
|
||||
ruby-dev && \
|
||||
wget http://www.geocities.jp/kosako3/oniguruma/archive/onig-5.9.6.tar.gz && \
|
||||
sha512sum onig-5.9.6.tar.gz | grep 4a181ea6f0e1a018bbaf6c87e666dfffd1ef4b8f5dcead07fa0b6564a76174e7e01854173924668433ae74c455dbced6a0e1b43e9066f0499b4a57e855e1a2b2 && \
|
||||
tar zxvf onig-5.9.6.tar.gz && \
|
||||
(cd onig-5.9.6 && \
|
||||
./configure --prefix=/usr/local && \
|
||||
make && \
|
||||
make install ) && \
|
||||
gem install bundler && \
|
||||
(cd /app/docs && bundle install) && \
|
||||
(cd /app && \
|
||||
autoreconf -i && \
|
||||
./configure --disable-valgrind --enable-all-static --prefix=/usr/local && \
|
||||
make -j8 && \
|
||||
make check && \
|
||||
make install && \
|
||||
make distclean ) && \
|
||||
(cd onig-5.9.6 && \
|
||||
make uninstall ) && \
|
||||
apt-get purge -y \
|
||||
build-essential \
|
||||
autoconf \
|
||||
libtool \
|
||||
bison \
|
||||
git \
|
||||
flex \
|
||||
ruby \
|
||||
ruby-dev && \
|
||||
apt-get autoremove -y && \
|
||||
rm -rf onig-5.9.6 && \
|
||||
rm -rf /var/lib/apt/lists/* /var/lib/gems
|
||||
|
||||
ENTRYPOINT ["/usr/local/bin/jq"]
|
||||
+70
-18
@@ -1,13 +1,13 @@
|
||||
|
||||
### C source files to be built and distributed.
|
||||
|
||||
LIBJQ_INCS = jq_parser.h builtin.h bytecode.h compile.h exec_stack.h \
|
||||
jv_alloc.h jv_dtoa.h jv_parse.h jv_unicode.h locfile.h \
|
||||
opcode_list.h parser.y jv_utf8_tables.h lexer.l
|
||||
libm.h jv_alloc.h jv_dtoa.h jv_unicode.h locfile.h \
|
||||
opcode_list.h parser.y jv_utf8_tables.h lexer.l util.h linker.h
|
||||
|
||||
LIBJQ_SRC = locfile.c bytecode.c compile.c execute.c builtin.c jv.c \
|
||||
jv_parse.c jv_print.c jv_dtoa.c jv_unicode.c jv_aux.c jv_file.c \
|
||||
jv_alloc.c jq_test.c ${LIBJQ_INCS}
|
||||
|
||||
jv_alloc.c jq_test.c util.c linker.c ${LIBJQ_INCS}
|
||||
|
||||
### C build options
|
||||
|
||||
@@ -20,10 +20,18 @@ ACLOCAL_AMFLAGS = -I config/m4
|
||||
|
||||
# While there is some autoconf macro support for lex/flex, it doesn't support
|
||||
# header file creation so we'll use good old make
|
||||
if MAINTAINER_MODE
|
||||
BUILT_SOURCES = lexer.h lexer.c parser.h parser.c version.h
|
||||
lexer.c: lexer.l
|
||||
$(AM_V_LEX) flex -o lexer.c --header-file=lexer.h $<
|
||||
lexer.h: lexer.c
|
||||
else
|
||||
BUILT_SOURCES =
|
||||
.y.c:
|
||||
$(AM_V_YACC) echo "NOT building parser.c!"
|
||||
.l.c:
|
||||
$(AM_V_LEX) echo "NOT building lexer.c!"
|
||||
endif
|
||||
|
||||
# Tell YACC (bison) autoconf macros that you want a header file created.
|
||||
# If the --warnings=all fails, you probably have an old version of bison
|
||||
@@ -39,6 +47,28 @@ libjq_la_LDFLAGS = -export-symbols-regex '^j[qv]_' -version-info 1:4:0
|
||||
|
||||
include_HEADERS = jv.h jq.h
|
||||
|
||||
### Running tests under Valgrind
|
||||
|
||||
if ENABLE_VALGRIND
|
||||
NO_VALGRIND =
|
||||
else
|
||||
NO_VALGRIND = 1
|
||||
endif
|
||||
|
||||
### Code coverage with gcov
|
||||
|
||||
if ENABLE_GCOV
|
||||
AM_CFLAGS += --coverage --no-inline
|
||||
endif
|
||||
|
||||
### Error injection for testing
|
||||
|
||||
if ENABLE_ERROR_INJECTION
|
||||
lib_LTLIBRARIES += libinject_errors.la
|
||||
libinject_errors_la_SOURCES = inject_errors.c
|
||||
libinject_errors_la_LIBADD = -ldl
|
||||
libinject_errors_la_LDFLAGS = -module
|
||||
endif
|
||||
|
||||
### Building the jq binary
|
||||
|
||||
@@ -50,16 +80,21 @@ generate_ver = ver="`{ $(srcdir)/scripts/version || echo '$(VERSION)' ; } | sed
|
||||
@ $(generate_ver); test "x`cat version.h 2>/dev/null`" = "x$$ver" || touch .remake-version-h
|
||||
version.h: .remake-version-h
|
||||
$(AM_V_GEN) $(generate_ver); echo "$$ver" > $@
|
||||
main.c: version.h
|
||||
|
||||
bin_PROGRAMS = jq
|
||||
jq_SOURCES = main.c
|
||||
jq_SOURCES = main.c version.h
|
||||
jq_LDFLAGS = -static-libtool-libs
|
||||
jq_LDADD = libjq.la -lm
|
||||
|
||||
if ENABLE_ALL_STATIC
|
||||
jq_LDFLAGS += -all-static
|
||||
endif
|
||||
|
||||
### Tests (make check)
|
||||
|
||||
TESTS = tests/all.test
|
||||
TEST_LOG_COMPILER = ${srcdir}/tests/run
|
||||
TESTS = tests/mantest tests/jqtest tests/onigtest tests/shtest
|
||||
TESTS_ENVIRONMENT = NO_VALGRIND=$(NO_VALGRIND)
|
||||
|
||||
|
||||
### Building the manpage
|
||||
@@ -74,19 +109,22 @@ man_MANS = jq.1
|
||||
if ENABLE_DOCS
|
||||
jq.1: $(srcdir)/docs/content/3.manual/manual.yml
|
||||
$(AM_V_GEN) ( cd ${abs_srcdir}/docs; '$(BUNDLER)' exec rake manpage ) > $@ || { rm -f $@; false; }
|
||||
jq.1.prebuilt: jq.1
|
||||
$(AM_V_GEN) cp $^ $@ || { rm -f $@; false; }
|
||||
jq.1.default: $(srcdir)/docs/default_manpage.md
|
||||
$(AM_V_GEN) ( cd ${abs_srcdir}/docs; '$(BUNDLER)' exec rake manpage_default ) > $@ || { rm -f $@; false; }
|
||||
real_docs: jq.1
|
||||
if cmp jq.1 $(srcdir)/jq.1.default > /dev/null; then\
|
||||
real_docs: jq.1 jq.1.prebuilt
|
||||
@if cmp jq.1 $(srcdir)/jq.1.default > /dev/null; then\
|
||||
rm -f jq.1; $(MAKE) $(AM_MAKEFLAGS) jq.1;\
|
||||
fi
|
||||
else
|
||||
jq.1: $(srcdir)/jq.1.default
|
||||
jq.1: $(srcdir)/jq.1.prebuilt
|
||||
$(AM_V_GEN) cp $^ $@
|
||||
real_docs:
|
||||
@echo "Ruby dependencies not found, cannot build manpage." > /dev/stderr
|
||||
@echo "Follow the instructions in docs/README.md to install them" > /dev/stderr
|
||||
@echo "and then rerun ./configure" > /dev/stderr
|
||||
@echo "A pre-built, possibly out-of-date manpage used." > /dev/stderr
|
||||
false
|
||||
endif
|
||||
|
||||
@@ -110,9 +148,19 @@ DOC_FILES = docs/content docs/public docs/templates docs/site.yml \
|
||||
# setup is only used by distribution developers, not package developers.
|
||||
# Still, as a matter of allowing patching, its not a bad idea to distribute
|
||||
# the developer setup script in the tarball.
|
||||
EXTRA_DIST = $(man_MANS) $(TESTS) $(TEST_LOG_COMPILER) \
|
||||
gen_utf8_tables.py jq.spec $(DOC_FILES) scripts/version \
|
||||
parser.h parser.c lexer.h lexer.c libjq.map
|
||||
EXTRA_DIST = $(man_MANS) $(TESTS) $(TEST_LOG_COMPILER) jq.spec \
|
||||
$(DOC_FILES) scripts/version parser.h parser.c lexer.h \
|
||||
lexer.c tests/onig.supp tests/torture/input0.json \
|
||||
tests/modules/.jq tests/modules/a.jq tests/modules/b/b.jq \
|
||||
tests/modules/c/c.jq tests/modules/c/d.jq \
|
||||
tests/modules/lib/jq/e/e.jq tests/modules/lib/jq/f.jq \
|
||||
tests/modules/data.json \
|
||||
tests/modules/syntaxerror/syntaxerror.jq \
|
||||
tests/modules/test_bind_order.jq \
|
||||
tests/modules/test_bind_order0.jq \
|
||||
tests/modules/test_bind_order1.jq \
|
||||
tests/modules/test_bind_order2.jq
|
||||
|
||||
|
||||
# README.md is expected in Github projects, good stuff in it, so we'll
|
||||
# distribute it and install it with the package in the doc directory.
|
||||
@@ -120,11 +168,11 @@ docdir = ${datadir}/doc/${PACKAGE}
|
||||
dist_doc_DATA = README.md COPYING AUTHORS README
|
||||
|
||||
RELEASE ?= 1
|
||||
rpm: jq
|
||||
rpm: dist jq.spec
|
||||
@echo "Packaging jq as an RPM ..."
|
||||
mkdir -p rpm/SOURCES rpm/BUILD rpm/BUILDROOT rpm/RPMS rpm/SPECS
|
||||
cp jq-$$(cat VERSION).tar.gz rpm/SOURCES/
|
||||
rpmbuild -tb --define "_topdir ${PWD}/rpm" --define "_prefix /usr" --define "myver $$(cat VERSION)" --define "myrel ${RELEASE}" rpm/SOURCES/jq-$$(cat VERSION).tar.gz
|
||||
cp jq-$(VERSION).tar.gz rpm/SOURCES/
|
||||
rpmbuild -tb --define "_topdir ${PWD}/rpm" --define "_prefix /usr" --define "myver $(VERSION)" --define "myrel ${RELEASE}" rpm/SOURCES/jq-$(VERSION).tar.gz
|
||||
find rpm/RPMS/ -name "*.rpm" -exec mv {} ./ \;
|
||||
rm -rf rpm
|
||||
|
||||
@@ -134,11 +182,15 @@ dist-clean-local:
|
||||
# Not sure why this doesn't get cleaned up automatically, guess
|
||||
# automake used to man pages which are hand coded?
|
||||
# 'make clean' doesn't delete the manpage if it can't be rebuilt
|
||||
.PHONY: clean-local-docs
|
||||
clean-local-docs:
|
||||
if ENABLE_DOCS
|
||||
rm -f jq.1
|
||||
endif
|
||||
|
||||
clean-local: clean-local-docs
|
||||
clean-local-gcov:
|
||||
rm -f *.gcno *.gcda *.gcov
|
||||
|
||||
clean-local: clean-local-docs clean-local-gcov
|
||||
rm -f version.h .remake-version-h
|
||||
|
||||
.PHONY: clean-local-docs clean-local-gcov
|
||||
@@ -0,0 +1,88 @@
|
||||
|
||||
Release history
|
||||
|
||||
* jq version 1.0 was released on Sun Oct 21 2012
|
||||
* jq version 1.1 was released on Sun Oct 21 2012
|
||||
* jq version 1.2 was released on Thu Dec 20 2012
|
||||
* jq version 1.3 was released on Sun May 19 2013
|
||||
* jq version 1.4 was released on Mon Jun 9 2014
|
||||
|
||||
New features in 1.5 since 1.4:
|
||||
|
||||
- regular expressions (with Oniguruma)
|
||||
|
||||
- a library/module system
|
||||
|
||||
- many new builtins
|
||||
|
||||
- datetime builtins
|
||||
- math builtins
|
||||
- regexp-related builtins
|
||||
- stream-related builtins (e.g., all/1, any/1)
|
||||
- minimal I/O builtins (`inputs`, `debug`)
|
||||
|
||||
- new syntactic features, including:
|
||||
|
||||
- destructuring (`. as [$first, $second] | ...`)
|
||||
- try/catch, generalized `?` operator, and label/break
|
||||
- `foreach`
|
||||
- multiple definitions of a function with different numbers of
|
||||
arguments
|
||||
|
||||
- command-line arguments
|
||||
|
||||
- --join-lines / -j for raw output
|
||||
- --argjson and --slurpfile
|
||||
- --tab and --indent
|
||||
- --stream (streaming JSON parser)
|
||||
- --seq (RFC7464 JSON text sequence)
|
||||
- --run-tests improvements
|
||||
|
||||
- optimizations:
|
||||
|
||||
- tail-call optimization
|
||||
- reduce and foreach no longer leak a reference to .
|
||||
|
||||
New features in 1.4 since 1.3:
|
||||
|
||||
- command-line arguments
|
||||
|
||||
- jq --arg-file variable file
|
||||
- jq --unbuffered
|
||||
- jq -e / --exit-status (set exit status based on outputs)
|
||||
- jq -S / --sort-keys (now jq no longer sorts object keys by
|
||||
default
|
||||
|
||||
- syntax
|
||||
|
||||
- .. -> like // in XPath (recursive traversal)
|
||||
- question mark (e.g., .a?) to suppress errors
|
||||
- ."foo" syntax (equivalent to .["foo"])
|
||||
- better error handling for .foo
|
||||
- added % operator (modulo)
|
||||
- allow negation without requiring extra parenthesis
|
||||
- more function arguments (up to six)
|
||||
|
||||
- filters:
|
||||
|
||||
- any, all
|
||||
- iterables, arrays, objects, scalars, nulls, booleans, numbers,
|
||||
strings, values
|
||||
|
||||
- string built-ins:
|
||||
|
||||
- split
|
||||
- join (join an array of strings with a given separator string)
|
||||
- ltrimstr, rtrimstr
|
||||
- startswith, endswith
|
||||
- explode, implode
|
||||
- fromjson, tojson
|
||||
- index, rindex, indices
|
||||
|
||||
- math functions
|
||||
|
||||
- floor, sqrt, cbrt, etetera (depends on what's available from libm)
|
||||
|
||||
- libjq -- a C API interface to jq's JSON representation and for
|
||||
running jq programs from C applications
|
||||
|
||||
@@ -4,21 +4,40 @@ jq
|
||||
jq is a command-line JSON processor.
|
||||
|
||||
If you want to learn to use jq, read the documentation at
|
||||
[http://stedolan.github.io/jq](http://stedolan.github.io/jq). This
|
||||
[http://stedolan.github.io/jq](http://stedolan.github.io/jq). This
|
||||
documentation is generated from the docs/ folder of this repository.
|
||||
You can also try it online at [jqplay.org](http://jqplay.org).
|
||||
|
||||
If you want to hack on jq, feel free, but be warned that its internals
|
||||
are not well-documented at the moment. Bring a hard hat and a
|
||||
shovel. Also, read the wiki: http://github.com/stedolan/jq/wiki
|
||||
shovel. Also, read the wiki: http://github.com/stedolan/jq/wiki
|
||||
|
||||
If you're building directly from the latest git, you'll need flex and
|
||||
bison installed. To build, run:
|
||||
Source tarball and built executable releases can be found on the
|
||||
homepage and on the github release page, https://github.com/stedolan/jq/releases
|
||||
|
||||
autoreconf -i
|
||||
If you're building directly from the latest git, you'll need flex,
|
||||
bison (3.0 or newer), libtool, make, and autoconf installed. To get
|
||||
regexp support you'll also need to install Oniguruma (note that jq's
|
||||
tests require regexp support to pass). To build, run:
|
||||
|
||||
autoreconf -i # if building from git
|
||||
./configure
|
||||
make
|
||||
make -j8
|
||||
make check
|
||||
|
||||
After make finishes, you'll be able to use `./jq`. You can also
|
||||
To build without bison or flex, add `--disable-maintainer-mode` to the
|
||||
./configure invocation:
|
||||
|
||||
./configure --disable-maintainer-mode
|
||||
|
||||
(Developers must not use `--disable-maintainer-mode`, not when making
|
||||
changes to the jq parser and/or lexer.)
|
||||
|
||||
To build a statically linked version of jq, run:
|
||||
|
||||
make LDFLAGS=-all-static
|
||||
|
||||
After make finishes, you'll be able to use `./jq`. You can also
|
||||
install it using:
|
||||
|
||||
sudo make install
|
||||
@@ -26,3 +45,20 @@ install it using:
|
||||
If you're not using the latest git version but instead building a
|
||||
released tarball (available on the website), then you won't need to
|
||||
run `autoreconf` (and shouldn't), and you won't need flex or bison.
|
||||
|
||||
To cross-compile for OS X and Windows, see docs/Rakefile's build task
|
||||
and scripts/crosscompile. You'll need a cross-compilation environment,
|
||||
such as Mingw for cross-compiling for Windows.
|
||||
|
||||
Cross-compilation requires a clean workspace, then:
|
||||
|
||||
# git clean ...
|
||||
autoreconf -i
|
||||
./configure
|
||||
make distclean
|
||||
scripts/crosscompile <name-of-build> <configure-options>
|
||||
|
||||
Use the --host= and --target= ./configure options to select a
|
||||
cross-compilation environment. See also the wiki.
|
||||
|
||||
Send questions to http://stackoverflow.com/questions/tagged/jq or to the #jq channel (http://irc.lc/freenode/%23jq/) on Freenode (https://webchat.freenode.net/).
|
||||
@@ -1,9 +1,10 @@
|
||||
#ifndef BUILTIN_H
|
||||
#define BUILTIN_H
|
||||
|
||||
#include "jq.h"
|
||||
#include "bytecode.h"
|
||||
#include "compile.h"
|
||||
|
||||
int builtins_bind(block*);
|
||||
int builtins_bind(jq_state *, block*);
|
||||
|
||||
#endif
|
||||
+9
-8
@@ -9,6 +9,7 @@
|
||||
#define NONE 0, 1
|
||||
#define CONSTANT OP_HAS_CONSTANT, 2
|
||||
#define VARIABLE (OP_HAS_VARIABLE | OP_HAS_BINDING), 3
|
||||
#define GLOBAL (OP_HAS_CONSTANT | OP_HAS_VARIABLE | OP_HAS_BINDING | OP_IS_CALL_PSEUDO), 4
|
||||
#define BRANCH OP_HAS_BRANCH, 2
|
||||
#define CFUNC (OP_HAS_CFUNC | OP_HAS_BINDING), 3
|
||||
#define UFUNC (OP_HAS_UFUNC | OP_HAS_BINDING | OP_IS_CALL_PSEUDO), 4
|
||||
@@ -36,9 +37,9 @@ const struct opcode_description* opcode_describe(opcode op) {
|
||||
}
|
||||
|
||||
|
||||
static int bytecode_operation_length(uint16_t* codeptr) {
|
||||
int bytecode_operation_length(uint16_t* codeptr) {
|
||||
int length = opcode_describe(*codeptr)->length;
|
||||
if (*codeptr == CALL_JQ) {
|
||||
if (*codeptr == CALL_JQ || *codeptr == TAIL_CALL_JQ) {
|
||||
length += codeptr[1] * 2;
|
||||
}
|
||||
return length;
|
||||
@@ -98,17 +99,17 @@ void dump_operation(struct bytecode* bc, uint16_t* codeptr) {
|
||||
printf("%s", op->name);
|
||||
if (op->length > 1) {
|
||||
uint16_t imm = bc->code[pc++];
|
||||
if (op->op == CALL_JQ) {
|
||||
if (op->op == CALL_JQ || op->op == TAIL_CALL_JQ) {
|
||||
for (int i=0; i<imm+1; i++) {
|
||||
uint16_t level = bc->code[pc++];
|
||||
uint16_t idx = bc->code[pc++];
|
||||
jv name;
|
||||
if (idx & ARG_NEWCLOSURE) {
|
||||
idx &= ~ARG_NEWCLOSURE;
|
||||
name = jv_object_get(jv_copy(getlevel(bc,level)->subfunctions[idx]->debuginfo),
|
||||
name = jv_object_get(jv_copy(getlevel(bc,level)->subfunctions[idx]->debuginfo),
|
||||
jv_string("name"));
|
||||
} else {
|
||||
name = jv_array_get(jv_object_get(jv_copy(getlevel(bc,level)->debuginfo),
|
||||
name = jv_array_get(jv_object_get(jv_copy(getlevel(bc,level)->debuginfo),
|
||||
jv_string("params")), idx);
|
||||
}
|
||||
printf(" %s:%d",
|
||||
@@ -132,8 +133,8 @@ void dump_operation(struct bytecode* bc, uint16_t* codeptr) {
|
||||
} else if (op->flags & OP_HAS_VARIABLE) {
|
||||
uint16_t v = bc->code[pc++];
|
||||
jv name = jv_array_get(jv_object_get(jv_copy(getlevel(bc,imm)->debuginfo), jv_string("locals")), v);
|
||||
printf(" $%s:%d",
|
||||
jv_string_value(name),
|
||||
printf(" $%s:%d",
|
||||
jv_string_value(name),
|
||||
v);
|
||||
jv_free(name);
|
||||
if (imm) {
|
||||
@@ -142,7 +143,7 @@ void dump_operation(struct bytecode* bc, uint16_t* codeptr) {
|
||||
} else {
|
||||
printf(" %d", imm);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void bytecode_free(struct bytecode* bc) {
|
||||
|
||||
+5
-1
@@ -11,7 +11,7 @@ typedef enum {
|
||||
} opcode;
|
||||
|
||||
enum {
|
||||
NUM_OPCODES =
|
||||
NUM_OPCODES =
|
||||
#define OP(name, imm, in, out) +1
|
||||
#include "opcode_list.h"
|
||||
#undef OP
|
||||
@@ -25,6 +25,9 @@ enum {
|
||||
OP_HAS_UFUNC = 64,
|
||||
OP_IS_CALL_PSEUDO = 128,
|
||||
OP_HAS_BINDING = 1024,
|
||||
// NOTE: Not actually part of any op -- a pseudo-op flag for special
|
||||
// handling of `break`.
|
||||
OP_BIND_WILDCARD = 2048,
|
||||
};
|
||||
struct opcode_description {
|
||||
opcode op;
|
||||
@@ -83,6 +86,7 @@ struct bytecode {
|
||||
void dump_disassembly(int, struct bytecode* code);
|
||||
void dump_operation(struct bytecode* bc, uint16_t* op);
|
||||
|
||||
int bytecode_operation_length(uint16_t* codeptr);
|
||||
void bytecode_free(struct bytecode* bc);
|
||||
|
||||
#endif
|
||||
Executable
+102
@@ -0,0 +1,102 @@
|
||||
#!/bin/bash
|
||||
|
||||
# old values
|
||||
OLD_CFLAGS=$CFLAGS
|
||||
OLD_LDFLAGS=$LDFLAGS
|
||||
ORIG_PWD=`pwd`
|
||||
|
||||
# For parallelism in make
|
||||
NJOBS="-j`sysctl -n hw.ncpu || echo 1`"
|
||||
|
||||
|
||||
# Get oniguruma
|
||||
rm -rf $PWD/build/ios onig-5.9.5
|
||||
echo "Downloading oniguruma 5.9.5"
|
||||
curl http://www.geocities.jp/kosako3/oniguruma/archive/onig-5.9.5.tar.gz | tar xz
|
||||
cd onig-5.9.5
|
||||
|
||||
# So, we need to remake the configure scripts so that the arm64 architecture
|
||||
# exists in config.sub. In order to keep autoreconf from failing, create
|
||||
# NEWS and ChangeLog.
|
||||
touch NEWS ChangeLog
|
||||
autoreconf -fi >/dev/null 2>&1
|
||||
|
||||
|
||||
|
||||
|
||||
CC=`xcrun -f clang`
|
||||
cd $ORIG_PWD
|
||||
|
||||
autoreconf -fi
|
||||
for arch in i386 x86_64 armv7 armv7s arm64; do
|
||||
|
||||
# Some of the architectures are a bit different...
|
||||
if [[ "$arch" = "i386" || "$arch" = "x86_64" ]]
|
||||
then
|
||||
SYSROOT=`xcrun -f --sdk iphonesimulator --show-sdk-path`
|
||||
else
|
||||
SYSROOT=`xcrun -f --sdk iphoneos --show-sdk-path`
|
||||
fi
|
||||
if [[ "$arch" = "arm64" ]]
|
||||
then
|
||||
HOST="aarch64-apple-darwin"
|
||||
else
|
||||
HOST="$arch-apple-darwin"
|
||||
fi
|
||||
|
||||
CFLAGS="-arch $arch -miphoneos-version-min=6.0 -isysroot $SYSROOT $OLD_CFLAGS"
|
||||
LDFLAGS="-arch $arch -miphoneos-version-min=6.0 -isysroot $SYSROOT $OLD_LDFLAGS"
|
||||
|
||||
|
||||
|
||||
# Build oniguruma for this architecture
|
||||
cd onig-5.9.5
|
||||
CC=$CC CFLAGS=$CFLAGS LDFLAGS=$LDFLAGS ./configure --disable-shared --enable-static --host=$HOST --prefix=$ORIG_PWD/build/ios/$arch
|
||||
STATUS=$?
|
||||
if [ $STATUS -ne 0 ]
|
||||
then
|
||||
echo "Failed to configure oniguruma for architecture $arch. Check `pwd`/config.log for details."
|
||||
cd $PWD
|
||||
exit $STATUS
|
||||
fi
|
||||
make clean
|
||||
make $NJBOS install
|
||||
STATUS=$?
|
||||
if [ $STATUS -ne 0 ]
|
||||
then
|
||||
echo "Failed to make oniguruma for architecture $arch."
|
||||
cd $PWD
|
||||
exit $STATUS
|
||||
fi
|
||||
|
||||
|
||||
|
||||
# Build jq for this architecture
|
||||
cd $ORIG_PWD
|
||||
CC=$CC CFLAGS=$CFLAGS LDFLAGS=$LDFLAGS ./configure --disable-shared --enable-static -host=$HOST --prefix=$ORIG_PWD/build/ios/$arch --with-oniguruma=$ORIG_PWD/build/ios/$arch
|
||||
STATUS=$?
|
||||
if [ $STATUS -ne 0 ]
|
||||
then
|
||||
echo "Failed to configure jq for architecture $arch"
|
||||
exit $STATUS
|
||||
fi
|
||||
make clean
|
||||
make $NJOBS install
|
||||
STATUS=$?
|
||||
if [ $STATUS -ne 0 ]
|
||||
then
|
||||
echo "Failed to make jq for architecture $arch"
|
||||
exit $STATUS
|
||||
fi
|
||||
done
|
||||
|
||||
|
||||
# lipo together the different architectures into a universal 'fat' file
|
||||
lipo -create -output $ORIG_PWD/build/ios/libonig.a $ORIG_PWD/build/ios/{i386,x86_64,armv7,armv7s,arm64}/lib/libonig.a
|
||||
lipo -create -output $ORIG_PWD/build/ios/libjq.a $ORIG_PWD/build/ios/{i386,x86_64,armv7,armv7s,arm64}/lib/libjq.a
|
||||
|
||||
# copy the products into the destination directory and clean up the single-architecture files.
|
||||
cp $ORIG_PWD/build/ios/i386/include/*.h $ORIG_PWD/build/ios/
|
||||
rm -rf $ORIG_PWD/build/ios/{i386,x86_64,armv7,armv7s,arm64}
|
||||
|
||||
echo "Products are in $ORIG_PWD/build/ios"
|
||||
@@ -1,16 +1,20 @@
|
||||
#ifndef _GNU_SOURCE
|
||||
#define _GNU_SOURCE // for strdup
|
||||
#endif
|
||||
#include <assert.h>
|
||||
#include <math.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include "compile.h"
|
||||
#include "bytecode.h"
|
||||
#include "locfile.h"
|
||||
#include "jv_alloc.h"
|
||||
#include "linker.h"
|
||||
|
||||
/*
|
||||
The intermediate representation for jq filters is as a sequence of
|
||||
struct inst, which form a doubly-linked list via the next and prev
|
||||
pointers.
|
||||
pointers.
|
||||
|
||||
A "block" represents a sequence of "struct inst", which may be
|
||||
empty.
|
||||
@@ -24,7 +28,7 @@ struct inst {
|
||||
struct inst* prev;
|
||||
|
||||
opcode op;
|
||||
|
||||
|
||||
struct {
|
||||
uint16_t intval;
|
||||
struct inst* target;
|
||||
@@ -32,6 +36,7 @@ struct inst {
|
||||
const struct cfunction* cfunc;
|
||||
} imm;
|
||||
|
||||
struct locfile* locfile;
|
||||
location source;
|
||||
|
||||
// Binding
|
||||
@@ -40,13 +45,16 @@ struct inst {
|
||||
// inst->bound_by = NULL - Unbound free variable
|
||||
// inst->bound_by = inst - This instruction binds a variable
|
||||
// inst->bound_by = other - Uses variable bound by other instruction
|
||||
// Unbound instructions (references to other things that may or may not
|
||||
// Unbound instructions (references to other things that may or may not
|
||||
// exist) are created by "gen_foo_unbound", and bindings are created by
|
||||
// block_bind(definition, body), which binds all instructions in
|
||||
// body which are unboudn and refer to "definition" by name.
|
||||
struct inst* bound_by;
|
||||
char* symbol;
|
||||
|
||||
int nformals;
|
||||
int nactuals;
|
||||
|
||||
block subfn; // used by CLOSURE_CREATE (body of function)
|
||||
block arglist; // used by CLOSURE_CREATE (formals) and CALL_JQ (arguments)
|
||||
|
||||
@@ -64,9 +72,12 @@ static inst* inst_new(opcode op) {
|
||||
i->bytecode_pos = -1;
|
||||
i->bound_by = 0;
|
||||
i->symbol = 0;
|
||||
i->nformals = -1;
|
||||
i->nactuals = -1;
|
||||
i->subfn = gen_noop();
|
||||
i->arglist = gen_noop();
|
||||
i->source = UNKNOWN_LOCATION;
|
||||
i->locfile = 0;
|
||||
return i;
|
||||
}
|
||||
|
||||
@@ -74,6 +85,8 @@ static void inst_free(struct inst* i) {
|
||||
jv_mem_free(i->symbol);
|
||||
block_free(i->subfn);
|
||||
block_free(i->arglist);
|
||||
if (i->locfile)
|
||||
locfile_free(i->locfile);
|
||||
if (opcode_describe(i->op)->flags & OP_HAS_CONSTANT) {
|
||||
jv_free(i->imm.constant);
|
||||
}
|
||||
@@ -85,7 +98,7 @@ static block inst_block(inst* i) {
|
||||
return b;
|
||||
}
|
||||
|
||||
static int block_is_single(block b) {
|
||||
int block_is_single(block b) {
|
||||
return b.first && b.first == b.last;
|
||||
}
|
||||
|
||||
@@ -103,11 +116,12 @@ static inst* block_take(block* b) {
|
||||
return i;
|
||||
}
|
||||
|
||||
block gen_location(location loc, block b) {
|
||||
block gen_location(location loc, struct locfile* l, block b) {
|
||||
for (inst* i = b.first; i; i = i->next) {
|
||||
if (i->source.start == UNKNOWN_LOCATION.start &&
|
||||
i->source.end == UNKNOWN_LOCATION.end) {
|
||||
i->source = loc;
|
||||
i->locfile = locfile_retain(l);
|
||||
}
|
||||
}
|
||||
return b;
|
||||
@@ -118,6 +132,10 @@ block gen_noop() {
|
||||
return b;
|
||||
}
|
||||
|
||||
int block_is_noop(block b) {
|
||||
return (b.first == 0 && b.last == 0);
|
||||
}
|
||||
|
||||
block gen_op_simple(opcode op) {
|
||||
assert(opcode_describe(op)->length == 1);
|
||||
return inst_block(inst_new(op));
|
||||
@@ -131,6 +149,35 @@ block gen_const(jv constant) {
|
||||
return inst_block(i);
|
||||
}
|
||||
|
||||
block gen_const_global(jv constant, const char *name) {
|
||||
assert((opcode_describe(STORE_GLOBAL)->flags & (OP_HAS_CONSTANT | OP_HAS_VARIABLE | OP_HAS_BINDING)) ==
|
||||
(OP_HAS_CONSTANT | OP_HAS_VARIABLE | OP_HAS_BINDING));
|
||||
inst* i = inst_new(STORE_GLOBAL);
|
||||
i->imm.constant = constant;
|
||||
i->symbol = strdup(name);
|
||||
return inst_block(i);
|
||||
}
|
||||
|
||||
int block_is_const(block b) {
|
||||
return (block_is_single(b) && b.first->op == LOADK);
|
||||
}
|
||||
|
||||
int block_is_const_inf(block b) {
|
||||
return (block_is_single(b) && b.first->op == LOADK &&
|
||||
jv_get_kind(b.first->imm.constant) == JV_KIND_NUMBER &&
|
||||
isinf(jv_number_value(b.first->imm.constant)));
|
||||
}
|
||||
|
||||
jv_kind block_const_kind(block b) {
|
||||
assert(block_is_const(b));
|
||||
return jv_get_kind(b.first->imm.constant);
|
||||
}
|
||||
|
||||
jv block_const(block b) {
|
||||
assert(block_is_const(b));
|
||||
return jv_copy(b.first->imm.constant);
|
||||
}
|
||||
|
||||
block gen_op_target(opcode op, block target) {
|
||||
assert(opcode_describe(op)->flags & OP_HAS_BRANCH);
|
||||
assert(target.last);
|
||||
@@ -198,47 +245,122 @@ block block_join(block a, block b) {
|
||||
return c;
|
||||
}
|
||||
|
||||
int block_has_only_binders(block binders, int bindflags) {
|
||||
int block_has_only_binders_and_imports(block binders, int bindflags) {
|
||||
bindflags |= OP_HAS_BINDING;
|
||||
for (inst* curr = binders.first; curr; curr = curr->next) {
|
||||
if ((opcode_describe(curr->op)->flags & bindflags) != bindflags) {
|
||||
if ((opcode_describe(curr->op)->flags & bindflags) != bindflags && curr->op != DEPS && curr->op != MODULEMETA) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int block_bind_subblock(block binder, block body, int bindflags) {
|
||||
assert(block_is_single(binder));
|
||||
assert((opcode_describe(binder.first->op)->flags & bindflags) == bindflags);
|
||||
assert(binder.first->symbol);
|
||||
assert(binder.first->bound_by == 0 || binder.first->bound_by == binder.first);
|
||||
static int inst_is_binder(inst *i, int bindflags) {
|
||||
return !((opcode_describe(i->op)->flags & bindflags) != bindflags && i->op != MODULEMETA);
|
||||
}
|
||||
|
||||
binder.first->bound_by = binder.first;
|
||||
int block_has_only_binders(block binders, int bindflags) {
|
||||
bindflags |= OP_HAS_BINDING;
|
||||
bindflags &= ~OP_BIND_WILDCARD;
|
||||
for (inst* curr = binders.first; curr; curr = curr->next) {
|
||||
if ((opcode_describe(curr->op)->flags & bindflags) != bindflags && curr->op != MODULEMETA) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Count a binder's (function) formal params
|
||||
static int block_count_formals(block b) {
|
||||
int args = 0;
|
||||
if (b.first->op == CLOSURE_CREATE_C)
|
||||
return b.first->imm.cfunc->nargs - 1;
|
||||
for (inst* i = b.first->arglist.first; i; i = i->next) {
|
||||
assert(i->op == CLOSURE_PARAM);
|
||||
args++;
|
||||
}
|
||||
return args;
|
||||
}
|
||||
|
||||
// Count a call site's actual params
|
||||
static int block_count_actuals(block b) {
|
||||
int args = 0;
|
||||
for (inst* i = b.first; i; i = i->next) {
|
||||
switch (i->op) {
|
||||
default: assert(0 && "Unknown function type"); break;
|
||||
case CLOSURE_CREATE:
|
||||
case CLOSURE_PARAM:
|
||||
case CLOSURE_CREATE_C:
|
||||
args++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return args;
|
||||
}
|
||||
|
||||
static int block_count_refs(block binder, block body) {
|
||||
int nrefs = 0;
|
||||
for (inst* i = body.first; i; i = i->next) {
|
||||
int flags = opcode_describe(i->op)->flags;
|
||||
if ((flags & bindflags) == bindflags &&
|
||||
i->bound_by == 0 &&
|
||||
!strcmp(i->symbol, binder.first->symbol)) {
|
||||
// bind this instruction
|
||||
i->bound_by = binder.first;
|
||||
if (i != binder.first && i->bound_by == binder.first) {
|
||||
nrefs++;
|
||||
}
|
||||
// binding recurses into closures
|
||||
nrefs += block_bind_subblock(binder, i->subfn, bindflags);
|
||||
// binding recurses into argument list
|
||||
nrefs += block_bind_subblock(binder, i->arglist, bindflags);
|
||||
// counting recurses into closures
|
||||
nrefs += block_count_refs(binder, i->subfn);
|
||||
// counting recurses into argument list
|
||||
nrefs += block_count_refs(binder, i->arglist);
|
||||
}
|
||||
return nrefs;
|
||||
}
|
||||
|
||||
static void block_bind_each(block binder, block body, int bindflags) {
|
||||
static int block_bind_subblock(block binder, block body, int bindflags, int break_distance) {
|
||||
assert(block_is_single(binder));
|
||||
assert((opcode_describe(binder.first->op)->flags & bindflags) == (bindflags & ~OP_BIND_WILDCARD));
|
||||
assert(binder.first->symbol);
|
||||
assert(binder.first->bound_by == 0 || binder.first->bound_by == binder.first);
|
||||
assert(break_distance >= 0);
|
||||
|
||||
binder.first->bound_by = binder.first;
|
||||
if (binder.first->nformals == -1)
|
||||
binder.first->nformals = block_count_formals(binder);
|
||||
int nrefs = 0;
|
||||
for (inst* i = body.first; i; i = i->next) {
|
||||
int flags = opcode_describe(i->op)->flags;
|
||||
if ((flags & bindflags) == (bindflags & ~OP_BIND_WILDCARD) && i->bound_by == 0 &&
|
||||
(!strcmp(i->symbol, binder.first->symbol) ||
|
||||
// Check for break/break2/break3; see parser.y
|
||||
((bindflags & OP_BIND_WILDCARD) && i->symbol[0] == '*' &&
|
||||
break_distance <= 3 && (i->symbol[1] == '1' + break_distance) &&
|
||||
i->symbol[2] == '\0'))) {
|
||||
// bind this instruction
|
||||
if (i->op == CALL_JQ && i->nactuals == -1)
|
||||
i->nactuals = block_count_actuals(i->arglist);
|
||||
if (i->nactuals == -1 || i->nactuals == binder.first->nformals) {
|
||||
i->bound_by = binder.first;
|
||||
nrefs++;
|
||||
}
|
||||
} else if ((flags & bindflags) == (bindflags & ~OP_BIND_WILDCARD) && i->bound_by != 0 &&
|
||||
!strncmp(binder.first->symbol, "*anonlabel", sizeof("*anonlabel") - 1) &&
|
||||
!strncmp(i->symbol, "*anonlabel", sizeof("*anonlabel") - 1)) {
|
||||
// Increment the break distance required for this binder to match
|
||||
// a break whenever we come across a STOREV of *anonlabel...
|
||||
break_distance++;
|
||||
}
|
||||
// binding recurses into closures
|
||||
nrefs += block_bind_subblock(binder, i->subfn, bindflags, break_distance);
|
||||
// binding recurses into argument list
|
||||
nrefs += block_bind_subblock(binder, i->arglist, bindflags, break_distance);
|
||||
}
|
||||
return nrefs;
|
||||
}
|
||||
|
||||
static int block_bind_each(block binder, block body, int bindflags) {
|
||||
assert(block_has_only_binders(binder, bindflags));
|
||||
bindflags |= OP_HAS_BINDING;
|
||||
int nrefs = 0;
|
||||
for (inst* curr = binder.first; curr; curr = curr->next) {
|
||||
block_bind_subblock(inst_block(curr), body, bindflags);
|
||||
nrefs += block_bind_subblock(inst_block(curr), body, bindflags, 0);
|
||||
}
|
||||
return nrefs;
|
||||
}
|
||||
|
||||
block block_bind(block binder, block body, int bindflags) {
|
||||
@@ -246,32 +368,171 @@ block block_bind(block binder, block body, int bindflags) {
|
||||
return block_join(binder, body);
|
||||
}
|
||||
|
||||
block block_bind_library(block binder, block body, int bindflags, const char *libname) {
|
||||
bindflags |= OP_HAS_BINDING;
|
||||
int nrefs = 0;
|
||||
int matchlen = (libname == NULL) ? 0 : strlen(libname);
|
||||
char *matchname = jv_mem_alloc(matchlen+2+1);
|
||||
matchname[0] = '\0';
|
||||
if (libname != NULL && libname[0] != '\0') {
|
||||
strcpy(matchname,libname);
|
||||
strcpy(matchname+matchlen, "::");
|
||||
matchlen += 2;
|
||||
}
|
||||
assert(block_has_only_binders(binder, bindflags));
|
||||
for (inst *curr = binder.first; curr; curr = curr->next) {
|
||||
int bindflags2 = bindflags;
|
||||
char* cname = curr->symbol;
|
||||
char* tname = jv_mem_alloc(strlen(curr->symbol)+matchlen+1);
|
||||
strcpy(tname, matchname);
|
||||
strcpy(tname+matchlen, curr->symbol);
|
||||
|
||||
// Ew
|
||||
if ((opcode_describe(curr->op)->flags & (OP_HAS_VARIABLE | OP_HAS_CONSTANT)))
|
||||
bindflags2 = OP_HAS_VARIABLE | OP_HAS_BINDING;
|
||||
|
||||
// This mutation is ugly, even if we undo it
|
||||
curr->symbol = tname;
|
||||
nrefs += block_bind_subblock(inst_block(curr), body, bindflags2, 0);
|
||||
curr->symbol = cname;
|
||||
free(tname);
|
||||
}
|
||||
free(matchname);
|
||||
return body; // We don't return a join because we don't want those sticking around...
|
||||
}
|
||||
|
||||
// Bind binder to body and throw away any defs in binder not referenced
|
||||
// (directly or indirectly) from body.
|
||||
block block_bind_referenced(block binder, block body, int bindflags) {
|
||||
assert(block_has_only_binders(binder, bindflags));
|
||||
bindflags |= OP_HAS_BINDING;
|
||||
block refd = gen_noop();
|
||||
for (inst* curr; (curr = block_take(&binder));) {
|
||||
block b = inst_block(curr);
|
||||
if (block_bind_subblock(b, body, bindflags)) {
|
||||
refd = BLOCK(refd, b);
|
||||
} else {
|
||||
block_free(b);
|
||||
block unrefd = gen_noop();
|
||||
int nrefs;
|
||||
for (int last_kept = 0, kept = 0; ; ) {
|
||||
for (inst* curr; (curr = block_take(&binder));) {
|
||||
block b = inst_block(curr);
|
||||
nrefs = block_bind_each(b, body, bindflags);
|
||||
// Check if this binder is referenced from any of the ones we
|
||||
// already know are referenced by body.
|
||||
nrefs += block_count_refs(b, refd);
|
||||
nrefs += block_count_refs(b, body);
|
||||
if (nrefs) {
|
||||
refd = BLOCK(refd, b);
|
||||
kept++;
|
||||
} else {
|
||||
unrefd = BLOCK(unrefd, b);
|
||||
}
|
||||
}
|
||||
if (kept == last_kept)
|
||||
break;
|
||||
last_kept = kept;
|
||||
binder = unrefd;
|
||||
unrefd = gen_noop();
|
||||
}
|
||||
block_free(unrefd);
|
||||
return block_join(refd, body);
|
||||
}
|
||||
|
||||
block block_drop_unreferenced(block body) {
|
||||
inst* curr;
|
||||
block refd = gen_noop();
|
||||
block unrefd = gen_noop();
|
||||
int drop;
|
||||
do {
|
||||
drop = 0;
|
||||
while ((curr = block_take(&body)) && curr->op != TOP) {
|
||||
block b = inst_block(curr);
|
||||
if (block_count_refs(b,refd) + block_count_refs(b,body) == 0) {
|
||||
unrefd = BLOCK(unrefd, b);
|
||||
drop++;
|
||||
} else {
|
||||
refd = BLOCK(refd, b);
|
||||
}
|
||||
}
|
||||
if (curr && curr->op == TOP) {
|
||||
body = BLOCK(inst_block(curr),body);
|
||||
}
|
||||
body = BLOCK(refd, body);
|
||||
refd = gen_noop();
|
||||
} while (drop != 0);
|
||||
block_free(unrefd);
|
||||
return body;
|
||||
}
|
||||
|
||||
jv block_take_imports(block* body) {
|
||||
jv imports = jv_array();
|
||||
|
||||
inst* top = NULL;
|
||||
if (body->first && body->first->op == TOP) {
|
||||
top = block_take(body);
|
||||
}
|
||||
while (body->first && (body->first->op == MODULEMETA || body->first->op == DEPS)) {
|
||||
inst* dep = block_take(body);
|
||||
if (dep->op == DEPS) {
|
||||
imports = jv_array_append(imports, jv_copy(dep->imm.constant));
|
||||
}
|
||||
inst_free(dep);
|
||||
}
|
||||
if (top) {
|
||||
*body = block_join(inst_block(top),*body);
|
||||
}
|
||||
return imports;
|
||||
}
|
||||
|
||||
block gen_module(block metadata) {
|
||||
inst* i = inst_new(MODULEMETA);
|
||||
i->imm.constant = block_const(metadata);
|
||||
if (jv_get_kind(i->imm.constant) != JV_KIND_OBJECT)
|
||||
i->imm.constant = jv_object_set(jv_object(), jv_string("metadata"), i->imm.constant);
|
||||
block_free(metadata);
|
||||
return inst_block(i);
|
||||
}
|
||||
|
||||
jv block_module_meta(block b) {
|
||||
if (b.first != NULL && b.first->op == MODULEMETA)
|
||||
return jv_copy(b.first->imm.constant);
|
||||
return jv_null();
|
||||
}
|
||||
|
||||
block gen_import(const char* name, block metadata, const char* as, int is_data) {
|
||||
assert(metadata.first == NULL || block_is_const(metadata));
|
||||
inst* i = inst_new(DEPS);
|
||||
jv meta;
|
||||
if (block_is_const(metadata))
|
||||
meta = block_const(metadata);
|
||||
else
|
||||
meta = jv_object();
|
||||
if (as != NULL)
|
||||
meta = jv_object_set(meta, jv_string("as"), jv_string(as));
|
||||
meta = jv_object_set(meta, jv_string("is_data"), is_data ? jv_true() : jv_false());
|
||||
meta = jv_object_set(meta, jv_string("relpath"), jv_string(name));
|
||||
i->imm.constant = meta;
|
||||
block_free(metadata);
|
||||
return inst_block(i);
|
||||
}
|
||||
|
||||
block gen_function(const char* name, block formals, block body) {
|
||||
block_bind_each(formals, body, OP_IS_CALL_PSEUDO);
|
||||
inst* i = inst_new(CLOSURE_CREATE);
|
||||
for (inst* i = formals.last; i; i = i->prev) {
|
||||
if (i->op == CLOSURE_PARAM_REGULAR) {
|
||||
i->op = CLOSURE_PARAM;
|
||||
body = gen_var_binding(gen_call(i->symbol, gen_noop()), i->symbol, body);
|
||||
}
|
||||
block_bind_subblock(inst_block(i), body, OP_IS_CALL_PSEUDO | OP_HAS_BINDING, 0);
|
||||
}
|
||||
i->subfn = body;
|
||||
i->symbol = strdup(name);
|
||||
i->arglist = formals;
|
||||
block b = inst_block(i);
|
||||
block_bind_subblock(b, b, OP_IS_CALL_PSEUDO | OP_HAS_BINDING);
|
||||
block_bind_subblock(b, b, OP_IS_CALL_PSEUDO | OP_HAS_BINDING, 0);
|
||||
return b;
|
||||
}
|
||||
|
||||
block gen_param_regular(const char* name) {
|
||||
return gen_op_unbound(CLOSURE_PARAM_REGULAR, name);
|
||||
}
|
||||
|
||||
block gen_param(const char* name) {
|
||||
return gen_op_unbound(CLOSURE_PARAM, name);
|
||||
}
|
||||
@@ -300,8 +561,116 @@ block gen_both(block a, block b) {
|
||||
return c;
|
||||
}
|
||||
|
||||
block gen_const_object(block expr) {
|
||||
int is_const = 1;
|
||||
jv o = jv_object();
|
||||
jv k = jv_null();
|
||||
jv v = jv_null();
|
||||
for (inst *i = expr.first; i; i = i->next) {
|
||||
if (i->op != SUBEXP_BEGIN ||
|
||||
i->next == NULL ||
|
||||
i->next->op != LOADK ||
|
||||
i->next->next == NULL ||
|
||||
i->next->next->op != SUBEXP_END) {
|
||||
is_const = 0;
|
||||
break;
|
||||
}
|
||||
k = jv_copy(i->next->imm.constant);
|
||||
i = i->next->next->next;
|
||||
if (i == NULL ||
|
||||
i->op != SUBEXP_BEGIN ||
|
||||
i->next == NULL ||
|
||||
i->next->op != LOADK ||
|
||||
i->next->next == NULL ||
|
||||
i->next->next->op != SUBEXP_END) {
|
||||
is_const = 0;
|
||||
break;
|
||||
}
|
||||
v = jv_copy(i->next->imm.constant);
|
||||
i = i->next->next->next;
|
||||
if (i == NULL || i->op != INSERT) {
|
||||
is_const = 0;
|
||||
break;
|
||||
}
|
||||
o = jv_object_set(o, k, v);
|
||||
k = jv_null();
|
||||
v = jv_null();
|
||||
}
|
||||
if (!is_const) {
|
||||
jv_free(o);
|
||||
jv_free(k);
|
||||
jv_free(v);
|
||||
block b = {0,0};
|
||||
return b;
|
||||
}
|
||||
block_free(expr);
|
||||
return gen_const(o);
|
||||
}
|
||||
|
||||
static block gen_const_array(block expr) {
|
||||
/*
|
||||
* An expr of all constant elements looks like this:
|
||||
*
|
||||
* 0009 FORK 0027
|
||||
* 0011 FORK 0023
|
||||
* 0013 FORK 0019
|
||||
* 0015 LOADK 1
|
||||
* 0017 JUMP 0021
|
||||
* 0019 LOADK 2
|
||||
* 0021 JUMP 0025
|
||||
* 0023 LOADK 3
|
||||
* 0025 JUMP 0029
|
||||
* 0027 LOADK 4
|
||||
*
|
||||
* That's: N-1 commas for N elements, N LOADKs, and a JUMP between
|
||||
* every LOADK. The sequence ends in a LOADK. Any deviation and it's
|
||||
* not a list of constants.
|
||||
*
|
||||
* Here we check for this pattern almost exactly. We don't check that
|
||||
* the targets of the FORK and JUMP instructions are in the right
|
||||
* sequence.
|
||||
*/
|
||||
int all_const = 1;
|
||||
int commas = 0;
|
||||
int normal = 1;
|
||||
jv a = jv_array();
|
||||
for (inst *i = expr.first; i; i = i->next) {
|
||||
if (i->op == FORK) {
|
||||
commas++;
|
||||
if (i->imm.target == NULL || i->imm.target->op != JUMP ||
|
||||
jv_array_length(jv_copy(a)) > 0) {
|
||||
normal = 0;
|
||||
break;
|
||||
}
|
||||
} else if (all_const && i->op == LOADK) {
|
||||
if (i->next != NULL && i->next->op != JUMP) {
|
||||
normal = 0;
|
||||
break;
|
||||
}
|
||||
a = jv_array_append(a, jv_copy(i->imm.constant));
|
||||
} else if (i->op != JUMP || i->imm.target == NULL ||
|
||||
i->imm.target->op != LOADK) {
|
||||
all_const = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (all_const && normal &&
|
||||
(expr.last == NULL || expr.last->op == LOADK) &&
|
||||
jv_array_length(jv_copy(a)) == commas + 1) {
|
||||
block_free(expr);
|
||||
return gen_const(a);
|
||||
}
|
||||
|
||||
jv_free(a);
|
||||
block b = {0,0};
|
||||
return b;
|
||||
}
|
||||
|
||||
block gen_collect(block expr) {
|
||||
block const_array = gen_const_array(expr);
|
||||
if (const_array.first != NULL)
|
||||
return const_array;
|
||||
|
||||
block array_var = gen_op_var_fresh(STOREV, "collect");
|
||||
block c = BLOCK(gen_op_simple(DUP), gen_const(jv_array()), array_var);
|
||||
|
||||
@@ -310,20 +679,30 @@ block gen_collect(block expr) {
|
||||
|
||||
return BLOCK(c,
|
||||
gen_op_target(FORK, tail),
|
||||
expr,
|
||||
expr,
|
||||
tail,
|
||||
gen_op_bound(LOADVN, array_var));
|
||||
}
|
||||
|
||||
block gen_reduce(const char* varname, block source, block init, block body) {
|
||||
static block bind_matcher(block matcher, block body) {
|
||||
// cannot call block_bind(matcher, body) because that requires
|
||||
// block_has_only_binders(matcher), which is not true here as matchers
|
||||
// may also contain code to extract the correct elements
|
||||
for (inst* i = matcher.first; i; i = i->next) {
|
||||
if (i->op == STOREV && !i->bound_by)
|
||||
block_bind_subblock(inst_block(i), body, OP_HAS_VARIABLE, 0);
|
||||
}
|
||||
return BLOCK(matcher, body);
|
||||
}
|
||||
|
||||
block gen_reduce(block source, block matcher, block init, block body) {
|
||||
block res_var = gen_op_var_fresh(STOREV, "reduce");
|
||||
block loop = BLOCK(gen_op_simple(DUP),
|
||||
block loop = BLOCK(gen_op_simple(DUPN),
|
||||
source,
|
||||
block_bind(gen_op_unbound(STOREV, varname),
|
||||
BLOCK(gen_op_bound(LOADVN, res_var),
|
||||
body,
|
||||
gen_op_bound(STOREV, res_var)),
|
||||
OP_HAS_VARIABLE),
|
||||
bind_matcher(matcher,
|
||||
BLOCK(gen_op_bound(LOADVN, res_var),
|
||||
body,
|
||||
gen_op_bound(STOREV, res_var))),
|
||||
gen_op_simple(BACKTRACK));
|
||||
return BLOCK(gen_op_simple(DUP),
|
||||
init,
|
||||
@@ -333,6 +712,49 @@ block gen_reduce(const char* varname, block source, block init, block body) {
|
||||
gen_op_bound(LOADVN, res_var));
|
||||
}
|
||||
|
||||
block gen_foreach(block source, block matcher, block init, block update, block extract) {
|
||||
block output = gen_op_targetlater(JUMP);
|
||||
block state_var = gen_op_var_fresh(STOREV, "foreach");
|
||||
block loop = BLOCK(gen_op_simple(DUPN),
|
||||
// get a value from the source expression:
|
||||
source,
|
||||
// destructure the value into variable(s) for all the code
|
||||
// in the body to see
|
||||
bind_matcher(matcher,
|
||||
// load the loop state variable
|
||||
BLOCK(gen_op_bound(LOADVN, state_var),
|
||||
// generate updated state
|
||||
update,
|
||||
// save the updated state for value extraction
|
||||
gen_op_simple(DUP),
|
||||
// save new state
|
||||
gen_op_bound(STOREV, state_var),
|
||||
// extract an output...
|
||||
extract,
|
||||
// ...and output it by jumping
|
||||
// past the BACKTRACK that comes
|
||||
// right after the loop body,
|
||||
// which in turn is there
|
||||
// because...
|
||||
//
|
||||
// (Incidentally, extract can also
|
||||
// backtrack, e.g., if it calls
|
||||
// empty, in which case we don't
|
||||
// get here.)
|
||||
output)));
|
||||
block foreach = BLOCK(gen_op_simple(DUP),
|
||||
init,
|
||||
state_var,
|
||||
gen_op_target(FORK, loop),
|
||||
loop,
|
||||
// ...at this point `foreach`'s original input
|
||||
// will be on top of the stack, and we don't
|
||||
// want to output it, so we backtrack.
|
||||
gen_op_simple(BACKTRACK));
|
||||
inst_set_target(output, foreach); // make that JUMP go bast the BACKTRACK at the end of the loop
|
||||
return foreach;
|
||||
}
|
||||
|
||||
block gen_definedor(block a, block b) {
|
||||
// var found := false
|
||||
block found_var = gen_op_var_fresh(STOREV, "found");
|
||||
@@ -365,6 +787,20 @@ block gen_definedor(block a, block b) {
|
||||
tail);
|
||||
}
|
||||
|
||||
int block_has_main(block top) {
|
||||
for (inst *c = top.first; c; c = c->next) {
|
||||
if (c->op == TOP)
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int block_is_funcdef(block b) {
|
||||
if (b.first != NULL && b.first->op == CLOSURE_CREATE)
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
block gen_condbranch(block iftrue, block iffalse) {
|
||||
iftrue = BLOCK(iftrue, gen_op_target(JUMP, iffalse));
|
||||
return BLOCK(gen_op_target(JUMP_F, iftrue), iftrue, iffalse);
|
||||
@@ -372,7 +808,7 @@ block gen_condbranch(block iftrue, block iffalse) {
|
||||
|
||||
block gen_and(block a, block b) {
|
||||
// a and b = if a then (if b then true else false) else false
|
||||
return BLOCK(gen_op_simple(DUP), a,
|
||||
return BLOCK(gen_op_simple(DUP), a,
|
||||
gen_condbranch(BLOCK(gen_op_simple(POP),
|
||||
b,
|
||||
gen_condbranch(gen_const(jv_true()),
|
||||
@@ -391,17 +827,112 @@ block gen_or(block a, block b) {
|
||||
}
|
||||
|
||||
block gen_var_binding(block var, const char* name, block body) {
|
||||
return gen_destructure(var, gen_op_unbound(STOREV, name), body);
|
||||
}
|
||||
|
||||
block gen_array_matcher(block left, block curr) {
|
||||
int index;
|
||||
if (block_is_noop(left))
|
||||
index = 0;
|
||||
else {
|
||||
// `left` was returned by this function, so the third inst is the
|
||||
// constant containing the previously used index
|
||||
assert(left.first->op == DUP);
|
||||
assert(left.first->next->op == SUBEXP_BEGIN);
|
||||
assert(left.first->next->next->op == LOADK);
|
||||
index = 1 + (int) jv_number_value(left.first->next->next->imm.constant);
|
||||
}
|
||||
|
||||
// `left` goes at the end so that the const index is in a predictable place
|
||||
return BLOCK(gen_op_simple(DUP), gen_subexp(gen_const(jv_number(index))),
|
||||
gen_op_simple(INDEX), curr, left);
|
||||
}
|
||||
|
||||
block gen_object_matcher(block name, block curr) {
|
||||
return BLOCK(gen_op_simple(DUP), gen_subexp(name), gen_op_simple(INDEX),
|
||||
curr);
|
||||
}
|
||||
|
||||
block gen_destructure(block var, block matcher, block body) {
|
||||
// var bindings can be added after coding the program; leave the TOP first.
|
||||
block top = gen_noop();
|
||||
if (body.first && body.first->op == TOP)
|
||||
top = inst_block(block_take(&body));
|
||||
|
||||
return BLOCK(top, gen_op_simple(DUP), var, bind_matcher(matcher, body));
|
||||
}
|
||||
|
||||
// Like gen_var_binding(), but bind `break`'s wildcard unbound variable
|
||||
static block gen_wildvar_binding(block var, const char* name, block body) {
|
||||
return BLOCK(gen_op_simple(DUP), var,
|
||||
block_bind(gen_op_unbound(STOREV, name),
|
||||
body, OP_HAS_VARIABLE));
|
||||
body, OP_HAS_VARIABLE | OP_BIND_WILDCARD));
|
||||
}
|
||||
|
||||
block gen_cond(block cond, block iftrue, block iffalse) {
|
||||
return BLOCK(gen_op_simple(DUP), cond,
|
||||
return BLOCK(gen_op_simple(DUP), cond,
|
||||
gen_condbranch(BLOCK(gen_op_simple(POP), iftrue),
|
||||
BLOCK(gen_op_simple(POP), iffalse)));
|
||||
}
|
||||
|
||||
block gen_try_handler(block handler) {
|
||||
// Quite a pain just to hide jq's internal errors.
|
||||
return gen_cond(// `if type=="object" and .__jq
|
||||
gen_and(gen_call("_equal",
|
||||
BLOCK(gen_lambda(gen_const(jv_string("object"))),
|
||||
gen_lambda(gen_noop()))),
|
||||
BLOCK(gen_subexp(gen_const(jv_string("__jq"))),
|
||||
gen_noop(),
|
||||
gen_op_simple(INDEX))),
|
||||
// `then error`
|
||||
gen_call("error", gen_noop()),
|
||||
// `else HANDLER end`
|
||||
handler);
|
||||
}
|
||||
|
||||
block gen_try(block exp, block handler) {
|
||||
/*
|
||||
* Produce something like:
|
||||
* FORK_OPT <address of handler>
|
||||
* <exp>
|
||||
* JUMP <end of handler>
|
||||
* <handler>
|
||||
*
|
||||
* If this is not an internal try/catch, then catch and re-raise
|
||||
* internal errors to prevent them from leaking.
|
||||
*
|
||||
* The handler will only execute if we backtrack to the FORK_OPT with
|
||||
* an error (exception). If <exp> produces no value then FORK_OPT
|
||||
* will backtrack (propagate the `empty`, as it were. If <exp>
|
||||
* produces a value then we'll execute whatever bytecode follows this
|
||||
* sequence.
|
||||
*/
|
||||
if (!handler.first && !handler.last)
|
||||
// A hack to deal with `.` as the handler; we could use a real NOOP here
|
||||
handler = BLOCK(gen_op_simple(DUP), gen_op_simple(POP), handler);
|
||||
exp = BLOCK(exp, gen_op_target(JUMP, handler));
|
||||
return BLOCK(gen_op_target(FORK_OPT, exp), exp, handler);
|
||||
}
|
||||
|
||||
block gen_label(const char *label, block exp) {
|
||||
block cond = gen_call("_equal",
|
||||
BLOCK(gen_lambda(gen_noop()),
|
||||
gen_lambda(gen_op_unbound(LOADV, label))));
|
||||
return gen_wildvar_binding(gen_op_simple(GENLABEL), label,
|
||||
BLOCK(gen_op_simple(POP),
|
||||
// try exp catch if . == $label
|
||||
// then empty
|
||||
// else error end
|
||||
//
|
||||
// Can't use gen_binop(), as that's firmly
|
||||
// stuck in parser.y as it refers to things
|
||||
// like EQ.
|
||||
gen_try(exp,
|
||||
gen_cond(cond,
|
||||
gen_op_simple(BACKTRACK),
|
||||
gen_call("error", gen_noop())))));
|
||||
}
|
||||
|
||||
block gen_cbinding(const struct cfunction* cfunctions, int ncfunctions, block code) {
|
||||
for (int cfunc=0; cfunc<ncfunctions; cfunc++) {
|
||||
inst* i = inst_new(CLOSURE_CREATE_C);
|
||||
@@ -414,7 +945,7 @@ block gen_cbinding(const struct cfunction* cfunctions, int ncfunctions, block co
|
||||
|
||||
static uint16_t nesting_level(struct bytecode* bc, inst* target) {
|
||||
uint16_t level = 0;
|
||||
assert(bc && target->compiled);
|
||||
assert(bc && target && target->compiled);
|
||||
while (bc && target->compiled != bc) {
|
||||
level++;
|
||||
bc = bc->parent;
|
||||
@@ -434,13 +965,16 @@ static int count_cfunctions(block b) {
|
||||
|
||||
|
||||
// Expands call instructions into a calling sequence
|
||||
static int expand_call_arglist(struct locfile* locations, block* b) {
|
||||
static int expand_call_arglist(block* b) {
|
||||
int errors = 0;
|
||||
block ret = gen_noop();
|
||||
for (inst* curr; (curr = block_take(b));) {
|
||||
if (opcode_describe(curr->op)->flags & OP_HAS_BINDING) {
|
||||
if (!curr->bound_by) {
|
||||
locfile_locate(locations, curr->source, "error: %s is not defined", curr->symbol);
|
||||
if (curr->symbol[0] == '*' && curr->symbol[1] >= '1' && curr->symbol[1] <= '3' && curr->symbol[2] == '\0')
|
||||
locfile_locate(curr->locfile, curr->source, "jq: error: break used outside labeled control structure");
|
||||
else
|
||||
locfile_locate(curr->locfile, curr->source, "jq: error: %s/%d is not defined", curr->symbol, block_count_actuals(curr->arglist));
|
||||
errors++;
|
||||
// don't process this instruction if it's not well-defined
|
||||
ret = BLOCK(ret, inst_block(curr));
|
||||
@@ -454,7 +988,7 @@ static int expand_call_arglist(struct locfile* locations, block* b) {
|
||||
// We expand the argument list as a series of instructions
|
||||
switch (curr->bound_by->op) {
|
||||
default: assert(0 && "Unknown function type"); break;
|
||||
case CLOSURE_CREATE:
|
||||
case CLOSURE_CREATE:
|
||||
case CLOSURE_PARAM: {
|
||||
block callargs = gen_noop();
|
||||
for (inst* i; (i = block_take(&curr->arglist));) {
|
||||
@@ -491,7 +1025,7 @@ static int expand_call_arglist(struct locfile* locations, block* b) {
|
||||
i->subfn = gen_noop();
|
||||
inst_free(i);
|
||||
// arguments should be pushed in reverse order, prepend them to prelude
|
||||
errors += expand_call_arglist(locations, &body);
|
||||
errors += expand_call_arglist(&body);
|
||||
prelude = BLOCK(gen_subexp(body), prelude);
|
||||
actual_args++;
|
||||
}
|
||||
@@ -505,14 +1039,7 @@ static int expand_call_arglist(struct locfile* locations, block* b) {
|
||||
}
|
||||
}
|
||||
|
||||
if (actual_args != desired_args) {
|
||||
locfile_locate(locations, curr->source,
|
||||
"error: %s arguments to %s (expected %d but got %d)",
|
||||
actual_args > desired_args ? "too many" : "too few",
|
||||
curr->symbol, desired_args, actual_args);
|
||||
errors++;
|
||||
}
|
||||
|
||||
assert(actual_args == desired_args); // because now handle this above
|
||||
}
|
||||
ret = BLOCK(ret, prelude, inst_block(curr));
|
||||
}
|
||||
@@ -520,12 +1047,12 @@ static int expand_call_arglist(struct locfile* locations, block* b) {
|
||||
return errors;
|
||||
}
|
||||
|
||||
static int compile(struct locfile* locations, struct bytecode* bc, block b) {
|
||||
static int compile(struct bytecode* bc, block b, struct locfile* lf) {
|
||||
int errors = 0;
|
||||
int pos = 0;
|
||||
int var_frame_idx = 0;
|
||||
bc->nsubfunctions = 0;
|
||||
errors += expand_call_arglist(locations, &b);
|
||||
errors += expand_call_arglist(&b);
|
||||
b = BLOCK(b, gen_op_simple(RET));
|
||||
jv localnames = jv_array();
|
||||
for (inst* curr = b.first; curr; curr = curr->next) {
|
||||
@@ -561,6 +1088,13 @@ static int compile(struct locfile* locations, struct bytecode* bc, block b) {
|
||||
curr->imm.intval = idx;
|
||||
}
|
||||
}
|
||||
if (pos > 0xFFFF) {
|
||||
// too long for program counter to fit in uint16_t
|
||||
locfile_locate(lf, UNKNOWN_LOCATION,
|
||||
"function compiled to %d bytes which is too long", pos);
|
||||
errors++;
|
||||
}
|
||||
bc->codelen = pos;
|
||||
bc->debuginfo = jv_object_set(bc->debuginfo, jv_string("locals"), localnames);
|
||||
if (bc->nsubfunctions) {
|
||||
bc->subfunctions = jv_mem_alloc(sizeof(struct bytecode*) * bc->nsubfunctions);
|
||||
@@ -581,14 +1115,13 @@ static int compile(struct locfile* locations, struct bytecode* bc, block b) {
|
||||
params = jv_array_append(params, jv_string(param->symbol));
|
||||
}
|
||||
subfn->debuginfo = jv_object_set(subfn->debuginfo, jv_string("params"), params);
|
||||
errors += compile(locations, subfn, curr->subfn);
|
||||
errors += compile(subfn, curr->subfn, lf);
|
||||
curr->subfn = gen_noop();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
bc->subfunctions = 0;
|
||||
}
|
||||
bc->codelen = pos;
|
||||
uint16_t* code = jv_mem_alloc(sizeof(uint16_t) * bc->codelen);
|
||||
bc->code = code;
|
||||
pos = 0;
|
||||
@@ -610,13 +1143,20 @@ static int compile(struct locfile* locations, struct bytecode* bc, block b) {
|
||||
curr->bound_by->op == CLOSURE_PARAM);
|
||||
code[pos++] = (uint16_t)curr->imm.intval;
|
||||
code[pos++] = nesting_level(bc, curr->bound_by);
|
||||
code[pos++] = curr->bound_by->imm.intval |
|
||||
code[pos++] = curr->bound_by->imm.intval |
|
||||
(curr->bound_by->op == CLOSURE_CREATE ? ARG_NEWCLOSURE : 0);
|
||||
for (inst* arg = curr->arglist.first; arg; arg = arg->next) {
|
||||
assert(arg->op == CLOSURE_REF && arg->bound_by->op == CLOSURE_CREATE);
|
||||
code[pos++] = nesting_level(bc, arg->bound_by);
|
||||
code[pos++] = arg->bound_by->imm.intval | ARG_NEWCLOSURE;
|
||||
}
|
||||
} else if ((op->flags & OP_HAS_CONSTANT) && (op->flags & OP_HAS_VARIABLE)) {
|
||||
// STORE_GLOBAL: constant global, basically
|
||||
code[pos++] = jv_array_length(jv_copy(constant_pool));
|
||||
constant_pool = jv_array_append(constant_pool, jv_copy(curr->imm.constant));
|
||||
code[pos++] = nesting_level(bc, curr->bound_by);
|
||||
uint16_t var = (uint16_t)curr->bound_by->imm.intval;
|
||||
code[pos++] = var;
|
||||
} else if (op->flags & OP_HAS_CONSTANT) {
|
||||
code[pos++] = jv_array_length(jv_copy(constant_pool));
|
||||
constant_pool = jv_array_append(constant_pool, jv_copy(curr->imm.constant));
|
||||
@@ -640,7 +1180,7 @@ static int compile(struct locfile* locations, struct bytecode* bc, block b) {
|
||||
return errors;
|
||||
}
|
||||
|
||||
int block_compile(block b, struct locfile* locations, struct bytecode** out) {
|
||||
int block_compile(block b, struct bytecode** out, struct locfile* lf) {
|
||||
struct bytecode* bc = jv_mem_alloc(sizeof(struct bytecode));
|
||||
bc->parent = 0;
|
||||
bc->nclosures = 0;
|
||||
@@ -650,7 +1190,7 @@ int block_compile(block b, struct locfile* locations, struct bytecode** out) {
|
||||
bc->globals->cfunctions = jv_mem_alloc(sizeof(struct cfunction) * ncfunc);
|
||||
bc->globals->cfunc_names = jv_array();
|
||||
bc->debuginfo = jv_object_set(jv_object(), jv_string("name"), jv_null());
|
||||
int nerrors = compile(locations, bc, b);
|
||||
int nerrors = compile(bc, b, lf);
|
||||
assert(bc->globals->ncfunctions == ncfunc);
|
||||
if (nerrors > 0) {
|
||||
bytecode_free(bc);
|
||||
|
||||
@@ -14,42 +14,68 @@ typedef struct block {
|
||||
inst* last;
|
||||
} block;
|
||||
|
||||
block gen_location(location, block);
|
||||
block gen_location(location, struct locfile*, block);
|
||||
|
||||
block gen_noop();
|
||||
int block_is_noop(block b);
|
||||
block gen_op_simple(opcode op);
|
||||
block gen_const(jv constant);
|
||||
block gen_const_global(jv constant, const char *name);
|
||||
int block_is_const(block b);
|
||||
int block_is_const_inf(block b);
|
||||
jv_kind block_const_kind(block b);
|
||||
jv block_const(block b);
|
||||
block gen_op_target(opcode op, block target);
|
||||
block gen_op_unbound(opcode op, const char* name);
|
||||
block gen_op_bound(opcode op, block binder);
|
||||
block gen_op_var_fresh(opcode op, const char* name);
|
||||
|
||||
block gen_module(block metadata);
|
||||
jv block_module_meta(block b);
|
||||
block gen_import(const char* name, block metadata, const char *as, int is_data);
|
||||
block gen_function(const char* name, block formals, block body);
|
||||
block gen_param_regular(const char* name);
|
||||
block gen_param(const char* name);
|
||||
block gen_lambda(block body);
|
||||
block gen_call(const char* name, block body);
|
||||
block gen_subexp(block a);
|
||||
block gen_both(block a, block b);
|
||||
block gen_const_object(block expr);
|
||||
block gen_collect(block expr);
|
||||
block gen_reduce(const char* varname, block source, block init, block body);
|
||||
block gen_reduce(block source, block matcher, block init, block body);
|
||||
block gen_foreach(block source, block matcher, block init, block update, block extract);
|
||||
block gen_definedor(block a, block b);
|
||||
block gen_condbranch(block iftrue, block iffalse);
|
||||
block gen_and(block a, block b);
|
||||
block gen_or(block a, block b);
|
||||
|
||||
block gen_var_binding(block var, const char* name, block body);
|
||||
block gen_array_matcher(block left, block curr);
|
||||
block gen_object_matcher(block name, block curr);
|
||||
block gen_destructure(block var, block matcher, block body);
|
||||
|
||||
block gen_cond(block cond, block iftrue, block iffalse);
|
||||
block gen_try_handler(block handler);
|
||||
block gen_try(block exp, block handler);
|
||||
block gen_label(const char *label, block exp);
|
||||
|
||||
block gen_cbinding(const struct cfunction* functions, int nfunctions, block b);
|
||||
|
||||
void block_append(block* b, block b2);
|
||||
block block_join(block a, block b);
|
||||
int block_has_only_binders_and_imports(block, int bindflags);
|
||||
int block_has_only_binders(block, int bindflags);
|
||||
int block_has_main(block);
|
||||
int block_is_funcdef(block b);
|
||||
int block_is_single(block b);
|
||||
block block_bind(block binder, block body, int bindflags);
|
||||
block block_bind_library(block binder, block body, int bindflags, const char* libname);
|
||||
block block_bind_referenced(block binder, block body, int bindflags);
|
||||
block block_drop_unreferenced(block body);
|
||||
|
||||
int block_compile(block, struct locfile*, struct bytecode**);
|
||||
jv block_take_imports(block* body);
|
||||
|
||||
int block_compile(block, struct bytecode**, struct locfile*);
|
||||
|
||||
void block_free(block);
|
||||
|
||||
@@ -65,10 +91,11 @@ void block_free(block);
|
||||
#define BLOCK_5(b1,b2,b3,b4,b5) (block_join(BLOCK_4(b1,b2,b3,b4),(b5)))
|
||||
#define BLOCK_6(b1,b2,b3,b4,b5,b6) (block_join(BLOCK_5(b1,b2,b3,b4,b5),(b6)))
|
||||
#define BLOCK_7(b1,b2,b3,b4,b5,b6,b7) (block_join(BLOCK_6(b1,b2,b3,b4,b5,b6),(b7)))
|
||||
#define BLOCK_8(b1,b2,b3,b4,b5,b6,b7,b8) (block_join(BLOCK_7(b1,b2,b3,b4,b5,b6,b7),(b8)))
|
||||
|
||||
#define BLOCK_IDX(_1,_2,_3,_4,_5,_6,_7,NAME,...) NAME
|
||||
#define BLOCK_IDX(_1,_2,_3,_4,_5,_6,_7,_8,NAME,...) NAME
|
||||
#define BLOCK(...) \
|
||||
BLOCK_IDX(__VA_ARGS__, BLOCK_7, BLOCK_6, BLOCK_5, BLOCK_4, BLOCK_3, BLOCK_2, BLOCK_1)(__VA_ARGS__)
|
||||
BLOCK_IDX(__VA_ARGS__, BLOCK_8, BLOCK_7, BLOCK_6, BLOCK_5, BLOCK_4, BLOCK_3, BLOCK_2, BLOCK_1)(__VA_ARGS__)
|
||||
|
||||
|
||||
#endif
|
||||
@@ -6,3 +6,5 @@ install-sh
|
||||
ltmain.sh
|
||||
missing
|
||||
ylwrap
|
||||
test-driver
|
||||
ar-lib
|
||||
@@ -0,0 +1,4 @@
|
||||
dnl AC_FIND_FUNC(func, arguments)
|
||||
AC_DEFUN([AC_CHECK_MATH_FUNC], [
|
||||
AC_FIND_FUNC_NO_LIBS([$1], [m], [#include <math.h>], [$2])
|
||||
])
|
||||
@@ -0,0 +1,8 @@
|
||||
dnl Shamelessly stolen from Heimdal
|
||||
dnl
|
||||
dnl Look for function in any of the specified libraries
|
||||
dnl
|
||||
|
||||
dnl AC_FIND_FUNC_NO_LIBS(func, libraries, includes, arguments, extra libs, extra args)
|
||||
AC_DEFUN([AC_FIND_FUNC_NO_LIBS], [
|
||||
AC_FIND_FUNC_NO_LIBS2([$1], ["" $2], [$3], [$4], [$5], [$6])])
|
||||
@@ -0,0 +1,62 @@
|
||||
dnl Shamelessly stolen from Heimdal
|
||||
dnl
|
||||
dnl Look for function in any of the specified libraries
|
||||
dnl
|
||||
|
||||
dnl AC_FIND_FUNC_NO_LIBS2(func, libraries, includes, arguments, extra libs, extra args)
|
||||
AC_DEFUN([AC_FIND_FUNC_NO_LIBS2], [
|
||||
|
||||
AC_MSG_CHECKING([for $1])
|
||||
AC_CACHE_VAL(ac_cv_funclib_$1,
|
||||
[
|
||||
if eval "test \"\$ac_cv_func_$1\" != yes" ; then
|
||||
ac_save_LIBS="$LIBS"
|
||||
for ac_lib in $2; do
|
||||
case "$ac_lib" in
|
||||
"") ;;
|
||||
yes) ac_lib="" ;;
|
||||
no) continue ;;
|
||||
-l*) ;;
|
||||
*) ac_lib="-l$ac_lib" ;;
|
||||
esac
|
||||
LIBS="$6 $ac_lib $5 $ac_save_LIBS"
|
||||
AC_LINK_IFELSE([AC_LANG_PROGRAM([[$3]],[[$1($4)]])],[eval "if test -n \"$ac_lib\";then ac_cv_funclib_$1=$ac_lib; else ac_cv_funclib_$1=yes; fi";break])
|
||||
done
|
||||
eval "ac_cv_funclib_$1=\${ac_cv_funclib_$1-no}"
|
||||
LIBS="$ac_save_LIBS"
|
||||
fi
|
||||
])
|
||||
|
||||
eval "ac_res=\$ac_cv_funclib_$1"
|
||||
|
||||
if false; then
|
||||
AC_CHECK_FUNCS($1)
|
||||
dnl AC_CHECK_LIBS($2, foo)
|
||||
fi
|
||||
# $1
|
||||
eval "ac_tr_func=HAVE_[]upcase($1)"
|
||||
eval "ac_tr_lib=HAVE_LIB[]upcase($ac_res | sed -e 's/-l//')"
|
||||
eval "LIB_$1=$ac_res"
|
||||
|
||||
case "$ac_res" in
|
||||
yes)
|
||||
eval "ac_cv_func_$1=yes"
|
||||
eval "LIB_$1="
|
||||
AC_DEFINE_UNQUOTED($ac_tr_func)
|
||||
AC_MSG_RESULT([yes])
|
||||
;;
|
||||
no)
|
||||
eval "ac_cv_func_$1=no"
|
||||
eval "LIB_$1="
|
||||
AC_MSG_RESULT([no])
|
||||
;;
|
||||
*)
|
||||
eval "ac_cv_func_$1=yes"
|
||||
eval "ac_cv_lib_`echo "$ac_res" | sed 's/-l//'`=yes"
|
||||
AC_DEFINE_UNQUOTED($ac_tr_func)
|
||||
AC_DEFINE_UNQUOTED($ac_tr_lib)
|
||||
AC_MSG_RESULT([yes, in $ac_res])
|
||||
;;
|
||||
esac
|
||||
AC_SUBST(LIB_$1)
|
||||
])
|
||||
@@ -0,0 +1,9 @@
|
||||
dnl Shamelessly stolen from Heimdal
|
||||
dnl
|
||||
dnl AC_FIND_FUNC(func, libraries, includes, arguments)
|
||||
AC_DEFUN([AC_FIND_FUNC], [
|
||||
AC_FIND_FUNC_NO_LIBS([$1], [$2], [$3], [$4])
|
||||
if test -n "$LIB_$1"; then
|
||||
LIBS="$LIB_$1 $LIBS"
|
||||
fi
|
||||
])
|
||||
@@ -0,0 +1,3 @@
|
||||
|
||||
dnl Shamelessly stolen from Heimdal
|
||||
AC_DEFUN([upcase],[`echo $1 | tr abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ`])dnl
|
||||
+158
-11
@@ -1,11 +1,18 @@
|
||||
AC_INIT([jq], [1.3], [mu@netsoc.tcd.ie],
|
||||
[jq], [http://stedolan.github.com/jq/])
|
||||
m4_define([jq_version], m4_esyscmd_s([git describe --tags --dirty --match 'jq-*'|sed 's/^jq-//']))
|
||||
|
||||
AC_INIT([jq], [jq_version], [https://github.com/stedolan/jq/issues],
|
||||
[jq], [http://stedolan.github.io/jq])
|
||||
|
||||
m4_include([m4/ax_compare_version.m4])
|
||||
m4_include([m4/ax_prog_bison_version.m4])
|
||||
|
||||
dnl Created autoconf implementation thompson@dtosolutions, 26NOV12
|
||||
AC_PREREQ([2.61])
|
||||
AC_PREREQ([2.64])
|
||||
AC_CONFIG_AUX_DIR([config])
|
||||
AM_INIT_AUTOMAKE([parallel-tests foreign])
|
||||
AM_INIT_AUTOMAKE([1.11.2 parallel-tests foreign -Wall])
|
||||
AM_SILENT_RULES([yes])
|
||||
AM_PROG_AR
|
||||
AM_MAINTAINER_MODE([enable])
|
||||
AC_PROG_CC
|
||||
AC_PROG_CC_STDC
|
||||
AC_PROG_CPP_WERROR
|
||||
@@ -15,16 +22,55 @@ AC_EXEEXT
|
||||
LT_INIT([shared static win32-dll])
|
||||
AM_PROG_CC_C_O
|
||||
|
||||
|
||||
dnl couldn't use AM_PROG_LEX as it doesn't support header files like the
|
||||
dnl AC_PROG_YACC macros...
|
||||
|
||||
dnl
|
||||
dnl these program checks should probably be deleted
|
||||
dnl
|
||||
dnl check bison version
|
||||
|
||||
AC_PROG_LEX
|
||||
if test "x$LEX" != xflex; then
|
||||
LEX="$SHELL $missing_dir/missing flex"
|
||||
if test "$USE_MAINTAINER_MODE" = yes; then
|
||||
if test "$YACC" != "bison -y"; then
|
||||
AC_MSG_CHECKING([bison version])
|
||||
AC_MSG_RESULT([not bison])
|
||||
else
|
||||
AX_PROG_BISON_VERSION([3],
|
||||
[],
|
||||
[AC_MSG_ERROR([You need bison version 3.0 or greater, or use --disable-maintainer-mode.])])
|
||||
fi
|
||||
|
||||
AC_PROG_LEX
|
||||
if test "x$LEX" != xflex; then
|
||||
LEX="$SHELL $missing_dir/missing flex"
|
||||
fi
|
||||
fi
|
||||
|
||||
|
||||
##########################################################################
|
||||
# check for ONIGURUMA library
|
||||
##########################################################################
|
||||
|
||||
AC_ARG_WITH([oniguruma],
|
||||
[AS_HELP_STRING([--with-oniguruma=prefix],
|
||||
[try this for a non-standard install prefix of the oniguruma library])],
|
||||
[ONIGURUMAPATHSET=1],
|
||||
[ONIGURUMAPATHSET=0])
|
||||
|
||||
if test $ONIGURUMAPATHSET = 1; then
|
||||
CFLAGS="$CFLAGS -I${with_oniguruma}/include"
|
||||
LDFLAGS="$LDFLAGS -L${with_oniguruma}/lib"
|
||||
fi
|
||||
|
||||
# check for ONIGURUMA library
|
||||
HAVE_ONIGURUMA=0
|
||||
AC_CHECK_HEADER("oniguruma.h",
|
||||
AC_CHECK_LIB([onig],[onig_version],[LIBS="$LIBS -lonig"; HAVE_ONIGURUMA=1;]))
|
||||
|
||||
# handle check results
|
||||
if test $HAVE_ONIGURUMA != 1; then
|
||||
AC_MSG_NOTICE([Oniguruma was not found.])
|
||||
AC_MSG_NOTICE([ Try setting the location using '--with-oniguruma=PREFIX' ])
|
||||
else
|
||||
AC_DEFINE([HAVE_ONIGURUMA],1,[Define to 1 if Oniguruma is installed])
|
||||
fi
|
||||
|
||||
|
||||
@@ -33,12 +79,32 @@ AC_CHECK_PROGS(valgrind_cmd, valgrind)
|
||||
if test "x$valgrind_cmd" = "x" ; then
|
||||
AC_MSG_WARN([valgrind is required to test jq.])
|
||||
fi
|
||||
AC_CHECK_FUNCS(memmem)
|
||||
AC_CHECK_FUNCS(mkstemp)
|
||||
|
||||
|
||||
dnl Running tests with Valgrind is slow. It is faster to iterate on
|
||||
dnl code without Valgrind until tests pass, then enable Valgrind and
|
||||
dnl fix leaks.
|
||||
AC_ARG_ENABLE([valgrind],
|
||||
AC_HELP_STRING([--disable-valgrind], [do not run tests under Valgrind]))
|
||||
|
||||
dnl Code coverage
|
||||
AC_ARG_ENABLE([gcov],
|
||||
AC_HELP_STRING([--enable-gcov], [enable gcov code coverage tool]))
|
||||
|
||||
dnl Don't attempt to build docs if there's no Ruby lying around
|
||||
AC_ARG_ENABLE([docs],
|
||||
AC_HELP_STRING([--disable-docs], [don't build docs]))
|
||||
|
||||
dnl Don't attempt to build the error injection object (if there is no LD_PRELOAD support)
|
||||
AC_ARG_ENABLE([error-injection],
|
||||
AC_HELP_STRING([--enable-error-injection], [build and test with error injection]))
|
||||
|
||||
dnl Enable building all static
|
||||
AC_ARG_ENABLE([all-static],
|
||||
AC_HELP_STRING([--enable-all-static], [link jq with static libraries only]))
|
||||
|
||||
AS_IF([test "x$enable_docs" != "xno"],[
|
||||
AC_CHECK_PROGS(bundle_cmd, bundle)
|
||||
|
||||
@@ -63,11 +129,92 @@ EOF
|
||||
fi
|
||||
])
|
||||
|
||||
AM_CONDITIONAL([ENABLE_VALGRIND], [test "x$enable_valgrind" != xno])
|
||||
AM_CONDITIONAL([ENABLE_GCOV], [test "x$enable_gcov" = xyes])
|
||||
AM_CONDITIONAL([ENABLE_DOCS], [test "x$enable_docs" != xno])
|
||||
AM_CONDITIONAL([ENABLE_ERROR_INJECTION], [test "x$enable_error_injection" = xyes])
|
||||
AM_CONDITIONAL([ENABLE_ALL_STATIC], [test "x$enable_all_static" = xyes])
|
||||
|
||||
AC_FUNC_ALLOCA
|
||||
|
||||
AC_FIND_FUNC([isatty], [c], [#include <unistd.h>], [0])
|
||||
AC_FIND_FUNC([_isatty], [c], [#include <io.h>], [0])
|
||||
AC_FIND_FUNC([strptime], [c], [#include <time.h>], [0, 0, 0])
|
||||
AC_FIND_FUNC([strftime], [c], [#include <time.h>], [0, 0, 0, 0])
|
||||
AC_FIND_FUNC([timegm], [c], [#include <time.h>], [0])
|
||||
AC_FIND_FUNC([gmtime_r], [c], [#include <time.h>], [0, 0])
|
||||
AC_FIND_FUNC([gmtime], [c], [#include <time.h>], [0])
|
||||
AC_FIND_FUNC([gettimeofday], [c], [#include <time.h>], [0, 0])
|
||||
AC_CHECK_MEMBER([struct tm.tm_gmtoff], [AC_DEFINE([HAVE_TM_TM_GMT_OFF],1,[Define to 1 if the system has the tm_gmt_off field in struct tm])],
|
||||
[], [[#include <time.h>]])
|
||||
AC_CHECK_MEMBER([struct tm.__tm_gmtoff], [AC_DEFINE([HAVE_TM___TM_GMT_OFF],1,[Define to 1 if the system has the __tm_gmt_off field in struct tm])],
|
||||
[], [[#include <time.h>]])
|
||||
|
||||
AC_ARG_ENABLE([pthread-tls],
|
||||
[AC_HELP_STRING([--enable-pthread-tls],
|
||||
[Enable use of pthread thread local storage])],
|
||||
[],
|
||||
[enable_pthread_tls=no])
|
||||
|
||||
if test $enable_pthread_tls = yes; then
|
||||
AC_FIND_FUNC([pthread_key_create], [pthread], [#include <pthread.h>], [NULL, NULL])
|
||||
AC_FIND_FUNC([pthread_once], [pthread], [#include <pthread.h>], [NULL, NULL])
|
||||
AC_FIND_FUNC([atexit], [pthread], [#include <stdlib.h>], [NULL])
|
||||
fi
|
||||
|
||||
dnl libm math.h functions
|
||||
AC_CHECK_MATH_FUNC(acos, [.5])
|
||||
AC_CHECK_MATH_FUNC(acosh, [.5])
|
||||
AC_CHECK_MATH_FUNC(asin, [.5])
|
||||
AC_CHECK_MATH_FUNC(asinh, [.5])
|
||||
AC_CHECK_MATH_FUNC(atan, [.5])
|
||||
AC_CHECK_MATH_FUNC(atanh, [.5])
|
||||
AC_CHECK_MATH_FUNC(cbrt, [.5])
|
||||
AC_CHECK_MATH_FUNC(cos, [.5])
|
||||
AC_CHECK_MATH_FUNC(cosh, [.5])
|
||||
AC_CHECK_MATH_FUNC(exp2, [.5])
|
||||
AC_CHECK_MATH_FUNC(exp, [.5])
|
||||
AC_CHECK_MATH_FUNC(floor, [.5])
|
||||
AC_CHECK_MATH_FUNC(j0, [.5])
|
||||
AC_CHECK_MATH_FUNC(j1, [.5])
|
||||
AC_CHECK_MATH_FUNC(log10, [.5])
|
||||
AC_CHECK_MATH_FUNC(log2, [.5])
|
||||
AC_CHECK_MATH_FUNC(log, [.5])
|
||||
AC_CHECK_MATH_FUNC(sin, [.5])
|
||||
AC_CHECK_MATH_FUNC(sinh, [.5])
|
||||
AC_CHECK_MATH_FUNC(sqrt, [.5])
|
||||
AC_CHECK_MATH_FUNC(tan, [.5])
|
||||
AC_CHECK_MATH_FUNC(tanh, [.5])
|
||||
AC_CHECK_MATH_FUNC(tgamma, [.5])
|
||||
AC_CHECK_MATH_FUNC(y0, [.5])
|
||||
AC_CHECK_MATH_FUNC(y1, [.5])
|
||||
AC_CHECK_MATH_FUNC(pow, [2,2])
|
||||
AC_CHECK_MATH_FUNC(atan2, [.5,.5])
|
||||
AC_CHECK_MATH_FUNC(hypot, [.5,.5])
|
||||
AC_CHECK_MATH_FUNC(remainder, [3,2])
|
||||
|
||||
dnl Thread local storage
|
||||
have___thread=no
|
||||
AC_MSG_CHECKING(for thread-local storage)
|
||||
AC_LINK_IFELSE([AC_LANG_SOURCE([
|
||||
static __thread int x ;
|
||||
int main () { x = 123; return x; }
|
||||
])], have___thread=yes)
|
||||
if test $have___thread = yes; then
|
||||
AC_DEFINE([HAVE___THREAD],1,[Define to 1 if the system supports __thread])
|
||||
fi
|
||||
AC_MSG_RESULT($have___thread)
|
||||
|
||||
AC_C_BIGENDIAN(
|
||||
AC_DEFINE([IEEE_MC68k], 1, [machine is bigendian]),
|
||||
AC_DEFINE([IEEE_8087], 1, [machine is littleendian]),
|
||||
AC_MSG_ERROR(unknown endianess),
|
||||
AC_MSG_ERROR(universial endianess not supported)
|
||||
)
|
||||
|
||||
AC_SUBST([BUNDLER], ["$bundle_cmd"])
|
||||
|
||||
AC_CONFIG_MACRO_DIR([config/m4])
|
||||
AM_INIT_AUTOMAKE([-Wall])
|
||||
AC_CONFIG_FILES([Makefile])
|
||||
AC_OUTPUT
|
||||
|
||||
|
||||
+1
-2
@@ -75,7 +75,7 @@ $BINARIES = {
|
||||
|
||||
$BINARIES.each do |name, args|
|
||||
file "../build/#{name}" do |t|
|
||||
sh "MAKEFLAGS=-j4 ../scripts/crosscompile #{name} #{args}"
|
||||
sh "../scripts/crosscompile -j4 #{name} #{args}"
|
||||
end
|
||||
task :binaries => ["../build/#{name}"]
|
||||
end
|
||||
@@ -87,7 +87,6 @@ task :binaries => ["output/download"] do
|
||||
end
|
||||
|
||||
def load_manual
|
||||
YAML::ENGINE.yamler = 'syck'
|
||||
YAML::load(File.open("content/3.manual/manual.yml"))
|
||||
end
|
||||
|
||||
|
||||
+241
-160
@@ -2,123 +2,205 @@ headline: Tutorial
|
||||
body:
|
||||
- text: |
|
||||
|
||||
Twitter has a JSON API, so let's play with that. This URL gets
|
||||
us the last 5 tweets about JSON:
|
||||
|
||||
- command: "curl 'http://search.twitter.com/search.json?q=json&rpp=5&include_entities=true'"
|
||||
GitHub has a JSON API, so let's play with that. This URL gets
|
||||
us the last 5 commits from the jq repo:
|
||||
|
||||
- command: "curl 'https://api.github.com/repos/stedolan/jq/commits?per_page=5'"
|
||||
result: |
|
||||
{"completed_in":0.108,"max_id":247677287108067328,"max_id_str":"247677287108067328","next_page":"?page=2&max_id=247677287108067328&q=json&rpp=5&include_entities=1","page":1,"query":"json","refresh_url":"?since_id=247677287108067328&q=json&include_entities=1","results":[{"created_at":"Mon, 17 Sep 2012 12:44:01 +0000","entities":{"hashtags":[],"urls":[{"url":"http:\/\/t.co\/XRvh1ZVw","expanded_url":"http:\/\/jase.im\/Ri7I0M","display_url":"jase.im\/Ri7I0M","indices":[112,132]}],"user_mentions":[{"screen_name":"imagemechanics","name":"Jason Cotterell","id":57271393,"id_str":"57271393","indices":[3,18]}]},"from_user":"_AaronNg","from_user_id":79771704,"from_user_id_str":"79771704","from_user_name":"NgChenChong","geo":null,"id":247677287108067328,"id_str":"247677287108067328","iso_language_code":"en","metadata":{"result_type":"recent"},"profile_image_url":"http:\/\/a0.twimg.com\/profile_images\/2523558403\/ek8mo4j4beq84iw28gjl_normal.jpeg","profile_image_url_https":"https:\/\/si0.twimg.com\/profile_images\/2523558403\/ek8mo4j4beq84iw28gjl_normal.jpeg","source":"<a href="http:\/\/twitter.com\/">web<\/a>","text":"RT @imagemechanics: iPhone 5 website teardown: How Apple compresses video using JPEG, JSON, and <canvas>.\nhttp:\/\/t.co\/XRvh1ZVw","to_user":null,"to_user_id":0,"to_user_id_str":"0","to_user_name":null}, ...
|
||||
[
|
||||
{
|
||||
"sha": "d25341478381063d1c76e81b3a52e0592a7c997f",
|
||||
"commit": {
|
||||
"author": {
|
||||
"name": "Stephen Dolan",
|
||||
"email": "mu@netsoc.tcd.ie",
|
||||
"date": "2013-06-22T16:30:59Z"
|
||||
},
|
||||
"committer": {
|
||||
"name": "Stephen Dolan",
|
||||
"email": "mu@netsoc.tcd.ie",
|
||||
"date": "2013-06-22T16:30:59Z"
|
||||
},
|
||||
"message": "Merge pull request #162 from stedolan/utf8-fixes\n\nUtf8 fixes. Closes #161",
|
||||
"tree": {
|
||||
"sha": "6ab697a8dfb5a96e124666bf6d6213822599fb40",
|
||||
"url": "https://api.github.com/repos/stedolan/jq/git/trees/6ab697a8dfb5a96e124666bf6d6213822599fb40"
|
||||
},
|
||||
"url": "https://api.github.com/repos/stedolan/jq/git/commits/d25341478381063d1c76e81b3a52e0592a7c997f",
|
||||
"comment_count": 0
|
||||
},
|
||||
"url": "https://api.github.com/repos/stedolan/jq/commits/d25341478381063d1c76e81b3a52e0592a7c997f",
|
||||
"html_url": "https://github.com/stedolan/jq/commit/d25341478381063d1c76e81b3a52e0592a7c997f",
|
||||
"comments_url": "https://api.github.com/repos/stedolan/jq/commits/d25341478381063d1c76e81b3a52e0592a7c997f/comments",
|
||||
"author": {
|
||||
"login": "stedolan",
|
||||
...
|
||||
|
||||
- text: |
|
||||
Github returns a ton of info here, and it's relatively hard to
|
||||
read as it is. To syntax highlight it and order the attributes we pipe
|
||||
it through jq, telling jq to just spit the input back at us using
|
||||
the expression `.`:
|
||||
|
||||
- command: "curl 'https://api.github.com/repos/stedolan/jq/commits?per_page=5' | jq '.'"
|
||||
result: |
|
||||
[
|
||||
{
|
||||
"parents": [
|
||||
{
|
||||
"html_url": "https://github.com/stedolan/jq/commit/54b9c9bdb225af5d886466d72f47eafc51acb4f7",
|
||||
"url": "https://api.github.com/repos/stedolan/jq/commits/54b9c9bdb225af5d886466d72f47eafc51acb4f7",
|
||||
"sha": "54b9c9bdb225af5d886466d72f47eafc51acb4f7"
|
||||
},
|
||||
{
|
||||
"html_url": "https://github.com/stedolan/jq/commit/8b1b503609c161fea4b003a7179b3fbb2dd4345a",
|
||||
"url": "https://api.github.com/repos/stedolan/jq/commits/8b1b503609c161fea4b003a7179b3fbb2dd4345a",
|
||||
"sha": "8b1b503609c161fea4b003a7179b3fbb2dd4345a"
|
||||
}
|
||||
],
|
||||
"committer": {
|
||||
"type": "User",
|
||||
"received_events_url": "https://api.github.com/users/stedolan/received_events",
|
||||
"events_url": "https://api.github.com/users/stedolan/events{/privacy}",
|
||||
"repos_url": "https://api.github.com/users/stedolan/repos",
|
||||
"organizations_url": "https://api.github.com/users/stedolan/orgs",
|
||||
...
|
||||
|
||||
- text: |
|
||||
|
||||
There's lots of info and no whitespace, so to make it a bit more
|
||||
legible we pipe it through jq, telling jq to just spit the input
|
||||
back at us using the expression `.`:
|
||||
There's still far too much info for our purposes, so we can try
|
||||
just grab the first commit:
|
||||
|
||||
|
||||
- command: "curl 'http://search.twitter.com/search.json?q=json&rpp=5&include_entities=true' | jq '.'"
|
||||
|
||||
- command: "curl 'https://api.github.com/repos/stedolan/jq/commits?per_page=5' | jq '.[0]'"
|
||||
result: |
|
||||
|
||||
{
|
||||
"since_id_str": "0",
|
||||
"since_id": 0,
|
||||
"results_per_page": 5,
|
||||
"completed_in": 0.108,
|
||||
"max_id": 247677287108067330,
|
||||
"max_id_str": "247677287108067328",
|
||||
"next_page": "?page=2&max_id=247677287108067328&q=json&rpp=5&include_entities=1",
|
||||
"page": 1,
|
||||
"query": "json",
|
||||
"refresh_url": "?since_id=247677287108067328&q=json&include_entities=1",
|
||||
"results": [
|
||||
"parents": [
|
||||
{
|
||||
"from_user_name": "NgChenChong",
|
||||
"from_user_id_str": "79771704",
|
||||
"from_user_id": 79771704,
|
||||
"from_user": "_AaronNg",
|
||||
"iso_language_code": "en",
|
||||
"text": "RT @imagemechanics: iPhone 5 website teardown: How Apple compresses video using JPEG, JSON, and <canvas>.\nhttp://t.co/XRvh1ZVw",
|
||||
"to_user": null
|
||||
/* lots more fields... */
|
||||
"html_url": "https://github.com/stedolan/jq/commit/54b9c9bdb225af5d886466d72f47eafc51acb4f7",
|
||||
"url": "https://api.github.com/repos/stedolan/jq/commits/54b9c9bdb225af5d886466d72f47eafc51acb4f7",
|
||||
"sha": "54b9c9bdb225af5d886466d72f47eafc51acb4f7"
|
||||
},
|
||||
/* lots more results... */
|
||||
]
|
||||
{
|
||||
"html_url": "https://github.com/stedolan/jq/commit/8b1b503609c161fea4b003a7179b3fbb2dd4345a",
|
||||
"url": "https://api.github.com/repos/stedolan/jq/commits/8b1b503609c161fea4b003a7179b3fbb2dd4345a",
|
||||
"sha": "8b1b503609c161fea4b003a7179b3fbb2dd4345a"
|
||||
}
|
||||
],
|
||||
"committer": {
|
||||
"type": "User",
|
||||
"received_events_url": "https://api.github.com/users/stedolan/received_events",
|
||||
"events_url": "https://api.github.com/users/stedolan/events{/privacy}",
|
||||
"repos_url": "https://api.github.com/users/stedolan/repos",
|
||||
"organizations_url": "https://api.github.com/users/stedolan/orgs",
|
||||
"subscriptions_url": "https://api.github.com/users/stedolan/subscriptions",
|
||||
"starred_url": "https://api.github.com/users/stedolan/starred{/owner}{/repo}",
|
||||
"gists_url": "https://api.github.com/users/stedolan/gists{/gist_id}",
|
||||
"login": "stedolan",
|
||||
"id": 79765,
|
||||
"avatar_url": "https://1.gravatar.com/avatar/31de909d8e55dd07ed782d92ece59842?d=https%3A%2F%2Fidenticons.github.com%2Ffc5b6765b1c9cfaecea48ae71df4d279.png",
|
||||
"gravatar_id": "31de909d8e55dd07ed782d92ece59842",
|
||||
"url": "https://api.github.com/users/stedolan",
|
||||
"html_url": "https://github.com/stedolan",
|
||||
"followers_url": "https://api.github.com/users/stedolan/followers",
|
||||
"following_url": "https://api.github.com/users/stedolan/following{/other_user}"
|
||||
},
|
||||
"author": {
|
||||
"type": "User",
|
||||
"received_events_url": "https://api.github.com/users/stedolan/received_events",
|
||||
"events_url": "https://api.github.com/users/stedolan/events{/privacy}",
|
||||
"repos_url": "https://api.github.com/users/stedolan/repos",
|
||||
"organizations_url": "https://api.github.com/users/stedolan/orgs",
|
||||
"subscriptions_url": "https://api.github.com/users/stedolan/subscriptions",
|
||||
"starred_url": "https://api.github.com/users/stedolan/starred{/owner}{/repo}",
|
||||
"gists_url": "https://api.github.com/users/stedolan/gists{/gist_id}",
|
||||
"login": "stedolan",
|
||||
"id": 79765,
|
||||
"avatar_url": "https://1.gravatar.com/avatar/31de909d8e55dd07ed782d92ece59842?d=https%3A%2F%2Fidenticons.github.com%2Ffc5b6765b1c9cfaecea48ae71df4d279.png",
|
||||
"gravatar_id": "31de909d8e55dd07ed782d92ece59842",
|
||||
"url": "https://api.github.com/users/stedolan",
|
||||
"html_url": "https://github.com/stedolan",
|
||||
"followers_url": "https://api.github.com/users/stedolan/followers",
|
||||
"following_url": "https://api.github.com/users/stedolan/following{/other_user}"
|
||||
},
|
||||
"comments_url": "https://api.github.com/repos/stedolan/jq/commits/d25341478381063d1c76e81b3a52e0592a7c997f/comments",
|
||||
"html_url": "https://github.com/stedolan/jq/commit/d25341478381063d1c76e81b3a52e0592a7c997f",
|
||||
"url": "https://api.github.com/repos/stedolan/jq/commits/d25341478381063d1c76e81b3a52e0592a7c997f",
|
||||
"commit": {
|
||||
"comment_count": 0,
|
||||
"url": "https://api.github.com/repos/stedolan/jq/git/commits/d25341478381063d1c76e81b3a52e0592a7c997f",
|
||||
"tree": {
|
||||
"url": "https://api.github.com/repos/stedolan/jq/git/trees/6ab697a8dfb5a96e124666bf6d6213822599fb40",
|
||||
"sha": "6ab697a8dfb5a96e124666bf6d6213822599fb40"
|
||||
},
|
||||
"message": "Merge pull request #162 from stedolan/utf8-fixes\n\nUtf8 fixes. Closes #161",
|
||||
"committer": {
|
||||
"date": "2013-06-22T16:30:59Z",
|
||||
"email": "mu@netsoc.tcd.ie",
|
||||
"name": "Stephen Dolan"
|
||||
},
|
||||
"author": {
|
||||
"date": "2013-06-22T16:30:59Z",
|
||||
"email": "mu@netsoc.tcd.ie",
|
||||
"name": "Stephen Dolan"
|
||||
}
|
||||
},
|
||||
"sha": "d25341478381063d1c76e81b3a52e0592a7c997f"
|
||||
}
|
||||
|
||||
|
||||
- text: |
|
||||
|
||||
Let's pull out the first tweet:
|
||||
|
||||
|
||||
- command: "curl 'http://search.twitter.com/search.json?q=json&rpp=5&include_entities=true' | jq '.results[0]'"
|
||||
result: |
|
||||
{
|
||||
"from_user_name": "NgChenChong",
|
||||
"from_user_id_str": "79771704",
|
||||
"from_user_id": 79771704,
|
||||
"from_user": "_AaronNg",
|
||||
"iso_language_code": "en",
|
||||
"text": "RT @imagemechanics: iPhone 5 website teardown: How Apple compresses video using JPEG, JSON, and <canvas>.\nhttp://t.co/XRvh1ZVw",
|
||||
"to_user": null
|
||||
/* lots more fields... */
|
||||
},
|
||||
|
||||
- text: |
|
||||
|
||||
For the rest of the examples, I'll leave out the `curl` command - it's not going to change.
|
||||
|
||||
|
||||
There's still a lot of info we don't care about there, so we'll
|
||||
restrict it down to the most interesting fields.
|
||||
|
||||
|
||||
- command: "jq '.results[0] | {from_user, text}'"
|
||||
|
||||
- command: "jq '.[0] | {message: .commit.message, name: .commit.committer.name}'"
|
||||
result: |
|
||||
{
|
||||
"text": "RT @imagemechanics: iPhone 5 website teardown: How Apple compresses video using JPEG, JSON, and <canvas>.\nhttp://t.co/XRvh1ZVw",
|
||||
"from_user": "_AaronNg"
|
||||
"name": "Stephen Dolan",
|
||||
"message": "Merge pull request #162 from stedolan/utf8-fixes\n\nUtf8 fixes. Closes #161"
|
||||
}
|
||||
|
||||
|
||||
- text: |
|
||||
|
||||
|
||||
The `|` operator in jq feeds the output of one filter
|
||||
(`.results[0]` which gets the first element of the results
|
||||
array) into the input of another (`{from_user, text}` which
|
||||
builds an object of those two fields).
|
||||
|
||||
Now let's get the rest of the tweets:
|
||||
|
||||
|
||||
(`.[0]` which gets the first element of the array in the response)
|
||||
into the input of another (`{...}` which
|
||||
builds an object out of those fields). You can access nested attributes,
|
||||
such as `.commit.message`:
|
||||
|
||||
- command: "jq '.results[] | {from_user, text}'"
|
||||
Now let's get the rest of the commits:
|
||||
|
||||
|
||||
- command: "jq '.[] | {message: .commit.message, name: .commit.committer.name}'"
|
||||
result: |
|
||||
{
|
||||
"text": "RT @imagemechanics: iPhone 5 website teardown: How Apple compresses video using JPEG, JSON, and <canvas>.\nhttp://t.co/XRvh1ZVw",
|
||||
"from_user": "_AaronNg"
|
||||
"name": "Stephen Dolan",
|
||||
"message": "Merge pull request #162 from stedolan/utf8-fixes\n\nUtf8 fixes. Closes #161"
|
||||
}
|
||||
{
|
||||
"text": "RT @_kud: iPhone 5 website teardown: How Apple compresses video using JPEG, JSON, and <canvas> -- http://t.co/Lhp92IqD",
|
||||
"from_user": "blouerat"
|
||||
"name": "Stephen Dolan",
|
||||
"message": "Reject all overlong UTF8 sequences."
|
||||
}
|
||||
{
|
||||
"text": "Dynamic Forms Mobile App by pmadiset: Few Forms details are hosted on our server in the form of JSON. This JSON ... http://t.co/7KALdWaX",
|
||||
"from_user": "cebu_iphone"
|
||||
"name": "Stephen Dolan",
|
||||
"message": "Fix various UTF8 parsing bugs.\n\nIn particular, parse bad UTF8 by replacing the broken bits with U+FFFD\nand resychronise correctly after broken sequences."
|
||||
}
|
||||
{
|
||||
"text": "iPhone 5 website insanity - Video compressing using JPEG, JSON, and <canvas> http://t.co/28Jesbio (Oh #Apple, U So #Awesome...)",
|
||||
"from_user": "dieswaytoofast"
|
||||
"name": "Stephen Dolan",
|
||||
"message": "Fix example in manual for `floor`. See #155."
|
||||
}
|
||||
{
|
||||
"text": "RT @umutm: A very nice web-based JSON editor - http://t.co/M70snaIf",
|
||||
"from_user": "Leolik"
|
||||
"name": "Nicolas Williams",
|
||||
"message": "Document floor"
|
||||
}
|
||||
|
||||
|
||||
|
||||
- text: |
|
||||
|
||||
`.results[]` returns each element of the results array, one at a
|
||||
time, which are all fed into `{from_user, text}`.
|
||||
|
||||
`.[]` returns each element of the array returned in the response,
|
||||
one at a time, which are all fed into `{message: .commit.message, name: .commit.committer.name}`.
|
||||
|
||||
|
||||
Data in jq is represented as streams of JSON values - every jq
|
||||
@@ -128,109 +210,108 @@ body:
|
||||
Streams are serialised by just separating JSON values with
|
||||
whitespace. This is a `cat`-friendly format - you can just join
|
||||
two JSON streams together and get a valid JSON stream.
|
||||
|
||||
|
||||
If you want to get the output as a single array, you can tell jq to
|
||||
"collect" all of the answers by wrapping the filter in square
|
||||
brackets:
|
||||
|
||||
- command: "jq '[.results[] | {from_user, text}]'"
|
||||
- command: "jq '[.[] | {message: .commit.message, name: .commit.committer.name}]'"
|
||||
result: |
|
||||
[
|
||||
{
|
||||
"text": "RT @imagemechanics: iPhone 5 website teardown: How Apple compresses video using JPEG, JSON, and <canvas>.\nhttp://t.co/XRvh1ZVw",
|
||||
"from_user": "_AaronNg"
|
||||
"name": "Stephen Dolan",
|
||||
"message": "Merge pull request #162 from stedolan/utf8-fixes\n\nUtf8 fixes. Closes #161"
|
||||
},
|
||||
{
|
||||
"text": "RT @_kud: iPhone 5 website teardown: How Apple compresses video using JPEG, JSON, and <canvas> -- http://t.co/Lhp92IqD",
|
||||
"from_user": "blouerat"
|
||||
"name": "Stephen Dolan",
|
||||
"message": "Reject all overlong UTF8 sequences."
|
||||
},
|
||||
{
|
||||
"text": "Dynamic Forms Mobile App by pmadiset: Few Forms details are hosted on our server in the form of JSON. This JSON ... http://t.co/7KALdWaX",
|
||||
"from_user": "cebu_iphone"
|
||||
"name": "Stephen Dolan",
|
||||
"message": "Fix various UTF8 parsing bugs.\n\nIn particular, parse bad UTF8 by replacing the broken bits with U+FFFD\nand resychronise correctly after broken sequences."
|
||||
},
|
||||
{
|
||||
"text": "iPhone 5 website insanity - Video compressing using JPEG, JSON, and <canvas> http://t.co/28Jesbio (Oh #Apple, U So #Awesome...)",
|
||||
"from_user": "dieswaytoofast"
|
||||
"name": "Stephen Dolan",
|
||||
"message": "Fix example in manual for `floor`. See #155."
|
||||
},
|
||||
{
|
||||
"text": "RT @umutm: A very nice web-based JSON editor - http://t.co/M70snaIf",
|
||||
"from_user": "Leolik"
|
||||
"name": "Nicolas Williams",
|
||||
"message": "Document floor"
|
||||
}
|
||||
]
|
||||
|
||||
|
||||
- text: |
|
||||
|
||||
- - -
|
||||
|
||||
Next, let's try getting the URLs out of those API results as
|
||||
well. In each tweet, the Twitter API includes a field called
|
||||
"entities" which looks like this:
|
||||
|
||||
{
|
||||
"user_mentions": [],
|
||||
"urls": [{
|
||||
"indices": [83, 103],
|
||||
"display_url": "bit.ly/StniqT",
|
||||
"expanded_url": "http://bit.ly/StniqT",
|
||||
"url": "http://t.co/28Jesbio"
|
||||
}],
|
||||
"hashtags": [
|
||||
{"indices": [108, 114], "text": "Apple" },
|
||||
{"indices": [121, 129], "text": "Awesome"}
|
||||
]
|
||||
}
|
||||
Next, let's try getting the URLs of the parent commits out of the
|
||||
API results as well. In each commit, the GitHub API includes information
|
||||
about "parent" commits. There can be one or many.
|
||||
|
||||
We want to pull out all of the "url" fields inside that array of url
|
||||
objects, and make a simple list of strings to go along with the
|
||||
"from_user" and "text" fields we already have.
|
||||
|
||||
|
||||
"parents": [
|
||||
{
|
||||
"html_url": "https://github.com/stedolan/jq/commit/54b9c9bdb225af5d886466d72f47eafc51acb4f7",
|
||||
"url": "https://api.github.com/repos/stedolan/jq/commits/54b9c9bdb225af5d886466d72f47eafc51acb4f7",
|
||||
"sha": "54b9c9bdb225af5d886466d72f47eafc51acb4f7"
|
||||
},
|
||||
{
|
||||
"html_url": "https://github.com/stedolan/jq/commit/8b1b503609c161fea4b003a7179b3fbb2dd4345a",
|
||||
"url": "https://api.github.com/repos/stedolan/jq/commits/8b1b503609c161fea4b003a7179b3fbb2dd4345a",
|
||||
"sha": "8b1b503609c161fea4b003a7179b3fbb2dd4345a"
|
||||
}
|
||||
]
|
||||
|
||||
- command: "jq '.results[] | {from_user, text, urls: [.entities.urls[].url]}'"
|
||||
We want to pull out all of the "html_url" fields inside that array of parent
|
||||
commits and make a simple list of strings to go along with the
|
||||
"message" and "author" fields we already have.
|
||||
|
||||
|
||||
|
||||
- command: "jq '[.[] | {message: .commit.message, name: .commit.committer.name, parents: [.parents[].html_url]}]'"
|
||||
result: |
|
||||
{
|
||||
"urls": [
|
||||
"http://t.co/XRvh1ZVw"
|
||||
],
|
||||
"text": "RT @imagemechanics: iPhone 5 website teardown: How Apple compresses video using JPEG, JSON, and <canvas>.\nhttp://t.co/XRvh1ZVw",
|
||||
"from_user": "_AaronNg"
|
||||
}
|
||||
{
|
||||
"urls": [
|
||||
"http://t.co/Lhp92IqD"
|
||||
],
|
||||
"text": "RT @_kud: iPhone 5 website teardown: How Apple compresses video using JPEG, JSON, and <canvas> -- http://t.co/Lhp92IqD",
|
||||
"from_user": "blouerat"
|
||||
}
|
||||
{
|
||||
"urls": [
|
||||
"http://t.co/7KALdWaX"
|
||||
],
|
||||
"text": "Dynamic Forms Mobile App by pmadiset: Few Forms details are hosted on our server in the form of JSON. This JSON ... http://t.co/7KALdWaX",
|
||||
"from_user": "cebu_iphone"
|
||||
}
|
||||
{
|
||||
"urls": [
|
||||
"http://t.co/28Jesbio"
|
||||
],
|
||||
"text": "iPhone 5 website insanity - Video compressing using JPEG, JSON, and <canvas> http://t.co/28Jesbio (Oh #Apple, U So #Awesome...)",
|
||||
"from_user": "dieswaytoofast"
|
||||
}
|
||||
{
|
||||
"urls": [
|
||||
"http://t.co/M70snaIf"
|
||||
],
|
||||
"text": "RT @umutm: A very nice web-based JSON editor - http://t.co/M70snaIf",
|
||||
"from_user": "Leolik"
|
||||
}
|
||||
|
||||
|
||||
[
|
||||
{
|
||||
"parents": [
|
||||
"https://github.com/stedolan/jq/commit/54b9c9bdb225af5d886466d72f47eafc51acb4f7",
|
||||
"https://github.com/stedolan/jq/commit/8b1b503609c161fea4b003a7179b3fbb2dd4345a"
|
||||
],
|
||||
"name": "Stephen Dolan",
|
||||
"message": "Merge pull request #162 from stedolan/utf8-fixes\n\nUtf8 fixes. Closes #161"
|
||||
},
|
||||
{
|
||||
"parents": [
|
||||
"https://github.com/stedolan/jq/commit/ff48bd6ec538b01d1057be8e93b94eef6914e9ef"
|
||||
],
|
||||
"name": "Stephen Dolan",
|
||||
"message": "Reject all overlong UTF8 sequences."
|
||||
},
|
||||
{
|
||||
"parents": [
|
||||
"https://github.com/stedolan/jq/commit/54b9c9bdb225af5d886466d72f47eafc51acb4f7"
|
||||
],
|
||||
"name": "Stephen Dolan",
|
||||
"message": "Fix various UTF8 parsing bugs.\n\nIn particular, parse bad UTF8 by replacing the broken bits with U+FFFD\nand resychronise correctly after broken sequences."
|
||||
},
|
||||
{
|
||||
"parents": [
|
||||
"https://github.com/stedolan/jq/commit/3dcdc582ea993afea3f5503a78a77675967ecdfa"
|
||||
],
|
||||
"name": "Stephen Dolan",
|
||||
"message": "Fix example in manual for `floor`. See #155."
|
||||
},
|
||||
{
|
||||
"parents": [
|
||||
"https://github.com/stedolan/jq/commit/7c4171d414f647ab08bcd20c76a4d8ed68d9c602"
|
||||
],
|
||||
"name": "Nicolas Williams",
|
||||
"message": "Document floor"
|
||||
}
|
||||
]
|
||||
|
||||
- text: |
|
||||
|
||||
Here we're making an object as before, but this time the urls
|
||||
field is being set to `[.entities.urls[].url]`, which collects
|
||||
all of the URLs defined in the entities object.
|
||||
|
||||
Here we're making an object as before, but this time the `parents`
|
||||
field is being set to `[.parents[].html_url]}]`, which collects
|
||||
all of the parent commit URLs defined in the parents object.
|
||||
|
||||
- text: |
|
||||
|
||||
@@ -238,4 +319,4 @@ body:
|
||||
|
||||
Here endeth the tutorial! There's lots more to play with, go
|
||||
read [the manual](../manual/) if you're interested, and [download
|
||||
jq](../download/) if you haven't already.
|
||||
jq](../download/) if you haven't already.
|
||||
@@ -16,7 +16,8 @@ body:
|
||||
|
||||
### Linux
|
||||
|
||||
* Binaries for [64-bit](linux64/jq) or [32-bit](linux32/jq) systems.
|
||||
* jq 1.4 binaries for [64-bit](linux64/jq) or [32-bit](linux32/jq) systems.
|
||||
* jq 1.3 binaries for [64-bit](linux64/jq-1.3/jq) or [32-bit](linux32/jq-1.3/jq) systems.
|
||||
|
||||
* For Arch users, a PKGBUILD for jq-1.1 is in the
|
||||
[AUR](https://aur.archlinux.org/packages.php?ID=63849), as
|
||||
@@ -27,22 +28,38 @@ body:
|
||||
manually by following instructions on [Arch Linux's
|
||||
Wiki](https://wiki.archlinux.org/index.php/Arch_UseRepository).
|
||||
|
||||
* jq 1.2 is [in Debian](http://packages.debian.org/jq)
|
||||
* jq 1.4 is [in Debian](http://packages.debian.org/jq)
|
||||
|
||||
* jq 1.3.2 is in the official Fedora repository. You can install using
|
||||
`sudo yum install jq`.
|
||||
|
||||
### OS X
|
||||
|
||||
* `brew install jq` using [homebrew](http://mxcl.github.com/homebrew/)
|
||||
|
||||
* Or, grab prebuilt [64-bit binaries](osx64/jq) or [32-bit
|
||||
* Or, grab prebuilt jq 1.4 [64-bit binaries](osx64/jq) or [32-bit
|
||||
binaries](osx32/jq)
|
||||
|
||||
* Or, grab prebuilt jq 1.3 [64-bit binaries](osx64/jq-1.3/jq) or [32-bit
|
||||
binaries](osx32/jq-1.3/jq)
|
||||
|
||||
### Solaris
|
||||
|
||||
* `pkgutil -i jq` in [OpenCSW](http://www.opencsw.org/p/jq) for Solaris 10+, Sparc and x86
|
||||
|
||||
* jq 1.4 executables for Solaris 11 [64-bit](solaris11-64/jq) or [32-bit](solaris11-32/jq)
|
||||
|
||||
### Windows
|
||||
|
||||
* Executables for [64-bit](win64/jq.exe) or [32-bit](win32/jq.exe)
|
||||
* `chocolatey install jq` using [Chocolatey NuGet](https://chocolatey.org/)
|
||||
|
||||
### From source on Linux or OS X
|
||||
* jq 1.4 executables for [64-bit](win64/jq.exe) or [32-bit](win32/jq.exe)
|
||||
|
||||
* [Source tarball for jq $JQ_VERSION](source/jq-$JQ_VERSION.tar.gz)
|
||||
* jq 1.3 executables for [64-bit](win64/jq-1.3/jq-1.3.exe) or [32-bit](win32/jq-1.3/jq-1.3.exe)
|
||||
|
||||
### From source on Linux, OS X, Cygwin, and other POSIX-like operating systems
|
||||
|
||||
* [Source tarball for jq 1.4](source/jq-1.4.tar.gz)
|
||||
|
||||
You can build it using the usual `./configure && make && sudo
|
||||
make install` rigmarole.
|
||||
@@ -51,7 +68,7 @@ body:
|
||||
|
||||
git clone https://github.com/stedolan/jq.git
|
||||
cd jq
|
||||
autoreconf
|
||||
autoreconf -i
|
||||
./configure
|
||||
make
|
||||
sudo make install
|
||||
@@ -76,15 +93,21 @@ body:
|
||||
or [MacPorts](http://macports.org/).
|
||||
|
||||
`flex` and `bison` are used to generate the lexer and parser for
|
||||
jq, and some python scripts generate the UTF8 encoding tables
|
||||
needed for JSON parsing.
|
||||
jq. For jq 1.5, neither is required if you use `./configure
|
||||
--disable-maintainer-mode` instead of `./configure`. This flag says to use
|
||||
the pre-generated lexer and parser that come with the code.
|
||||
|
||||
#### Building the documentation
|
||||
|
||||
jq's documentation is compiled into static HTML using
|
||||
[Bonsai](http://www.tinytree.info). To view the documentation
|
||||
locally, run `rake serve` from the docs/ subdirectory. You'll
|
||||
need a few Ruby dependencies, which can be installed by
|
||||
following the instructions in `docs/README.md`.
|
||||
[Bonsai](http://www.tinytree.info). To view the documentation
|
||||
locally, run `rake serve` (or `bundle exec rake serve`) from the
|
||||
docs/ subdirectory. To build the docs just `rake build` from the
|
||||
docs subdirectory. You'll need a few Ruby dependencies, which can
|
||||
be installed by following the instructions in `docs/README.md`.
|
||||
|
||||
The man page is built by `make jq.1`, or just `make`, also from
|
||||
the YAML docs, and you'll still need the Ruby dependencies to
|
||||
build the manpage.
|
||||
|
||||
|
||||
+1779
-201
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
@@ -1,9 +1,9 @@
|
||||
headline: jq
|
||||
|
||||
blurb: |
|
||||
|
||||
|
||||
jq is a lightweight and flexible command-line JSON processor.
|
||||
|
||||
|
||||
body1: |
|
||||
|
||||
jq is like `sed` for JSON data - you can use it to slice and filter
|
||||
@@ -14,21 +14,37 @@ body2: |
|
||||
|
||||
jq is written in portable C, and it has zero runtime
|
||||
dependencies. You can download a single binary, `scp` it to a far away
|
||||
machine, and expect it to work.
|
||||
machine of the same type, and expect it to work.
|
||||
|
||||
body3: |
|
||||
|
||||
jq can mangle the data format that you have into the one that you
|
||||
want with very little effort, and the program to do so is often
|
||||
shorter and simpler than you'd expect.
|
||||
|
||||
|
||||
tail: |
|
||||
|
||||
Go read the [tutorial](tutorial/) for more, or the [manual](manual/)
|
||||
for *way* more.
|
||||
for *way* more. Ask questions over on
|
||||
[stackoverflow](http://stackoverflow.com/) using the [jq
|
||||
tag](http://stackoverflow.com/questions/tagged/jq), or on the
|
||||
[#jq](http://irc.lc/freenode/%23jq/) channel over on
|
||||
[Freenode](https://webchat.freenode.net/).
|
||||
|
||||
news:
|
||||
- date: 2015-01-01
|
||||
body: |
|
||||
|
||||
jq 1.5rc1 Get it on the
|
||||
[releases](https://github.com/stedolan/jq/releases) page.
|
||||
|
||||
- date: 2014-06-08
|
||||
body: |
|
||||
|
||||
jq 1.4 (finally) released! Get it on the [download](download/) page.
|
||||
|
||||
- date: 2013-05-19
|
||||
body: |
|
||||
|
||||
jq 1.3 (finally) released! Get it on the [download](download/) page.
|
||||
|
||||
jq 1.3 released.
|
||||
|
||||
@@ -9,7 +9,7 @@ reducing and otherwise mangling JSON documents.
|
||||
This version of `jq` was built without a manual, so this manpage is a
|
||||
stub. For full documentation of the `jq` language, see:
|
||||
|
||||
http://stedolan.github.com/jq
|
||||
http://stedolan.github.io/jq
|
||||
|
||||
## BUGS
|
||||
|
||||
|
||||
+1
-1
@@ -4,7 +4,7 @@
|
||||
|
||||
# This line is modified by the Makefile. To change the version number,
|
||||
# edit the Autoconf version number at the top of configure.ac
|
||||
jq_version: "1.3"
|
||||
jq_version: "1.4-2-g15c4a7f-dirty"
|
||||
|
||||
root: '/jq'
|
||||
footer: |
|
||||
|
||||
Vendored
+10
-1
@@ -10,6 +10,7 @@
|
||||
<img src="{{root}}/jq.png" style="width: 400px; height: 220px; float: left; margin-right: 60px">
|
||||
<div id="blurb">
|
||||
{{blurb | markdownify}}
|
||||
|
||||
<div class="btn-group" style="float:right">
|
||||
<a class="btn btn-primary dropdown-toggle" data-toggle="dropdown" href="#">
|
||||
Download jq-{{jq_version}}
|
||||
@@ -18,10 +19,18 @@
|
||||
<ul class="dropdown-menu">
|
||||
<li><a href="{{root}}/download/linux64/jq">Linux (64-bit)</a></li>
|
||||
<li><a href="{{root}}/download/osx64/jq">OS X (64-bit)</a></li>
|
||||
<li><a href="{{root}}/download/">Other platforms and source</a></li>
|
||||
<li><a href="{{root}}/download/solaris11-64/jq">Solaris (64-bit)</a></li>
|
||||
<li><a href="{{root}}/download/win64/jq.exe">Windows (64-bit)</a></li>
|
||||
<li><a href="{{root}}/download/">Other platforms, older versions, and source</a></li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<div class="btn-group" style="float:right">
|
||||
<a class="btn btn-primary" href="http://jqplay.org">
|
||||
Try online at jqplay.org!
|
||||
</a>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
<div style="clear:both;"></div>
|
||||
</div>
|
||||
|
||||
Vendored
+15
-3
@@ -35,6 +35,17 @@
|
||||
}
|
||||
});
|
||||
});
|
||||
// add "Run" button to execute examples on jqplay.org
|
||||
$(function() {
|
||||
$.each($('table.manual-example'), function(index, value) {
|
||||
$value = $(value)
|
||||
var j = $value.find('tr:nth-child(2) td:first').text();
|
||||
var q = $value.find('.jqprogram').text().replace(/^jq /, '').replace(/^'(.+)'$/, '$1');
|
||||
var url = 'https://jqplay.org/jq?q=' + encodeURIComponent(q) +'&j=' + encodeURIComponent(j)
|
||||
var $last_tr = $value.find('tr:last');
|
||||
$last_tr.after('<tr><th><a href="' + url + '" class="btn btn-primary btn-small">Run</a></th><th></th></tr><tr><th></th><th></th></tr>');
|
||||
});
|
||||
});
|
||||
</script>
|
||||
<input type="text"
|
||||
class="input-medium search-query"
|
||||
@@ -46,6 +57,7 @@
|
||||
</div>
|
||||
<div class="span9">
|
||||
<h1>{{headline}}</h1>
|
||||
{{ history | markdownify }}
|
||||
{{ body | markdownify }}
|
||||
{% for section in sections %}
|
||||
<section id="{{section.title | sanitize}}">
|
||||
@@ -70,8 +82,8 @@
|
||||
|
||||
{% for example in entry.examples %}
|
||||
<table class="manual-example">
|
||||
<tr><th></th><td class="jqprogram">jq '{{example.program}}'</td></tr>
|
||||
<tr><th>Input</th><td>{{example.input}}</td></tr>
|
||||
<tr><th></th><td class="jqprogram">jq '{{example.program | escape}}'</td></tr>
|
||||
<tr><th>Input</th><td>{{example.input | escape}}</td></tr>
|
||||
{% unless example.output[0] %}
|
||||
<tr>
|
||||
<th>Output</th>
|
||||
@@ -85,7 +97,7 @@
|
||||
{% else %}
|
||||
<th></th>
|
||||
{% endif %}
|
||||
<td>{{output}}</td>
|
||||
<td>{{output | escape}}</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
|
||||
|
||||
Vendored
+2
@@ -17,6 +17,8 @@
|
||||
{% endfor %}
|
||||
<li><a href="https://github.com/stedolan/jq/issues">Issues</a></li>
|
||||
<li><a href="https://github.com/stedolan/jq">Source</a></li>
|
||||
<li><a href="http://jqplay.org">Try online!</a></li>
|
||||
<li><a href="https://raw.githubusercontent.com/stedolan/jq/master/NEWS">News</a></li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
+32
-2
@@ -4,6 +4,36 @@
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "jv_alloc.h"
|
||||
|
||||
/*
|
||||
* The stack is a directed forest of variably sized blocks. Each block has a
|
||||
* "next" block which is at a higher memory address, or 0 if the block has no
|
||||
* "next" block. More than one block may have no "next" block. A block may be
|
||||
* the "next" block of more than one other block. Pushed blocks are added at
|
||||
* the low-address end of the stack.
|
||||
*
|
||||
* Stack pointers are negative integers that are offsets relative to "mem_end",
|
||||
* the end of the allocated region. The stack "bound" is the stack pointer of
|
||||
* the last block that would be able to fit in the currently allocated region.
|
||||
* The stack "limit" is the stack pointer of the last block currently in the
|
||||
* stack. The stack pointer of the "next" block is stored directly below each
|
||||
* block.
|
||||
*
|
||||
* <- mem_end = 0x100
|
||||
* 0xF8 +------------+
|
||||
* 0xF0 | |
|
||||
* 0xE8 +------------+ <- stack_ptr1 = -0x18
|
||||
* 0xE0 next = 0
|
||||
* 0xD8 +------------+
|
||||
* 0xD0 | |
|
||||
* 0xC8 | |
|
||||
* 0xC0 +------------+ <- stack_ptr2 = limit = -0x40
|
||||
* 0xB8 next = -0x18
|
||||
* 0xB0
|
||||
* 0xA8 <- bound = -0x58
|
||||
* 0xA0
|
||||
*/
|
||||
|
||||
struct determine_alignment {
|
||||
char x;
|
||||
@@ -51,9 +81,9 @@ static stack_ptr* stack_block_next(struct stack* s, stack_ptr p) {
|
||||
static void stack_reallocate(struct stack* s, size_t sz) {
|
||||
int old_mem_length = -(s->bound) + ALIGNMENT;
|
||||
char* old_mem_start = s->mem_end - old_mem_length;
|
||||
|
||||
|
||||
int new_mem_length = align_round_up((old_mem_length + sz + 256) * 2);
|
||||
char* new_mem_start = realloc(old_mem_start, new_mem_length);
|
||||
char* new_mem_start = jv_mem_realloc(old_mem_start, new_mem_length);
|
||||
memmove(new_mem_start + (new_mem_length - old_mem_length),
|
||||
new_mem_start, old_mem_length);
|
||||
s->mem_end = new_mem_start + new_mem_length;
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include "exec_stack.h"
|
||||
#include "bytecode.h"
|
||||
@@ -13,40 +16,55 @@
|
||||
#include "jq.h"
|
||||
#include "parser.h"
|
||||
#include "builtin.h"
|
||||
#include "util.h"
|
||||
#include "linker.h"
|
||||
|
||||
struct jq_state {
|
||||
void (*nomem_handler)(void *);
|
||||
void *nomem_handler_data;
|
||||
struct bytecode* bc;
|
||||
|
||||
jq_msg_cb err_cb;
|
||||
void *err_cb_data;
|
||||
jv error;
|
||||
|
||||
struct stack stk;
|
||||
stack_ptr curr_frame;
|
||||
stack_ptr stk_top;
|
||||
stack_ptr fork_top;
|
||||
|
||||
jv path;
|
||||
jv value_at_path;
|
||||
int subexp_nest;
|
||||
int debug_trace_enabled;
|
||||
int initial_execution;
|
||||
unsigned next_label;
|
||||
|
||||
jv attrs;
|
||||
jq_input_cb input_cb;
|
||||
void *input_cb_data;
|
||||
jq_msg_cb debug_cb;
|
||||
void *debug_cb_data;
|
||||
};
|
||||
|
||||
struct closure {
|
||||
struct bytecode* bc;
|
||||
stack_ptr env;
|
||||
struct bytecode* bc; // jq bytecode
|
||||
stack_ptr env; // jq stack address of closed frame
|
||||
};
|
||||
|
||||
// locals for any function called: either a closure or a local variable
|
||||
union frame_entry {
|
||||
struct closure closure;
|
||||
jv localvar;
|
||||
};
|
||||
|
||||
// jq function call frame
|
||||
struct frame {
|
||||
struct bytecode* bc;
|
||||
stack_ptr env;
|
||||
stack_ptr retdata;
|
||||
uint16_t* retaddr;
|
||||
/* bc->nclosures closures followed by bc->nlocals local variables */
|
||||
union frame_entry entries[0];
|
||||
struct bytecode* bc; // jq bytecode for callee
|
||||
stack_ptr env; // jq stack address of frame to return to
|
||||
stack_ptr retdata; // jq stack address to unwind to on RET
|
||||
uint16_t* retaddr; // jq bytecode return address
|
||||
union frame_entry entries[0]; // nclosures + nlocals
|
||||
};
|
||||
|
||||
static int frame_size(struct bytecode* bc) {
|
||||
@@ -89,12 +107,16 @@ static struct closure make_closure(struct jq_state* jq, uint16_t* pc) {
|
||||
stack_ptr fridx = frame_get_level(jq, level);
|
||||
struct frame* fr = stack_block(&jq->stk, fridx);
|
||||
if (idx & ARG_NEWCLOSURE) {
|
||||
// A new closure closing the frame identified by level, and with
|
||||
// the bytecode body of the idx'th subfunction of that frame
|
||||
int subfn_idx = idx & ~ARG_NEWCLOSURE;
|
||||
assert(subfn_idx < fr->bc->nsubfunctions);
|
||||
struct closure cl = {fr->bc->subfunctions[subfn_idx],
|
||||
fridx};
|
||||
return cl;
|
||||
} else {
|
||||
// A reference to a closure from the frame identified by level; copy
|
||||
// it as-is
|
||||
int closure = idx;
|
||||
assert(closure >= 0);
|
||||
assert(closure < fr->bc->nclosures);
|
||||
@@ -102,7 +124,7 @@ static struct closure make_closure(struct jq_state* jq, uint16_t* pc) {
|
||||
}
|
||||
}
|
||||
|
||||
static struct frame* frame_push(struct jq_state* jq, struct closure callee,
|
||||
static struct frame* frame_push(struct jq_state* jq, struct closure callee,
|
||||
uint16_t* argdef, int nargs) {
|
||||
stack_ptr new_frame_idx = stack_push_block(&jq->stk, jq->curr_frame, frame_size(callee.bc));
|
||||
struct frame* new_frame = stack_block(&jq->stk, new_frame_idx);
|
||||
@@ -152,11 +174,25 @@ jv stack_pop(jq_state *jq) {
|
||||
return val;
|
||||
}
|
||||
|
||||
// Like stack_pop(), but assert !stack_pop_will_free() and replace with
|
||||
// jv_null() on the stack.
|
||||
jv stack_popn(jq_state *jq) {
|
||||
jv* sval = stack_block(&jq->stk, jq->stk_top);
|
||||
jv val = *sval;
|
||||
if (!stack_pop_will_free(&jq->stk, jq->stk_top)) {
|
||||
*sval = jv_null();
|
||||
}
|
||||
jq->stk_top = stack_pop_block(&jq->stk, jq->stk_top, sizeof(jv));
|
||||
assert(jv_is_valid(val));
|
||||
return val;
|
||||
}
|
||||
|
||||
|
||||
struct forkpoint {
|
||||
stack_ptr saved_data_stack;
|
||||
stack_ptr saved_curr_frame;
|
||||
int path_len, subexp_nest;
|
||||
jv value_at_path;
|
||||
uint16_t* return_address;
|
||||
};
|
||||
|
||||
@@ -174,22 +210,35 @@ void stack_save(jq_state *jq, uint16_t* retaddr, struct stack_pos sp){
|
||||
struct forkpoint* fork = stack_block(&jq->stk, jq->fork_top);
|
||||
fork->saved_data_stack = jq->stk_top;
|
||||
fork->saved_curr_frame = jq->curr_frame;
|
||||
fork->path_len =
|
||||
fork->path_len =
|
||||
jv_get_kind(jq->path) == JV_KIND_ARRAY ? jv_array_length(jv_copy(jq->path)) : 0;
|
||||
fork->value_at_path = jv_copy(jq->value_at_path);
|
||||
fork->subexp_nest = jq->subexp_nest;
|
||||
fork->return_address = retaddr;
|
||||
jq->stk_top = sp.saved_data_stack;
|
||||
jq->curr_frame = sp.saved_curr_frame;
|
||||
}
|
||||
|
||||
void path_append(jq_state* jq, jv component) {
|
||||
static int path_intact(jq_state *jq, jv curr) {
|
||||
if (jq->subexp_nest == 0 && jv_get_kind(jq->path) == JV_KIND_ARRAY) {
|
||||
return jv_identical(curr, jv_copy(jq->value_at_path));
|
||||
} else {
|
||||
jv_free(curr);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
static void path_append(jq_state* jq, jv component, jv value_at_path) {
|
||||
if (jq->subexp_nest == 0 && jv_get_kind(jq->path) == JV_KIND_ARRAY) {
|
||||
int n1 = jv_array_length(jv_copy(jq->path));
|
||||
jq->path = jv_array_append(jq->path, component);
|
||||
int n2 = jv_array_length(jv_copy(jq->path));
|
||||
assert(n2 == n1 + 1);
|
||||
jv_free(jq->value_at_path);
|
||||
jq->value_at_path = value_at_path;
|
||||
} else {
|
||||
jv_free(component);
|
||||
jv_free(value_at_path);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -219,6 +268,8 @@ uint16_t* stack_restore(jq_state *jq){
|
||||
} else {
|
||||
assert(path_len == 0);
|
||||
}
|
||||
jv_free(jq->value_at_path);
|
||||
jq->value_at_path = fork->value_at_path;
|
||||
jq->subexp_nest = fork->subexp_nest;
|
||||
jq->fork_top = stack_pop_block(&jq->stk, jq->fork_top, sizeof(struct forkpoint));
|
||||
return retaddr;
|
||||
@@ -231,24 +282,29 @@ static void jq_reset(jq_state *jq) {
|
||||
assert(jq->fork_top == 0);
|
||||
assert(jq->curr_frame == 0);
|
||||
stack_reset(&jq->stk);
|
||||
jv_free(jq->error);
|
||||
jq->error = jv_null();
|
||||
|
||||
if (jv_get_kind(jq->path) != JV_KIND_INVALID)
|
||||
jv_free(jq->path);
|
||||
jq->path = jv_null();
|
||||
jv_free(jq->value_at_path);
|
||||
jq->value_at_path = jv_null();
|
||||
jq->subexp_nest = 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void print_error(jv value) {
|
||||
assert(!jv_is_valid(value));
|
||||
jv msg = jv_invalid_get_msg(value);
|
||||
if (jv_get_kind(msg) == JV_KIND_STRING) {
|
||||
fprintf(stderr, "jq: error: %s\n", jv_string_value(msg));
|
||||
}
|
||||
jv_free(msg);
|
||||
void jq_report_error(jq_state *jq, jv value) {
|
||||
assert(jq->err_cb);
|
||||
// callback must jv_free() its jv argument
|
||||
jq->err_cb(jq->err_cb_data, value);
|
||||
}
|
||||
|
||||
static void set_error(jq_state *jq, jv value) {
|
||||
// Record so try/catch can find it.
|
||||
jv_free(jq->error);
|
||||
jq->error = value;
|
||||
}
|
||||
|
||||
#define ON_BACKTRACK(op) ((op)+NUM_OPCODES)
|
||||
|
||||
jv jq_next(jq_state *jq) {
|
||||
@@ -259,10 +315,13 @@ jv jq_next(jq_state *jq) {
|
||||
uint16_t* pc = stack_restore(jq);
|
||||
assert(pc);
|
||||
|
||||
int raising;
|
||||
int backtracking = !jq->initial_execution;
|
||||
jq->initial_execution = 0;
|
||||
assert(jv_get_kind(jq->error) == JV_KIND_NULL);
|
||||
while (1) {
|
||||
uint16_t opcode = *pc;
|
||||
raising = 0;
|
||||
|
||||
if (jq->debug_trace_enabled) {
|
||||
dump_operation(frame_current(jq)->bc, pc);
|
||||
@@ -280,7 +339,7 @@ jv jq_next(jq_state *jq) {
|
||||
param = *stack_block_next(&jq->stk, param);
|
||||
}
|
||||
if (!param) break;
|
||||
jv_dump(jv_copy(*(jv*)stack_block(&jq->stk, param)), 0);
|
||||
jv_dump(jv_copy(*(jv*)stack_block(&jq->stk, param)), JV_PRINT_REFCOUNT);
|
||||
//printf("<%d>", jv_get_refcnt(param->val));
|
||||
//printf(" -- ");
|
||||
//jv_dump(jv_copy(jq->path), 0);
|
||||
@@ -295,12 +354,15 @@ jv jq_next(jq_state *jq) {
|
||||
if (backtracking) {
|
||||
opcode = ON_BACKTRACK(opcode);
|
||||
backtracking = 0;
|
||||
raising = !jv_is_valid(jq->error);
|
||||
}
|
||||
pc++;
|
||||
|
||||
switch (opcode) {
|
||||
default: assert(0 && "invalid instruction");
|
||||
|
||||
case TOP: break;
|
||||
|
||||
case LOADK: {
|
||||
jv v = jv_array_get(jv_copy(frame_current(jq)->bc->constants), *pc++);
|
||||
assert(jv_is_valid(v));
|
||||
@@ -309,6 +371,11 @@ jv jq_next(jq_state *jq) {
|
||||
break;
|
||||
}
|
||||
|
||||
case GENLABEL: {
|
||||
stack_push(jq, JV_OBJECT(jv_string("__jq"), jv_number(jq->next_label++)));
|
||||
break;
|
||||
}
|
||||
|
||||
case DUP: {
|
||||
jv v = stack_pop(jq);
|
||||
stack_push(jq, jv_copy(v));
|
||||
@@ -316,6 +383,13 @@ jv jq_next(jq_state *jq) {
|
||||
break;
|
||||
}
|
||||
|
||||
case DUPN: {
|
||||
jv v = stack_popn(jq);
|
||||
stack_push(jq, jv_copy(v));
|
||||
stack_push(jq, v);
|
||||
break;
|
||||
}
|
||||
|
||||
case DUP2: {
|
||||
jv keep = stack_pop(jq);
|
||||
jv v = stack_pop(jq);
|
||||
@@ -342,7 +416,7 @@ jv jq_next(jq_state *jq) {
|
||||
stack_push(jq, b);
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
case POP: {
|
||||
jv_free(stack_pop(jq));
|
||||
break;
|
||||
@@ -368,8 +442,10 @@ jv jq_next(jq_state *jq) {
|
||||
stack_push(jq, jv_object_set(objv, k, v));
|
||||
stack_push(jq, stktop);
|
||||
} else {
|
||||
print_error(jv_invalid_with_msg(jv_string_fmt("Cannot use %s as object key",
|
||||
jv_kind_name(jv_get_kind(k)))));
|
||||
char errbuf[15];
|
||||
set_error(jq, jv_invalid_with_msg(jv_string_fmt("Cannot use %s (%s) as object key",
|
||||
jv_kind_name(jv_get_kind(k)),
|
||||
jv_dump_string_trunc(jv_copy(k), errbuf, sizeof(errbuf)))));
|
||||
jv_free(stktop);
|
||||
jv_free(v);
|
||||
jv_free(k);
|
||||
@@ -385,9 +461,10 @@ jv jq_next(jq_state *jq) {
|
||||
uint16_t v = *pc++;
|
||||
jv* var = frame_local_var(jq, v, level);
|
||||
jv max = stack_pop(jq);
|
||||
if (raising) goto do_backtrack;
|
||||
if (jv_get_kind(*var) != JV_KIND_NUMBER ||
|
||||
jv_get_kind(max) != JV_KIND_NUMBER) {
|
||||
print_error(jv_invalid_with_msg(jv_string_fmt("Range bounds must be numeric")));
|
||||
set_error(jq, jv_invalid_with_msg(jv_string_fmt("Range bounds must be numeric")));
|
||||
jv_free(max);
|
||||
goto do_backtrack;
|
||||
} else if (jv_number_value(jv_copy(*var)) >= jv_number_value(jv_copy(max))) {
|
||||
@@ -414,7 +491,7 @@ jv jq_next(jq_state *jq) {
|
||||
if (jq->debug_trace_enabled) {
|
||||
printf("V%d = ", v);
|
||||
jv_dump(jv_copy(*var), 0);
|
||||
printf("\n");
|
||||
printf(" (%d)\n", jv_get_refcnt(*var));
|
||||
}
|
||||
jv_free(stack_pop(jq));
|
||||
stack_push(jq, jv_copy(*var));
|
||||
@@ -429,9 +506,9 @@ jv jq_next(jq_state *jq) {
|
||||
if (jq->debug_trace_enabled) {
|
||||
printf("V%d = ", v);
|
||||
jv_dump(jv_copy(*var), 0);
|
||||
printf("\n");
|
||||
printf(" (%d)\n", jv_get_refcnt(*var));
|
||||
}
|
||||
jv_free(stack_pop(jq));
|
||||
jv_free(stack_popn(jq));
|
||||
stack_push(jq, *var);
|
||||
*var = jv_null();
|
||||
break;
|
||||
@@ -445,7 +522,26 @@ jv jq_next(jq_state *jq) {
|
||||
if (jq->debug_trace_enabled) {
|
||||
printf("V%d = ", v);
|
||||
jv_dump(jv_copy(val), 0);
|
||||
printf("\n");
|
||||
printf(" (%d)\n", jv_get_refcnt(val));
|
||||
}
|
||||
jv_free(*var);
|
||||
*var = val;
|
||||
break;
|
||||
}
|
||||
|
||||
case STORE_GLOBAL: {
|
||||
// Get the constant
|
||||
jv val = jv_array_get(jv_copy(frame_current(jq)->bc->constants), *pc++);
|
||||
assert(jv_is_valid(val));
|
||||
|
||||
// Store the var
|
||||
uint16_t level = *pc++;
|
||||
uint16_t v = *pc++;
|
||||
jv* var = frame_local_var(jq, v, level);
|
||||
if (jq->debug_trace_enabled) {
|
||||
printf("V%d = ", v);
|
||||
jv_dump(jv_copy(val), 0);
|
||||
printf(" (%d)\n", jv_get_refcnt(val));
|
||||
}
|
||||
jv_free(*var);
|
||||
*var = val;
|
||||
@@ -459,17 +555,29 @@ jv jq_next(jq_state *jq) {
|
||||
stack_save(jq, pc - 1, stack_get_pos(jq));
|
||||
|
||||
stack_push(jq, jv_number(jq->subexp_nest));
|
||||
stack_push(jq, v);
|
||||
stack_push(jq, jq->value_at_path);
|
||||
stack_push(jq, jv_copy(v));
|
||||
|
||||
jq->path = jv_array();
|
||||
jq->value_at_path = v; // next INDEX operation must index into v
|
||||
jq->subexp_nest = 0;
|
||||
break;
|
||||
}
|
||||
|
||||
case PATH_END: {
|
||||
jv v = stack_pop(jq);
|
||||
// detect invalid path expression like path(.a | reverse)
|
||||
if (!path_intact(jq, jv_copy(v))) {
|
||||
char errbuf[30];
|
||||
jv msg = jv_string_fmt(
|
||||
"Invalid path expression with result %s",
|
||||
jv_dump_string_trunc(v, errbuf, sizeof(errbuf)));
|
||||
set_error(jq, jv_invalid_with_msg(msg));
|
||||
goto do_backtrack;
|
||||
}
|
||||
jv_free(v); // discard value, only keep path
|
||||
|
||||
jv old_value_at_path = stack_pop(jq);
|
||||
int old_subexp_nest = (int)jv_number_value(stack_pop(jq));
|
||||
|
||||
jv path = jq->path;
|
||||
@@ -481,6 +589,8 @@ jv jq_next(jq_state *jq) {
|
||||
|
||||
stack_push(jq, path);
|
||||
jq->subexp_nest = old_subexp_nest;
|
||||
jv_free(jq->value_at_path);
|
||||
jq->value_at_path = old_value_at_path;
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -491,15 +601,31 @@ jv jq_next(jq_state *jq) {
|
||||
goto do_backtrack;
|
||||
}
|
||||
|
||||
case INDEX: {
|
||||
case INDEX:
|
||||
case INDEX_OPT: {
|
||||
jv t = stack_pop(jq);
|
||||
jv k = stack_pop(jq);
|
||||
path_append(jq, jv_copy(k));
|
||||
jv v = jv_get(t, k);
|
||||
// detect invalid path expression like path(reverse | .a)
|
||||
if (!path_intact(jq, jv_copy(t))) {
|
||||
char keybuf[15];
|
||||
char objbuf[30];
|
||||
jv msg = jv_string_fmt(
|
||||
"Invalid path expression near attempt to access element %s of %s",
|
||||
jv_dump_string_trunc(k, keybuf, sizeof(keybuf)),
|
||||
jv_dump_string_trunc(t, objbuf, sizeof(objbuf)));
|
||||
set_error(jq, jv_invalid_with_msg(msg));
|
||||
goto do_backtrack;
|
||||
}
|
||||
jv v = jv_get(t, jv_copy(k));
|
||||
if (jv_is_valid(v)) {
|
||||
path_append(jq, k, jv_copy(v));
|
||||
stack_push(jq, v);
|
||||
} else {
|
||||
print_error(v);
|
||||
jv_free(k);
|
||||
if (opcode == INDEX)
|
||||
set_error(jq, v);
|
||||
else
|
||||
jv_free(v);
|
||||
goto do_backtrack;
|
||||
}
|
||||
break;
|
||||
@@ -523,17 +649,31 @@ jv jq_next(jq_state *jq) {
|
||||
break;
|
||||
}
|
||||
|
||||
case EACH:
|
||||
case EACH:
|
||||
case EACH_OPT: {
|
||||
jv container = stack_pop(jq);
|
||||
// detect invalid path expression like path(reverse | .[])
|
||||
if (!path_intact(jq, jv_copy(container))) {
|
||||
char errbuf[30];
|
||||
jv msg = jv_string_fmt(
|
||||
"Invalid path expression near attempt to iterate through %s",
|
||||
jv_dump_string_trunc(container, errbuf, sizeof(errbuf)));
|
||||
set_error(jq, jv_invalid_with_msg(msg));
|
||||
goto do_backtrack;
|
||||
}
|
||||
stack_push(jq, container);
|
||||
stack_push(jq, jv_number(-1));
|
||||
// fallthrough
|
||||
case ON_BACKTRACK(EACH): {
|
||||
}
|
||||
case ON_BACKTRACK(EACH):
|
||||
case ON_BACKTRACK(EACH_OPT): {
|
||||
int idx = jv_number_value(stack_pop(jq));
|
||||
jv container = stack_pop(jq);
|
||||
|
||||
int keep_going, is_last = 0;
|
||||
jv key, value;
|
||||
if (jv_get_kind(container) == JV_KIND_ARRAY) {
|
||||
if (opcode == EACH) idx = 0;
|
||||
if (opcode == EACH || opcode == EACH_OPT) idx = 0;
|
||||
else idx = idx + 1;
|
||||
int len = jv_array_length(jv_copy(container));
|
||||
keep_going = idx < len;
|
||||
@@ -543,7 +683,7 @@ jv jq_next(jq_state *jq) {
|
||||
value = jv_array_get(jv_copy(container), idx);
|
||||
}
|
||||
} else if (jv_get_kind(container) == JV_KIND_OBJECT) {
|
||||
if (opcode == EACH) idx = jv_object_iter(container);
|
||||
if (opcode == EACH || opcode == EACH_OPT) idx = jv_object_iter(container);
|
||||
else idx = jv_object_iter_next(container, idx);
|
||||
keep_going = jv_object_iter_valid(container, idx);
|
||||
if (keep_going) {
|
||||
@@ -551,26 +691,33 @@ jv jq_next(jq_state *jq) {
|
||||
value = jv_object_iter_value(container, idx);
|
||||
}
|
||||
} else {
|
||||
assert(opcode == EACH);
|
||||
print_error(jv_invalid_with_msg(jv_string_fmt("Cannot iterate over %s",
|
||||
jv_kind_name(jv_get_kind(container)))));
|
||||
assert(opcode == EACH || opcode == EACH_OPT);
|
||||
if (opcode == EACH) {
|
||||
char errbuf[15];
|
||||
set_error(jq,
|
||||
jv_invalid_with_msg(jv_string_fmt("Cannot iterate over %s (%s)",
|
||||
jv_kind_name(jv_get_kind(container)),
|
||||
jv_dump_string_trunc(jv_copy(container), errbuf, sizeof(errbuf)))));
|
||||
}
|
||||
keep_going = 0;
|
||||
}
|
||||
|
||||
if (!keep_going) {
|
||||
if (!keep_going || raising) {
|
||||
if (keep_going)
|
||||
jv_free(value);
|
||||
jv_free(container);
|
||||
goto do_backtrack;
|
||||
} else if (is_last) {
|
||||
// we don't need to make a backtrack point
|
||||
jv_free(container);
|
||||
path_append(jq, key);
|
||||
path_append(jq, key, jv_copy(value));
|
||||
stack_push(jq, value);
|
||||
} else {
|
||||
struct stack_pos spos = stack_get_pos(jq);
|
||||
stack_push(jq, container);
|
||||
stack_push(jq, jv_number(idx));
|
||||
stack_save(jq, pc - 1, spos);
|
||||
path_append(jq, key);
|
||||
path_append(jq, key, jv_copy(value));
|
||||
stack_push(jq, value);
|
||||
}
|
||||
break;
|
||||
@@ -580,24 +727,45 @@ jv jq_next(jq_state *jq) {
|
||||
case BACKTRACK: {
|
||||
pc = stack_restore(jq);
|
||||
if (!pc) {
|
||||
if (!jv_is_valid(jq->error)) {
|
||||
jv error = jq->error;
|
||||
jq->error = jv_null();
|
||||
return error;
|
||||
}
|
||||
return jv_invalid();
|
||||
}
|
||||
backtracking = 1;
|
||||
break;
|
||||
}
|
||||
|
||||
case FORK_OPT:
|
||||
case FORK: {
|
||||
stack_save(jq, pc - 1, stack_get_pos(jq));
|
||||
pc++; // skip offset this time
|
||||
break;
|
||||
}
|
||||
|
||||
case ON_BACKTRACK(FORK): {
|
||||
case ON_BACKTRACK(FORK_OPT): {
|
||||
if (jv_is_valid(jq->error)) {
|
||||
// `try EXP ...` backtracked here (no value, `empty`), so we backtrack more
|
||||
jv_free(stack_pop(jq));
|
||||
goto do_backtrack;
|
||||
}
|
||||
// `try EXP ...` exception caught in EXP
|
||||
jv_free(stack_pop(jq)); // free the input
|
||||
stack_push(jq, jv_invalid_get_msg(jq->error)); // push the error's message
|
||||
jq->error = jv_null();
|
||||
uint16_t offset = *pc++;
|
||||
pc += offset;
|
||||
break;
|
||||
}
|
||||
|
||||
case ON_BACKTRACK(FORK): {
|
||||
if (raising) goto do_backtrack;
|
||||
uint16_t offset = *pc++;
|
||||
pc += offset;
|
||||
break;
|
||||
}
|
||||
|
||||
case CALL_BUILTIN: {
|
||||
int nargs = *pc++;
|
||||
jv top = stack_pop(jq);
|
||||
@@ -607,36 +775,74 @@ jv jq_next(jq_state *jq) {
|
||||
in[i] = stack_pop(jq);
|
||||
}
|
||||
struct cfunction* function = &frame_current(jq)->bc->globals->cfunctions[*pc++];
|
||||
typedef jv (*func_1)(jv);
|
||||
typedef jv (*func_2)(jv,jv);
|
||||
typedef jv (*func_3)(jv,jv,jv);
|
||||
typedef jv (*func_4)(jv,jv,jv,jv);
|
||||
typedef jv (*func_5)(jv,jv,jv,jv,jv);
|
||||
typedef jv (*func_1)(jq_state*,jv);
|
||||
typedef jv (*func_2)(jq_state*,jv,jv);
|
||||
typedef jv (*func_3)(jq_state*,jv,jv,jv);
|
||||
typedef jv (*func_4)(jq_state*,jv,jv,jv,jv);
|
||||
typedef jv (*func_5)(jq_state*,jv,jv,jv,jv,jv);
|
||||
switch (function->nargs) {
|
||||
case 1: top = ((func_1)function->fptr)(in[0]); break;
|
||||
case 2: top = ((func_2)function->fptr)(in[0], in[1]); break;
|
||||
case 3: top = ((func_3)function->fptr)(in[0], in[1], in[2]); break;
|
||||
case 4: top = ((func_4)function->fptr)(in[0], in[1], in[2], in[3]); break;
|
||||
case 5: top = ((func_5)function->fptr)(in[0], in[1], in[2], in[3], in[4]); break;
|
||||
case 1: top = ((func_1)function->fptr)(jq, in[0]); break;
|
||||
case 2: top = ((func_2)function->fptr)(jq, in[0], in[1]); break;
|
||||
case 3: top = ((func_3)function->fptr)(jq, in[0], in[1], in[2]); break;
|
||||
case 4: top = ((func_4)function->fptr)(jq, in[0], in[1], in[2], in[3]); break;
|
||||
case 5: top = ((func_5)function->fptr)(jq, in[0], in[1], in[2], in[3], in[4]); break;
|
||||
// FIXME: a) up to 7 arguments (input + 6), b) should assert
|
||||
// because the compiler should not generate this error.
|
||||
default: return jv_invalid_with_msg(jv_string("Function takes too many arguments"));
|
||||
}
|
||||
|
||||
|
||||
if (jv_is_valid(top)) {
|
||||
stack_push(jq, top);
|
||||
} else if (jv_invalid_has_msg(jv_copy(top))) {
|
||||
set_error(jq, top);
|
||||
goto do_backtrack;
|
||||
} else {
|
||||
print_error(top);
|
||||
// C-coded function returns invalid w/o msg? -> backtrack, as if
|
||||
// it had returned `empty`
|
||||
goto do_backtrack;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case TAIL_CALL_JQ:
|
||||
case CALL_JQ: {
|
||||
/*
|
||||
* Bytecode layout here:
|
||||
*
|
||||
* CALL_JQ
|
||||
* <nclosures> (i.e., number of call arguments)
|
||||
* <callee closure> (what we're calling)
|
||||
* <nclosures' worth of closures> (frame reference + code pointer)
|
||||
*
|
||||
* <next instruction (to return to)>
|
||||
*
|
||||
* Each closure consists of two uint16_t values: a "level"
|
||||
* identifying the frame to be closed over, and an index.
|
||||
*
|
||||
* The level is a relative number of call frames reachable from
|
||||
* the currently one; 0 -> current frame, 1 -> previous frame, and
|
||||
* so on.
|
||||
*
|
||||
* The index is either an index of the closed frame's subfunctions
|
||||
* or of the closed frame's parameter closures. If the latter,
|
||||
* that closure will be passed, else the closed frame's pointer
|
||||
* and the subfunction's code will form the closure to be passed.
|
||||
*
|
||||
* See make_closure() for more information.
|
||||
*/
|
||||
jv input = stack_pop(jq);
|
||||
uint16_t nclosures = *pc++;
|
||||
uint16_t* retaddr = pc + 2 + nclosures*2;
|
||||
struct frame* new_frame = frame_push(jq, make_closure(jq, pc),
|
||||
pc + 2, nclosures);
|
||||
new_frame->retdata = jq->stk_top;
|
||||
stack_ptr retdata = jq->stk_top;
|
||||
struct frame* new_frame;
|
||||
struct closure cl = make_closure(jq, pc);
|
||||
if (opcode == TAIL_CALL_JQ) {
|
||||
retaddr = frame_current(jq)->retaddr;
|
||||
retdata = frame_current(jq)->retdata;
|
||||
frame_pop(jq);
|
||||
}
|
||||
new_frame = frame_push(jq, cl, pc + 2, nclosures);
|
||||
new_frame->retdata = retdata;
|
||||
new_frame->retaddr = retaddr;
|
||||
pc = new_frame->bc->code;
|
||||
stack_push(jq, input);
|
||||
@@ -669,6 +875,48 @@ jv jq_next(jq_state *jq) {
|
||||
}
|
||||
}
|
||||
|
||||
jv jq_format_error(jv msg) {
|
||||
if (jv_get_kind(msg) == JV_KIND_NULL ||
|
||||
(jv_get_kind(msg) == JV_KIND_INVALID && !jv_invalid_has_msg(jv_copy(msg)))) {
|
||||
jv_free(msg);
|
||||
fprintf(stderr, "jq: error: out of memory\n");
|
||||
return jv_null();
|
||||
}
|
||||
|
||||
if (jv_get_kind(msg) == JV_KIND_STRING)
|
||||
return msg; // expected to already be formatted
|
||||
|
||||
if (jv_get_kind(msg) == JV_KIND_INVALID)
|
||||
msg = jv_invalid_get_msg(msg);
|
||||
|
||||
if (jv_get_kind(msg) == JV_KIND_NULL)
|
||||
return jq_format_error(msg); // ENOMEM
|
||||
|
||||
// Invalid with msg; prefix with "jq: error: "
|
||||
|
||||
if (jv_get_kind(msg) != JV_KIND_INVALID) {
|
||||
if (jv_get_kind(msg) == JV_KIND_STRING)
|
||||
return jv_string_fmt("jq: error: %s", jv_string_value(msg));
|
||||
|
||||
msg = jv_dump_string(msg, JV_PRINT_INVALID);
|
||||
if (jv_get_kind(msg) == JV_KIND_STRING)
|
||||
return jv_string_fmt("jq: error: %s", jv_string_value(msg));
|
||||
return jq_format_error(jv_null()); // ENOMEM
|
||||
}
|
||||
|
||||
// An invalid inside an invalid!
|
||||
return jq_format_error(jv_invalid_get_msg(msg));
|
||||
}
|
||||
|
||||
// XXX Refactor into a utility function that returns a jv and one that
|
||||
// uses it and then prints that jv's string as the complete error
|
||||
// message.
|
||||
static void default_err_cb(void *data, jv msg) {
|
||||
msg = jq_format_error(msg);
|
||||
fprintf((FILE *)data, "%s\n", jv_string_value(msg));
|
||||
jv_free(msg);
|
||||
}
|
||||
|
||||
jq_state *jq_init(void) {
|
||||
jq_state *jq;
|
||||
jq = jv_mem_alloc_unguarded(sizeof(*jq));
|
||||
@@ -676,16 +924,38 @@ jq_state *jq_init(void) {
|
||||
return NULL;
|
||||
|
||||
jq->bc = 0;
|
||||
jq->next_label = 0;
|
||||
|
||||
stack_init(&jq->stk);
|
||||
jq->stk_top = 0;
|
||||
jq->fork_top = 0;
|
||||
jq->curr_frame = 0;
|
||||
jq->error = jv_null();
|
||||
|
||||
jq->err_cb = default_err_cb;
|
||||
jq->err_cb_data = stderr;
|
||||
|
||||
jq->attrs = jv_object();
|
||||
jq->path = jv_null();
|
||||
jq->value_at_path = jv_null();
|
||||
return jq;
|
||||
}
|
||||
|
||||
void jq_set_error_cb(jq_state *jq, jq_msg_cb cb, void *data) {
|
||||
if (cb == NULL) {
|
||||
jq->err_cb = default_err_cb;
|
||||
jq->err_cb_data = stderr;
|
||||
} else {
|
||||
jq->err_cb = cb;
|
||||
jq->err_cb_data = data;
|
||||
}
|
||||
}
|
||||
|
||||
void jq_get_error_cb(jq_state *jq, jq_msg_cb *cb, void **data) {
|
||||
*cb = jq->err_cb;
|
||||
*data = jq->err_cb_data;
|
||||
}
|
||||
|
||||
void jq_set_nomem_handler(jq_state *jq, void (*nomem_handler)(void *), void *data) {
|
||||
jv_nomem_handler(nomem_handler, data);
|
||||
jq->nomem_handler = nomem_handler;
|
||||
@@ -696,7 +966,7 @@ void jq_set_nomem_handler(jq_state *jq, void (*nomem_handler)(void *), void *dat
|
||||
void jq_start(jq_state *jq, jv input, int flags) {
|
||||
jv_nomem_handler(jq->nomem_handler, jq->nomem_handler_data);
|
||||
jq_reset(jq);
|
||||
|
||||
|
||||
struct closure top = {jq->bc, -1};
|
||||
struct frame* top_frame = frame_push(jq, top, 0, 0);
|
||||
top_frame->retdata = 0;
|
||||
@@ -721,40 +991,112 @@ void jq_teardown(jq_state **jq) {
|
||||
jq_reset(old_jq);
|
||||
bytecode_free(old_jq->bc);
|
||||
old_jq->bc = 0;
|
||||
jv_free(old_jq->attrs);
|
||||
|
||||
jv_mem_free(old_jq);
|
||||
}
|
||||
|
||||
static int ret_follows(uint16_t *pc) {
|
||||
if (*pc == RET)
|
||||
return 1;
|
||||
if (*pc++ != JUMP)
|
||||
return 0;
|
||||
return ret_follows(pc + *pc + 1); // FIXME, might be ironic
|
||||
}
|
||||
|
||||
/*
|
||||
* Look for tail calls that can be optimized: tail calls with no
|
||||
* references left to the current frame.
|
||||
*
|
||||
* We're staring at this bytecode layout:
|
||||
*
|
||||
* CALL_JQ
|
||||
* <nclosures>
|
||||
* <callee closure> (2 units)
|
||||
* <nclosures closures> (2 units each)
|
||||
* <next instruction>
|
||||
*
|
||||
* A closure is:
|
||||
*
|
||||
* <level> (a relative frame count chased via the current frame's env)
|
||||
* <index> (an index of a subfunction or closure in that frame)
|
||||
*
|
||||
* We're looking for:
|
||||
*
|
||||
* a) the next instruction is a RET or a chain of unconditional JUMPs
|
||||
* that ends in a RET, and
|
||||
*
|
||||
* b) none of the closures -callee included- have level == 0.
|
||||
*/
|
||||
static uint16_t tail_call_analyze(uint16_t *pc) {
|
||||
assert(*pc == CALL_JQ);
|
||||
pc++;
|
||||
// + 1 for the callee closure
|
||||
for (uint16_t nclosures = *pc++ + 1; nclosures > 0; pc++, nclosures--) {
|
||||
if (*pc++ == 0)
|
||||
return CALL_JQ;
|
||||
}
|
||||
if (ret_follows(pc))
|
||||
return TAIL_CALL_JQ;
|
||||
return CALL_JQ;
|
||||
}
|
||||
|
||||
static struct bytecode *optimize_code(struct bytecode *bc) {
|
||||
uint16_t *pc = bc->code;
|
||||
// FIXME: Don't mutate bc->code...
|
||||
while (pc < bc->code + bc->codelen) {
|
||||
switch (*pc) {
|
||||
case CALL_JQ:
|
||||
*pc = tail_call_analyze(pc);
|
||||
break;
|
||||
|
||||
// Other bytecode optimizations here. A peephole optimizer would
|
||||
// fit right in.
|
||||
default: break;
|
||||
}
|
||||
pc += bytecode_operation_length(pc);
|
||||
}
|
||||
return bc;
|
||||
}
|
||||
|
||||
static struct bytecode *optimize(struct bytecode *bc) {
|
||||
for (int i=0; i<bc->nsubfunctions; i++) {
|
||||
bc->subfunctions[i] = optimize(bc->subfunctions[i]);
|
||||
}
|
||||
return optimize_code(bc);
|
||||
}
|
||||
|
||||
int jq_compile_args(jq_state *jq, const char* str, jv args) {
|
||||
jv_nomem_handler(jq->nomem_handler, jq->nomem_handler_data);
|
||||
assert(jv_get_kind(args) == JV_KIND_ARRAY);
|
||||
struct locfile locations;
|
||||
locfile_init(&locations, str, strlen(str));
|
||||
struct locfile* locations;
|
||||
locations = locfile_init(jq, "<top-level>", str, strlen(str));
|
||||
block program;
|
||||
jq_reset(jq);
|
||||
if (jq->bc) {
|
||||
bytecode_free(jq->bc);
|
||||
jq->bc = 0;
|
||||
}
|
||||
int nerrors = jq_parse(&locations, &program);
|
||||
int nerrors = load_program(jq, locations, &program);
|
||||
if (nerrors == 0) {
|
||||
for (int i=0; i<jv_array_length(jv_copy(args)); i++) {
|
||||
jv arg = jv_array_get(jv_copy(args), i);
|
||||
jv_array_foreach(args, i, arg) {
|
||||
jv name = jv_object_get(jv_copy(arg), jv_string("name"));
|
||||
jv value = jv_object_get(arg, jv_string("value"));
|
||||
program = gen_var_binding(gen_const(value), jv_string_value(name), program);
|
||||
jv_free(name);
|
||||
}
|
||||
jv_free(args);
|
||||
nerrors = builtins_bind(&program);
|
||||
|
||||
nerrors = builtins_bind(jq, &program);
|
||||
if (nerrors == 0) {
|
||||
nerrors = block_compile(program, &locations, &jq->bc);
|
||||
nerrors = block_compile(program, &jq->bc, locations);
|
||||
}
|
||||
}
|
||||
if (nerrors) {
|
||||
fprintf(stderr, "%d compile %s\n", nerrors, nerrors > 1 ? "errors" : "error");
|
||||
}
|
||||
locfile_free(&locations);
|
||||
if (nerrors)
|
||||
jq_report_error(jq, jv_string_fmt("jq: %d compile %s", nerrors, nerrors > 1 ? "errors" : "error"));
|
||||
if (jq->bc)
|
||||
jq->bc = optimize(jq->bc);
|
||||
jv_free(args);
|
||||
locfile_free(locations);
|
||||
return jq->bc != NULL;
|
||||
}
|
||||
|
||||
@@ -762,6 +1104,52 @@ int jq_compile(jq_state *jq, const char* str) {
|
||||
return jq_compile_args(jq, str, jv_array());
|
||||
}
|
||||
|
||||
jv jq_get_jq_origin(jq_state *jq) {
|
||||
return jq_get_attr(jq, jv_string("JQ_ORIGIN"));
|
||||
}
|
||||
|
||||
jv jq_get_prog_origin(jq_state *jq) {
|
||||
return jq_get_attr(jq, jv_string("PROGRAM_ORIGIN"));
|
||||
}
|
||||
|
||||
jv jq_get_lib_dirs(jq_state *jq) {
|
||||
return jq_get_attr(jq, jv_string("JQ_LIBRARY_PATH"));
|
||||
}
|
||||
|
||||
void jq_set_attrs(jq_state *jq, jv attrs) {
|
||||
assert(jv_get_kind(attrs) == JV_KIND_OBJECT);
|
||||
jv_free(jq->attrs);
|
||||
jq->attrs = attrs;
|
||||
}
|
||||
|
||||
void jq_set_attr(jq_state *jq, jv attr, jv val) {
|
||||
jq->attrs = jv_object_set(jq->attrs, attr, val);
|
||||
}
|
||||
|
||||
jv jq_get_attr(jq_state *jq, jv attr) {
|
||||
return jv_object_get(jv_copy(jq->attrs), attr);
|
||||
}
|
||||
|
||||
void jq_dump_disassembly(jq_state *jq, int indent) {
|
||||
dump_disassembly(indent, jq->bc);
|
||||
}
|
||||
|
||||
void jq_set_input_cb(jq_state *jq, jq_input_cb cb, void *data) {
|
||||
jq->input_cb = cb;
|
||||
jq->input_cb_data = data;
|
||||
}
|
||||
|
||||
void jq_get_input_cb(jq_state *jq, jq_input_cb *cb, void **data) {
|
||||
*cb = jq->input_cb;
|
||||
*data = jq->input_cb_data;
|
||||
}
|
||||
|
||||
void jq_set_debug_cb(jq_state *jq, jq_msg_cb cb, void *data) {
|
||||
jq->debug_cb = cb;
|
||||
jq->debug_cb_data = data;
|
||||
}
|
||||
|
||||
void jq_get_debug_cb(jq_state *jq, jq_msg_cb *cb, void **data) {
|
||||
*cb = jq->debug_cb;
|
||||
*data = jq->debug_cb_data;
|
||||
}
|
||||
+112
@@ -0,0 +1,112 @@
|
||||
|
||||
#define _GNU_SOURCE /* for RTLD_NEXT */
|
||||
#include <assert.h>
|
||||
#include <dlfcn.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
static FILE *fail;
|
||||
static FILE *fail_read;
|
||||
static FILE *fail_write;
|
||||
static FILE *fail_close;
|
||||
static int error;
|
||||
|
||||
static FILE * (*real_fopen)(const char *, const char *);
|
||||
static int (*real_fclose)(FILE *);
|
||||
static int (*real_ferror)(FILE *);
|
||||
static void (*real_clearerr)(FILE *);
|
||||
static char * (*real_fgets)(char *, int, FILE *);
|
||||
static size_t (*real_fread)(void *, size_t, size_t, FILE *);
|
||||
static size_t (*real_fwrite)(const void *, size_t, size_t, FILE *);
|
||||
|
||||
#define GET_REAL(sym) \
|
||||
do { \
|
||||
if (real_ ## sym == 0) { \
|
||||
real_ ## sym = dlsym(RTLD_NEXT, #sym); \
|
||||
assert(real_ ## sym != 0); \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
#define dbg_write(msg) (void)write(2, msg, sizeof(msg) - 1)
|
||||
|
||||
#define dbg() \
|
||||
do { \
|
||||
dbg_write("here: "); \
|
||||
dbg_write(__func__); \
|
||||
dbg_write("!\n"); \
|
||||
} while (0)
|
||||
|
||||
FILE *fopen(const char *path, const char *mode) {
|
||||
GET_REAL(fopen);
|
||||
fail = fail_read = fail_write = fail_close = 0;
|
||||
FILE *f = real_fopen(path, mode);
|
||||
error = EIO;
|
||||
if (strcmp(path, "fail_read") == 0) {
|
||||
fail = fail_read = f;
|
||||
} else if (strncmp(path, "fail_write", sizeof("fail_write") - 1) == 0) {
|
||||
// Not that jq opens files for write anyways...
|
||||
fail = fail_write = f;
|
||||
if (strcmp(path, "fail_write_enospc") == 0)
|
||||
error = ENOSPC;
|
||||
} else if (strncmp(path, "fail_close", sizeof("fail_close") - 1) == 0) {
|
||||
fail = fail_close = f;
|
||||
if (strcmp(path, "fail_close_enospc") == 0)
|
||||
error = ENOSPC;
|
||||
}
|
||||
return f;
|
||||
}
|
||||
|
||||
int fclose(FILE *f) {
|
||||
GET_REAL(fclose);
|
||||
int res = real_fclose(f);
|
||||
if (fail_close == f) {
|
||||
fail = fail_read = fail_write = fail_close = 0;
|
||||
return EOF;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
char * fgets(char *buf, int len, FILE *f) {
|
||||
GET_REAL(fgets);
|
||||
char *res = real_fgets(buf, len, f);
|
||||
if (fail_read == f)
|
||||
return 0;
|
||||
return res;
|
||||
}
|
||||
|
||||
size_t fread(void *buf, size_t sz, size_t nemb, FILE *f) {
|
||||
GET_REAL(fread);
|
||||
size_t res = real_fread(buf, sz, nemb, f);
|
||||
if (fail_read == f)
|
||||
return 0;
|
||||
return res;
|
||||
}
|
||||
|
||||
size_t fwrite(const void *buf, size_t sz, size_t nemb, FILE *f) {
|
||||
GET_REAL(fwrite);
|
||||
size_t res = real_fwrite(buf, sz, nemb, f);
|
||||
if (fail_write == f)
|
||||
return 0;
|
||||
return res;
|
||||
}
|
||||
|
||||
int ferror(FILE *f) {
|
||||
GET_REAL(ferror);
|
||||
int res = real_ferror(f);
|
||||
if (fail == f) {
|
||||
errno = error;
|
||||
return 1;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
void clearerr(FILE *f) {
|
||||
GET_REAL(clearerr);
|
||||
real_clearerr(f);
|
||||
if (fail == f) {
|
||||
fail = fail_read = fail_write = fail_close = 0;
|
||||
error = 0;
|
||||
}
|
||||
}
|
||||
+3075
File diff suppressed because it is too large.
Load diff
@@ -1,18 +1,60 @@
|
||||
#ifndef _JQ_H_
|
||||
#define _JQ_H_
|
||||
#ifndef JQ_H
|
||||
#define JQ_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <jv.h>
|
||||
|
||||
enum {JQ_DEBUG_TRACE = 1};
|
||||
|
||||
typedef struct jq_state jq_state;
|
||||
typedef void (*jq_msg_cb)(void *, jv);
|
||||
|
||||
jq_state *jq_init(void);
|
||||
void jq_set_error_cb(jq_state *, jq_msg_cb, void *);
|
||||
void jq_get_error_cb(jq_state *, jq_msg_cb *, void **);
|
||||
void jq_set_nomem_handler(jq_state *, void (*)(void *), void *);
|
||||
int jq_compile(jq_state *, const char* str);
|
||||
int jq_compile_args(jq_state *, const char* str, jv args);
|
||||
jv jq_format_error(jv msg);
|
||||
void jq_report_error(jq_state *, jv);
|
||||
int jq_compile(jq_state *, const char*);
|
||||
int jq_compile_args(jq_state *, const char*, jv);
|
||||
void jq_dump_disassembly(jq_state *, int);
|
||||
void jq_start(jq_state *, jv value, int flags);
|
||||
void jq_start(jq_state *, jv value, int);
|
||||
jv jq_next(jq_state *);
|
||||
void jq_teardown(jq_state **);
|
||||
|
||||
#endif /* !_JQ_H_ */
|
||||
typedef jv (*jq_input_cb)(jq_state *, void *);
|
||||
void jq_set_input_cb(jq_state *, jq_input_cb, void *);
|
||||
void jq_get_input_cb(jq_state *, jq_input_cb *, void **);
|
||||
|
||||
void jq_set_debug_cb(jq_state *, jq_msg_cb, void *);
|
||||
void jq_get_debug_cb(jq_state *, jq_msg_cb *, void **);
|
||||
|
||||
void jq_set_attrs(jq_state *, jv);
|
||||
jv jq_get_attrs(jq_state *);
|
||||
jv jq_get_jq_origin(jq_state *);
|
||||
jv jq_get_prog_origin(jq_state *);
|
||||
jv jq_get_lib_dirs(jq_state *);
|
||||
void jq_set_attr(jq_state *, jv, jv);
|
||||
jv jq_get_attr(jq_state *, jv);
|
||||
|
||||
/*
|
||||
* We use char * instead of jf for filenames here because filenames
|
||||
* should be in the process' locale's codeset, which may not be UTF-8,
|
||||
* whereas jv string values must be in UTF-8. This way the caller
|
||||
* doesn't have to perform any codeset conversions.
|
||||
*/
|
||||
typedef struct jq_util_input_state jq_util_input_state;
|
||||
typedef void (*jq_util_msg_cb)(void *, const char *);
|
||||
|
||||
jq_util_input_state *jq_util_input_init(jq_util_msg_cb, void *);
|
||||
void jq_util_input_set_parser(jq_util_input_state *, jv_parser *, int);
|
||||
void jq_util_input_free(jq_util_input_state **);
|
||||
void jq_util_input_add_input(jq_util_input_state *, const char *);
|
||||
int jq_util_input_errors(jq_util_input_state *);
|
||||
jv jq_util_input_next_input(jq_util_input_state *);
|
||||
jv jq_util_input_next_input_cb(jq_state *, void *);
|
||||
jv jq_util_input_get_position(jq_state*);
|
||||
jv jq_util_input_get_current_filename(jq_state*);
|
||||
jv jq_util_input_get_current_line(jq_state*);
|
||||
|
||||
#endif /* !JQ_H */
|
||||
@@ -5,7 +5,7 @@ Name: jq
|
||||
Version: %{myver}
|
||||
Release: %{myrel}%{?dist}
|
||||
Source0: jq-%{myver}.tar.gz
|
||||
URL: https://github.com/stedolan/jq
|
||||
URL: http://stedolan.github.io/jq
|
||||
License: BSD
|
||||
AutoReqProv: no
|
||||
#BuildPrereq: autoconf, libtool, automake, flex, bison, python
|
||||
@@ -52,11 +52,16 @@ rm -rf %{buildroot}
|
||||
%endif
|
||||
%{_datadir}/doc/%{name}/AUTHORS
|
||||
%{_datadir}/doc/%{name}/COPYING
|
||||
%{_datadir}/doc/%{name}/INSTALL
|
||||
%{_datadir}/doc/%{name}/NEWS
|
||||
%{_datadir}/doc/%{name}/README
|
||||
%{_datadir}/doc/%{name}/README.md
|
||||
%{_datadir}/man/man1/jq.1
|
||||
%{_includedir}/jq.h
|
||||
%{_includedir}/jv.h
|
||||
%{_prefix}/lib/libjq.a
|
||||
%{_prefix}/lib/libjq.la
|
||||
%{_prefix}/lib/libjq.so
|
||||
%{_prefix}/lib/libjq.so.1
|
||||
%{_prefix}/lib/libjq.so.1.0.4
|
||||
|
||||
%changelog
|
||||
|
||||
|
||||
@@ -6,20 +6,20 @@
|
||||
#include "jq.h"
|
||||
|
||||
static void jv_test();
|
||||
static void run_jq_tests();
|
||||
static void run_jq_tests(jv, int, FILE *);
|
||||
|
||||
|
||||
int jq_testsuite(int argc, char* argv[]) {
|
||||
int jq_testsuite(jv libdirs, int verbose, int argc, char* argv[]) {
|
||||
FILE *testdata = stdin;
|
||||
jv_test();
|
||||
if (argc > 2) {
|
||||
testdata = fopen(argv[2], "r");
|
||||
if (argc > 0) {
|
||||
testdata = fopen(argv[0], "r");
|
||||
if (!testdata) {
|
||||
perror("fopen");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
run_jq_tests(testdata);
|
||||
run_jq_tests(libdirs, verbose, testdata);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -30,39 +30,123 @@ static int skipline(const char* buf) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void run_jq_tests(FILE *testdata) {
|
||||
static int checkerrormsg(const char* buf) {
|
||||
return strcmp(buf, "%%FAIL\n") == 0;
|
||||
}
|
||||
|
||||
static int checkfail(const char* buf) {
|
||||
return strcmp(buf, "%%FAIL\n") == 0 || strcmp(buf, "%%FAIL IGNORE MSG\n") == 0;
|
||||
}
|
||||
|
||||
struct err_data {
|
||||
char buf[4096];
|
||||
};
|
||||
|
||||
static void test_err_cb(void *data, jv e) {
|
||||
struct err_data *err_data = data;
|
||||
if (jv_get_kind(e) != JV_KIND_STRING)
|
||||
e = jv_dump_string(e, JV_PRINT_INVALID);
|
||||
if (!strncmp(jv_string_value(e), "jq: error", sizeof("jq: error") - 1))
|
||||
snprintf(err_data->buf, sizeof(err_data->buf), "%s", jv_string_value(e));
|
||||
if (strchr(err_data->buf, '\n'))
|
||||
*(strchr(err_data->buf, '\n')) = '\0';
|
||||
jv_free(e);
|
||||
}
|
||||
|
||||
static void run_jq_tests(jv lib_dirs, int verbose, FILE *testdata) {
|
||||
char prog[4096];
|
||||
char buf[4096];
|
||||
struct err_data err_msg;
|
||||
int tests = 0, passed = 0, invalid = 0;
|
||||
unsigned int lineno = 0;
|
||||
int must_fail = 0;
|
||||
int check_msg = 0;
|
||||
jq_state *jq = NULL;
|
||||
|
||||
jq = jq_init();
|
||||
assert(jq);
|
||||
if (jv_get_kind(lib_dirs) == JV_KIND_NULL)
|
||||
lib_dirs = jv_array();
|
||||
jq_set_attr(jq, jv_string("JQ_LIBRARY_PATH"), lib_dirs);
|
||||
|
||||
while (1) {
|
||||
if (!fgets(buf, sizeof(buf), testdata)) break;
|
||||
if (skipline(buf)) continue;
|
||||
if (buf[strlen(buf)-1] == '\n') buf[strlen(buf)-1] = 0;
|
||||
printf("Testing %s\n", buf);
|
||||
if (!fgets(prog, sizeof(prog), testdata)) break;
|
||||
lineno++;
|
||||
if (skipline(prog)) continue;
|
||||
if (checkfail(prog)) {
|
||||
must_fail = 1;
|
||||
check_msg = checkerrormsg(prog);
|
||||
jq_set_error_cb(jq, test_err_cb, &err_msg);
|
||||
continue;
|
||||
}
|
||||
if (prog[strlen(prog)-1] == '\n') prog[strlen(prog)-1] = 0;
|
||||
printf("Testing '%s' at line number %u\n", prog, lineno);
|
||||
int pass = 1;
|
||||
tests++;
|
||||
int compiled = jq_compile(jq, buf);
|
||||
if (!compiled) {invalid++; continue;}
|
||||
printf("Disassembly:\n");
|
||||
jq_dump_disassembly(jq, 2);
|
||||
printf("\n");
|
||||
int compiled = jq_compile(jq, prog);
|
||||
|
||||
if (must_fail) {
|
||||
jq_set_error_cb(jq, NULL, NULL);
|
||||
if (!fgets(buf, sizeof(buf), testdata)) { invalid++; break; }
|
||||
lineno++;
|
||||
if (buf[strlen(buf)-1] == '\n') buf[strlen(buf)-1] = 0;
|
||||
if (compiled) {
|
||||
printf("*** Test program compiled that should not have at line %u: %s\n", lineno, prog);
|
||||
must_fail = 0;
|
||||
check_msg = 0;
|
||||
invalid++;
|
||||
continue;
|
||||
}
|
||||
if (check_msg && strcmp(buf, err_msg.buf) != 0) {
|
||||
printf("*** Erroneous test program failed with wrong message (%s) at line %u: %s\n", err_msg.buf, lineno, prog);
|
||||
invalid++;
|
||||
} else {
|
||||
passed++;
|
||||
}
|
||||
must_fail = 0;
|
||||
check_msg = 0;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!compiled) {
|
||||
printf("*** Test program failed to compile at line %u: %s\n", lineno, prog);
|
||||
invalid++;
|
||||
// skip past test data
|
||||
while (fgets(buf, sizeof(buf), testdata)) {
|
||||
lineno++;
|
||||
if (buf[0] == '\n' || (buf[0] == '\r' && buf[1] == '\n'))
|
||||
break;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (verbose) {
|
||||
printf("Disassembly:\n");
|
||||
jq_dump_disassembly(jq, 2);
|
||||
printf("\n");
|
||||
}
|
||||
if (!fgets(buf, sizeof(buf), testdata)) { invalid++; break; }
|
||||
lineno++;
|
||||
jv input = jv_parse(buf);
|
||||
if (!jv_is_valid(input)){ invalid++; continue; }
|
||||
jq_start(jq, input, JQ_DEBUG_TRACE);
|
||||
if (!jv_is_valid(input)) {
|
||||
printf("*** Input is invalid on line %u: %s\n", lineno, buf);
|
||||
invalid++;
|
||||
continue;
|
||||
}
|
||||
jq_start(jq, input, verbose ? JQ_DEBUG_TRACE : 0);
|
||||
|
||||
while (fgets(buf, sizeof(buf), testdata)) {
|
||||
lineno++;
|
||||
if (skipline(buf)) break;
|
||||
jv expected = jv_parse(buf);
|
||||
if (!jv_is_valid(expected)){ invalid++; continue; }
|
||||
if (!jv_is_valid(expected)) {
|
||||
printf("*** Expected result is invalid on line %u: %s\n", lineno, buf);
|
||||
invalid++;
|
||||
continue;
|
||||
}
|
||||
jv actual = jq_next(jq);
|
||||
if (!jv_is_valid(actual)) {
|
||||
jv_free(actual);
|
||||
printf("*** Insufficient results\n");
|
||||
printf("*** Insufficient results for test at line number %u: %s\n", lineno, prog);
|
||||
pass = 0;
|
||||
break;
|
||||
} else if (!jv_equal(jv_copy(expected), jv_copy(actual))) {
|
||||
@@ -70,10 +154,10 @@ static void run_jq_tests(FILE *testdata) {
|
||||
jv_dump(jv_copy(expected), 0);
|
||||
printf(", but got ");
|
||||
jv_dump(jv_copy(actual), 0);
|
||||
printf("\n");
|
||||
printf(" for test at line number %u: %s\n", lineno, prog);
|
||||
pass = 0;
|
||||
}
|
||||
jv as_string = jv_dump_string(jv_copy(expected), rand() & ~JV_PRINT_COLOUR);
|
||||
jv as_string = jv_dump_string(jv_copy(expected), rand() & ~(JV_PRINT_COLOUR|JV_PRINT_REFCOUNT));
|
||||
jv reparsed = jv_parse_sized(jv_string_value(as_string), jv_string_length_bytes(jv_copy(as_string)));
|
||||
assert(jv_equal(jv_copy(expected), jv_copy(reparsed)));
|
||||
jv_free(as_string);
|
||||
@@ -86,7 +170,7 @@ static void run_jq_tests(FILE *testdata) {
|
||||
if (jv_is_valid(extra)) {
|
||||
printf("*** Superfluous result: ");
|
||||
jv_dump(extra, 0);
|
||||
printf("\n");
|
||||
printf(" for test at line number %u, %s\n", lineno, prog);
|
||||
pass = 0;
|
||||
} else {
|
||||
jv_free(extra);
|
||||
@@ -101,13 +185,21 @@ static void run_jq_tests(FILE *testdata) {
|
||||
|
||||
|
||||
static void jv_test() {
|
||||
/// JSON parser regression tests
|
||||
{
|
||||
jv v = jv_parse("{\"a':\"12\"}");
|
||||
assert(jv_get_kind(v) == JV_KIND_INVALID);
|
||||
v = jv_invalid_get_msg(v);
|
||||
assert(strcmp(jv_string_value(v), "Expected separator between values at line 1, column 9 (while parsing '{\"a':\"12\"}')") == 0);
|
||||
jv_free(v);
|
||||
}
|
||||
/// Arrays and numbers
|
||||
{
|
||||
jv a = jv_array();
|
||||
assert(jv_get_kind(a) == JV_KIND_ARRAY);
|
||||
assert(jv_array_length(jv_copy(a)) == 0);
|
||||
assert(jv_array_length(jv_copy(a)) == 0);
|
||||
|
||||
|
||||
a = jv_array_append(a, jv_number(42));
|
||||
assert(jv_array_length(jv_copy(a)) == 1);
|
||||
assert(jv_number_value(jv_array_get(jv_copy(a), 0)) == 42);
|
||||
@@ -160,9 +252,9 @@ static void jv_test() {
|
||||
|
||||
jv_free(subarray);
|
||||
|
||||
void* before = sub2.val.nontrivial.ptr;
|
||||
void* before = sub2.u.ptr;
|
||||
sub2 = jv_array_append(sub2, jv_number(200));
|
||||
void* after = sub2.val.nontrivial.ptr;
|
||||
void* after = sub2.u.ptr;
|
||||
assert(before == after);
|
||||
jv_free(sub2);
|
||||
|
||||
@@ -182,8 +274,10 @@ static void jv_test() {
|
||||
a4 = jv_array_slice(a4, 0, 1);
|
||||
assert(jv_array_length(jv_copy(a4)) == 1);
|
||||
a4 = jv_array_append(a4, jv_number(4));
|
||||
assert(jv_array_length(a4) == 2);
|
||||
assert(jv_array_length(a5) == 2);
|
||||
assert(jv_array_length(jv_copy(a4)) == 2);
|
||||
assert(jv_array_length(jv_copy(a5)) == 2);
|
||||
jv_free(a4);
|
||||
jv_free(a5);
|
||||
|
||||
|
||||
assert(jv_array_length(jv_copy(a)) == 2);
|
||||
@@ -191,7 +285,7 @@ static void jv_test() {
|
||||
assert(jv_array_length(jv_array_get(jv_copy(a), 1)) == 1);
|
||||
|
||||
|
||||
jv_dump(jv_copy(a), 0); printf("\n");
|
||||
//jv_dump(jv_copy(a), 0); printf("\n");
|
||||
jv_free(a);
|
||||
}
|
||||
|
||||
@@ -201,16 +295,18 @@ static void jv_test() {
|
||||
assert(jv_equal(jv_string("foo"), jv_string_sized("foo", 3)));
|
||||
char nasty[] = "foo\0";
|
||||
jv shortstr = jv_string(nasty), longstr = jv_string_sized(nasty, sizeof(nasty));
|
||||
assert(jv_string_length_bytes(shortstr) == (int)strlen(nasty));
|
||||
assert(jv_string_length_bytes(longstr) == (int)sizeof(nasty));
|
||||
assert(jv_string_length_bytes(jv_copy(shortstr)) == (int)strlen(nasty));
|
||||
assert(jv_string_length_bytes(jv_copy(longstr)) == (int)sizeof(nasty));
|
||||
jv_free(shortstr);
|
||||
jv_free(longstr);
|
||||
|
||||
|
||||
|
||||
char a1s[] = "hello", a2s[] = "hello", bs[] = "goodbye";
|
||||
jv a1 = jv_string(a1s), a2 = jv_string(a2s), b = jv_string(bs);
|
||||
assert(jv_equal(jv_copy(a1), jv_copy(a2)));
|
||||
assert(jv_equal(jv_copy(a2), jv_copy(a1)));
|
||||
assert(!jv_equal(jv_copy(a1), jv_copy(b)));
|
||||
|
||||
|
||||
assert(jv_string_hash(jv_copy(a1)) == jv_string_hash(jv_copy(a1)));
|
||||
assert(jv_string_hash(jv_copy(a1)) == jv_string_hash(jv_copy(a2)));
|
||||
assert(jv_string_hash(jv_copy(b)) != jv_string_hash(jv_copy(a1)));
|
||||
@@ -244,8 +340,7 @@ static void jv_test() {
|
||||
jv_free(o1);
|
||||
assert(jv_number_value(jv_object_get(jv_copy(o2), jv_string("bar"))) == 240);
|
||||
|
||||
jv_dump(jv_copy(o2), 0); printf("\n");
|
||||
|
||||
//jv_dump(jv_copy(o2), 0); printf("\n");
|
||||
jv_free(o2);
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,10 @@
|
||||
#ifndef JV_H
|
||||
#define JV_H
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
|
||||
typedef enum {
|
||||
JV_KIND_INVALID,
|
||||
JV_KIND_NULL,
|
||||
@@ -13,17 +17,18 @@ typedef enum {
|
||||
} jv_kind;
|
||||
|
||||
struct jv_refcnt;
|
||||
typedef struct{
|
||||
struct jv_refcnt* ptr;
|
||||
int i[2];
|
||||
} jv_nontrivial;
|
||||
|
||||
/* All of the fields of this struct are private.
|
||||
Really. Do not play with them. */
|
||||
typedef struct {
|
||||
jv_kind kind;
|
||||
unsigned char kind_flags;
|
||||
unsigned char pad_;
|
||||
unsigned short offset; /* array offsets */
|
||||
int size;
|
||||
union {
|
||||
struct jv_refcnt* ptr;
|
||||
double number;
|
||||
jv_nontrivial nontrivial;
|
||||
} val;
|
||||
} u;
|
||||
} jv;
|
||||
|
||||
/*
|
||||
@@ -41,23 +46,25 @@ void jv_free(jv);
|
||||
int jv_get_refcnt(jv);
|
||||
|
||||
int jv_equal(jv, jv);
|
||||
int jv_identical(jv, jv);
|
||||
int jv_contains(jv, jv);
|
||||
|
||||
jv jv_invalid();
|
||||
jv jv_invalid(void);
|
||||
jv jv_invalid_with_msg(jv);
|
||||
jv jv_invalid_get_msg(jv);
|
||||
int jv_invalid_has_msg(jv);
|
||||
|
||||
|
||||
jv jv_null();
|
||||
jv jv_true();
|
||||
jv jv_false();
|
||||
jv jv_null(void);
|
||||
jv jv_true(void);
|
||||
jv jv_false(void);
|
||||
jv jv_bool(int);
|
||||
|
||||
jv jv_number(double);
|
||||
double jv_number_value(jv);
|
||||
int jv_is_integer(jv);
|
||||
|
||||
jv jv_array();
|
||||
jv jv_array(void);
|
||||
jv jv_array_sized(int);
|
||||
int jv_array_length(jv);
|
||||
jv jv_array_get(jv, int);
|
||||
@@ -65,6 +72,7 @@ jv jv_array_set(jv, int, jv);
|
||||
jv jv_array_append(jv, jv);
|
||||
jv jv_array_concat(jv, jv);
|
||||
jv jv_array_slice(jv, int, int);
|
||||
jv jv_array_indexes(jv, jv);
|
||||
#define jv_array_foreach(a, i, x) \
|
||||
for (int jv_len__ = jv_array_length(jv_copy(a)), i=0, jv_j__ = 1; \
|
||||
jv_j__; jv_j__ = 0) \
|
||||
@@ -73,23 +81,53 @@ jv jv_array_slice(jv, int, int);
|
||||
(x = jv_array_get(jv_copy(a), i), 1) : 0; \
|
||||
i++)
|
||||
|
||||
#define JV_ARRAY_1(e) (jv_array_append(jv_array(),e))
|
||||
#define JV_ARRAY_2(e1,e2) (jv_array_append(JV_ARRAY_1(e1),e2))
|
||||
#define JV_ARRAY_3(e1,e2,e3) (jv_array_append(JV_ARRAY_2(e1,e2),e3))
|
||||
#define JV_ARRAY_4(e1,e2,e3,e4) (jv_array_append(JV_ARRAY_3(e1,e2,e3),e4))
|
||||
#define JV_ARRAY_5(e1,e2,e3,e4,e5) (jv_array_append(JV_ARRAY_4(e1,e2,e3,e4),e5))
|
||||
#define JV_ARRAY_6(e1,e2,e3,e4,e5,e6) (jv_array_append(JV_ARRAY_5(e1,e2,e3,e4,e5),e6))
|
||||
#define JV_ARRAY_7(e1,e2,e3,e4,e5,e6,e7) (jv_array_append(JV_ARRAY_6(e1,e2,e3,e4,e5,e6),e7))
|
||||
#define JV_ARRAY_8(e1,e2,e3,e4,e5,e6,e7,e8) (jv_array_append(JV_ARRAY_7(e1,e2,e3,e4,e5,e6,e7),e8))
|
||||
#define JV_ARRAY_9(e1,e2,e3,e4,e5,e6,e7,e8,e9) (jv_array_append(JV_ARRAY_8(e1,e2,e3,e4,e5,e6,e7,e8),e9))
|
||||
#define JV_ARRAY_IDX(_1,_2,_3,_4,_5,_6,_7,_8,_9,NAME,...) NAME
|
||||
#define JV_ARRAY(...) \
|
||||
JV_ARRAY_IDX(__VA_ARGS__, JV_ARRAY_9, JV_ARRAY_8, JV_ARRAY_7, JV_ARRAY_6, JV_ARRAY_5, JV_ARRAY_4, JV_ARRAY_3, JV_ARRAY_2, JV_ARRAY_1)(__VA_ARGS__)
|
||||
|
||||
#ifdef __GNUC__
|
||||
#define JV_PRINTF_LIKE(fmt_arg_num, args_num) \
|
||||
__attribute__ ((__format__( __printf__, fmt_arg_num, args_num)))
|
||||
#define JV_VPRINTF_LIKE(fmt_arg_num) \
|
||||
__attribute__ ((__format__( __printf__, fmt_arg_num, 0)))
|
||||
#endif
|
||||
|
||||
|
||||
jv jv_string(const char*);
|
||||
jv jv_string_sized(const char*, int);
|
||||
jv jv_string_empty(int len);
|
||||
int jv_string_length_bytes(jv);
|
||||
int jv_string_length_codepoints(jv);
|
||||
unsigned long jv_string_hash(jv);
|
||||
const char* jv_string_value(jv);
|
||||
jv jv_string_indexes(jv j, jv k);
|
||||
jv jv_string_slice(jv j, int start, int end);
|
||||
jv jv_string_concat(jv, jv);
|
||||
jv jv_string_fmt(const char*, ...);
|
||||
jv jv_string_vfmt(const char*, va_list) JV_VPRINTF_LIKE(1);
|
||||
jv jv_string_fmt(const char*, ...) JV_PRINTF_LIKE(1, 2);
|
||||
jv jv_string_append_codepoint(jv a, uint32_t c);
|
||||
jv jv_string_append_buf(jv a, const char* buf, int len);
|
||||
jv jv_string_append_str(jv a, const char* str);
|
||||
jv jv_string_split(jv j, jv sep);
|
||||
jv jv_string_explode(jv j);
|
||||
jv jv_string_implode(jv j);
|
||||
|
||||
jv jv_object();
|
||||
jv jv_object(void);
|
||||
jv jv_object_get(jv object, jv key);
|
||||
jv jv_object_set(jv object, jv key, jv value);
|
||||
jv jv_object_delete(jv object, jv key);
|
||||
int jv_object_length(jv object);
|
||||
jv jv_object_merge(jv, jv);
|
||||
jv jv_object_merge_recursive(jv, jv);
|
||||
|
||||
int jv_object_iter(jv);
|
||||
int jv_object_iter_next(jv, int);
|
||||
@@ -105,14 +143,49 @@ jv jv_object_iter_value(jv, int);
|
||||
1) \
|
||||
: 0; \
|
||||
jv_i__ = jv_object_iter_next(t, jv_i__)) \
|
||||
|
||||
|
||||
#define JV_OBJECT_1(k) (jv_object_set(jv_object(),(k),jv_null()))
|
||||
#define JV_OBJECT_2(k1,v1) (jv_object_set(jv_object(),(k1),(v1)))
|
||||
#define JV_OBJECT_3(k1,v1,k2) (jv_object_set(JV_OBJECT_2(k1,v1),k2,jv_null()))
|
||||
#define JV_OBJECT_4(k1,v1,k2,v2) (jv_object_set(JV_OBJECT_2(k1,v1),k2,v2))
|
||||
#define JV_OBJECT_5(k1,v1,k2,v2,k3) (jv_object_set(JV_OBJECT_4(k1,v1,k2,v2),k3,jv_null))
|
||||
#define JV_OBJECT_6(k1,v1,k2,v2,k3,v3) (jv_object_set(JV_OBJECT_4(k1,v1,k2,v2),k3,v3))
|
||||
#define JV_OBJECT_7(k1,v1,k2,v2,k3,v3,k4) (jv_object_set(JV_OBJECT_6(k1,v1,k2,v2,k3,v3),k4,jv_null()))
|
||||
#define JV_OBJECT_8(k1,v1,k2,v2,k3,v3,k4,v4) (jv_object_set(JV_OBJECT_6(k1,v1,k2,v2,k3,v3),k4,v4))
|
||||
#define JV_OBJECT_IDX(_1,_2,_3,_4,_5,_6,_7,_8,NAME,...) NAME
|
||||
#define JV_OBJECT(...) \
|
||||
JV_OBJECT_IDX(__VA_ARGS__, JV_OBJECT_8, JV_OBJECT_7, JV_OBJECT_6, JV_OBJECT_5, JV_OBJECT_4, JV_OBJECT_3, JV_OBJECT_2, JV_OBJECT_1)(__VA_ARGS__)
|
||||
|
||||
|
||||
|
||||
int jv_get_refcnt(jv);
|
||||
|
||||
enum { JV_PRINT_PRETTY = 1, JV_PRINT_ASCII = 2, JV_PRINT_COLOUR = 4, JV_PRINT_SORTED = 8 };
|
||||
enum jv_print_flags {
|
||||
JV_PRINT_PRETTY = 1,
|
||||
JV_PRINT_ASCII = 2,
|
||||
JV_PRINT_COLOUR = 4,
|
||||
JV_PRINT_SORTED = 8,
|
||||
JV_PRINT_INVALID = 16,
|
||||
JV_PRINT_REFCOUNT = 32,
|
||||
JV_PRINT_TAB = 64,
|
||||
JV_PRINT_ISATTY = 128,
|
||||
JV_PRINT_SPACE0 = 256,
|
||||
JV_PRINT_SPACE1 = 512,
|
||||
JV_PRINT_SPACE2 = 1024,
|
||||
};
|
||||
#define JV_PRINT_INDENT_FLAGS(n) \
|
||||
((n) < 0 || (n) > 7 ? JV_PRINT_TAB | JV_PRINT_PRETTY : (n) == 0 ? 0 : (n) << 8 | JV_PRINT_PRETTY)
|
||||
void jv_dumpf(jv, FILE *f, int flags);
|
||||
void jv_dump(jv, int flags);
|
||||
void jv_show(jv, int flags);
|
||||
jv jv_dump_string(jv, int flags);
|
||||
char *jv_dump_string_trunc(jv x, char *outbuf, size_t bufsize);
|
||||
|
||||
enum {
|
||||
JV_PARSE_SEQ = 1,
|
||||
JV_PARSE_STREAMING = 2,
|
||||
JV_PARSE_STREAM_ERRORS = 4,
|
||||
};
|
||||
|
||||
jv jv_parse(const char* string);
|
||||
jv jv_parse_sized(const char* string, int length);
|
||||
@@ -122,11 +195,12 @@ void jv_nomem_handler(jv_nomem_handler_f, void *);
|
||||
|
||||
jv jv_load_file(const char *, int);
|
||||
|
||||
struct jv_parser;
|
||||
struct jv_parser* jv_parser_new();
|
||||
void jv_parser_set_buf(struct jv_parser*, const char*, int, int);
|
||||
jv jv_parser_next(struct jv_parser*);
|
||||
void jv_parser_free(struct jv_parser*);
|
||||
typedef struct jv_parser jv_parser;
|
||||
jv_parser* jv_parser_new(int);
|
||||
void jv_parser_set_buf(jv_parser*, const char*, int, int);
|
||||
int jv_parser_remaining(jv_parser*);
|
||||
jv jv_parser_next(jv_parser*);
|
||||
void jv_parser_free(jv_parser*);
|
||||
|
||||
jv jv_get(jv, jv);
|
||||
jv jv_set(jv, jv, jv);
|
||||
@@ -135,6 +209,7 @@ jv jv_setpath(jv, jv, jv);
|
||||
jv jv_getpath(jv, jv);
|
||||
jv jv_delpaths(jv, jv);
|
||||
jv jv_keys(jv /*object or array*/);
|
||||
jv jv_keys_unsorted(jv /*object or array*/);
|
||||
int jv_cmp(jv, jv);
|
||||
jv jv_group(jv, jv);
|
||||
jv jv_sort(jv, jv);
|
||||
|
||||
+80
-20
@@ -1,5 +1,6 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "jv_alloc.h"
|
||||
|
||||
struct nomem_handler {
|
||||
@@ -7,13 +8,27 @@ struct nomem_handler {
|
||||
void *data;
|
||||
};
|
||||
|
||||
#ifndef USE_PTHREAD_KEY
|
||||
#ifdef _MSC_VER
|
||||
static __declspec(thread) struct nomem_handler nomem_handler;
|
||||
#else
|
||||
static __thread struct nomem_handler nomem_handler;
|
||||
#endif
|
||||
#if !defined(HAVE_PTHREAD_KEY_CREATE) || \
|
||||
!defined(HAVE_PTHREAD_ONCE) || \
|
||||
!defined(HAVE_ATEXIT)
|
||||
|
||||
/* Try thread-local storage? */
|
||||
|
||||
#ifdef _MSC_VER
|
||||
/* Visual C++: yes */
|
||||
static __declspec(thread) struct nomem_handler nomem_handler;
|
||||
#define USE_TLS
|
||||
#else
|
||||
#ifdef HAVE___THREAD
|
||||
/* GCC and friends: yes */
|
||||
static __thread struct nomem_handler nomem_handler;
|
||||
#define USE_TLS
|
||||
#endif /* HAVE___THREAD */
|
||||
#endif /* _MSC_VER */
|
||||
|
||||
#endif /* !HAVE_PTHREAD_KEY_CREATE */
|
||||
|
||||
#ifdef USE_TLS
|
||||
void jv_nomem_handler(jv_nomem_handler_f handler, void *data) {
|
||||
nomem_handler.handler = handler;
|
||||
}
|
||||
@@ -25,44 +40,60 @@ static void memory_exhausted() {
|
||||
fprintf(stderr, "error: cannot allocate memory\n");
|
||||
abort();
|
||||
}
|
||||
#else
|
||||
#else /* USE_TLS */
|
||||
|
||||
#ifdef HAVE_PTHREAD_KEY_CREATE
|
||||
#include <pthread.h>
|
||||
|
||||
pthread_key_t nomem_handler_key;
|
||||
pthread_once_t mem_once = PTHREAD_ONCE_INIT;
|
||||
|
||||
static void tsd_fini(void) {
|
||||
struct nomem_handler *nomem_handler;
|
||||
nomem_handler = pthread_getspecific(nomem_handler_key);
|
||||
if (nomem_handler) {
|
||||
(void) pthread_setspecific(nomem_handler_key, NULL);
|
||||
free(nomem_handler);
|
||||
}
|
||||
}
|
||||
|
||||
static void tsd_init(void) {
|
||||
if (pthread_key_create(&nomem_handler_key, NULL) != 0) {
|
||||
fprintf(stderr, "error: cannot create thread specific key");
|
||||
abort();
|
||||
}
|
||||
if (atexit(tsd_fini) != 0) {
|
||||
fprintf(stderr, "error: cannot set an exit handler");
|
||||
abort();
|
||||
}
|
||||
struct nomem_handler *nomem_handler = calloc(1, sizeof(struct nomem_handler));
|
||||
if (pthread_setspecific(nomem_handler_key, nomem_handler) != 0) {
|
||||
fprintf(stderr, "error: cannot set thread specific data");
|
||||
abort();
|
||||
}
|
||||
}
|
||||
|
||||
void jv_nomem_handler(jv_nomem_handler_f handler, void *data) {
|
||||
pthread_once(&mem_once, tsd_init); // cannot fail
|
||||
struct nomem_handler *nomem_handler = calloc(1, sizeof (nomem_handler));
|
||||
struct nomem_handler *nomem_handler;
|
||||
|
||||
nomem_handler = pthread_getspecific(nomem_handler_key);
|
||||
if (nomem_handler == NULL) {
|
||||
handler(data);
|
||||
fprintf(stderr, "error: cannot allocate memory\n");
|
||||
abort();
|
||||
}
|
||||
nomem_handler.handler = handler;
|
||||
nomem_handler.data = data;
|
||||
if (pthread_setspecific(nomem_handler_key, nomem_handler) != 0) {
|
||||
handler(data);
|
||||
fprintf(stderr, "error: cannot set thread specific data");
|
||||
abort();
|
||||
}
|
||||
nomem_handler->handler = handler;
|
||||
nomem_handler->data = data;
|
||||
}
|
||||
|
||||
static void memory_exhausted() {
|
||||
jv_nomem_handler_f handler;
|
||||
struct nomem_handler *nomem_handler;
|
||||
|
||||
pthread_once(&mem_once, tsd_init);
|
||||
handler = pthread_getspecific(nomem_handler_key);
|
||||
if (handler)
|
||||
handler(); // Maybe handler() will longjmp() to safety
|
||||
nomem_handler = pthread_getspecific(nomem_handler_key);
|
||||
if (nomem_handler)
|
||||
nomem_handler->handler(nomem_handler->data); // Maybe handler() will longjmp() to safety
|
||||
// Or not
|
||||
fprintf(stderr, "error: cannot allocate memory\n");
|
||||
abort();
|
||||
@@ -70,6 +101,8 @@ static void memory_exhausted() {
|
||||
|
||||
#else
|
||||
|
||||
/* No thread-local storage of any kind that we know how to handle */
|
||||
|
||||
static struct nomem_handler nomem_handler;
|
||||
void jv_nomem_handler(jv_nomem_handler_f handler, void *data) {
|
||||
nomem_handler.handler = handler;
|
||||
@@ -82,7 +115,7 @@ static void memory_exhausted() {
|
||||
}
|
||||
|
||||
#endif /* HAVE_PTHREAD_KEY_CREATE */
|
||||
#endif /* !USE_PTHREAD_KEY */
|
||||
#endif /* USE_TLS */
|
||||
|
||||
|
||||
void* jv_mem_alloc(size_t sz) {
|
||||
@@ -97,6 +130,30 @@ void* jv_mem_alloc_unguarded(size_t sz) {
|
||||
return malloc(sz);
|
||||
}
|
||||
|
||||
void* jv_mem_calloc(size_t nemb, size_t sz) {
|
||||
void* p = calloc(nemb, sz);
|
||||
if (!p) {
|
||||
memory_exhausted();
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
void* jv_mem_calloc_unguarded(size_t nemb, size_t sz) {
|
||||
return calloc(nemb, sz);
|
||||
}
|
||||
|
||||
char* jv_mem_strdup(const char *s) {
|
||||
char *p = strdup(s);
|
||||
if (!p) {
|
||||
memory_exhausted();
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
char* jv_mem_strdup_unguarded(const char *s) {
|
||||
return strdup(s);
|
||||
}
|
||||
|
||||
void jv_mem_free(void* p) {
|
||||
free(p);
|
||||
}
|
||||
@@ -112,8 +169,11 @@ void* jv_mem_realloc(void* p, size_t sz) {
|
||||
#ifndef NDEBUG
|
||||
volatile char jv_mem_uninitialised;
|
||||
__attribute__((constructor)) void jv_mem_uninit_setup(){
|
||||
// ignore warning that this reads uninitialized memory - that's the point!
|
||||
#ifndef __clang_analyzer__
|
||||
char* p = malloc(1);
|
||||
jv_mem_uninitialised = *p;
|
||||
free(p);
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
@@ -17,6 +17,10 @@ static void jv_mem_invalidate(void* mem, size_t n) {
|
||||
|
||||
void* jv_mem_alloc(size_t);
|
||||
void* jv_mem_alloc_unguarded(size_t);
|
||||
void* jv_mem_calloc(size_t, size_t);
|
||||
void* jv_mem_calloc_unguarded(size_t, size_t);
|
||||
char* jv_mem_strdup(const char *);
|
||||
char* jv_mem_strdup_unguarded(const char *);
|
||||
void jv_mem_free(void*);
|
||||
__attribute__((warn_unused_result)) void* jv_mem_realloc(void*, size_t);
|
||||
|
||||
|
||||
@@ -3,15 +3,19 @@
|
||||
#include <assert.h>
|
||||
#include "jv_alloc.h"
|
||||
|
||||
static int parse_slice(jv array, jv slice, int* pstart, int* pend) {
|
||||
static int parse_slice(jv j, jv slice, int* pstart, int* pend) {
|
||||
// Array slices
|
||||
int len = jv_array_length(jv_copy(array));
|
||||
jv start_jv = jv_object_get(jv_copy(slice), jv_string("start"));
|
||||
jv end_jv = jv_object_get(slice, jv_string("end"));
|
||||
if (jv_get_kind(start_jv) == JV_KIND_NULL) {
|
||||
jv_free(start_jv);
|
||||
start_jv = jv_number(0);
|
||||
}
|
||||
int len;
|
||||
if (jv_get_kind(j) == JV_KIND_ARRAY)
|
||||
len = jv_array_length(jv_copy(j));
|
||||
else
|
||||
len = jv_string_length_codepoints(jv_copy(j));
|
||||
if (jv_get_kind(end_jv) == JV_KIND_NULL) {
|
||||
jv_free(end_jv);
|
||||
end_jv = jv_number(len);
|
||||
@@ -22,13 +26,18 @@ static int parse_slice(jv array, jv slice, int* pstart, int* pend) {
|
||||
jv_free(end_jv);
|
||||
return 0;
|
||||
} else {
|
||||
int start = (int)jv_number_value(start_jv);
|
||||
int end = (int)jv_number_value(end_jv);
|
||||
if (start < 0) start = len + start;
|
||||
if (end < 0) end = len + end;
|
||||
double dstart = jv_number_value(start_jv);
|
||||
double dend = jv_number_value(end_jv);
|
||||
if (dstart < 0) dstart += len;
|
||||
if (dend < 0) dend += len;
|
||||
if (dstart < 0) dstart = 0;
|
||||
if (dstart > len) dstart = len;
|
||||
|
||||
int start = (int)dstart;
|
||||
int end = (dend > len) ? len : (int)dend;
|
||||
// Ends are exclusive but e.g. 1 < 1.5 so :1.5 should be :2 not :1
|
||||
if(end < dend) end += 1;
|
||||
|
||||
if (start < 0) start = 0;
|
||||
if (start > len) start = len;
|
||||
if (end > len) end = len;
|
||||
if (end < start) end = start;
|
||||
assert(0 <= start && start <= end && end <= len);
|
||||
@@ -47,10 +56,18 @@ jv jv_get(jv t, jv k) {
|
||||
v = jv_null();
|
||||
}
|
||||
} else if (jv_get_kind(t) == JV_KIND_ARRAY && jv_get_kind(k) == JV_KIND_NUMBER) {
|
||||
// FIXME: don't do lookup for noninteger index
|
||||
v = jv_array_get(t, (int)jv_number_value(k));
|
||||
if (!jv_is_valid(v)) {
|
||||
jv_free(v);
|
||||
if(jv_is_integer(k)){
|
||||
int idx = (int)jv_number_value(k);
|
||||
if (idx < 0)
|
||||
idx += jv_array_length(jv_copy(t));
|
||||
v = jv_array_get(t, idx);
|
||||
if (!jv_is_valid(v)) {
|
||||
jv_free(v);
|
||||
v = jv_null();
|
||||
}
|
||||
} else {
|
||||
jv_free(t);
|
||||
jv_free(k);
|
||||
v = jv_null();
|
||||
}
|
||||
} else if (jv_get_kind(t) == JV_KIND_ARRAY && jv_get_kind(k) == JV_KIND_OBJECT) {
|
||||
@@ -61,17 +78,38 @@ jv jv_get(jv t, jv k) {
|
||||
v = jv_invalid_with_msg(jv_string_fmt("Start and end indices of an array slice must be numbers"));
|
||||
jv_free(t);
|
||||
}
|
||||
} else if (jv_get_kind(t) == JV_KIND_NULL &&
|
||||
(jv_get_kind(k) == JV_KIND_STRING ||
|
||||
jv_get_kind(k) == JV_KIND_NUMBER ||
|
||||
} else if (jv_get_kind(t) == JV_KIND_STRING && jv_get_kind(k) == JV_KIND_OBJECT) {
|
||||
int start, end;
|
||||
if (parse_slice(t, k, &start, &end)) {
|
||||
v = jv_string_slice(t, start, end);
|
||||
} else {
|
||||
v = jv_invalid_with_msg(jv_string_fmt("Start and end indices of an string slice must be numbers"));
|
||||
jv_free(t);
|
||||
}
|
||||
} else if (jv_get_kind(t) == JV_KIND_ARRAY && jv_get_kind(k) == JV_KIND_ARRAY) {
|
||||
v = jv_array_indexes(t, k);
|
||||
} else if (jv_get_kind(t) == JV_KIND_NULL &&
|
||||
(jv_get_kind(k) == JV_KIND_STRING ||
|
||||
jv_get_kind(k) == JV_KIND_NUMBER ||
|
||||
jv_get_kind(k) == JV_KIND_OBJECT)) {
|
||||
jv_free(t);
|
||||
jv_free(k);
|
||||
v = jv_null();
|
||||
} else {
|
||||
v = jv_invalid_with_msg(jv_string_fmt("Cannot index %s with %s",
|
||||
jv_kind_name(jv_get_kind(t)),
|
||||
jv_kind_name(jv_get_kind(k))));
|
||||
/*
|
||||
* If k is a short string it's probably from a jq .foo expression or
|
||||
* similar, in which case putting it in the invalid msg may help the
|
||||
* user. The length 30 is arbitrary.
|
||||
*/
|
||||
if (jv_get_kind(k) == JV_KIND_STRING && jv_string_length_bytes(jv_copy(k)) < 30) {
|
||||
v = jv_invalid_with_msg(jv_string_fmt("Cannot index %s with string \"%s\"",
|
||||
jv_kind_name(jv_get_kind(t)),
|
||||
jv_string_value(k)));
|
||||
} else {
|
||||
v = jv_invalid_with_msg(jv_string_fmt("Cannot index %s with %s",
|
||||
jv_kind_name(jv_get_kind(t)),
|
||||
jv_kind_name(jv_get_kind(k))));
|
||||
}
|
||||
jv_free(t);
|
||||
jv_free(k);
|
||||
}
|
||||
@@ -85,7 +123,7 @@ jv jv_set(jv t, jv k, jv v) {
|
||||
return v;
|
||||
}
|
||||
int isnull = jv_get_kind(t) == JV_KIND_NULL;
|
||||
if (jv_get_kind(k) == JV_KIND_STRING &&
|
||||
if (jv_get_kind(k) == JV_KIND_STRING &&
|
||||
(jv_get_kind(t) == JV_KIND_OBJECT || isnull)) {
|
||||
if (isnull) t = jv_object();
|
||||
t = jv_object_set(t, k, v);
|
||||
@@ -176,7 +214,7 @@ jv jv_has(jv t, jv k) {
|
||||
jv jv_dels(jv t, jv keys) {
|
||||
assert(jv_get_kind(keys) == JV_KIND_ARRAY);
|
||||
assert(jv_is_valid(t));
|
||||
|
||||
|
||||
if (jv_get_kind(t) == JV_KIND_NULL || jv_array_length(jv_copy(keys)) == 0) {
|
||||
// no change
|
||||
} else if (jv_get_kind(t) == JV_KIND_ARRAY) {
|
||||
@@ -247,7 +285,7 @@ jv jv_dels(jv t, jv keys) {
|
||||
jv_free(k);
|
||||
break;
|
||||
}
|
||||
t = jv_object_delete(t, k);
|
||||
t = jv_object_delete(t, k);
|
||||
}
|
||||
} else {
|
||||
jv err = jv_invalid_with_msg(jv_string_fmt("Cannot delete fields from %s",
|
||||
@@ -278,7 +316,7 @@ jv jv_setpath(jv root, jv path, jv value) {
|
||||
}
|
||||
jv pathcurr = jv_array_get(jv_copy(path), 0);
|
||||
jv pathrest = jv_array_slice(path, 1, jv_array_length(jv_copy(path)));
|
||||
return jv_set(root, pathcurr,
|
||||
return jv_set(root, pathcurr,
|
||||
jv_setpath(jv_get(jv_copy(root), jv_copy(pathcurr)), pathrest, value));
|
||||
}
|
||||
|
||||
@@ -344,7 +382,7 @@ static jv delpaths_sorted(jv object, jv paths, int start) {
|
||||
jv_free(paths);
|
||||
if (jv_is_valid(object))
|
||||
object = jv_dels(object, delkeys);
|
||||
else
|
||||
else
|
||||
jv_free(delkeys);
|
||||
return object;
|
||||
}
|
||||
@@ -388,6 +426,18 @@ static int string_cmp(const void* pa, const void* pb){
|
||||
return r;
|
||||
}
|
||||
|
||||
jv jv_keys_unsorted(jv x) {
|
||||
if (jv_get_kind(x) != JV_KIND_OBJECT)
|
||||
return jv_keys(x);
|
||||
jv answer = jv_array_sized(jv_object_length(jv_copy(x)));
|
||||
jv_object_foreach(x, key, value) {
|
||||
answer = jv_array_append(answer, key);
|
||||
jv_free(value);
|
||||
}
|
||||
jv_free(x);
|
||||
return answer;
|
||||
}
|
||||
|
||||
jv jv_keys(jv x) {
|
||||
if (jv_get_kind(x) == JV_KIND_OBJECT) {
|
||||
int nkeys = jv_object_length(jv_copy(x));
|
||||
@@ -438,7 +488,7 @@ int jv_cmp(jv a, jv b) {
|
||||
|
||||
case JV_KIND_NUMBER: {
|
||||
double da = jv_number_value(a), db = jv_number_value(b);
|
||||
|
||||
|
||||
// handle NaN as though it were null
|
||||
if (da != da) r = jv_cmp(jv_null(), jv_number(db));
|
||||
else if (db != db) r = jv_cmp(jv_number(da), jv_null());
|
||||
@@ -545,21 +595,23 @@ jv jv_group(jv objects, jv keys) {
|
||||
int n = jv_array_length(jv_copy(objects));
|
||||
struct sort_entry* entries = sort_items(objects, keys);
|
||||
jv ret = jv_array();
|
||||
jv curr_key = entries[0].key;
|
||||
jv group = jv_array_append(jv_array(), entries[0].object);
|
||||
for (int i = 1; i < n; i++) {
|
||||
if (jv_equal(jv_copy(curr_key), jv_copy(entries[i].key))) {
|
||||
jv_free(entries[i].key);
|
||||
} else {
|
||||
jv_free(curr_key);
|
||||
curr_key = entries[i].key;
|
||||
ret = jv_array_append(ret, group);
|
||||
group = jv_array();
|
||||
if (n > 0) {
|
||||
jv curr_key = entries[0].key;
|
||||
jv group = jv_array_append(jv_array(), entries[0].object);
|
||||
for (int i = 1; i < n; i++) {
|
||||
if (jv_equal(jv_copy(curr_key), jv_copy(entries[i].key))) {
|
||||
jv_free(entries[i].key);
|
||||
} else {
|
||||
jv_free(curr_key);
|
||||
curr_key = entries[i].key;
|
||||
ret = jv_array_append(ret, group);
|
||||
group = jv_array();
|
||||
}
|
||||
group = jv_array_append(group, entries[i].object);
|
||||
}
|
||||
group = jv_array_append(group, entries[i].object);
|
||||
jv_free(curr_key);
|
||||
ret = jv_array_append(ret, group);
|
||||
}
|
||||
jv_free(curr_key);
|
||||
ret = jv_array_append(ret, group);
|
||||
jv_mem_free(entries);
|
||||
return ret;
|
||||
}
|
||||
@@ -187,7 +187,6 @@
|
||||
* used for input more than STRTOD_DIGLIM digits long (default 40).
|
||||
*/
|
||||
|
||||
#define IEEE_8087
|
||||
#define NO_ERRNO
|
||||
#define NO_HEX_FP
|
||||
#define No_Hex_NaN
|
||||
@@ -526,7 +525,7 @@ void jvp_dtoa_context_init(struct dtoa_context* C) {
|
||||
}
|
||||
C->p5s = 0;
|
||||
}
|
||||
|
||||
|
||||
static Bigint *
|
||||
Balloc(struct dtoa_context* C, int k)
|
||||
{
|
||||
@@ -4225,7 +4224,7 @@ jvp_dtoa
|
||||
while((*b++ = *s++));
|
||||
goto done0;
|
||||
}
|
||||
if (decpt <= -4 || decpt > se - s + 5) {
|
||||
if (decpt <= -4 || decpt > se - s + 15) {
|
||||
*b++ = *s++;
|
||||
if (*s) {
|
||||
*b++ = '.';
|
||||
|
||||
@@ -17,6 +17,6 @@ double jvp_strtod(struct dtoa_context* C, const char* s, char** se);
|
||||
char* jvp_dtoa(struct dtoa_context* C, double dd, int mode, int ndigits, int *decpt, int *sign, char **rve);
|
||||
void jvp_freedtoa(struct dtoa_context* C, char *s);
|
||||
|
||||
#define JVP_DTOA_FMT_MAX_LEN 32
|
||||
#define JVP_DTOA_FMT_MAX_LEN 64
|
||||
char* jvp_dtoa_fmt(struct dtoa_context* C, register char *b, double x);
|
||||
#endif
|
||||
@@ -18,7 +18,7 @@ jv jv_load_file(const char* filename, int raw) {
|
||||
data = jv_string("");
|
||||
} else {
|
||||
data = jv_array();
|
||||
parser = jv_parser_new();
|
||||
parser = jv_parser_new(0);
|
||||
}
|
||||
while (!feof(file) && !ferror(file)) {
|
||||
char buf[4096];
|
||||
@@ -30,14 +30,17 @@ jv jv_load_file(const char* filename, int raw) {
|
||||
jv value;
|
||||
while (jv_is_valid((value = jv_parser_next(parser))))
|
||||
data = jv_array_append(data, value);
|
||||
jv_free(value);
|
||||
if (jv_invalid_has_msg(jv_copy(value))) {
|
||||
jv_free(data);
|
||||
data = value;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!raw)
|
||||
jv_parser_free(parser);
|
||||
int badread = ferror(file);
|
||||
fclose(file);
|
||||
if (badread) {
|
||||
if (fclose(file) != 0 || badread) {
|
||||
jv_free(data);
|
||||
return jv_invalid_with_msg(jv_string_fmt("Error reading from %s",
|
||||
filename));
|
||||
|
||||
+404
-38
@@ -17,60 +17,118 @@ typedef const char* presult;
|
||||
#define pfunc presult
|
||||
#endif
|
||||
|
||||
enum last_seen {
|
||||
JV_LAST_NONE = 0,
|
||||
JV_LAST_OPEN_ARRAY = '[',
|
||||
JV_LAST_OPEN_OBJECT = '{',
|
||||
JV_LAST_COLON = ':',
|
||||
JV_LAST_COMMA = ',',
|
||||
JV_LAST_VALUE = 'V',
|
||||
};
|
||||
|
||||
struct jv_parser {
|
||||
const char* curr_buf;
|
||||
int curr_buf_length;
|
||||
int curr_buf_pos;
|
||||
int curr_buf_is_partial;
|
||||
int eof;
|
||||
unsigned bom_strip_position;
|
||||
|
||||
jv* stack;
|
||||
int stackpos;
|
||||
int stacklen;
|
||||
jv next;
|
||||
|
||||
int flags;
|
||||
|
||||
jv* stack; // parser
|
||||
int stackpos; // parser
|
||||
int stacklen; // both (optimization; it's really pathlen for streaming)
|
||||
jv path; // streamer
|
||||
enum last_seen last_seen; // streamer
|
||||
jv output; // streamer
|
||||
jv next; // both
|
||||
|
||||
char* tokenbuf;
|
||||
int tokenpos;
|
||||
int tokenlen;
|
||||
|
||||
int line, column;
|
||||
|
||||
|
||||
struct dtoa_context dtoa;
|
||||
|
||||
enum {
|
||||
JV_PARSER_NORMAL,
|
||||
JV_PARSER_STRING,
|
||||
JV_PARSER_STRING_ESCAPE
|
||||
JV_PARSER_STRING_ESCAPE,
|
||||
JV_PARSER_WAITING_FOR_RS // parse error, waiting for RS
|
||||
} st;
|
||||
unsigned int last_ch_was_ws:1;
|
||||
};
|
||||
|
||||
|
||||
static void parser_init(struct jv_parser* p) {
|
||||
static void parser_init(struct jv_parser* p, int flags) {
|
||||
p->flags = flags;
|
||||
if ((p->flags & JV_PARSE_STREAMING)) {
|
||||
p->path = jv_array();
|
||||
} else {
|
||||
p->path = jv_invalid();
|
||||
p->flags &= ~(JV_PARSE_STREAM_ERRORS);
|
||||
}
|
||||
p->stack = 0;
|
||||
p->stacklen = p->stackpos = 0;
|
||||
p->last_seen = JV_LAST_NONE;
|
||||
p->output = jv_invalid();
|
||||
p->next = jv_invalid();
|
||||
p->tokenbuf = 0;
|
||||
p->tokenlen = p->tokenpos = 0;
|
||||
p->st = JV_PARSER_NORMAL;
|
||||
if ((p->flags & JV_PARSE_SEQ))
|
||||
p->st = JV_PARSER_WAITING_FOR_RS;
|
||||
else
|
||||
p->st = JV_PARSER_NORMAL;
|
||||
p->eof = 0;
|
||||
p->curr_buf = 0;
|
||||
p->curr_buf_length = p->curr_buf_pos = p->curr_buf_is_partial = 0;
|
||||
p->bom_strip_position = 0;
|
||||
p->last_ch_was_ws = 0;
|
||||
p->line = 1;
|
||||
p->column = 0;
|
||||
jvp_dtoa_context_init(&p->dtoa);
|
||||
}
|
||||
|
||||
static void parser_free(struct jv_parser* p) {
|
||||
static void parser_reset(struct jv_parser* p) {
|
||||
if ((p->flags & JV_PARSE_STREAMING)) {
|
||||
jv_free(p->path);
|
||||
p->path = jv_array();
|
||||
p->stacklen = 0;
|
||||
}
|
||||
p->last_seen = JV_LAST_NONE;
|
||||
jv_free(p->output);
|
||||
p->output = jv_invalid();
|
||||
jv_free(p->next);
|
||||
for (int i=0; i<p->stackpos; i++)
|
||||
p->next = jv_invalid();
|
||||
for (int i=0; i<p->stackpos; i++)
|
||||
jv_free(p->stack[i]);
|
||||
p->stackpos = 0;
|
||||
p->tokenpos = 0;
|
||||
p->st = JV_PARSER_NORMAL;
|
||||
}
|
||||
|
||||
static void parser_free(struct jv_parser* p) {
|
||||
parser_reset(p);
|
||||
jv_free(p->path);
|
||||
jv_free(p->output);
|
||||
jv_mem_free(p->stack);
|
||||
jv_mem_free(p->tokenbuf);
|
||||
jvp_dtoa_context_free(&p->dtoa);
|
||||
}
|
||||
|
||||
static pfunc value(struct jv_parser* p, jv val) {
|
||||
if (jv_is_valid(p->next)) return "Expected separator between values";
|
||||
if ((p->flags & JV_PARSE_STREAMING)) {
|
||||
if (jv_is_valid(p->next) || p->last_seen == JV_LAST_VALUE)
|
||||
return "Expected separator between values";
|
||||
if (p->stacklen > 0)
|
||||
p->last_seen = JV_LAST_VALUE;
|
||||
else
|
||||
p->last_seen = JV_LAST_NONE;
|
||||
} else {
|
||||
if (jv_is_valid(p->next)) return "Expected separator between values";
|
||||
}
|
||||
jv_free(p->next);
|
||||
p->next = val;
|
||||
return 0;
|
||||
@@ -86,7 +144,7 @@ static void push(struct jv_parser* p, jv v) {
|
||||
p->stack[p->stackpos++] = v;
|
||||
}
|
||||
|
||||
static pfunc token(struct jv_parser* p, char ch) {
|
||||
static pfunc parse_token(struct jv_parser* p, char ch) {
|
||||
switch (ch) {
|
||||
case '[':
|
||||
if (jv_is_valid(p->next)) return "Expected separator between values";
|
||||
@@ -99,7 +157,7 @@ static pfunc token(struct jv_parser* p, char ch) {
|
||||
break;
|
||||
|
||||
case ':':
|
||||
if (!jv_is_valid(p->next))
|
||||
if (!jv_is_valid(p->next))
|
||||
return "Expected string key before ':'";
|
||||
if (p->stackpos == 0 || jv_get_kind(p->stack[p->stackpos-1]) != JV_KIND_OBJECT)
|
||||
return "':' not as part of an object";
|
||||
@@ -119,7 +177,7 @@ static pfunc token(struct jv_parser* p, char ch) {
|
||||
p->next = jv_invalid();
|
||||
} else if (jv_get_kind(p->stack[p->stackpos-1]) == JV_KIND_STRING) {
|
||||
assert(p->stackpos > 1 && jv_get_kind(p->stack[p->stackpos-2]) == JV_KIND_OBJECT);
|
||||
p->stack[p->stackpos-2] = jv_object_set(p->stack[p->stackpos-2],
|
||||
p->stack[p->stackpos-2] = jv_object_set(p->stack[p->stackpos-2],
|
||||
p->stack[p->stackpos-1], p->next);
|
||||
p->stackpos--;
|
||||
p->next = jv_invalid();
|
||||
@@ -152,7 +210,7 @@ static pfunc token(struct jv_parser* p, char ch) {
|
||||
if (jv_get_kind(p->stack[p->stackpos-1]) != JV_KIND_STRING)
|
||||
return "Objects must consist of key:value pairs";
|
||||
assert(p->stackpos > 1 && jv_get_kind(p->stack[p->stackpos-2]) == JV_KIND_OBJECT);
|
||||
p->stack[p->stackpos-2] = jv_object_set(p->stack[p->stackpos-2],
|
||||
p->stack[p->stackpos-2] = jv_object_set(p->stack[p->stackpos-2],
|
||||
p->stack[p->stackpos-1], p->next);
|
||||
p->stackpos--;
|
||||
p->next = jv_invalid();
|
||||
@@ -169,6 +227,159 @@ static pfunc token(struct jv_parser* p, char ch) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static pfunc stream_token(struct jv_parser* p, char ch) {
|
||||
jv_kind k;
|
||||
jv last;
|
||||
|
||||
switch (ch) {
|
||||
case '[':
|
||||
if (jv_is_valid(p->next))
|
||||
return "Expected a separator between values";
|
||||
p->path = jv_array_append(p->path, jv_number(0)); // push
|
||||
p->last_seen = JV_LAST_OPEN_ARRAY;
|
||||
p->stacklen++;
|
||||
break;
|
||||
|
||||
case '{':
|
||||
if (p->last_seen == JV_LAST_VALUE)
|
||||
return "Expected a separator between values";
|
||||
// Push object key: null, since we don't know it yet
|
||||
p->path = jv_array_append(p->path, jv_null()); // push
|
||||
p->last_seen = JV_LAST_OPEN_OBJECT;
|
||||
p->stacklen++;
|
||||
break;
|
||||
|
||||
case ':':
|
||||
if (p->stacklen == 0 || jv_get_kind(jv_array_get(jv_copy(p->path), p->stacklen - 1)) == JV_KIND_NUMBER)
|
||||
return "':' not as part of an object";
|
||||
if (!jv_is_valid(p->next) || p->last_seen == JV_LAST_NONE)
|
||||
return "Expected string key before ':'";
|
||||
if (jv_get_kind(p->next) != JV_KIND_STRING)
|
||||
return "Object keys must be strings";
|
||||
if (p->last_seen != JV_LAST_VALUE)
|
||||
return "':' should follow a key";
|
||||
p->last_seen = JV_LAST_COLON;
|
||||
p->path = jv_array_set(p->path, p->stacklen - 1, p->next);
|
||||
p->next = jv_invalid();
|
||||
break;
|
||||
|
||||
case ',':
|
||||
if (p->last_seen != JV_LAST_VALUE)
|
||||
return "Expected value before ','";
|
||||
if (p->stacklen == 0)
|
||||
return "',' not as part of an object or array";
|
||||
last = jv_array_get(jv_copy(p->path), p->stacklen - 1);
|
||||
k = jv_get_kind(last);
|
||||
if (k == JV_KIND_NUMBER) {
|
||||
int idx = jv_number_value(last);
|
||||
|
||||
if (jv_is_valid(p->next)) {
|
||||
p->output = JV_ARRAY(jv_copy(p->path), p->next);
|
||||
p->next = jv_invalid();
|
||||
}
|
||||
p->path = jv_array_set(p->path, p->stacklen - 1, jv_number(idx + 1));
|
||||
p->last_seen = JV_LAST_COMMA;
|
||||
} else if (k == JV_KIND_STRING) {
|
||||
if (jv_is_valid(p->next)) {
|
||||
p->output = JV_ARRAY(jv_copy(p->path), p->next);
|
||||
p->next = jv_invalid();
|
||||
}
|
||||
p->path = jv_array_set(p->path, p->stacklen - 1, jv_true()); // ready for another name:value pair
|
||||
p->last_seen = JV_LAST_COMMA;
|
||||
} else {
|
||||
assert(k == JV_KIND_NULL);
|
||||
// this case hits on input like {,}
|
||||
// make sure to handle input like {"a", "b"} and {"a":, ...}
|
||||
jv_free(last);
|
||||
return "Objects must consist of key:value pairs";
|
||||
}
|
||||
jv_free(last);
|
||||
break;
|
||||
|
||||
case ']':
|
||||
if (p->stacklen == 0)
|
||||
return "Unmatched ']' at the top-level";
|
||||
if (p->last_seen == JV_LAST_COMMA)
|
||||
return "Expected another array element";
|
||||
if (p->last_seen == JV_LAST_OPEN_ARRAY)
|
||||
assert(!jv_is_valid(p->next));
|
||||
|
||||
last = jv_array_get(jv_copy(p->path), p->stacklen - 1);
|
||||
k = jv_get_kind(last);
|
||||
jv_free(last);
|
||||
|
||||
if (k != JV_KIND_NUMBER)
|
||||
return "Unmatched ']' in the middle of an object";
|
||||
if (jv_is_valid(p->next)) {
|
||||
p->output = JV_ARRAY(jv_copy(p->path), p->next, jv_true());
|
||||
p->next = jv_invalid();
|
||||
} else if (p->last_seen != JV_LAST_OPEN_ARRAY) {
|
||||
p->output = JV_ARRAY(jv_copy(p->path));
|
||||
}
|
||||
|
||||
p->path = jv_array_slice(p->path, 0, --(p->stacklen)); // pop
|
||||
//assert(!jv_is_valid(p->next));
|
||||
jv_free(p->next);
|
||||
p->next = jv_invalid();
|
||||
|
||||
if (p->last_seen == JV_LAST_OPEN_ARRAY)
|
||||
p->output = JV_ARRAY(jv_copy(p->path), jv_array()); // Empty arrays are leaves
|
||||
|
||||
if (p->stacklen == 0)
|
||||
p->last_seen = JV_LAST_NONE;
|
||||
else
|
||||
p->last_seen = JV_LAST_VALUE;
|
||||
break;
|
||||
|
||||
case '}':
|
||||
if (p->stacklen == 0)
|
||||
return "Unmatched '}' at the top-level";
|
||||
if (p->last_seen == JV_LAST_COMMA)
|
||||
return "Expected another key:value pair";
|
||||
if (p->last_seen == JV_LAST_OPEN_OBJECT)
|
||||
assert(!jv_is_valid(p->next));
|
||||
|
||||
last = jv_array_get(jv_copy(p->path), p->stacklen - 1);
|
||||
k = jv_get_kind(last);
|
||||
jv_free(last);
|
||||
if (k == JV_KIND_NUMBER)
|
||||
return "Unmatched '}' in the middle of an array";
|
||||
|
||||
if (jv_is_valid(p->next)) {
|
||||
if (k != JV_KIND_STRING)
|
||||
return "Objects must consist of key:value pairs";
|
||||
p->output = JV_ARRAY(jv_copy(p->path), p->next, jv_true());
|
||||
p->next = jv_invalid();
|
||||
} else {
|
||||
// Perhaps {"a":[]}
|
||||
if (p->last_seen == JV_LAST_COLON)
|
||||
// Looks like {"a":}
|
||||
return "Missing value in key:value pair";
|
||||
if (p->last_seen == JV_LAST_COMMA)
|
||||
// Looks like {"a":0,}
|
||||
return "Expected another key-value pair";
|
||||
if (p->last_seen == JV_LAST_OPEN_ARRAY)
|
||||
return "Unmatched '}' in the middle of an array";
|
||||
if (p->last_seen != JV_LAST_VALUE && p->last_seen != JV_LAST_OPEN_OBJECT)
|
||||
return "Unmatched '}'";
|
||||
if (p->last_seen != JV_LAST_OPEN_OBJECT)
|
||||
p->output = JV_ARRAY(jv_copy(p->path));
|
||||
}
|
||||
p->path = jv_array_slice(p->path, 0, --(p->stacklen)); // pop
|
||||
jv_free(p->next);
|
||||
p->next = jv_invalid();
|
||||
|
||||
if (p->last_seen == JV_LAST_OPEN_OBJECT)
|
||||
p->output = JV_ARRAY(jv_copy(p->path), jv_object()); // Empty arrays are leaves
|
||||
|
||||
if (p->stacklen == 0)
|
||||
p->last_seen = JV_LAST_NONE;
|
||||
else
|
||||
p->last_seen = JV_LAST_VALUE;
|
||||
break;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void tokenadd(struct jv_parser* p, char c) {
|
||||
assert(p->tokenpos <= p->tokenlen);
|
||||
@@ -199,7 +410,7 @@ static pfunc found_string(struct jv_parser* p) {
|
||||
char* in = p->tokenbuf;
|
||||
char* out = p->tokenbuf;
|
||||
char* end = p->tokenbuf + p->tokenpos;
|
||||
|
||||
|
||||
while (in < end) {
|
||||
char c = *in++;
|
||||
if (c == '\\') {
|
||||
@@ -236,7 +447,8 @@ static pfunc found_string(struct jv_parser* p) {
|
||||
codepoint = 0x10000 + (((codepoint - 0xD800) << 10)
|
||||
|(surrogate - 0xDC00));
|
||||
}
|
||||
// FIXME assert valid codepoint
|
||||
if (codepoint > 0x10FFFF)
|
||||
codepoint = 0xFFFD; // U+FFFD REPLACEMENT CHARACTER
|
||||
out += jvp_utf8_encode(codepoint, out);
|
||||
break;
|
||||
|
||||
@@ -244,6 +456,8 @@ static pfunc found_string(struct jv_parser* p) {
|
||||
return "Invalid escape";
|
||||
}
|
||||
} else {
|
||||
if (c > 0 && c < 0x001f)
|
||||
return "Invalid string: control characters from U+0000 through U+001F must be escaped";
|
||||
*out++ = c;
|
||||
}
|
||||
}
|
||||
@@ -265,7 +479,7 @@ static pfunc check_literal(struct jv_parser* p) {
|
||||
}
|
||||
if (pattern) {
|
||||
if (p->tokenpos != plen) return "Invalid literal";
|
||||
for (int i=0; i<plen; i++)
|
||||
for (int i=0; i<plen; i++)
|
||||
if (p->tokenbuf[i] != pattern[i])
|
||||
return "Invalid literal";
|
||||
TRY(value(p, v));
|
||||
@@ -314,7 +528,7 @@ static chclass classify(char c) {
|
||||
|
||||
static const presult OK = "output produced";
|
||||
|
||||
static int check_done(struct jv_parser* p, jv* out) {
|
||||
static int parse_check_done(struct jv_parser* p, jv* out) {
|
||||
if (p->stackpos == 0 && jv_is_valid(p->next)) {
|
||||
*out = p->next;
|
||||
p->next = jv_invalid();
|
||||
@@ -324,15 +538,85 @@ static int check_done(struct jv_parser* p, jv* out) {
|
||||
}
|
||||
}
|
||||
|
||||
static int stream_check_done(struct jv_parser* p, jv* out) {
|
||||
if (p->stacklen == 0 && jv_is_valid(p->next)) {
|
||||
*out = JV_ARRAY(jv_copy(p->path),p->next);
|
||||
p->next = jv_invalid();
|
||||
return 1;
|
||||
} else if (jv_is_valid(p->output)) {
|
||||
if (jv_array_length(jv_copy(p->output)) > 2) {
|
||||
// At end of an array or object, necessitating one more output by
|
||||
// which to indicate this
|
||||
*out = jv_array_slice(jv_copy(p->output), 0, 2);
|
||||
p->output = jv_array_slice(p->output, 0, 1); // arrange one more output
|
||||
} else {
|
||||
// No further processing needed
|
||||
*out = p->output;
|
||||
p->output = jv_invalid();
|
||||
}
|
||||
return 1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
static int parse_check_truncation(struct jv_parser* p) {
|
||||
return ((p->flags & JV_PARSE_SEQ) && !p->last_ch_was_ws && (p->stackpos > 0 || p->tokenpos > 0 || jv_get_kind(p->next) == JV_KIND_NUMBER));
|
||||
}
|
||||
|
||||
static int stream_check_truncation(struct jv_parser* p) {
|
||||
jv_kind k = jv_get_kind(p->next);
|
||||
return (p->stacklen > 0 || k == JV_KIND_NUMBER || k == JV_KIND_TRUE || k == JV_KIND_FALSE || k == JV_KIND_NULL);
|
||||
}
|
||||
|
||||
static int parse_is_top_num(struct jv_parser* p) {
|
||||
return (p->stackpos == 0 && jv_get_kind(p->next) == JV_KIND_NUMBER);
|
||||
}
|
||||
|
||||
static int stream_is_top_num(struct jv_parser* p) {
|
||||
return (p->stacklen == 0 && jv_get_kind(p->next) == JV_KIND_NUMBER);
|
||||
}
|
||||
|
||||
#define check_done(p, o) \
|
||||
(((p)->flags & JV_PARSE_STREAMING) ? stream_check_done((p), (o)) : parse_check_done((p), (o)))
|
||||
|
||||
#define token(p, ch) \
|
||||
(((p)->flags & JV_PARSE_STREAMING) ? stream_token((p), (ch)) : parse_token((p), (ch)))
|
||||
|
||||
#define check_truncation(p) \
|
||||
(((p)->flags & JV_PARSE_STREAMING) ? stream_check_truncation((p)) : parse_check_truncation((p)))
|
||||
|
||||
#define is_top_num(p) \
|
||||
(((p)->flags & JV_PARSE_STREAMING) ? stream_is_top_num((p)) : parse_is_top_num((p)))
|
||||
|
||||
static pfunc scan(struct jv_parser* p, char ch, jv* out) {
|
||||
p->column++;
|
||||
if (ch == '\n') {
|
||||
p->line++;
|
||||
p->column = 0;
|
||||
}
|
||||
if (ch == '\036' /* ASCII RS; see draft-ietf-json-sequence-07 */) {
|
||||
if (check_truncation(p)) {
|
||||
if (check_literal(p) == 0 && is_top_num(p))
|
||||
return "Potentially truncated top-level numeric value";
|
||||
return "Truncated value";
|
||||
}
|
||||
TRY(check_literal(p));
|
||||
if (p->st == JV_PARSER_NORMAL && check_done(p, out))
|
||||
return OK;
|
||||
// shouldn't happen?
|
||||
assert(!jv_is_valid(*out));
|
||||
parser_reset(p);
|
||||
jv_free(*out);
|
||||
*out = jv_invalid();
|
||||
return OK;
|
||||
}
|
||||
presult answer = 0;
|
||||
p->last_ch_was_ws = 0;
|
||||
if (p->st == JV_PARSER_NORMAL) {
|
||||
chclass cls = classify(ch);
|
||||
if (cls == WHITESPACE)
|
||||
p->last_ch_was_ws = 1;
|
||||
if (cls != LITERAL) {
|
||||
TRY(check_literal(p));
|
||||
if (check_done(p, out)) answer = OK;
|
||||
@@ -370,9 +654,10 @@ static pfunc scan(struct jv_parser* p, char ch, jv* out) {
|
||||
return answer;
|
||||
}
|
||||
|
||||
struct jv_parser* jv_parser_new() {
|
||||
struct jv_parser* jv_parser_new(int flags) {
|
||||
struct jv_parser* p = jv_mem_alloc(sizeof(struct jv_parser));
|
||||
parser_init(p);
|
||||
parser_init(p, flags);
|
||||
p->flags = flags;
|
||||
return p;
|
||||
}
|
||||
|
||||
@@ -383,10 +668,16 @@ void jv_parser_free(struct jv_parser* p) {
|
||||
|
||||
static const unsigned char UTF8_BOM[] = {0xEF,0xBB,0xBF};
|
||||
|
||||
int jv_parser_remaining(struct jv_parser* p) {
|
||||
if (p->curr_buf == 0)
|
||||
return 0;
|
||||
return (p->curr_buf_length - p->curr_buf_pos);
|
||||
}
|
||||
|
||||
void jv_parser_set_buf(struct jv_parser* p, const char* buf, int length, int is_partial) {
|
||||
assert((p->curr_buf == 0 || p->curr_buf_pos == p->curr_buf_length)
|
||||
&& "previous buffer not exhausted");
|
||||
while (p->bom_strip_position < sizeof(UTF8_BOM)) {
|
||||
while (length > 0 && p->bom_strip_position < sizeof(UTF8_BOM)) {
|
||||
if ((unsigned char)*buf == UTF8_BOM[p->bom_strip_position]) {
|
||||
// matched a BOM character
|
||||
buf++;
|
||||
@@ -408,43 +699,118 @@ void jv_parser_set_buf(struct jv_parser* p, const char* buf, int length, int is_
|
||||
p->curr_buf_is_partial = is_partial;
|
||||
}
|
||||
|
||||
static jv make_error(struct jv_parser*, const char *, ...) JV_PRINTF_LIKE(2, 3);
|
||||
|
||||
static jv make_error(struct jv_parser* p, const char *fmt, ...) {
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
jv e = jv_string_vfmt(fmt, ap);
|
||||
va_end(ap);
|
||||
if ((p->flags & JV_PARSE_STREAM_ERRORS))
|
||||
return JV_ARRAY(e, jv_copy(p->path));
|
||||
return jv_invalid_with_msg(e);
|
||||
}
|
||||
|
||||
jv jv_parser_next(struct jv_parser* p) {
|
||||
assert(p->curr_buf && "a buffer must be provided");
|
||||
if (p->bom_strip_position == 0xff) return jv_invalid_with_msg(jv_string("Malformed BOM"));
|
||||
jv value;
|
||||
if (p->eof)
|
||||
return jv_invalid();
|
||||
if (!p->curr_buf)
|
||||
return jv_invalid(); // Need a buffer
|
||||
if (p->bom_strip_position == 0xff) {
|
||||
if (!(p->flags & JV_PARSE_SEQ))
|
||||
return jv_invalid_with_msg(jv_string("Malformed BOM"));
|
||||
p->st =JV_PARSER_WAITING_FOR_RS;
|
||||
parser_reset(p);
|
||||
}
|
||||
jv value = jv_invalid();
|
||||
if ((p->flags & JV_PARSE_STREAMING) && stream_check_done(p, &value))
|
||||
return value;
|
||||
char ch;
|
||||
presult msg = 0;
|
||||
while (!msg && p->curr_buf_pos < p->curr_buf_length) {
|
||||
char ch = p->curr_buf[p->curr_buf_pos++];
|
||||
ch = p->curr_buf[p->curr_buf_pos++];
|
||||
if (p->st == JV_PARSER_WAITING_FOR_RS) {
|
||||
if (ch == '\n') {
|
||||
p->line++;
|
||||
p->column = 0;
|
||||
} else {
|
||||
p->column++;
|
||||
}
|
||||
if (ch == '\036')
|
||||
p->st = JV_PARSER_NORMAL;
|
||||
continue; // need to resync, wait for RS
|
||||
}
|
||||
msg = scan(p, ch, &value);
|
||||
}
|
||||
if (msg == OK) {
|
||||
return value;
|
||||
} else if (msg) {
|
||||
return jv_invalid_with_msg(jv_string_fmt("%s at line %d, column %d", msg, p->line, p->column));
|
||||
jv_free(value);
|
||||
if (ch != '\036' && (p->flags & JV_PARSE_SEQ)) {
|
||||
// Skip to the next RS
|
||||
p->st = JV_PARSER_WAITING_FOR_RS;
|
||||
value = make_error(p, "%s at line %d, column %d (need RS to resync)", msg, p->line, p->column);
|
||||
parser_reset(p);
|
||||
return value;
|
||||
}
|
||||
value = make_error(p, "%s at line %d, column %d", msg, p->line, p->column);
|
||||
parser_reset(p);
|
||||
if (!(p->flags & JV_PARSE_SEQ)) {
|
||||
// We're not parsing a JSON text sequence; throw this buffer away.
|
||||
// XXX We should fail permanently here.
|
||||
p->curr_buf = 0;
|
||||
p->curr_buf_pos = 0;
|
||||
} // Else ch must be RS; don't clear buf so we can start parsing again after this ch
|
||||
return value;
|
||||
} else if (p->curr_buf_is_partial) {
|
||||
assert(p->curr_buf_pos == p->curr_buf_length);
|
||||
// need another buffer
|
||||
return jv_invalid();
|
||||
} else {
|
||||
assert(p->curr_buf_pos == p->curr_buf_length);
|
||||
// at EOF
|
||||
if (p->st != JV_PARSER_NORMAL)
|
||||
return jv_invalid_with_msg(jv_string("Unfinished string"));
|
||||
if ((msg = check_literal(p)))
|
||||
return jv_invalid_with_msg(jv_string(msg));
|
||||
if (p->stackpos != 0)
|
||||
return jv_invalid_with_msg(jv_string("Unfinished JSON term"));
|
||||
// p->next is either invalid (nothing here but no syntax error)
|
||||
p->eof = 1;
|
||||
assert(p->curr_buf_pos == p->curr_buf_length);
|
||||
jv_free(value);
|
||||
if (p->st == JV_PARSER_WAITING_FOR_RS)
|
||||
return make_error(p, "Unfinished abandoned text at EOF at line %d, column %d", p->line, p->column);
|
||||
if (p->st != JV_PARSER_NORMAL) {
|
||||
value = make_error(p, "Unfinished string at EOF at line %d, column %d", p->line, p->column);
|
||||
parser_reset(p);
|
||||
p->st = JV_PARSER_WAITING_FOR_RS;
|
||||
return value;
|
||||
}
|
||||
if ((msg = check_literal(p))) {
|
||||
value = make_error(p, "%s at EOF at line %d, column %d", msg, p->line, p->column);
|
||||
parser_reset(p);
|
||||
p->st = JV_PARSER_WAITING_FOR_RS;
|
||||
return value;
|
||||
}
|
||||
if (((p->flags & JV_PARSE_STREAMING) && p->stacklen != 0) ||
|
||||
(!(p->flags & JV_PARSE_STREAMING) && p->stackpos != 0)) {
|
||||
value = make_error(p, "Unfinished JSON term at EOF at line %d, column %d", p->line, p->column);
|
||||
parser_reset(p);
|
||||
p->st = JV_PARSER_WAITING_FOR_RS;
|
||||
return value;
|
||||
}
|
||||
// p->next is either invalid (nothing here, but no syntax error)
|
||||
// or valid (this is the value). either way it's the thing to return
|
||||
value = p->next;
|
||||
if ((p->flags & JV_PARSE_STREAMING) && jv_is_valid(p->next)) {
|
||||
value = JV_ARRAY(jv_copy(p->path), p->next); // except in streaming mode we've got to make it [path,value]
|
||||
} else {
|
||||
value = p->next;
|
||||
}
|
||||
p->next = jv_invalid();
|
||||
if ((p->flags & JV_PARSE_SEQ) && !p->last_ch_was_ws && jv_get_kind(value) == JV_KIND_NUMBER) {
|
||||
jv_free(value);
|
||||
return make_error(p, "Potentially truncated top-level numeric value at EOF at line %d, column %d", p->line, p->column);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
jv jv_parse_sized(const char* string, int length) {
|
||||
struct jv_parser parser;
|
||||
parser_init(&parser);
|
||||
parser_init(&parser, 0);
|
||||
jv_parser_set_buf(&parser, string, length, 0);
|
||||
jv value = jv_parser_next(&parser);
|
||||
if (jv_is_valid(value)) {
|
||||
|
||||
+148
-77
@@ -1,9 +1,15 @@
|
||||
#include "jv.h"
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <float.h>
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
|
||||
#ifdef WIN32
|
||||
#include <windows.h>
|
||||
#include <io.h>
|
||||
#include <fileapi.h>
|
||||
#endif
|
||||
|
||||
#include "jv.h"
|
||||
#include "jv_dtoa.h"
|
||||
#include "jv_unicode.h"
|
||||
|
||||
@@ -13,75 +19,88 @@
|
||||
|
||||
// Colour table. See http://en.wikipedia.org/wiki/ANSI_escape_code#Colors
|
||||
// for how to choose these.
|
||||
static const jv_kind colour_kinds[] =
|
||||
static const jv_kind colour_kinds[] =
|
||||
{JV_KIND_NULL, JV_KIND_FALSE, JV_KIND_TRUE, JV_KIND_NUMBER,
|
||||
JV_KIND_STRING, JV_KIND_ARRAY, JV_KIND_OBJECT};
|
||||
static const char* const colours[] =
|
||||
{COL("30;1"), COL("0"), COL("0"), COL("0"),
|
||||
COL("32"), COL("37"), COL("37")};
|
||||
{COL("1;30"), COL("0;39"), COL("0;39"), COL("0;39"),
|
||||
COL("0;32"), COL("1;39"), COL("1;39")};
|
||||
#define FIELD_COLOUR COL("34;1")
|
||||
|
||||
static void put_buf(const char* s, int len, FILE* fout, jv* strout) {
|
||||
static void put_buf(const char *s, int len, FILE *fout, jv *strout, int is_tty) {
|
||||
if (strout) {
|
||||
*strout = jv_string_append_buf(*strout, s, len);
|
||||
} else {
|
||||
#ifdef WIN32
|
||||
/* See util.h */
|
||||
if (is_tty)
|
||||
WriteFile((HANDLE)_get_osfhandle(fileno(fout)), s, len, NULL, NULL);
|
||||
else
|
||||
fwrite(s, 1, len, fout);
|
||||
#else
|
||||
fwrite(s, 1, len, fout);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
static void put_char(char c, FILE* fout, jv* strout) {
|
||||
put_buf(&c, 1, fout, strout);
|
||||
static void put_char(char c, FILE* fout, jv* strout, int T) {
|
||||
put_buf(&c, 1, fout, strout, T);
|
||||
}
|
||||
|
||||
static void put_str(const char* s, FILE* fout, jv* strout) {
|
||||
put_buf(s, strlen(s), fout, strout);
|
||||
static void put_str(const char* s, FILE* fout, jv* strout, int T) {
|
||||
put_buf(s, strlen(s), fout, strout, T);
|
||||
}
|
||||
|
||||
static void put_space(int n, FILE* fout, jv* strout) {
|
||||
while (n--) {
|
||||
put_char(' ', fout, strout);
|
||||
static void put_indent(int n, int flags, FILE* fout, jv* strout, int T) {
|
||||
if (flags & JV_PRINT_TAB) {
|
||||
while (n--)
|
||||
put_char('\t', fout, strout, T);
|
||||
} else {
|
||||
n *= ((flags & (JV_PRINT_SPACE0 | JV_PRINT_SPACE1 | JV_PRINT_SPACE2)) >> 8);
|
||||
while (n--)
|
||||
put_char(' ', fout, strout, T);
|
||||
}
|
||||
}
|
||||
|
||||
static void jvp_dump_string(jv str, int ascii_only, FILE* F, jv* S) {
|
||||
static void jvp_dump_string(jv str, int ascii_only, FILE* F, jv* S, int T) {
|
||||
assert(jv_get_kind(str) == JV_KIND_STRING);
|
||||
const char* i = jv_string_value(str);
|
||||
const char* end = i + jv_string_length_bytes(jv_copy(str));
|
||||
const char* cstart;
|
||||
int c = 0;
|
||||
char buf[32];
|
||||
put_char('"', F, S);
|
||||
put_char('"', F, S, T);
|
||||
while ((i = jvp_utf8_next((cstart = i), end, &c))) {
|
||||
assert(c != -1);
|
||||
int unicode_escape = 0;
|
||||
if (0x20 <= c && c <= 0x7E) {
|
||||
// printable ASCII
|
||||
if (c == '"' || c == '\\') {
|
||||
put_char('\\', F, S);
|
||||
put_char('\\', F, S, T);
|
||||
}
|
||||
put_char(c, F, S);
|
||||
put_char(c, F, S, T);
|
||||
} else if (c < 0x20 || c == 0x7F) {
|
||||
// ASCII control character
|
||||
switch (c) {
|
||||
case '\b':
|
||||
put_char('\\', F, S);
|
||||
put_char('b', F, S);
|
||||
put_char('\\', F, S, T);
|
||||
put_char('b', F, S, T);
|
||||
break;
|
||||
case '\t':
|
||||
put_char('\\', F, S);
|
||||
put_char('t', F, S);
|
||||
put_char('\\', F, S, T);
|
||||
put_char('t', F, S, T);
|
||||
break;
|
||||
case '\r':
|
||||
put_char('\\', F, S);
|
||||
put_char('r', F, S);
|
||||
put_char('\\', F, S, T);
|
||||
put_char('r', F, S, T);
|
||||
break;
|
||||
case '\n':
|
||||
put_char('\\', F, S);
|
||||
put_char('n', F, S);
|
||||
put_char('\\', F, S, T);
|
||||
put_char('n', F, S, T);
|
||||
break;
|
||||
case '\f':
|
||||
put_char('\\', F, S);
|
||||
put_char('f', F, S);
|
||||
put_char('\\', F, S, T);
|
||||
put_char('f', F, S, T);
|
||||
break;
|
||||
default:
|
||||
unicode_escape = 1;
|
||||
@@ -91,7 +110,7 @@ static void jvp_dump_string(jv str, int ascii_only, FILE* F, jv* S) {
|
||||
if (ascii_only) {
|
||||
unicode_escape = 1;
|
||||
} else {
|
||||
put_buf(cstart, i - cstart, F, S);
|
||||
put_buf(cstart, i - cstart, F, S, T);
|
||||
}
|
||||
}
|
||||
if (unicode_escape) {
|
||||
@@ -99,27 +118,34 @@ static void jvp_dump_string(jv str, int ascii_only, FILE* F, jv* S) {
|
||||
sprintf(buf, "\\u%04x", c);
|
||||
} else {
|
||||
c -= 0x10000;
|
||||
sprintf(buf, "\\u%04x\\u%04x",
|
||||
sprintf(buf, "\\u%04x\\u%04x",
|
||||
0xD800 | ((c & 0xffc00) >> 10),
|
||||
0xDC00 | (c & 0x003ff));
|
||||
}
|
||||
put_str(buf, F, S);
|
||||
put_str(buf, F, S, T);
|
||||
}
|
||||
}
|
||||
assert(c != -1);
|
||||
put_char('"', F, S);
|
||||
put_char('"', F, S, T);
|
||||
}
|
||||
|
||||
enum { INDENT = 2 };
|
||||
static void put_refcnt(struct dtoa_context* C, int refcnt, FILE *F, jv* S, int T){
|
||||
char buf[JVP_DTOA_FMT_MAX_LEN];
|
||||
put_char(' ', F, S, T);
|
||||
put_char('(', F, S, T);
|
||||
put_str(jvp_dtoa_fmt(C, buf, refcnt), F, S, T);
|
||||
put_char(')', F, S, T);
|
||||
}
|
||||
|
||||
static void jv_dump_term(struct dtoa_context* C, jv x, int flags, int indent, FILE* F, jv* S) {
|
||||
char buf[JVP_DTOA_FMT_MAX_LEN];
|
||||
const char* colour = 0;
|
||||
double refcnt = (flags & JV_PRINT_REFCOUNT) ? jv_get_refcnt(x) - 1 : -1;
|
||||
if (flags & JV_PRINT_COLOUR) {
|
||||
for (unsigned i=0; i<sizeof(colour_kinds)/sizeof(colour_kinds[0]); i++) {
|
||||
if (jv_get_kind(x) == colour_kinds[i]) {
|
||||
colour = colours[i];
|
||||
put_str(colour, F, S);
|
||||
put_str(colour, F, S, flags & JV_PRINT_ISATTY);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -127,72 +153,87 @@ static void jv_dump_term(struct dtoa_context* C, jv x, int flags, int indent, FI
|
||||
switch (jv_get_kind(x)) {
|
||||
default:
|
||||
case JV_KIND_INVALID:
|
||||
assert(0 && "Invalid value");
|
||||
if (flags & JV_PRINT_INVALID) {
|
||||
jv msg = jv_invalid_get_msg(jv_copy(x));
|
||||
if (jv_get_kind(msg) == JV_KIND_STRING) {
|
||||
put_str("<invalid:", F, S, flags & JV_PRINT_ISATTY);
|
||||
jvp_dump_string(msg, flags | JV_PRINT_ASCII, F, S, flags & JV_PRINT_ISATTY);
|
||||
put_str(">", F, S, flags & JV_PRINT_ISATTY);
|
||||
} else {
|
||||
put_str("<invalid>", F, S, flags & JV_PRINT_ISATTY);
|
||||
}
|
||||
} else {
|
||||
assert(0 && "Invalid value");
|
||||
}
|
||||
break;
|
||||
case JV_KIND_NULL:
|
||||
put_str("null", F, S);
|
||||
put_str("null", F, S, flags & JV_PRINT_ISATTY);
|
||||
break;
|
||||
case JV_KIND_FALSE:
|
||||
put_str("false", F, S);
|
||||
put_str("false", F, S, flags & JV_PRINT_ISATTY);
|
||||
break;
|
||||
case JV_KIND_TRUE:
|
||||
put_str("true", F, S);
|
||||
put_str("true", F, S, flags & JV_PRINT_ISATTY);
|
||||
break;
|
||||
case JV_KIND_NUMBER: {
|
||||
double d = jv_number_value(x);
|
||||
if (d != d) {
|
||||
// JSON doesn't have NaN, so we'll render it as "null"
|
||||
put_str("null", F, S);
|
||||
put_str("null", F, S, flags & JV_PRINT_ISATTY);
|
||||
} else {
|
||||
// Normalise infinities to something we can print in valid JSON
|
||||
if (d > DBL_MAX) d = DBL_MAX;
|
||||
if (d < -DBL_MAX) d = -DBL_MAX;
|
||||
put_str(jvp_dtoa_fmt(C, buf, d), F, S);
|
||||
put_str(jvp_dtoa_fmt(C, buf, d), F, S, flags & JV_PRINT_ISATTY);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case JV_KIND_STRING:
|
||||
jvp_dump_string(x, flags & JV_PRINT_ASCII, F, S);
|
||||
jvp_dump_string(x, flags & JV_PRINT_ASCII, F, S, flags & JV_PRINT_ISATTY);
|
||||
if (flags & JV_PRINT_REFCOUNT)
|
||||
put_refcnt(C, refcnt, F, S, flags & JV_PRINT_ISATTY);
|
||||
break;
|
||||
case JV_KIND_ARRAY: {
|
||||
if (jv_array_length(jv_copy(x)) == 0) {
|
||||
put_str("[]", F, S);
|
||||
put_str("[]", F, S, flags & JV_PRINT_ISATTY);
|
||||
break;
|
||||
}
|
||||
put_str("[", F, S);
|
||||
put_str("[", F, S, flags & JV_PRINT_ISATTY);
|
||||
if (flags & JV_PRINT_PRETTY) {
|
||||
put_char('\n', F, S);
|
||||
put_space(indent+INDENT, F, S);
|
||||
put_char('\n', F, S, flags & JV_PRINT_ISATTY);
|
||||
put_indent(indent + 1, flags, F, S, flags & JV_PRINT_ISATTY);
|
||||
}
|
||||
jv_array_foreach(x, i, elem) {
|
||||
if (i!=0) {
|
||||
if (flags & JV_PRINT_PRETTY) {
|
||||
put_str(",\n", F, S);
|
||||
put_space(indent+INDENT, F, S);
|
||||
put_str(",\n", F, S, flags & JV_PRINT_ISATTY);
|
||||
put_indent(indent + 1, flags, F, S, flags & JV_PRINT_ISATTY);
|
||||
} else {
|
||||
put_str(",", F, S);
|
||||
put_str(",", F, S, flags & JV_PRINT_ISATTY);
|
||||
}
|
||||
}
|
||||
jv_dump_term(C, elem, flags, indent + INDENT, F, S);
|
||||
if (colour) put_str(colour, F, S);
|
||||
jv_dump_term(C, elem, flags, indent + 1, F, S);
|
||||
if (colour) put_str(colour, F, S, flags & JV_PRINT_ISATTY);
|
||||
}
|
||||
if (flags & JV_PRINT_PRETTY) {
|
||||
put_char('\n', F, S);
|
||||
put_space(indent, F, S);
|
||||
put_char('\n', F, S, flags & JV_PRINT_ISATTY);
|
||||
put_indent(indent, flags, F, S, flags & JV_PRINT_ISATTY);
|
||||
}
|
||||
if (colour) put_str(colour, F, S);
|
||||
put_char(']', F, S);
|
||||
if (colour) put_str(colour, F, S, flags & JV_PRINT_ISATTY);
|
||||
put_char(']', F, S, flags & JV_PRINT_ISATTY);
|
||||
if (flags & JV_PRINT_REFCOUNT)
|
||||
put_refcnt(C, refcnt, F, S, flags & JV_PRINT_ISATTY);
|
||||
break;
|
||||
}
|
||||
case JV_KIND_OBJECT: {
|
||||
if (jv_object_length(jv_copy(x)) == 0) {
|
||||
put_str("{}", F, S);
|
||||
put_str("{}", F, S, flags & JV_PRINT_ISATTY);
|
||||
break;
|
||||
}
|
||||
put_char('{', F, S);
|
||||
put_char('{', F, S, flags & JV_PRINT_ISATTY);
|
||||
if (flags & JV_PRINT_PRETTY) {
|
||||
put_char('\n', F, S);
|
||||
put_space(indent+INDENT, F, S);
|
||||
put_char('\n', F, S, flags & JV_PRINT_ISATTY);
|
||||
put_indent(indent + 1, flags, F, S, flags & JV_PRINT_ISATTY);
|
||||
}
|
||||
int first = 1;
|
||||
int i = 0;
|
||||
@@ -225,48 +266,62 @@ static void jv_dump_term(struct dtoa_context* C, jv x, int flags, int indent, FI
|
||||
|
||||
if (!first) {
|
||||
if (flags & JV_PRINT_PRETTY){
|
||||
put_str(",\n", F, S);
|
||||
put_space(indent+INDENT, F, S);
|
||||
put_str(",\n", F, S, flags & JV_PRINT_ISATTY);
|
||||
put_indent(indent + 1, flags, F, S, flags & JV_PRINT_ISATTY);
|
||||
} else {
|
||||
put_str(",", F, S);
|
||||
put_str(",", F, S, flags & JV_PRINT_ISATTY);
|
||||
}
|
||||
}
|
||||
if (colour) put_str(COLRESET, F, S);
|
||||
if (colour) put_str(COLRESET, F, S, flags & JV_PRINT_ISATTY);
|
||||
|
||||
first = 0;
|
||||
if (colour) put_str(FIELD_COLOUR, F, S);
|
||||
jvp_dump_string(key, flags & JV_PRINT_ASCII, F, S);
|
||||
if (colour) put_str(FIELD_COLOUR, F, S, flags & JV_PRINT_ISATTY);
|
||||
jvp_dump_string(key, flags & JV_PRINT_ASCII, F, S, flags & JV_PRINT_ISATTY);
|
||||
jv_free(key);
|
||||
if (colour) put_str(COLRESET, F, S);
|
||||
if (colour) put_str(COLRESET, F, S, flags & JV_PRINT_ISATTY);
|
||||
|
||||
if (colour) put_str(colour, F, S);
|
||||
put_str((flags & JV_PRINT_PRETTY) ? ": " : ":", F, S);
|
||||
if (colour) put_str(COLRESET, F, S);
|
||||
|
||||
jv_dump_term(C, value, flags, indent + INDENT, F, S);
|
||||
if (colour) put_str(colour, F, S);
|
||||
if (colour) put_str(colour, F, S, flags & JV_PRINT_ISATTY);
|
||||
put_str((flags & JV_PRINT_PRETTY) ? ": " : ":", F, S, flags & JV_PRINT_ISATTY);
|
||||
if (colour) put_str(COLRESET, F, S, flags & JV_PRINT_ISATTY);
|
||||
|
||||
jv_dump_term(C, value, flags, indent + 1, F, S);
|
||||
if (colour) put_str(colour, F, S, flags & JV_PRINT_ISATTY);
|
||||
}
|
||||
if (flags & JV_PRINT_PRETTY) {
|
||||
put_char('\n', F, S);
|
||||
put_space(indent, F, S);
|
||||
put_char('\n', F, S, flags & JV_PRINT_ISATTY);
|
||||
put_indent(indent, flags, F, S, flags & JV_PRINT_ISATTY);
|
||||
}
|
||||
if (colour) put_str(colour, F, S);
|
||||
put_char('}', F, S);
|
||||
if (colour) put_str(colour, F, S, flags & JV_PRINT_ISATTY);
|
||||
put_char('}', F, S, flags & JV_PRINT_ISATTY);
|
||||
if (flags & JV_PRINT_REFCOUNT)
|
||||
put_refcnt(C, refcnt, F, S, flags & JV_PRINT_ISATTY);
|
||||
}
|
||||
}
|
||||
jv_free(x);
|
||||
if (colour) {
|
||||
put_str(COLRESET, F, S);
|
||||
put_str(COLRESET, F, S, flags & JV_PRINT_ISATTY);
|
||||
}
|
||||
}
|
||||
|
||||
void jv_dump(jv x, int flags) {
|
||||
void jv_dumpf(jv x, FILE *f, int flags) {
|
||||
struct dtoa_context C;
|
||||
jvp_dtoa_context_init(&C);
|
||||
jv_dump_term(&C, x, flags, 0, stdout, 0);
|
||||
jv_dump_term(&C, x, flags, 0, f, 0);
|
||||
jvp_dtoa_context_free(&C);
|
||||
}
|
||||
|
||||
void jv_dump(jv x, int flags) {
|
||||
jv_dumpf(x, stdout, flags);
|
||||
}
|
||||
|
||||
/* This one is nice for use in debuggers */
|
||||
void jv_show(jv x, int flags) {
|
||||
if (flags == -1)
|
||||
flags = JV_PRINT_PRETTY | JV_PRINT_COLOUR | JV_PRINT_INDENT_FLAGS(2);
|
||||
jv_dumpf(jv_copy(x), stderr, flags | JV_PRINT_INVALID);
|
||||
fflush(stderr);
|
||||
}
|
||||
|
||||
jv jv_dump_string(jv x, int flags) {
|
||||
struct dtoa_context C;
|
||||
jvp_dtoa_context_init(&C);
|
||||
@@ -275,3 +330,19 @@ jv jv_dump_string(jv x, int flags) {
|
||||
jvp_dtoa_context_free(&C);
|
||||
return s;
|
||||
}
|
||||
|
||||
char *jv_dump_string_trunc(jv x, char *outbuf, size_t bufsize) {
|
||||
x = jv_dump_string(x,0);
|
||||
const char* p = jv_string_value(x);
|
||||
const size_t len = strlen(p);
|
||||
strncpy(outbuf, p, bufsize);
|
||||
outbuf[bufsize - 1] = 0;
|
||||
if (len > bufsize - 1 && bufsize >= 4) {
|
||||
// Indicate truncation with '...'
|
||||
outbuf[bufsize - 2]='.';
|
||||
outbuf[bufsize - 3]='.';
|
||||
outbuf[bufsize - 4]='.';
|
||||
}
|
||||
jv_free(x);
|
||||
return outbuf;
|
||||
}
|
||||
+48
-16
@@ -3,35 +3,67 @@
|
||||
#include "jv_unicode.h"
|
||||
#include "jv_utf8_tables.h"
|
||||
|
||||
const char* jvp_utf8_next(const char* in, const char* end, int* codepoint) {
|
||||
const char* jvp_utf8_next(const char* in, const char* end, int* codepoint_ret) {
|
||||
assert(in <= end);
|
||||
if (in == end) {
|
||||
codepoint = 0;
|
||||
return 0;
|
||||
}
|
||||
int codepoint = -1;
|
||||
unsigned char first = (unsigned char)in[0];
|
||||
int length = utf8_coding_length[first];
|
||||
if (length == 0 || length == UTF8_CONTINUATION_BYTE || in + length > end) {
|
||||
*codepoint = -1;
|
||||
return 0;
|
||||
}
|
||||
*codepoint = ((unsigned)in[0]) & utf8_coding_bits[first];
|
||||
for (int i=1; i<length; i++) {
|
||||
int ch = (unsigned char)in[i];
|
||||
if (utf8_coding_length[(unsigned char)in[i]] != UTF8_CONTINUATION_BYTE){
|
||||
*codepoint = -1;
|
||||
return 0;
|
||||
if ((first & 0x80) == 0) {
|
||||
/* Fast-path for ASCII */
|
||||
codepoint = first;
|
||||
length = 1;
|
||||
} else if (length == 0 || length == UTF8_CONTINUATION_BYTE) {
|
||||
/* Bad single byte - either an invalid byte or an out-of-place continuation byte */
|
||||
length = 1;
|
||||
} else if (in + length > end) {
|
||||
/* String ends before UTF8 sequence ends */
|
||||
length = end - in;
|
||||
} else {
|
||||
codepoint = ((unsigned)in[0]) & utf8_coding_bits[first];
|
||||
for (int i=1; i<length; i++) {
|
||||
unsigned ch = (unsigned char)in[i];
|
||||
if (utf8_coding_length[ch] != UTF8_CONTINUATION_BYTE){
|
||||
/* Invalid UTF8 sequence - not followed by the right number of continuation bytes */
|
||||
codepoint = -1;
|
||||
length = i;
|
||||
break;
|
||||
}
|
||||
codepoint = (codepoint << 6) | (ch & 0x3f);
|
||||
}
|
||||
if (codepoint < utf8_first_codepoint[length]) {
|
||||
/* Overlong UTF8 sequence */
|
||||
codepoint = -1;
|
||||
}
|
||||
if (0xD800 <= codepoint && codepoint <= 0xDFFF) {
|
||||
/* Surrogate codepoints can't be encoded in UTF8 */
|
||||
codepoint = -1;
|
||||
}
|
||||
if (codepoint > 0x10FFFF) {
|
||||
/* Outside Unicode range */
|
||||
codepoint = -1;
|
||||
}
|
||||
*codepoint = (*codepoint << 6) | (ch & 0x3f);
|
||||
}
|
||||
assert(length > 0);
|
||||
*codepoint_ret = codepoint;
|
||||
return in + length;
|
||||
}
|
||||
|
||||
int jvp_utf8_verify(const char* in, const char* end) {
|
||||
int codepoint = 0;
|
||||
int jvp_utf8_is_valid(const char* in, const char* end) {
|
||||
int codepoint;
|
||||
while ((in = jvp_utf8_next(in, end, &codepoint))) {
|
||||
if (codepoint == -1) return 0;
|
||||
}
|
||||
return codepoint != -1;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int jvp_utf8_decode_length(char startchar) {
|
||||
if ((startchar & 0x80) == 0) return 1;
|
||||
else if ((startchar & 0xC0) == 0xC0) return 2;
|
||||
else if ((startchar & 0xE0) == 0xE0) return 3;
|
||||
else return 4;
|
||||
}
|
||||
|
||||
int jvp_utf8_encode_length(int codepoint) {
|
||||
|
||||
+1
-1
@@ -2,7 +2,7 @@
|
||||
#define JV_UNICODE_H
|
||||
|
||||
const char* jvp_utf8_next(const char* in, const char* end, int* codepoint);
|
||||
|
||||
int jvp_utf8_is_valid(const char* in, const char* end);
|
||||
|
||||
int jvp_utf8_decode_length(char startchar);
|
||||
|
||||
|
||||
@@ -33,3 +33,5 @@ static const unsigned char utf8_coding_bits[] =
|
||||
0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f,
|
||||
0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f,
|
||||
0x07, 0x07, 0x07, 0x07, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
|
||||
static const int utf8_first_codepoint[] =
|
||||
{0x00, 0x00, 0x80, 0x800, 0x10000};
|
||||
@@ -0,0 +1,362 @@
|
||||
#ifndef jq_yyHEADER_H
|
||||
#define jq_yyHEADER_H 1
|
||||
#define jq_yyIN_HEADER 1
|
||||
|
||||
#line 6 "lexer.h"
|
||||
|
||||
#line 8 "lexer.h"
|
||||
|
||||
#define YY_INT_ALIGNED short int
|
||||
|
||||
/* A lexical scanner generated by flex */
|
||||
|
||||
#define FLEX_SCANNER
|
||||
#define YY_FLEX_MAJOR_VERSION 2
|
||||
#define YY_FLEX_MINOR_VERSION 5
|
||||
#define YY_FLEX_SUBMINOR_VERSION 35
|
||||
#if YY_FLEX_SUBMINOR_VERSION > 0
|
||||
#define FLEX_BETA
|
||||
#endif
|
||||
|
||||
/* First, we deal with platform-specific or compiler-specific issues. */
|
||||
|
||||
/* begin standard C headers. */
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
/* end standard C headers. */
|
||||
|
||||
/* flex integer type definitions */
|
||||
|
||||
#ifndef FLEXINT_H
|
||||
#define FLEXINT_H
|
||||
|
||||
/* C99 systems have <inttypes.h>. Non-C99 systems may or may not. */
|
||||
|
||||
#if defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
|
||||
|
||||
/* C99 says to define __STDC_LIMIT_MACROS before including stdint.h,
|
||||
* if you want the limit (max/min) macros for int types.
|
||||
*/
|
||||
#ifndef __STDC_LIMIT_MACROS
|
||||
#define __STDC_LIMIT_MACROS 1
|
||||
#endif
|
||||
|
||||
#include <inttypes.h>
|
||||
typedef int8_t flex_int8_t;
|
||||
typedef uint8_t flex_uint8_t;
|
||||
typedef int16_t flex_int16_t;
|
||||
typedef uint16_t flex_uint16_t;
|
||||
typedef int32_t flex_int32_t;
|
||||
typedef uint32_t flex_uint32_t;
|
||||
#else
|
||||
typedef signed char flex_int8_t;
|
||||
typedef short int flex_int16_t;
|
||||
typedef int flex_int32_t;
|
||||
typedef unsigned char flex_uint8_t;
|
||||
typedef unsigned short int flex_uint16_t;
|
||||
typedef unsigned int flex_uint32_t;
|
||||
|
||||
/* Limits of integral types. */
|
||||
#ifndef INT8_MIN
|
||||
#define INT8_MIN (-128)
|
||||
#endif
|
||||
#ifndef INT16_MIN
|
||||
#define INT16_MIN (-32767-1)
|
||||
#endif
|
||||
#ifndef INT32_MIN
|
||||
#define INT32_MIN (-2147483647-1)
|
||||
#endif
|
||||
#ifndef INT8_MAX
|
||||
#define INT8_MAX (127)
|
||||
#endif
|
||||
#ifndef INT16_MAX
|
||||
#define INT16_MAX (32767)
|
||||
#endif
|
||||
#ifndef INT32_MAX
|
||||
#define INT32_MAX (2147483647)
|
||||
#endif
|
||||
#ifndef UINT8_MAX
|
||||
#define UINT8_MAX (255U)
|
||||
#endif
|
||||
#ifndef UINT16_MAX
|
||||
#define UINT16_MAX (65535U)
|
||||
#endif
|
||||
#ifndef UINT32_MAX
|
||||
#define UINT32_MAX (4294967295U)
|
||||
#endif
|
||||
|
||||
#endif /* ! C99 */
|
||||
|
||||
#endif /* ! FLEXINT_H */
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
||||
/* The "const" storage-class-modifier is valid. */
|
||||
#define YY_USE_CONST
|
||||
|
||||
#else /* ! __cplusplus */
|
||||
|
||||
/* C99 requires __STDC__ to be defined as 1. */
|
||||
#if defined (__STDC__)
|
||||
|
||||
#define YY_USE_CONST
|
||||
|
||||
#endif /* defined (__STDC__) */
|
||||
#endif /* ! __cplusplus */
|
||||
|
||||
#ifdef YY_USE_CONST
|
||||
#define yyconst const
|
||||
#else
|
||||
#define yyconst
|
||||
#endif
|
||||
|
||||
/* An opaque pointer. */
|
||||
#ifndef YY_TYPEDEF_YY_SCANNER_T
|
||||
#define YY_TYPEDEF_YY_SCANNER_T
|
||||
typedef void* yyscan_t;
|
||||
#endif
|
||||
|
||||
/* For convenience, these vars (plus the bison vars far below)
|
||||
are macros in the reentrant scanner. */
|
||||
#define yyin yyg->yyin_r
|
||||
#define yyout yyg->yyout_r
|
||||
#define yyextra yyg->yyextra_r
|
||||
#define yyleng yyg->yyleng_r
|
||||
#define yytext yyg->yytext_r
|
||||
#define yylineno (YY_CURRENT_BUFFER_LVALUE->yy_bs_lineno)
|
||||
#define yycolumn (YY_CURRENT_BUFFER_LVALUE->yy_bs_column)
|
||||
#define yy_flex_debug yyg->yy_flex_debug_r
|
||||
|
||||
/* Size of default input buffer. */
|
||||
#ifndef YY_BUF_SIZE
|
||||
#ifdef __ia64__
|
||||
/* On IA-64, the buffer size is 16k, not 8k.
|
||||
* Moreover, YY_BUF_SIZE is 2*YY_READ_BUF_SIZE in the general case.
|
||||
* Ditto for the __ia64__ case accordingly.
|
||||
*/
|
||||
#define YY_BUF_SIZE 32768
|
||||
#else
|
||||
#define YY_BUF_SIZE 16384
|
||||
#endif /* __ia64__ */
|
||||
#endif
|
||||
|
||||
#ifndef YY_TYPEDEF_YY_BUFFER_STATE
|
||||
#define YY_TYPEDEF_YY_BUFFER_STATE
|
||||
typedef struct yy_buffer_state *YY_BUFFER_STATE;
|
||||
#endif
|
||||
|
||||
#ifndef YY_TYPEDEF_YY_SIZE_T
|
||||
#define YY_TYPEDEF_YY_SIZE_T
|
||||
typedef size_t yy_size_t;
|
||||
#endif
|
||||
|
||||
#ifndef YY_STRUCT_YY_BUFFER_STATE
|
||||
#define YY_STRUCT_YY_BUFFER_STATE
|
||||
struct yy_buffer_state
|
||||
{
|
||||
FILE *yy_input_file;
|
||||
|
||||
char *yy_ch_buf; /* input buffer */
|
||||
char *yy_buf_pos; /* current position in input buffer */
|
||||
|
||||
/* Size of input buffer in bytes, not including room for EOB
|
||||
* characters.
|
||||
*/
|
||||
yy_size_t yy_buf_size;
|
||||
|
||||
/* Number of characters read into yy_ch_buf, not including EOB
|
||||
* characters.
|
||||
*/
|
||||
int yy_n_chars;
|
||||
|
||||
/* Whether we "own" the buffer - i.e., we know we created it,
|
||||
* and can realloc() it to grow it, and should free() it to
|
||||
* delete it.
|
||||
*/
|
||||
int yy_is_our_buffer;
|
||||
|
||||
/* Whether this is an "interactive" input source; if so, and
|
||||
* if we're using stdio for input, then we want to use getc()
|
||||
* instead of fread(), to make sure we stop fetching input after
|
||||
* each newline.
|
||||
*/
|
||||
int yy_is_interactive;
|
||||
|
||||
/* Whether we're considered to be at the beginning of a line.
|
||||
* If so, '^' rules will be active on the next match, otherwise
|
||||
* not.
|
||||
*/
|
||||
int yy_at_bol;
|
||||
|
||||
int yy_bs_lineno; /**< The line count. */
|
||||
int yy_bs_column; /**< The column count. */
|
||||
|
||||
/* Whether to try to fill the input buffer when we reach the
|
||||
* end of it.
|
||||
*/
|
||||
int yy_fill_buffer;
|
||||
|
||||
int yy_buffer_status;
|
||||
|
||||
};
|
||||
#endif /* !YY_STRUCT_YY_BUFFER_STATE */
|
||||
|
||||
void jq_yyrestart (FILE *input_file ,yyscan_t yyscanner );
|
||||
void jq_yy_switch_to_buffer (YY_BUFFER_STATE new_buffer ,yyscan_t yyscanner );
|
||||
YY_BUFFER_STATE jq_yy_create_buffer (FILE *file,int size ,yyscan_t yyscanner );
|
||||
void jq_yy_delete_buffer (YY_BUFFER_STATE b ,yyscan_t yyscanner );
|
||||
void jq_yy_flush_buffer (YY_BUFFER_STATE b ,yyscan_t yyscanner );
|
||||
void jq_yypush_buffer_state (YY_BUFFER_STATE new_buffer ,yyscan_t yyscanner );
|
||||
void jq_yypop_buffer_state (yyscan_t yyscanner );
|
||||
|
||||
YY_BUFFER_STATE jq_yy_scan_buffer (char *base,yy_size_t size ,yyscan_t yyscanner );
|
||||
YY_BUFFER_STATE jq_yy_scan_string (yyconst char *yy_str ,yyscan_t yyscanner );
|
||||
YY_BUFFER_STATE jq_yy_scan_bytes (yyconst char *bytes,int len ,yyscan_t yyscanner );
|
||||
|
||||
void *jq_yyalloc (yy_size_t ,yyscan_t yyscanner );
|
||||
void *jq_yyrealloc (void *,yy_size_t ,yyscan_t yyscanner );
|
||||
void jq_yyfree (void * ,yyscan_t yyscanner );
|
||||
|
||||
/* Begin user sect3 */
|
||||
|
||||
#define jq_yywrap(n) 1
|
||||
#define YY_SKIP_YYWRAP
|
||||
|
||||
#define yytext_ptr yytext_r
|
||||
|
||||
#ifdef YY_HEADER_EXPORT_START_CONDITIONS
|
||||
#define INITIAL 0
|
||||
#define IN_PAREN 1
|
||||
#define IN_BRACKET 2
|
||||
#define IN_BRACE 3
|
||||
#define IN_QQINTERP 4
|
||||
#define IN_QQSTRING 5
|
||||
|
||||
#endif
|
||||
|
||||
#ifndef YY_NO_UNISTD_H
|
||||
/* Special case for "unistd.h", since it is non-ANSI. We include it way
|
||||
* down here because we want the user's section 1 to have been scanned first.
|
||||
* The user has a chance to override it with an option.
|
||||
*/
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
#define YY_EXTRA_TYPE int
|
||||
|
||||
int jq_yylex_init (yyscan_t* scanner);
|
||||
|
||||
int jq_yylex_init_extra (YY_EXTRA_TYPE user_defined,yyscan_t* scanner);
|
||||
|
||||
/* Accessor methods to globals.
|
||||
These are made visible to non-reentrant scanners for convenience. */
|
||||
|
||||
int jq_yylex_destroy (yyscan_t yyscanner );
|
||||
|
||||
int jq_yyget_debug (yyscan_t yyscanner );
|
||||
|
||||
void jq_yyset_debug (int debug_flag ,yyscan_t yyscanner );
|
||||
|
||||
YY_EXTRA_TYPE jq_yyget_extra (yyscan_t yyscanner );
|
||||
|
||||
void jq_yyset_extra (YY_EXTRA_TYPE user_defined ,yyscan_t yyscanner );
|
||||
|
||||
FILE *jq_yyget_in (yyscan_t yyscanner );
|
||||
|
||||
void jq_yyset_in (FILE * in_str ,yyscan_t yyscanner );
|
||||
|
||||
FILE *jq_yyget_out (yyscan_t yyscanner );
|
||||
|
||||
void jq_yyset_out (FILE * out_str ,yyscan_t yyscanner );
|
||||
|
||||
int jq_yyget_leng (yyscan_t yyscanner );
|
||||
|
||||
char *jq_yyget_text (yyscan_t yyscanner );
|
||||
|
||||
int jq_yyget_lineno (yyscan_t yyscanner );
|
||||
|
||||
void jq_yyset_lineno (int line_number ,yyscan_t yyscanner );
|
||||
|
||||
YYSTYPE * jq_yyget_lval (yyscan_t yyscanner );
|
||||
|
||||
void jq_yyset_lval (YYSTYPE * yylval_param ,yyscan_t yyscanner );
|
||||
|
||||
YYLTYPE *jq_yyget_lloc (yyscan_t yyscanner );
|
||||
|
||||
void jq_yyset_lloc (YYLTYPE * yylloc_param ,yyscan_t yyscanner );
|
||||
|
||||
/* Macros after this point can all be overridden by user definitions in
|
||||
* section 1.
|
||||
*/
|
||||
|
||||
#ifndef YY_SKIP_YYWRAP
|
||||
#ifdef __cplusplus
|
||||
extern "C" int jq_yywrap (yyscan_t yyscanner );
|
||||
#else
|
||||
extern int jq_yywrap (yyscan_t yyscanner );
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifndef yytext_ptr
|
||||
static void yy_flex_strncpy (char *,yyconst char *,int ,yyscan_t yyscanner);
|
||||
#endif
|
||||
|
||||
#ifdef YY_NEED_STRLEN
|
||||
static int yy_flex_strlen (yyconst char * ,yyscan_t yyscanner);
|
||||
#endif
|
||||
|
||||
#ifndef YY_NO_INPUT
|
||||
|
||||
#endif
|
||||
|
||||
/* Amount of stuff to slurp up with each read. */
|
||||
#ifndef YY_READ_BUF_SIZE
|
||||
#ifdef __ia64__
|
||||
/* On IA-64, the buffer size is 16k, not 8k */
|
||||
#define YY_READ_BUF_SIZE 16384
|
||||
#else
|
||||
#define YY_READ_BUF_SIZE 8192
|
||||
#endif /* __ia64__ */
|
||||
#endif
|
||||
|
||||
/* Number of entries by which start-condition stack grows. */
|
||||
#ifndef YY_START_STACK_INCR
|
||||
#define YY_START_STACK_INCR 25
|
||||
#endif
|
||||
|
||||
/* Default declaration of generated scanner - a define so the user can
|
||||
* easily add parameters.
|
||||
*/
|
||||
#ifndef YY_DECL
|
||||
#define YY_DECL_IS_OURS 1
|
||||
|
||||
extern int jq_yylex \
|
||||
(YYSTYPE * yylval_param,YYLTYPE * yylloc_param ,yyscan_t yyscanner);
|
||||
|
||||
#define YY_DECL int jq_yylex \
|
||||
(YYSTYPE * yylval_param, YYLTYPE * yylloc_param , yyscan_t yyscanner)
|
||||
#endif /* !YY_DECL */
|
||||
|
||||
/* yy_get_previous_state - get the state just before the EOB char was reached */
|
||||
|
||||
#undef YY_NEW_FILE
|
||||
#undef YY_FLUSH_BUFFER
|
||||
#undef yy_set_bol
|
||||
#undef yy_new_buffer
|
||||
#undef yy_set_interactive
|
||||
#undef YY_DO_BEFORE_ACTION
|
||||
|
||||
#ifdef YY_DECL_IS_OURS
|
||||
#undef YY_DECL_IS_OURS
|
||||
#undef YY_DECL
|
||||
#endif
|
||||
|
||||
#line 129 "lexer.l"
|
||||
|
||||
|
||||
#line 361 "lexer.h"
|
||||
#undef jq_yyIN_HEADER
|
||||
#endif /* jq_yyHEADER_H */
|
||||
@@ -42,6 +42,9 @@ struct lexer_param;
|
||||
"!=" { return NEQ; }
|
||||
"==" { return EQ; }
|
||||
"as" { return AS; }
|
||||
"import" { return IMPORT; }
|
||||
"include" { return INCLUDE; }
|
||||
"module" { return MODULE; }
|
||||
"def" { return DEF; }
|
||||
"if" { return IF; }
|
||||
"then" { return THEN; }
|
||||
@@ -51,16 +54,24 @@ struct lexer_param;
|
||||
"or" { return OR; }
|
||||
"end" { return END; }
|
||||
"reduce" { return REDUCE; }
|
||||
"foreach" { return FOREACH; }
|
||||
"//" { return DEFINEDOR; }
|
||||
"try" { return TRY; }
|
||||
"catch" { return CATCH; }
|
||||
"label" { return LABEL; }
|
||||
"break" { return BREAK; }
|
||||
"__loc__" { return LOC; }
|
||||
"|=" { return SETPIPE; }
|
||||
"+=" { return SETPLUS; }
|
||||
"-=" { return SETMINUS; }
|
||||
"*=" { return SETMULT; }
|
||||
"/=" { return SETDIV; }
|
||||
"%=" { return SETMOD; }
|
||||
"//=" { return SETDEFINEDOR; }
|
||||
"<=" { return LESSEQ; }
|
||||
">=" { return GREATEREQ; }
|
||||
"."|"="|";"|","|":"|"|"|"+"|"-"|"*"|"/"|"\$"|"<"|">" { return yytext[0];}
|
||||
".." { return REC; }
|
||||
"."|"?"|"="|";"|","|":"|"|"|"+"|"-"|"*"|"/"|"%"|"\$"|"<"|">" { return yytext[0];}
|
||||
|
||||
"["|"{"|"(" {
|
||||
return enter(yytext[0], YY_START, yyscanner);
|
||||
@@ -93,7 +104,7 @@ struct lexer_param;
|
||||
}
|
||||
(\\[^u(]|\\u[a-zA-Z0-9]{0,4})+ {
|
||||
/* pass escapes to the json parser */
|
||||
jv escapes = jv_string_fmt("\"%.*s\"", yyleng, yytext);
|
||||
jv escapes = jv_string_fmt("\"%.*s\"", (int)yyleng, yytext);
|
||||
yylval->literal = jv_parse_sized(jv_string_value(escapes), jv_string_length_bytes(jv_copy(escapes)));
|
||||
jv_free(escapes);
|
||||
return QQSTRING_TEXT;
|
||||
@@ -108,7 +119,7 @@ struct lexer_param;
|
||||
}
|
||||
|
||||
|
||||
[a-zA-Z_][a-zA-Z_0-9]* { yylval->literal = jv_string(yytext); return IDENT;}
|
||||
([a-zA-Z_][a-zA-Z_0-9]*::)*[a-zA-Z_][a-zA-Z_0-9]* { yylval->literal = jv_string(yytext); return IDENT;}
|
||||
\.[a-zA-Z_][a-zA-Z_0-9]* { yylval->literal = jv_string(yytext+1); return FIELD;}
|
||||
|
||||
[ \n\t]+ {}
|
||||
|
||||
@@ -0,0 +1,160 @@
|
||||
#ifdef HAVE_ACOS
|
||||
LIBM_DD(acos)
|
||||
#else
|
||||
LIBM_DD_NO(acos)
|
||||
#endif
|
||||
#ifdef HAVE_ACOSH
|
||||
LIBM_DD(acosh)
|
||||
#else
|
||||
LIBM_DD_NO(acosh)
|
||||
#endif
|
||||
#ifdef HAVE_ASIN
|
||||
LIBM_DD(asin)
|
||||
#else
|
||||
LIBM_DD_NO(asin)
|
||||
#endif
|
||||
#ifdef HAVE_ASINH
|
||||
LIBM_DD(asinh)
|
||||
#else
|
||||
LIBM_DD_NO(asinh)
|
||||
#endif
|
||||
#ifdef HAVE_ATAN
|
||||
LIBM_DD(atan)
|
||||
#else
|
||||
LIBM_DD_NO(atan)
|
||||
#endif
|
||||
#ifdef HAVE_ATANH
|
||||
LIBM_DD(atanh)
|
||||
#else
|
||||
LIBM_DD_NO(atanh)
|
||||
#endif
|
||||
#ifdef HAVE_CBRT
|
||||
LIBM_DD(cbrt)
|
||||
#else
|
||||
LIBM_DD_NO(cbrt)
|
||||
#endif
|
||||
#ifdef HAVE_COS
|
||||
LIBM_DD(cos)
|
||||
#else
|
||||
LIBM_DD_NO(cos)
|
||||
#endif
|
||||
#ifdef HAVE_COSH
|
||||
LIBM_DD(cosh)
|
||||
#else
|
||||
LIBM_DD_NO(cosh)
|
||||
#endif
|
||||
#ifdef HAVE_EXP2
|
||||
LIBM_DD(exp2)
|
||||
#else
|
||||
LIBM_DD_NO(exp2)
|
||||
#endif
|
||||
#ifdef HAVE_EXP
|
||||
LIBM_DD(exp)
|
||||
#else
|
||||
LIBM_DD_NO(exp)
|
||||
#endif
|
||||
#ifdef HAVE_FLOOR
|
||||
LIBM_DD(floor)
|
||||
#else
|
||||
LIBM_DD_NO(floor)
|
||||
#endif
|
||||
#ifdef HAVE_J0
|
||||
LIBM_DD(j0)
|
||||
#else
|
||||
LIBM_DD_NO(j0)
|
||||
#endif
|
||||
#ifdef HAVE_J1
|
||||
LIBM_DD(j1)
|
||||
#else
|
||||
LIBM_DD_NO(j1)
|
||||
#endif
|
||||
#ifdef HAVE_LOG10
|
||||
LIBM_DD(log10)
|
||||
#else
|
||||
LIBM_DD_NO(log10)
|
||||
#endif
|
||||
#ifdef HAVE_LOG2
|
||||
LIBM_DD(log2)
|
||||
#else
|
||||
LIBM_DD_NO(log2)
|
||||
#endif
|
||||
#ifdef HAVE_LOG
|
||||
LIBM_DD(log)
|
||||
#else
|
||||
LIBM_DD_NO(log)
|
||||
#endif
|
||||
#ifdef HAVE_SIN
|
||||
LIBM_DD(sin)
|
||||
#else
|
||||
LIBM_DD_NO(sin)
|
||||
#endif
|
||||
#ifdef HAVE_SINH
|
||||
LIBM_DD(sinh)
|
||||
#else
|
||||
LIBM_DD_NO(sinh)
|
||||
#endif
|
||||
#ifdef HAVE_SQRT
|
||||
LIBM_DD(sqrt)
|
||||
#else
|
||||
LIBM_DD_NO(sqrt)
|
||||
#endif
|
||||
#ifdef HAVE_TAN
|
||||
LIBM_DD(tan)
|
||||
#else
|
||||
LIBM_DD_NO(tan)
|
||||
#endif
|
||||
#ifdef HAVE_TANH
|
||||
LIBM_DD(tanh)
|
||||
#else
|
||||
LIBM_DD_NO(tanh)
|
||||
#endif
|
||||
#ifdef HAVE_TGAMMA
|
||||
LIBM_DD(tgamma)
|
||||
#else
|
||||
LIBM_DD_NO(tgamma)
|
||||
#endif
|
||||
#ifdef HAVE_Y0
|
||||
LIBM_DD(y0)
|
||||
#else
|
||||
LIBM_DD_NO(y0)
|
||||
#endif
|
||||
#ifdef HAVE_Y1
|
||||
LIBM_DD(y1)
|
||||
#else
|
||||
LIBM_DD_NO(y1)
|
||||
#endif
|
||||
#ifdef HAVE_POW
|
||||
LIBM_DDD(pow)
|
||||
#else
|
||||
LIBM_DDD_NO(pow)
|
||||
#endif
|
||||
#ifdef HAVE_JN
|
||||
LIBM_DID(jn)
|
||||
#endif
|
||||
#ifdef HAVE_YN
|
||||
LIBM_DID(yn)
|
||||
#endif
|
||||
#ifdef HAVE_ATAN2
|
||||
LIBM_DDD(atan2)
|
||||
#else
|
||||
LIBM_DDD_NO(atan2)
|
||||
#endif
|
||||
#ifdef HAVE_HYPOT
|
||||
LIBM_DDD(hypot)
|
||||
#else
|
||||
LIBM_DDD_NO(hypot)
|
||||
#endif
|
||||
#ifdef HAVE_REMAINDER
|
||||
LIBM_DDD(remainder)
|
||||
#else
|
||||
LIBM_DDD_NO(remainder)
|
||||
#endif
|
||||
|
||||
#if 0
|
||||
#ifdef HAVE_SCALBN
|
||||
LIBM_DDI(scalbn)
|
||||
#endif
|
||||
#ifdef HAVE_LGAMMA_R
|
||||
LIBM_DDIP(lgamma_r)
|
||||
#endif
|
||||
#endif
|
||||
@@ -0,0 +1,358 @@
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <limits.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <sys/stat.h>
|
||||
#include <libgen.h>
|
||||
|
||||
#include "jq_parser.h"
|
||||
#include "locfile.h"
|
||||
#include "jv.h"
|
||||
#include "jq.h"
|
||||
#include "parser.h"
|
||||
#include "util.h"
|
||||
#include "compile.h"
|
||||
|
||||
struct lib_loading_state {
|
||||
char **names;
|
||||
block *defs;
|
||||
uint64_t ct;
|
||||
};
|
||||
static int load_library(jq_state *jq, jv lib_path, int is_data, int raw,
|
||||
const char *as, block *out_block,
|
||||
struct lib_loading_state *lib_state);
|
||||
|
||||
|
||||
// Given a lib_path to search first, creates a chain of search paths
|
||||
// in the following order:
|
||||
// 1. lib_path
|
||||
// 2. -L paths passed in on the command line (from jq_state*) or builtin list
|
||||
static jv build_lib_search_chain(jq_state *jq, jv search_path, jv jq_origin, jv lib_origin) {
|
||||
assert(jv_get_kind(search_path) == JV_KIND_ARRAY);
|
||||
jv expanded = jv_array();
|
||||
jv expanded_elt;
|
||||
jv_array_foreach(search_path, i, path) {
|
||||
if (strcmp(".",jv_string_value(path)) == 0) {
|
||||
expanded_elt = jv_copy(path);
|
||||
} else if (jv_get_kind(lib_origin) == JV_KIND_STRING &&
|
||||
strncmp("./", jv_string_value(path),sizeof("./")-1) == 0) {
|
||||
expanded_elt = jv_string_fmt("%s/%s",
|
||||
jv_string_value(lib_origin),
|
||||
jv_string_value(path) + sizeof ("./") - 1);
|
||||
} else if (strncmp("$ORIGIN/",jv_string_value(path),sizeof("$ORIGIN/")-1) == 0) {
|
||||
expanded_elt = jv_string_fmt("%s/%s",
|
||||
jv_string_value(jq_origin),
|
||||
jv_string_value(path) + sizeof ("$ORIGIN/") - 1);
|
||||
} else {
|
||||
expanded_elt = expand_path(path);
|
||||
if (!jv_is_valid(expanded_elt)) {
|
||||
jv_free(search_path);
|
||||
jv_free(expanded);
|
||||
jv_free(path);
|
||||
return expanded_elt;
|
||||
}
|
||||
path = jv_invalid();
|
||||
}
|
||||
expanded = jv_array_append(expanded, expanded_elt);
|
||||
jv_free(path);
|
||||
}
|
||||
jv_free(jq_origin);
|
||||
jv_free(lib_origin);
|
||||
jv_free(search_path);
|
||||
return expanded;
|
||||
}
|
||||
|
||||
// Doesn't actually check that name not be an absolute path, and we
|
||||
// don't have to: we always append relative paths to others (with a '/'
|
||||
// in between).
|
||||
static jv validate_relpath(jv name) {
|
||||
const char *s = jv_string_value(name);
|
||||
if (strchr(s, '\\')) {
|
||||
jv res = jv_invalid_with_msg(jv_string_fmt("Modules must be named by relative paths using '/', not '\\' (%s)", s));
|
||||
jv_free(name);
|
||||
return res;
|
||||
}
|
||||
jv components = jv_string_split(jv_copy(name), jv_string("/"));
|
||||
jv rp = jv_array_get(jv_copy(components), 0);
|
||||
components = jv_array_slice(components, 1, jv_array_length(jv_copy(components)));
|
||||
jv_array_foreach(components, i, x) {
|
||||
if (!strcmp(jv_string_value(x), "..")) {
|
||||
jv_free(x);
|
||||
jv_free(rp);
|
||||
jv_free(components);
|
||||
jv res = jv_invalid_with_msg(jv_string_fmt("Relative paths to modules may not traverse to parent directories (%s)", s));
|
||||
jv_free(name);
|
||||
return res;
|
||||
}
|
||||
if (i > 0 && jv_equal(jv_copy(x), jv_array_get(jv_copy(components), i - 1))) {
|
||||
jv_free(x);
|
||||
jv_free(rp);
|
||||
jv_free(components);
|
||||
jv res = jv_invalid_with_msg(jv_string_fmt("module names must not have equal consecutive components: %s",
|
||||
jv_string_value(name)));
|
||||
jv_free(name);
|
||||
return res;
|
||||
}
|
||||
rp = jv_string_concat(rp, jv_string_concat(jv_string("/"), x));
|
||||
}
|
||||
jv_free(components);
|
||||
jv_free(name);
|
||||
return rp;
|
||||
}
|
||||
|
||||
// Assumes name has been validated
|
||||
static jv jv_basename(jv name) {
|
||||
const char *s = jv_string_value(name);
|
||||
const char *p = strrchr(s, '/');
|
||||
if (!p)
|
||||
return name;
|
||||
jv res = jv_string_fmt("%s", p);
|
||||
jv_free(name);
|
||||
return res;
|
||||
}
|
||||
|
||||
// Asummes validated relative path to module
|
||||
static jv find_lib(jq_state *jq, jv rel_path, jv search, const char *suffix, jv jq_origin, jv lib_origin) {
|
||||
if (jv_get_kind(search) != JV_KIND_ARRAY)
|
||||
return jv_invalid_with_msg(jv_string_fmt("Module search path must be an array"));
|
||||
if (jv_get_kind(rel_path) != JV_KIND_STRING)
|
||||
return jv_invalid_with_msg(jv_string_fmt("Module path must be a string"));
|
||||
|
||||
struct stat st;
|
||||
int ret;
|
||||
|
||||
// Ideally we should cache this somewhere
|
||||
search = build_lib_search_chain(jq, search, jq_origin, lib_origin);
|
||||
|
||||
jv bname = jv_basename(jv_copy(rel_path));
|
||||
|
||||
jv_array_foreach(search, i, spath) {
|
||||
if (jv_get_kind(spath) == JV_KIND_NULL) {
|
||||
jv_free(spath);
|
||||
break;
|
||||
}
|
||||
if (jv_get_kind(spath) != JV_KIND_STRING ||
|
||||
strcmp(jv_string_value(spath), "") == 0) {
|
||||
jv_free(spath);
|
||||
continue; /* XXX report non-strings in search path?? */
|
||||
}
|
||||
// Try ${search_dir}/${rel_path}.jq
|
||||
jv testpath = jq_realpath(jv_string_fmt("%s/%s%s",
|
||||
jv_string_value(spath),
|
||||
jv_string_value(rel_path),
|
||||
suffix));
|
||||
ret = stat(jv_string_value(testpath),&st);
|
||||
if (ret == -1 && errno == ENOENT) {
|
||||
jv_free(testpath);
|
||||
// Try ${search_dir}/$(dirname ${rel_path})/jq/main.jq
|
||||
testpath = jq_realpath(jv_string_fmt("%s/%s/%s%s",
|
||||
jv_string_value(spath),
|
||||
jv_string_value(rel_path),
|
||||
"jq/main",
|
||||
suffix));
|
||||
ret = stat(jv_string_value(testpath),&st);
|
||||
}
|
||||
if (ret == -1 && errno == ENOENT) {
|
||||
jv_free(testpath);
|
||||
// Try ${search_dir}/${rel_path}/$(basename ${rel_path}).jq
|
||||
testpath = jq_realpath(jv_string_fmt("%s/%s/%s%s",
|
||||
jv_string_value(spath),
|
||||
jv_string_value(rel_path),
|
||||
jv_string_value(bname),
|
||||
suffix));
|
||||
ret = stat(jv_string_value(testpath),&st);
|
||||
}
|
||||
if (ret == 0) {
|
||||
jv_free(rel_path);
|
||||
jv_free(search);
|
||||
jv_free(bname);
|
||||
jv_free(spath);
|
||||
return testpath;
|
||||
}
|
||||
jv_free(testpath);
|
||||
jv_free(spath);
|
||||
}
|
||||
jv output = jv_invalid_with_msg(jv_string_fmt("module not found: %s", jv_string_value(rel_path)));
|
||||
jv_free(rel_path);
|
||||
jv_free(search);
|
||||
jv_free(bname);
|
||||
return output;
|
||||
}
|
||||
|
||||
static jv default_search(jq_state *jq, jv value) {
|
||||
if (!jv_is_valid(value)) {
|
||||
// dependent didn't say; prepend . to system search path listj
|
||||
jv_free(value);
|
||||
return jv_array_concat(JV_ARRAY(jv_string(".")), jq_get_lib_dirs(jq));
|
||||
}
|
||||
if (jv_get_kind(value) != JV_KIND_ARRAY)
|
||||
return JV_ARRAY(value);
|
||||
return value;
|
||||
}
|
||||
|
||||
// XXX Split this into a util that takes a callback, and then...
|
||||
static int process_dependencies(jq_state *jq, jv jq_origin, jv lib_origin, block *src_block, struct lib_loading_state *lib_state) {
|
||||
jv deps = block_take_imports(src_block);
|
||||
block bk = *src_block;
|
||||
int nerrors = 0;
|
||||
const char *as_str = NULL;
|
||||
|
||||
jv_array_foreach(deps, i, dep) {
|
||||
int is_data = jv_get_kind(jv_object_get(jv_copy(dep), jv_string("is_data"))) == JV_KIND_TRUE;
|
||||
int raw = 0;
|
||||
jv v = jv_object_get(jv_copy(dep), jv_string("raw"));
|
||||
if (jv_get_kind(v) == JV_KIND_TRUE)
|
||||
raw = 1;
|
||||
jv_free(v);
|
||||
jv relpath = validate_relpath(jv_object_get(jv_copy(dep), jv_string("relpath")));
|
||||
jv as = jv_object_get(jv_copy(dep), jv_string("as"));
|
||||
assert(!jv_is_valid(as) || jv_get_kind(as) == JV_KIND_STRING);
|
||||
if (jv_get_kind(as) == JV_KIND_STRING)
|
||||
as_str = jv_string_value(as);
|
||||
jv search = default_search(jq, jv_object_get(dep, jv_string("search")));
|
||||
// dep is now freed; do not reuse
|
||||
|
||||
// find_lib does a lot of work that could be cached...
|
||||
jv resolved = find_lib(jq, relpath, search, is_data ? ".json" : ".jq", jv_copy(jq_origin), jv_copy(lib_origin));
|
||||
// XXX ...move the rest of this into a callback.
|
||||
if (!jv_is_valid(resolved)) {
|
||||
jv emsg = jv_invalid_get_msg(resolved);
|
||||
jq_report_error(jq, jv_string_fmt("jq: error: %s\n",jv_string_value(emsg)));
|
||||
jv_free(emsg);
|
||||
jv_free(as);
|
||||
jv_free(deps);
|
||||
jv_free(jq_origin);
|
||||
jv_free(lib_origin);
|
||||
return 1;
|
||||
}
|
||||
uint64_t state_idx = 0;
|
||||
for (; state_idx < lib_state->ct; ++state_idx) {
|
||||
if (strcmp(lib_state->names[state_idx],jv_string_value(resolved)) == 0)
|
||||
break;
|
||||
}
|
||||
if (state_idx < lib_state->ct) { // Found
|
||||
jv_free(resolved);
|
||||
// Bind the library to the program
|
||||
bk = block_bind_library(lib_state->defs[state_idx], bk, OP_IS_CALL_PSEUDO, as_str);
|
||||
} else { // Not found. Add it to the table before binding.
|
||||
block dep_def_block = gen_noop();
|
||||
nerrors += load_library(jq, resolved, is_data, raw, as_str, &dep_def_block, lib_state);
|
||||
// resolved has been freed
|
||||
if (nerrors == 0) {
|
||||
// Bind the library to the program
|
||||
bk = block_bind_library(dep_def_block, bk, OP_IS_CALL_PSEUDO, as_str);
|
||||
}
|
||||
}
|
||||
jv_free(as);
|
||||
}
|
||||
jv_free(lib_origin);
|
||||
jv_free(jq_origin);
|
||||
jv_free(deps);
|
||||
return nerrors;
|
||||
}
|
||||
|
||||
// Loads the library at lib_path into lib_state, putting the library's defs
|
||||
// into *out_block
|
||||
static int load_library(jq_state *jq, jv lib_path, int is_data, int raw, const char *as, block *out_block, struct lib_loading_state *lib_state) {
|
||||
int nerrors = 0;
|
||||
struct locfile* src = NULL;
|
||||
block program;
|
||||
jv data;
|
||||
if (is_data && !raw)
|
||||
data = jv_load_file(jv_string_value(lib_path), 0);
|
||||
else
|
||||
data = jv_load_file(jv_string_value(lib_path), 1);
|
||||
int state_idx;
|
||||
if (!jv_is_valid(data)) {
|
||||
if (jv_invalid_has_msg(jv_copy(data)))
|
||||
data = jv_invalid_get_msg(data);
|
||||
else
|
||||
data = jv_string("unknown error");
|
||||
jq_report_error(jq, jv_string_fmt("jq: error loading data file %s: %s\n", jv_string_value(lib_path), jv_string_value(data)));
|
||||
nerrors++;
|
||||
goto out;
|
||||
} else if (is_data) {
|
||||
// import "foo" as $bar;
|
||||
program = gen_const_global(jv_copy(data), as);
|
||||
} else {
|
||||
// import "foo" as bar;
|
||||
src = locfile_init(jq, jv_string_value(lib_path), jv_string_value(data), jv_string_length_bytes(jv_copy(data)));
|
||||
nerrors += jq_parse_library(src, &program);
|
||||
if (nerrors == 0) {
|
||||
char *lib_origin = strdup(jv_string_value(lib_path));
|
||||
nerrors += process_dependencies(jq, jq_get_jq_origin(jq),
|
||||
jv_string(dirname(lib_origin)),
|
||||
&program, lib_state);
|
||||
free(lib_origin);
|
||||
}
|
||||
}
|
||||
state_idx = lib_state->ct++;
|
||||
lib_state->names = realloc(lib_state->names, lib_state->ct * sizeof(const char *));
|
||||
lib_state->defs = realloc(lib_state->defs, lib_state->ct * sizeof(block));
|
||||
lib_state->names[state_idx] = strdup(jv_string_value(lib_path));
|
||||
lib_state->defs[state_idx] = program;
|
||||
*out_block = program;
|
||||
if (src)
|
||||
locfile_free(src);
|
||||
out:
|
||||
jv_free(lib_path);
|
||||
jv_free(data);
|
||||
return nerrors;
|
||||
}
|
||||
|
||||
// FIXME It'd be nice to have an option to search the same search path
|
||||
// as we do in process_dependencies.
|
||||
jv load_module_meta(jq_state *jq, jv mod_relpath) {
|
||||
// We can't know the caller's origin; we could though, if it was passed in
|
||||
jv lib_path = find_lib(jq, validate_relpath(mod_relpath), jq_get_lib_dirs(jq), ".jq", jq_get_jq_origin(jq), jv_null());
|
||||
if (!jv_is_valid(lib_path))
|
||||
return lib_path;
|
||||
jv meta = jv_null();
|
||||
jv data = jv_load_file(jv_string_value(lib_path), 1);
|
||||
if (jv_is_valid(data)) {
|
||||
block program;
|
||||
struct locfile* src = locfile_init(jq, jv_string_value(lib_path), jv_string_value(data), jv_string_length_bytes(jv_copy(data)));
|
||||
int nerrors = jq_parse_library(src, &program);
|
||||
if (nerrors == 0) {
|
||||
meta = block_module_meta(program);
|
||||
if (jv_get_kind(meta) == JV_KIND_NULL)
|
||||
meta = jv_object();
|
||||
meta = jv_object_set(meta, jv_string("deps"), block_take_imports(&program));
|
||||
}
|
||||
locfile_free(src);
|
||||
block_free(program);
|
||||
}
|
||||
jv_free(lib_path);
|
||||
jv_free(data);
|
||||
return meta;
|
||||
}
|
||||
|
||||
int load_program(jq_state *jq, struct locfile* src, block *out_block) {
|
||||
int nerrors = 0;
|
||||
block program;
|
||||
struct lib_loading_state lib_state = {0,0,0};
|
||||
nerrors = jq_parse(src, &program);
|
||||
if (nerrors)
|
||||
return nerrors;
|
||||
|
||||
nerrors = process_dependencies(jq, jq_get_jq_origin(jq), jq_get_prog_origin(jq), &program, &lib_state);
|
||||
block libs = gen_noop();
|
||||
for (uint64_t i = 0; i < lib_state.ct; ++i) {
|
||||
free(lib_state.names[i]);
|
||||
if (nerrors == 0 && !block_is_const(lib_state.defs[i]))
|
||||
libs = block_join(libs, lib_state.defs[i]);
|
||||
else
|
||||
block_free(lib_state.defs[i]);
|
||||
}
|
||||
free(lib_state.names);
|
||||
free(lib_state.defs);
|
||||
if (nerrors)
|
||||
block_free(program);
|
||||
else
|
||||
*out_block = block_drop_unreferenced(block_join(libs, program));
|
||||
|
||||
return nerrors;
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
#ifndef LINKER_H
|
||||
#define LINKER_H
|
||||
|
||||
int load_program(jq_state *jq, struct locfile* src, block *out_block);
|
||||
jv load_module_meta(jq_state *jq, jv modname);
|
||||
|
||||
#endif
|
||||
@@ -1,15 +1,23 @@
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <assert.h>
|
||||
#include <stdarg.h>
|
||||
#include <string.h>
|
||||
#include "jq.h"
|
||||
#include "jv_alloc.h"
|
||||
#include "locfile.h"
|
||||
|
||||
|
||||
void locfile_init(struct locfile* l, const char* data, int length) {
|
||||
l->data = data;
|
||||
struct locfile* locfile_init(jq_state *jq, const char *fname, const char* data, int length) {
|
||||
struct locfile* l = jv_mem_alloc(sizeof(struct locfile));
|
||||
l->jq = jq;
|
||||
l->fname = jv_string(fname);
|
||||
l->data = jv_mem_alloc(length);
|
||||
memcpy((char*)l->data,data,length);
|
||||
l->length = length;
|
||||
l->nlines = 1;
|
||||
l->refct = 1;
|
||||
for (int i=0; i<length; i++) {
|
||||
if (data[i] == '\n') l->nlines++;
|
||||
}
|
||||
@@ -23,13 +31,23 @@ void locfile_init(struct locfile* l, const char* data, int length) {
|
||||
}
|
||||
}
|
||||
l->linemap[l->nlines] = length+1; // virtual last \n
|
||||
return l;
|
||||
}
|
||||
|
||||
struct locfile* locfile_retain(struct locfile* l) {
|
||||
l->refct++;
|
||||
return l;
|
||||
}
|
||||
void locfile_free(struct locfile* l) {
|
||||
jv_mem_free(l->linemap);
|
||||
if (--(l->refct) == 0) {
|
||||
jv_free(l->fname);
|
||||
jv_mem_free(l->linemap);
|
||||
jv_mem_free((char*)l->data);
|
||||
jv_mem_free(l);
|
||||
}
|
||||
}
|
||||
|
||||
static int locfile_get_line(struct locfile* l, int pos) {
|
||||
int locfile_get_line(struct locfile* l, int pos) {
|
||||
assert(pos < l->length);
|
||||
int line = 1;
|
||||
while (l->linemap[line] <= pos) line++; // == if pos at start (before, never ==, because pos never on \n)
|
||||
@@ -45,21 +63,29 @@ static int locfile_line_length(struct locfile* l, int line) {
|
||||
void locfile_locate(struct locfile* l, location loc, const char* fmt, ...) {
|
||||
va_list fmtargs;
|
||||
va_start(fmtargs, fmt);
|
||||
vfprintf(stderr, fmt, fmtargs);
|
||||
va_end(fmtargs);
|
||||
fprintf(stderr, "\n");
|
||||
if (loc.start == -1) {
|
||||
fprintf(stderr, "<unknown location>\n");
|
||||
int startline;
|
||||
int offset;
|
||||
|
||||
if (loc.start != -1) {
|
||||
startline = locfile_get_line(l, loc.start);
|
||||
offset = l->linemap[startline];
|
||||
}
|
||||
|
||||
jv m1 = jv_string_vfmt(fmt, fmtargs);
|
||||
if (!jv_is_valid(m1)) {
|
||||
jq_report_error(l->jq, m1);
|
||||
return;
|
||||
}
|
||||
int startline = locfile_get_line(l, loc.start);
|
||||
int offset = l->linemap[startline];
|
||||
fprintf(stderr, "%.*s\n", locfile_line_length(l, startline), l->data + offset);
|
||||
fprintf(stderr, "%*s", loc.start - offset, "");
|
||||
for (int i = loc.start;
|
||||
i < loc.end && i < offset + locfile_line_length(l, startline);
|
||||
i++){
|
||||
fprintf(stderr, "^");
|
||||
if (loc.start == -1) {
|
||||
jq_report_error(l->jq, jv_string_fmt("jq: error: %s\n<unknown location>", jv_string_value(m1)));
|
||||
jv_free(m1);
|
||||
return;
|
||||
}
|
||||
fprintf(stderr, "\n");
|
||||
jv m2 = jv_string_fmt("%s at %s, line %d:\n%.*s%*s", jv_string_value(m1),
|
||||
jv_string_value(l->fname), startline + 1,
|
||||
locfile_line_length(l, startline), l->data + offset,
|
||||
loc.start - offset, "");
|
||||
jv_free(m1);
|
||||
jq_report_error(l->jq, m2);
|
||||
return;
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
#ifndef _LOCFILE_H
|
||||
#define _LOCFILE_H
|
||||
#ifndef LOCFILE_H
|
||||
#define LOCFILE_H
|
||||
|
||||
#include "jq.h"
|
||||
|
||||
typedef struct {
|
||||
int start, end;
|
||||
@@ -8,16 +10,20 @@ typedef struct {
|
||||
static const location UNKNOWN_LOCATION = {-1, -1};
|
||||
|
||||
struct locfile {
|
||||
jv fname;
|
||||
const char* data;
|
||||
int length;
|
||||
int* linemap;
|
||||
int nlines;
|
||||
char *error;
|
||||
jq_state *jq;
|
||||
int refct;
|
||||
};
|
||||
|
||||
void locfile_init(struct locfile* l, const char* data, int length);
|
||||
|
||||
void locfile_free(struct locfile* l);
|
||||
|
||||
void locfile_locate(struct locfile* l, location loc, const char* fmt, ...);
|
||||
struct locfile* locfile_init(jq_state *, const char *, const char *, int);
|
||||
struct locfile* locfile_retain(struct locfile *);
|
||||
int locfile_get_line(struct locfile *, int);
|
||||
void locfile_free(struct locfile *);
|
||||
void locfile_locate(struct locfile *, location, const char *, ...);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,177 @@
|
||||
# ===========================================================================
|
||||
# http://www.gnu.org/software/autoconf-archive/ax_compare_version.html
|
||||
# ===========================================================================
|
||||
#
|
||||
# SYNOPSIS
|
||||
#
|
||||
# AX_COMPARE_VERSION(VERSION_A, OP, VERSION_B, [ACTION-IF-TRUE], [ACTION-IF-FALSE])
|
||||
#
|
||||
# DESCRIPTION
|
||||
#
|
||||
# This macro compares two version strings. Due to the various number of
|
||||
# minor-version numbers that can exist, and the fact that string
|
||||
# comparisons are not compatible with numeric comparisons, this is not
|
||||
# necessarily trivial to do in a autoconf script. This macro makes doing
|
||||
# these comparisons easy.
|
||||
#
|
||||
# The six basic comparisons are available, as well as checking equality
|
||||
# limited to a certain number of minor-version levels.
|
||||
#
|
||||
# The operator OP determines what type of comparison to do, and can be one
|
||||
# of:
|
||||
#
|
||||
# eq - equal (test A == B)
|
||||
# ne - not equal (test A != B)
|
||||
# le - less than or equal (test A <= B)
|
||||
# ge - greater than or equal (test A >= B)
|
||||
# lt - less than (test A < B)
|
||||
# gt - greater than (test A > B)
|
||||
#
|
||||
# Additionally, the eq and ne operator can have a number after it to limit
|
||||
# the test to that number of minor versions.
|
||||
#
|
||||
# eq0 - equal up to the length of the shorter version
|
||||
# ne0 - not equal up to the length of the shorter version
|
||||
# eqN - equal up to N sub-version levels
|
||||
# neN - not equal up to N sub-version levels
|
||||
#
|
||||
# When the condition is true, shell commands ACTION-IF-TRUE are run,
|
||||
# otherwise shell commands ACTION-IF-FALSE are run. The environment
|
||||
# variable 'ax_compare_version' is always set to either 'true' or 'false'
|
||||
# as well.
|
||||
#
|
||||
# Examples:
|
||||
#
|
||||
# AX_COMPARE_VERSION([3.15.7],[lt],[3.15.8])
|
||||
# AX_COMPARE_VERSION([3.15],[lt],[3.15.8])
|
||||
#
|
||||
# would both be true.
|
||||
#
|
||||
# AX_COMPARE_VERSION([3.15.7],[eq],[3.15.8])
|
||||
# AX_COMPARE_VERSION([3.15],[gt],[3.15.8])
|
||||
#
|
||||
# would both be false.
|
||||
#
|
||||
# AX_COMPARE_VERSION([3.15.7],[eq2],[3.15.8])
|
||||
#
|
||||
# would be true because it is only comparing two minor versions.
|
||||
#
|
||||
# AX_COMPARE_VERSION([3.15.7],[eq0],[3.15])
|
||||
#
|
||||
# would be true because it is only comparing the lesser number of minor
|
||||
# versions of the two values.
|
||||
#
|
||||
# Note: The characters that separate the version numbers do not matter. An
|
||||
# empty string is the same as version 0. OP is evaluated by autoconf, not
|
||||
# configure, so must be a string, not a variable.
|
||||
#
|
||||
# The author would like to acknowledge Guido Draheim whose advice about
|
||||
# the m4_case and m4_ifvaln functions make this macro only include the
|
||||
# portions necessary to perform the specific comparison specified by the
|
||||
# OP argument in the final configure script.
|
||||
#
|
||||
# LICENSE
|
||||
#
|
||||
# Copyright (c) 2008 Tim Toolan <toolan@ele.uri.edu>
|
||||
#
|
||||
# Copying and distribution of this file, with or without modification, are
|
||||
# permitted in any medium without royalty provided the copyright notice
|
||||
# and this notice are preserved. This file is offered as-is, without any
|
||||
# warranty.
|
||||
|
||||
#serial 11
|
||||
|
||||
dnl #########################################################################
|
||||
AC_DEFUN([AX_COMPARE_VERSION], [
|
||||
AC_REQUIRE([AC_PROG_AWK])
|
||||
|
||||
# Used to indicate true or false condition
|
||||
ax_compare_version=false
|
||||
|
||||
# Convert the two version strings to be compared into a format that
|
||||
# allows a simple string comparison. The end result is that a version
|
||||
# string of the form 1.12.5-r617 will be converted to the form
|
||||
# 0001001200050617. In other words, each number is zero padded to four
|
||||
# digits, and non digits are removed.
|
||||
AS_VAR_PUSHDEF([A],[ax_compare_version_A])
|
||||
A=`echo "$1" | sed -e 's/\([[0-9]]*\)/Z\1Z/g' \
|
||||
-e 's/Z\([[0-9]]\)Z/Z0\1Z/g' \
|
||||
-e 's/Z\([[0-9]][[0-9]]\)Z/Z0\1Z/g' \
|
||||
-e 's/Z\([[0-9]][[0-9]][[0-9]]\)Z/Z0\1Z/g' \
|
||||
-e 's/[[^0-9]]//g'`
|
||||
|
||||
AS_VAR_PUSHDEF([B],[ax_compare_version_B])
|
||||
B=`echo "$3" | sed -e 's/\([[0-9]]*\)/Z\1Z/g' \
|
||||
-e 's/Z\([[0-9]]\)Z/Z0\1Z/g' \
|
||||
-e 's/Z\([[0-9]][[0-9]]\)Z/Z0\1Z/g' \
|
||||
-e 's/Z\([[0-9]][[0-9]][[0-9]]\)Z/Z0\1Z/g' \
|
||||
-e 's/[[^0-9]]//g'`
|
||||
|
||||
dnl # In the case of le, ge, lt, and gt, the strings are sorted as necessary
|
||||
dnl # then the first line is used to determine if the condition is true.
|
||||
dnl # The sed right after the echo is to remove any indented white space.
|
||||
m4_case(m4_tolower($2),
|
||||
[lt],[
|
||||
ax_compare_version=`echo "x$A
|
||||
x$B" | sed 's/^ *//' | sort -r | sed "s/x${A}/false/;s/x${B}/true/;1q"`
|
||||
],
|
||||
[gt],[
|
||||
ax_compare_version=`echo "x$A
|
||||
x$B" | sed 's/^ *//' | sort | sed "s/x${A}/false/;s/x${B}/true/;1q"`
|
||||
],
|
||||
[le],[
|
||||
ax_compare_version=`echo "x$A
|
||||
x$B" | sed 's/^ *//' | sort | sed "s/x${A}/true/;s/x${B}/false/;1q"`
|
||||
],
|
||||
[ge],[
|
||||
ax_compare_version=`echo "x$A
|
||||
x$B" | sed 's/^ *//' | sort -r | sed "s/x${A}/true/;s/x${B}/false/;1q"`
|
||||
],[
|
||||
dnl Split the operator from the subversion count if present.
|
||||
m4_bmatch(m4_substr($2,2),
|
||||
[0],[
|
||||
# A count of zero means use the length of the shorter version.
|
||||
# Determine the number of characters in A and B.
|
||||
ax_compare_version_len_A=`echo "$A" | $AWK '{print(length)}'`
|
||||
ax_compare_version_len_B=`echo "$B" | $AWK '{print(length)}'`
|
||||
|
||||
# Set A to no more than B's length and B to no more than A's length.
|
||||
A=`echo "$A" | sed "s/\(.\{$ax_compare_version_len_B\}\).*/\1/"`
|
||||
B=`echo "$B" | sed "s/\(.\{$ax_compare_version_len_A\}\).*/\1/"`
|
||||
],
|
||||
[[0-9]+],[
|
||||
# A count greater than zero means use only that many subversions
|
||||
A=`echo "$A" | sed "s/\(\([[0-9]]\{4\}\)\{m4_substr($2,2)\}\).*/\1/"`
|
||||
B=`echo "$B" | sed "s/\(\([[0-9]]\{4\}\)\{m4_substr($2,2)\}\).*/\1/"`
|
||||
],
|
||||
[.+],[
|
||||
AC_WARNING(
|
||||
[illegal OP numeric parameter: $2])
|
||||
],[])
|
||||
|
||||
# Pad zeros at end of numbers to make same length.
|
||||
ax_compare_version_tmp_A="$A`echo $B | sed 's/./0/g'`"
|
||||
B="$B`echo $A | sed 's/./0/g'`"
|
||||
A="$ax_compare_version_tmp_A"
|
||||
|
||||
# Check for equality or inequality as necessary.
|
||||
m4_case(m4_tolower(m4_substr($2,0,2)),
|
||||
[eq],[
|
||||
test "x$A" = "x$B" && ax_compare_version=true
|
||||
],
|
||||
[ne],[
|
||||
test "x$A" != "x$B" && ax_compare_version=true
|
||||
],[
|
||||
AC_WARNING([illegal OP parameter: $2])
|
||||
])
|
||||
])
|
||||
|
||||
AS_VAR_POPDEF([A])dnl
|
||||
AS_VAR_POPDEF([B])dnl
|
||||
|
||||
dnl # Execute ACTION-IF-TRUE / ACTION-IF-FALSE.
|
||||
if test "$ax_compare_version" = "true" ; then
|
||||
m4_ifvaln([$4],[$4],[:])dnl
|
||||
m4_ifvaln([$5],[else $5])dnl
|
||||
fi
|
||||
]) dnl AX_COMPARE_VERSION
|
||||
@@ -0,0 +1,68 @@
|
||||
# ===========================================================================
|
||||
# Modified from
|
||||
# http://www.gnu.org/software/autoconf-archive/ax_prog_perl_version.html
|
||||
# ===========================================================================
|
||||
#
|
||||
# SYNOPSIS
|
||||
#
|
||||
# AX_PROG_BISON_VERSION([VERSION],[ACTION-IF-TRUE],[ACTION-IF-FALSE])
|
||||
#
|
||||
# DESCRIPTION
|
||||
#
|
||||
# Makes sure that bison supports the version indicated. If true the shell
|
||||
# commands in ACTION-IF-TRUE are executed. If not the shell commands in
|
||||
# ACTION-IF-FALSE are run. Note if $PERL is not set the macro will fail.
|
||||
#
|
||||
# Example:
|
||||
#
|
||||
# AC_PROG_YACC
|
||||
# AX_PROG_BISON_VERSION([3.0.0],[ ... ],[ ... ])
|
||||
#
|
||||
# This will check to make sure that the bison you have supports at least
|
||||
# version 3.0.0.
|
||||
#
|
||||
# NOTE: This macro uses the $YACC variable to perform the check.
|
||||
# AX_WITH_YACC can be used to set that variable prior to running this
|
||||
# macro. The $BISON_VERSION variable will be set with the detected
|
||||
# version.
|
||||
#
|
||||
# LICENSE
|
||||
#
|
||||
# Copyright (c) 2009 Francesco Salvestrini <salvestrini@users.sourceforge.net>
|
||||
#
|
||||
# Copying and distribution of this file, with or without modification, are
|
||||
# permitted in any medium without royalty provided the copyright notice
|
||||
# and this notice are preserved. This file is offered as-is, without any
|
||||
# warranty.
|
||||
|
||||
#serial 12
|
||||
|
||||
AC_DEFUN([AX_PROG_BISON_VERSION],[
|
||||
AC_REQUIRE([AC_PROG_SED])
|
||||
AC_REQUIRE([AC_PROG_GREP])
|
||||
|
||||
AS_IF([test -n "$YACC"],[
|
||||
ax_bison_version="$1"
|
||||
|
||||
AC_MSG_CHECKING([for bison version])
|
||||
changequote(<<,>>)
|
||||
bison_version=`$YACC --version 2>&1 \
|
||||
| $GREP bison \
|
||||
| $SED -e 's/^[^0-9]*//' -e 's/[^0-9]*$//'`
|
||||
changequote([,])
|
||||
AC_MSG_RESULT($bison_version)
|
||||
|
||||
AC_SUBST([BISON_VERSION],[$bison_version])
|
||||
|
||||
AX_COMPARE_VERSION([$ax_bison_version],[le],[$bison_version],[
|
||||
:
|
||||
$2
|
||||
],[
|
||||
:
|
||||
$3
|
||||
])
|
||||
],[
|
||||
AC_MSG_WARN([could not find bison])
|
||||
$3
|
||||
])
|
||||
])
|
||||
@@ -1,33 +1,76 @@
|
||||
#include <assert.h>
|
||||
#include <ctype.h>
|
||||
#include <errno.h>
|
||||
#include <libgen.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#ifdef WIN32
|
||||
#include <windows.h>
|
||||
#include <io.h>
|
||||
#include <fcntl.h>
|
||||
#include <processenv.h>
|
||||
#include <shellapi.h>
|
||||
#include <wchar.h>
|
||||
#include <wtypes.h>
|
||||
#endif
|
||||
|
||||
#include "compile.h"
|
||||
#include "jv.h"
|
||||
#include "jq.h"
|
||||
#include "jv_alloc.h"
|
||||
#include "util.h"
|
||||
#include "version.h"
|
||||
|
||||
int jq_testsuite(int argc, char* argv[]);
|
||||
int jq_testsuite(jv lib_dirs, int verbose, int argc, char* argv[]);
|
||||
|
||||
static const char* progname;
|
||||
|
||||
static void usage() {
|
||||
fprintf(stderr, "\njq - commandline JSON processor [version %s]\n", JQ_VERSION);
|
||||
fprintf(stderr, "Usage: %s [options] <jq filter> [file...]\n\n", progname);
|
||||
fprintf(stderr, "For a description of the command line options and\n");
|
||||
fprintf(stderr, "how to write jq filters (and why you might want to)\n");
|
||||
fprintf(stderr, "see the jq manpage, or the online documentation at\n");
|
||||
fprintf(stderr, "http://stedolan.github.com/jq\n\n");
|
||||
exit(1);
|
||||
/*
|
||||
* For a longer help message we could use a better option parsing
|
||||
* strategy, one that lets stack options.
|
||||
*/
|
||||
static void usage(int code) {
|
||||
FILE *f = stderr;
|
||||
|
||||
if (code == 0)
|
||||
f = stdout;
|
||||
|
||||
int ret = fprintf(f,
|
||||
"jq - commandline JSON processor [version %s]\n"
|
||||
"Usage: %s [options] <jq filter> [file...]\n\n"
|
||||
"\tjq is a tool for processing JSON inputs, applying the\n"
|
||||
"\tgiven filter to its JSON text inputs and producing the\n"
|
||||
"\tfilter's results as JSON on standard output.\n"
|
||||
"\tThe simplest filter is ., which is the identity filter,\n"
|
||||
"\tcopying jq's input to its output unmodified (except for\n"
|
||||
"\tformatting).\n"
|
||||
"\tFor more advanced filters see the jq(1) manpage (\"man jq\")\n"
|
||||
"\tand/or http://stedolan.github.io/jq\n\n"
|
||||
"\tSome of the options include:\n"
|
||||
"\t -c\t\tcompact instead of pretty-printed output;\n"
|
||||
"\t -n\t\tuse `null` as the single input value;\n"
|
||||
"\t -e\t\tset the exit status code based on the output;\n"
|
||||
"\t -s\t\tread (slurp) all inputs into an array; apply filter to it;\n"
|
||||
"\t -r\t\toutput raw strings, not JSON texts;\n"
|
||||
"\t -R\t\tread raw strings, not JSON texts;\n"
|
||||
"\t -C\t\tcolorize JSON;\n"
|
||||
"\t -M\t\tmonochrome (don't colorize JSON);\n"
|
||||
"\t -S\t\tsort keys of objects on output;\n"
|
||||
"\t --tab\tuse tabs for indentation;\n"
|
||||
"\t --arg a v\tset variable $a to value <v>;\n"
|
||||
"\t --argjson a v\tset variable $a to JSON value <v>;\n"
|
||||
"\t --slurpfile a f\tset variable $a to an array of JSON texts read from <f>;\n"
|
||||
"\tSee the manpage for more options.\n", JQ_VERSION, progname);
|
||||
exit((ret < 0 && code == 0) ? 2 : code);
|
||||
}
|
||||
|
||||
static void die() {
|
||||
fprintf(stderr, "Use %s --help for help with command-line options,\n", progname);
|
||||
fprintf(stderr, "or see the jq documentation at http://stedolan.github.com/jq\n");
|
||||
exit(1);
|
||||
fprintf(stderr, "or see the jq manpage, or online docs at http://stedolan.github.io/jq\n");
|
||||
exit(2);
|
||||
}
|
||||
|
||||
|
||||
@@ -37,269 +80,487 @@ static int isoptish(const char* text) {
|
||||
return text[0] == '-' && (text[1] == '-' || isalpha(text[1]));
|
||||
}
|
||||
|
||||
static int isoption(const char* text, char shortopt, const char* longopt) {
|
||||
static int isoption(const char* text, char shortopt, const char* longopt, size_t *short_opts) {
|
||||
if (text[0] != '-' || text[1] == '-')
|
||||
*short_opts = 0;
|
||||
if (text[0] != '-') return 0;
|
||||
if (strlen(text) == 2 && text[1] == shortopt) return 1;
|
||||
|
||||
// check long option
|
||||
if (text[1] == '-' && !strcmp(text+2, longopt)) return 1;
|
||||
else if (text[1] == '-') return 0;
|
||||
|
||||
// must be short option; check it and...
|
||||
if (!shortopt) return 0;
|
||||
if (strchr(text, shortopt) != NULL) {
|
||||
(*short_opts)++; // ...count it (for option stacking)
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
enum {
|
||||
SLURP = 1,
|
||||
RAW_INPUT = 2,
|
||||
PROVIDE_NULL = 4,
|
||||
|
||||
RAW_OUTPUT = 8,
|
||||
COMPACT_OUTPUT = 16,
|
||||
ASCII_OUTPUT = 32,
|
||||
COLOUR_OUTPUT = 64,
|
||||
NO_COLOUR_OUTPUT = 128,
|
||||
SORTED_OUTPUT = 256,
|
||||
|
||||
FROM_FILE = 512,
|
||||
|
||||
SLURP = 1,
|
||||
RAW_INPUT = 2,
|
||||
PROVIDE_NULL = 4,
|
||||
RAW_OUTPUT = 8,
|
||||
ASCII_OUTPUT = 32,
|
||||
COLOUR_OUTPUT = 64,
|
||||
NO_COLOUR_OUTPUT = 128,
|
||||
SORTED_OUTPUT = 256,
|
||||
FROM_FILE = 512,
|
||||
RAW_NO_LF = 1024,
|
||||
UNBUFFERED_OUTPUT = 2048,
|
||||
EXIT_STATUS = 4096,
|
||||
SEQ = 8192,
|
||||
RUN_TESTS = 16384,
|
||||
/* debugging only */
|
||||
DUMP_DISASM = 2048,
|
||||
DUMP_DISASM = 32768,
|
||||
};
|
||||
static int options = 0;
|
||||
|
||||
static void process(jq_state *jq, jv value, int flags) {
|
||||
static int process(jq_state *jq, jv value, int flags, int dumpopts) {
|
||||
int ret = 14; // No valid results && -e -> exit(4)
|
||||
jq_start(jq, value, flags);
|
||||
jv result;
|
||||
while (jv_is_valid(result = jq_next(jq))) {
|
||||
if ((options & RAW_OUTPUT) && jv_get_kind(result) == JV_KIND_STRING) {
|
||||
fwrite(jv_string_value(result), 1, jv_string_length_bytes(jv_copy(result)), stdout);
|
||||
ret = 0;
|
||||
jv_free(result);
|
||||
} else {
|
||||
int dumpopts;
|
||||
/* Disable colour by default on Windows builds as Windows
|
||||
terminals tend not to display it correctly */
|
||||
#ifdef WIN32
|
||||
dumpopts = 0;
|
||||
#else
|
||||
dumpopts = isatty(fileno(stdout)) ? JV_PRINT_COLOUR : 0;
|
||||
#endif
|
||||
if (options & SORTED_OUTPUT) dumpopts |= JV_PRINT_SORTED;
|
||||
if (!(options & COMPACT_OUTPUT)) dumpopts |= JV_PRINT_PRETTY;
|
||||
if (options & ASCII_OUTPUT) dumpopts |= JV_PRINT_ASCII;
|
||||
if (options & COLOUR_OUTPUT) dumpopts |= JV_PRINT_COLOUR;
|
||||
if (options & NO_COLOUR_OUTPUT) dumpopts &= ~JV_PRINT_COLOUR;
|
||||
if (jv_get_kind(result) == JV_KIND_FALSE || jv_get_kind(result) == JV_KIND_NULL)
|
||||
ret = 11;
|
||||
else
|
||||
ret = 0;
|
||||
if (options & SEQ)
|
||||
priv_fwrite("\036", 1, stdout, dumpopts & JV_PRINT_ISATTY);
|
||||
jv_dump(result, dumpopts);
|
||||
}
|
||||
printf("\n");
|
||||
if (!(options & RAW_NO_LF))
|
||||
priv_fwrite("\n", 1, stdout, dumpopts & JV_PRINT_ISATTY);
|
||||
if (options & UNBUFFERED_OUTPUT)
|
||||
fflush(stdout);
|
||||
}
|
||||
if (jv_invalid_has_msg(jv_copy(result))) {
|
||||
// Uncaught jq exception
|
||||
jv msg = jv_invalid_get_msg(jv_copy(result));
|
||||
jv input_pos = jq_util_input_get_position(jq);
|
||||
if (jv_get_kind(msg) == JV_KIND_STRING) {
|
||||
fprintf(stderr, "jq: error (at %s): %s\n",
|
||||
jv_string_value(input_pos), jv_string_value(msg));
|
||||
} else {
|
||||
msg = jv_dump_string(msg, 0);
|
||||
fprintf(stderr, "jq: error (at %s) (not a string): %s\n",
|
||||
jv_string_value(input_pos), jv_string_value(msg));
|
||||
}
|
||||
ret = 5;
|
||||
jv_free(input_pos);
|
||||
jv_free(msg);
|
||||
}
|
||||
jv_free(result);
|
||||
return ret;
|
||||
}
|
||||
|
||||
FILE* current_input;
|
||||
const char** input_filenames;
|
||||
int ninput_files;
|
||||
int next_input_idx;
|
||||
static int read_more(char* buf, size_t size) {
|
||||
while (!current_input || feof(current_input)) {
|
||||
if (current_input) {
|
||||
fclose(current_input);
|
||||
current_input = 0;
|
||||
}
|
||||
if (next_input_idx == ninput_files) {
|
||||
return 0;
|
||||
}
|
||||
if (!strcmp(input_filenames[next_input_idx], "-")) {
|
||||
current_input = stdin;
|
||||
} else {
|
||||
current_input = fopen(input_filenames[next_input_idx], "r");
|
||||
}
|
||||
if (!current_input) {
|
||||
fprintf(stderr, "%s: %s: %s\n", progname, input_filenames[next_input_idx], strerror(errno));
|
||||
}
|
||||
next_input_idx++;
|
||||
}
|
||||
|
||||
if (!fgets(buf, size, current_input)) buf[0] = 0;
|
||||
return 1;
|
||||
static void debug_cb(void *data, jv input) {
|
||||
int dumpopts = *(int *)data;
|
||||
jv_dumpf(JV_ARRAY(jv_string("DEBUG:"), input), stderr, dumpopts & ~(JV_PRINT_PRETTY));
|
||||
fprintf(stderr, "\n");
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
jq_state *jq;
|
||||
jq_state *jq = NULL;
|
||||
int ret = 0;
|
||||
int compiled = 0;
|
||||
int parser_flags = 0;
|
||||
int nfiles = 0;
|
||||
int badwrite;
|
||||
jv program_arguments = jv_array();
|
||||
|
||||
#ifdef WIN32
|
||||
SetConsoleOutputCP(CP_UTF8);
|
||||
fflush(stdout);
|
||||
fflush(stderr);
|
||||
_setmode(fileno(stdout), _O_TEXT | _O_U8TEXT);
|
||||
_setmode(fileno(stderr), _O_TEXT | _O_U8TEXT);
|
||||
int wargc;
|
||||
wchar_t **wargv = CommandLineToArgvW(GetCommandLineW(), &wargc);
|
||||
assert(wargc == argc);
|
||||
size_t arg_sz;
|
||||
for (int i = 0; i < argc; i++) {
|
||||
argv[i] = alloca((arg_sz = WideCharToMultiByte(CP_UTF8,
|
||||
0,
|
||||
wargv[i],
|
||||
-1, 0, 0, 0, 0)));
|
||||
WideCharToMultiByte(CP_UTF8, 0, wargv[i], -1, argv[i], arg_sz, 0, 0);
|
||||
}
|
||||
#endif
|
||||
|
||||
if (argc) progname = argv[0];
|
||||
|
||||
if (argc > 1 && !strcmp(argv[1], "--run-tests")) {
|
||||
return jq_testsuite(argc, argv);
|
||||
}
|
||||
|
||||
jq = jq_init();
|
||||
if (jq == NULL) {
|
||||
perror("malloc");
|
||||
return 1;
|
||||
ret = 2;
|
||||
goto out;
|
||||
}
|
||||
|
||||
int dumpopts = JV_PRINT_INDENT_FLAGS(2);
|
||||
const char* program = 0;
|
||||
input_filenames = jv_mem_alloc(sizeof(const char*) * argc);
|
||||
ninput_files = 0;
|
||||
|
||||
jq_util_input_state *input_state = jq_util_input_init(NULL, NULL); // XXX add err_cb
|
||||
|
||||
int further_args_are_files = 0;
|
||||
int jq_flags = 0;
|
||||
jv program_arguments = jv_array();
|
||||
for (int i=1; i<argc; i++) {
|
||||
size_t short_opts = 0;
|
||||
jv lib_search_paths = jv_null();
|
||||
for (int i=1; i<argc; i++, short_opts = 0) {
|
||||
if (further_args_are_files) {
|
||||
input_filenames[ninput_files++] = argv[i];
|
||||
jq_util_input_add_input(input_state, argv[i]);
|
||||
nfiles++;
|
||||
} else if (!strcmp(argv[i], "--")) {
|
||||
if (!program) usage();
|
||||
if (!program) usage(2);
|
||||
further_args_are_files = 1;
|
||||
} else if (!isoptish(argv[i])) {
|
||||
if (program) {
|
||||
input_filenames[ninput_files++] = argv[i];
|
||||
jq_util_input_add_input(input_state, argv[i]);
|
||||
nfiles++;
|
||||
} else {
|
||||
program = argv[i];
|
||||
}
|
||||
} else if (isoption(argv[i], 's', "slurp")) {
|
||||
options |= SLURP;
|
||||
} else if (isoption(argv[i], 'r', "raw-output")) {
|
||||
options |= RAW_OUTPUT;
|
||||
} else if (isoption(argv[i], 'c', "compact-output")) {
|
||||
options |= COMPACT_OUTPUT;
|
||||
} else if (isoption(argv[i], 'C', "color-output")) {
|
||||
options |= COLOUR_OUTPUT;
|
||||
} else if (isoption(argv[i], 'M', "monochrome-output")) {
|
||||
options |= NO_COLOUR_OUTPUT;
|
||||
} else if (isoption(argv[i], 'a', "ascii-output")) {
|
||||
options |= ASCII_OUTPUT;
|
||||
} else if (isoption(argv[i], 'S', "sort-keys")) {
|
||||
options |= SORTED_OUTPUT;
|
||||
} else if (isoption(argv[i], 'R', "raw-input")) {
|
||||
options |= RAW_INPUT;
|
||||
} else if (isoption(argv[i], 'n', "null-input")) {
|
||||
options |= PROVIDE_NULL;
|
||||
} else if (isoption(argv[i], 'f', "from-file")) {
|
||||
options |= FROM_FILE;
|
||||
} else if (isoption(argv[i], 0, "arg")) {
|
||||
if (i >= argc - 2) {
|
||||
fprintf(stderr, "%s: --arg takes two parameters (e.g. -a varname value)\n", progname);
|
||||
die();
|
||||
}
|
||||
jv arg = jv_object();
|
||||
arg = jv_object_set(arg, jv_string("name"), jv_string(argv[i+1]));
|
||||
arg = jv_object_set(arg, jv_string("value"), jv_string(argv[i+2]));
|
||||
program_arguments = jv_array_append(program_arguments, arg);
|
||||
i += 2; // skip the next two arguments
|
||||
} else if (isoption(argv[i], 0, "argfile")) {
|
||||
if (i >= argc - 2) {
|
||||
fprintf(stderr, "%s: --argfile takes two parameters (e.g. -a varname filename)\n", progname);
|
||||
die();
|
||||
}
|
||||
jv arg = jv_object();
|
||||
arg = jv_object_set(arg, jv_string("name"), jv_string(argv[i+1]));
|
||||
jv data = jv_load_file(argv[i+2], 0);
|
||||
if (!jv_is_valid(data)) {
|
||||
data = jv_invalid_get_msg(data);
|
||||
fprintf(stderr, "%s: Bad JSON in --argfile %s %s: %s\n", progname,
|
||||
argv[i+1], argv[i+2], jv_string_value(data));
|
||||
jv_free(data);
|
||||
return 1;
|
||||
}
|
||||
if (jv_get_kind(data) == JV_KIND_ARRAY && jv_array_length(jv_copy(data)) == 1)
|
||||
data = jv_array_get(data, 0);
|
||||
arg = jv_object_set(arg, jv_string("value"), data);
|
||||
program_arguments = jv_array_append(program_arguments, arg);
|
||||
i += 2; // skip the next two arguments
|
||||
} else if (isoption(argv[i], 0, "debug-dump-disasm")) {
|
||||
options |= DUMP_DISASM;
|
||||
} else if (isoption(argv[i], 0, "debug-trace")) {
|
||||
jq_flags |= JQ_DEBUG_TRACE;
|
||||
} else if (isoption(argv[i], 'h', "help")) {
|
||||
usage();
|
||||
} else if (isoption(argv[i], 'V', "version")) {
|
||||
printf("jq-%s\n", JQ_VERSION);
|
||||
return 0;
|
||||
} else {
|
||||
fprintf(stderr, "%s: Unknown option %s\n", progname, argv[i]);
|
||||
die();
|
||||
if (argv[i][1] == 'L') {
|
||||
if (jv_get_kind(lib_search_paths) == JV_KIND_NULL)
|
||||
lib_search_paths = jv_array();
|
||||
if (argv[i][2] != 0) { // -Lname (faster check than strlen)
|
||||
lib_search_paths = jv_array_append(lib_search_paths, jv_string(argv[i]+2));
|
||||
} else if (i >= argc - 1) {
|
||||
fprintf(stderr, "-L takes a parameter: (e.g. -L /search/path or -L/search/path)\n");
|
||||
die();
|
||||
} else {
|
||||
lib_search_paths = jv_array_append(lib_search_paths, jv_string(argv[i+1]));
|
||||
i++;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (isoption(argv[i], 's', "slurp", &short_opts)) {
|
||||
options |= SLURP;
|
||||
if (!short_opts) continue;
|
||||
}
|
||||
if (isoption(argv[i], 'r', "raw-output", &short_opts)) {
|
||||
options |= RAW_OUTPUT;
|
||||
if (!short_opts) continue;
|
||||
}
|
||||
if (isoption(argv[i], 'c', "compact-output", &short_opts)) {
|
||||
dumpopts &= ~(JV_PRINT_TAB | JV_PRINT_INDENT_FLAGS(7));
|
||||
if (!short_opts) continue;
|
||||
}
|
||||
if (isoption(argv[i], 'C', "color-output", &short_opts)) {
|
||||
options |= COLOUR_OUTPUT;
|
||||
if (!short_opts) continue;
|
||||
}
|
||||
if (isoption(argv[i], 'M', "monochrome-output", &short_opts)) {
|
||||
options |= NO_COLOUR_OUTPUT;
|
||||
if (!short_opts) continue;
|
||||
}
|
||||
if (isoption(argv[i], 'a', "ascii-output", &short_opts)) {
|
||||
options |= ASCII_OUTPUT;
|
||||
if (!short_opts) continue;
|
||||
}
|
||||
if (isoption(argv[i], 0, "unbuffered", &short_opts)) {
|
||||
options |= UNBUFFERED_OUTPUT;
|
||||
if (!short_opts) continue;
|
||||
}
|
||||
if (isoption(argv[i], 'S', "sort-keys", &short_opts)) {
|
||||
options |= SORTED_OUTPUT;
|
||||
if (!short_opts) continue;
|
||||
}
|
||||
if (isoption(argv[i], 'R', "raw-input", &short_opts)) {
|
||||
options |= RAW_INPUT;
|
||||
if (!short_opts) continue;
|
||||
}
|
||||
if (isoption(argv[i], 'n', "null-input", &short_opts)) {
|
||||
options |= PROVIDE_NULL;
|
||||
if (!short_opts) continue;
|
||||
}
|
||||
if (isoption(argv[i], 'f', "from-file", &short_opts)) {
|
||||
options |= FROM_FILE;
|
||||
if (!short_opts) continue;
|
||||
}
|
||||
if (isoption(argv[i], 'j', "join-output", &short_opts)) {
|
||||
options |= RAW_OUTPUT | RAW_NO_LF;
|
||||
if (!short_opts) continue;
|
||||
}
|
||||
if (isoption(argv[i], 0, "tab", &short_opts)) {
|
||||
dumpopts &= ~JV_PRINT_INDENT_FLAGS(7);
|
||||
dumpopts |= JV_PRINT_TAB | JV_PRINT_PRETTY;
|
||||
if (!short_opts) continue;
|
||||
}
|
||||
if (isoption(argv[i], 0, "indent", &short_opts)) {
|
||||
if (i >= argc - 1) {
|
||||
fprintf(stderr, "%s: --indent takes one parameter\n", progname);
|
||||
die();
|
||||
}
|
||||
dumpopts &= ~(JV_PRINT_TAB | JV_PRINT_INDENT_FLAGS(7));
|
||||
int indent = atoi(argv[i+1]);
|
||||
if (indent < -1 || indent > 7) {
|
||||
fprintf(stderr, "%s: --indent takes a number between -1 and 7\n", progname);
|
||||
die();
|
||||
}
|
||||
dumpopts |= JV_PRINT_INDENT_FLAGS(indent);
|
||||
i++;
|
||||
if (!short_opts) continue;
|
||||
}
|
||||
if (isoption(argv[i], 0, "seq", &short_opts)) {
|
||||
options |= SEQ;
|
||||
if (!short_opts) continue;
|
||||
}
|
||||
if (isoption(argv[i], 0, "stream", &short_opts)) {
|
||||
parser_flags |= JV_PARSE_STREAMING;
|
||||
if (!short_opts) continue;
|
||||
}
|
||||
if (isoption(argv[i], 0, "stream-errors", &short_opts)) {
|
||||
parser_flags |= JV_PARSE_STREAM_ERRORS;
|
||||
if (!short_opts) continue;
|
||||
}
|
||||
if (isoption(argv[i], 'e', "exit-status", &short_opts)) {
|
||||
options |= EXIT_STATUS;
|
||||
if (!short_opts) continue;
|
||||
}
|
||||
// FIXME: For --arg* we should check that the varname is acceptable
|
||||
if (isoption(argv[i], 0, "arg", &short_opts)) {
|
||||
if (i >= argc - 2) {
|
||||
fprintf(stderr, "%s: --arg takes two parameters (e.g. --arg varname value)\n", progname);
|
||||
die();
|
||||
}
|
||||
jv arg = jv_object();
|
||||
arg = jv_object_set(arg, jv_string("name"), jv_string(argv[i+1]));
|
||||
arg = jv_object_set(arg, jv_string("value"), jv_string(argv[i+2]));
|
||||
program_arguments = jv_array_append(program_arguments, arg);
|
||||
i += 2; // skip the next two arguments
|
||||
if (!short_opts) continue;
|
||||
}
|
||||
if (isoption(argv[i], 0, "argjson", &short_opts)) {
|
||||
if (i >= argc - 2) {
|
||||
fprintf(stderr, "%s: --argjson takes two parameters (e.g. --argjson varname text)\n", progname);
|
||||
die();
|
||||
}
|
||||
jv v = jv_parse(argv[i+2]);
|
||||
if (!jv_is_valid(v)) {
|
||||
fprintf(stderr, "%s: invalid JSON text passed to --argjson\n", progname);
|
||||
die();
|
||||
}
|
||||
jv arg = jv_object();
|
||||
arg = jv_object_set(arg, jv_string("name"), jv_string(argv[i+1]));
|
||||
arg = jv_object_set(arg, jv_string("value"), v);
|
||||
program_arguments = jv_array_append(program_arguments, arg);
|
||||
i += 2; // skip the next two arguments
|
||||
if (!short_opts) continue;
|
||||
}
|
||||
if (isoption(argv[i], 0, "argfile", &short_opts) ||
|
||||
isoption(argv[i], 0, "slurpfile", &short_opts)) {
|
||||
const char *which;
|
||||
if (isoption(argv[i], 0, "argfile", &short_opts))
|
||||
which = "argfile";
|
||||
else
|
||||
which = "slurpfile";
|
||||
if (i >= argc - 2) {
|
||||
fprintf(stderr, "%s: --%s takes two parameters (e.g. --%s varname filename)\n", progname, which, which);
|
||||
die();
|
||||
}
|
||||
jv arg = jv_object();
|
||||
arg = jv_object_set(arg, jv_string("name"), jv_string(argv[i+1]));
|
||||
jv data = jv_load_file(argv[i+2], 0);
|
||||
if (!jv_is_valid(data)) {
|
||||
data = jv_invalid_get_msg(data);
|
||||
fprintf(stderr, "%s: Bad JSON in --%s %s %s: %s\n", progname, which,
|
||||
argv[i+1], argv[i+2], jv_string_value(data));
|
||||
jv_free(data);
|
||||
jv_free(arg);
|
||||
ret = 2;
|
||||
goto out;
|
||||
}
|
||||
if (isoption(argv[i], 0, "argfile", &short_opts) &&
|
||||
jv_get_kind(data) == JV_KIND_ARRAY && jv_array_length(jv_copy(data)) == 1)
|
||||
data = jv_array_get(data, 0);
|
||||
arg = jv_object_set(arg, jv_string("value"), data);
|
||||
program_arguments = jv_array_append(program_arguments, arg);
|
||||
i += 2; // skip the next two arguments
|
||||
if (!short_opts) continue;
|
||||
}
|
||||
if (isoption(argv[i], 0, "debug-dump-disasm", &short_opts)) {
|
||||
options |= DUMP_DISASM;
|
||||
if (!short_opts) continue;
|
||||
}
|
||||
if (isoption(argv[i], 0, "debug-trace", &short_opts)) {
|
||||
jq_flags |= JQ_DEBUG_TRACE;
|
||||
if (!short_opts) continue;
|
||||
}
|
||||
if (isoption(argv[i], 'h', "help", &short_opts)) {
|
||||
usage(0);
|
||||
if (!short_opts) continue;
|
||||
}
|
||||
if (isoption(argv[i], 'V', "version", &short_opts)) {
|
||||
printf("jq-%s\n", JQ_VERSION);
|
||||
ret = 0;
|
||||
goto out;
|
||||
}
|
||||
if (isoption(argv[i], 0, "run-tests", &short_opts)) {
|
||||
i++;
|
||||
// XXX Pass program_arguments, even a whole jq_state *, through;
|
||||
// could be useful for testing
|
||||
ret = jq_testsuite(lib_search_paths,
|
||||
(options & DUMP_DISASM) || (jq_flags & JQ_DEBUG_TRACE),
|
||||
argc - i, argv + i);
|
||||
goto out;
|
||||
}
|
||||
|
||||
// check for unknown options... if this argument was a short option
|
||||
if (strlen(argv[i]) != short_opts + 1) {
|
||||
fprintf(stderr, "%s: Unknown option %s\n", progname, argv[i]);
|
||||
die();
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!program) usage();
|
||||
if (ninput_files == 0) current_input = stdin;
|
||||
|
||||
if ((options & PROVIDE_NULL) && (options & (RAW_INPUT | SLURP))) {
|
||||
fprintf(stderr, "%s: --null-input cannot be used with --raw-input or --slurp\n", progname);
|
||||
die();
|
||||
if (isatty(fileno(stdout))) {
|
||||
dumpopts |= JV_PRINT_ISATTY;
|
||||
#ifndef WIN32
|
||||
/* Disable colour by default on Windows builds as Windows
|
||||
terminals tend not to display it correctly */
|
||||
dumpopts |= JV_PRINT_COLOUR;
|
||||
#endif
|
||||
}
|
||||
|
||||
if (options & SORTED_OUTPUT) dumpopts |= JV_PRINT_SORTED;
|
||||
if (options & ASCII_OUTPUT) dumpopts |= JV_PRINT_ASCII;
|
||||
if (options & COLOUR_OUTPUT) dumpopts |= JV_PRINT_COLOUR;
|
||||
if (options & NO_COLOUR_OUTPUT) dumpopts &= ~JV_PRINT_COLOUR;
|
||||
|
||||
if (jv_get_kind(lib_search_paths) == JV_KIND_NULL) {
|
||||
// Default search path list
|
||||
lib_search_paths = JV_ARRAY(jv_string("~/.jq"),
|
||||
jv_string("$ORIGIN/../lib/jq"),
|
||||
jv_string("$ORIGIN/lib"));
|
||||
}
|
||||
jq_set_attr(jq, jv_string("JQ_LIBRARY_PATH"), lib_search_paths);
|
||||
|
||||
char *origin = strdup(argv[0]);
|
||||
if (origin == NULL) {
|
||||
fprintf(stderr, "Error: out of memory\n");
|
||||
exit(1);
|
||||
}
|
||||
jq_set_attr(jq, jv_string("JQ_ORIGIN"), jv_string(dirname(origin)));
|
||||
free(origin);
|
||||
|
||||
if (strchr(JQ_VERSION, '-') == NULL)
|
||||
jq_set_attr(jq, jv_string("VERSION_DIR"), jv_string(JQ_VERSION));
|
||||
else
|
||||
jq_set_attr(jq, jv_string("VERSION_DIR"), jv_string_fmt("%.*s-master", (int)(strchr(JQ_VERSION, '-') - JQ_VERSION), JQ_VERSION));
|
||||
|
||||
#if (!defined(WIN32) && defined(HAVE_ISATTY)) || defined(HAVE__ISATTY)
|
||||
|
||||
#if defined(HAVE__ISATTY) && defined(isatty)
|
||||
#undef isatty
|
||||
#define isatty _isatty
|
||||
#endif
|
||||
|
||||
if (!program && isatty(STDOUT_FILENO) && !isatty(STDIN_FILENO))
|
||||
program = ".";
|
||||
#endif
|
||||
|
||||
if (!program) usage(2);
|
||||
|
||||
if (options & FROM_FILE) {
|
||||
char *program_origin = strdup(program);
|
||||
if (program_origin == NULL) {
|
||||
perror("malloc");
|
||||
exit(2);
|
||||
}
|
||||
|
||||
jv data = jv_load_file(program, 1);
|
||||
if (!jv_is_valid(data)) {
|
||||
data = jv_invalid_get_msg(data);
|
||||
fprintf(stderr, "%s: %s\n", progname, jv_string_value(data));
|
||||
jv_free(data);
|
||||
return 1;
|
||||
ret = 2;
|
||||
goto out;
|
||||
}
|
||||
compiled = jq_compile_args(jq, jv_string_value(data), program_arguments);
|
||||
jq_set_attr(jq, jv_string("PROGRAM_ORIGIN"), jq_realpath(jv_string(dirname(program_origin))));
|
||||
compiled = jq_compile_args(jq, jv_string_value(data), jv_copy(program_arguments));
|
||||
free(program_origin);
|
||||
jv_free(data);
|
||||
} else {
|
||||
compiled = jq_compile_args(jq, program, program_arguments);
|
||||
jq_set_attr(jq, jv_string("PROGRAM_ORIGIN"), jq_realpath(jv_string("."))); // XXX is this good?
|
||||
compiled = jq_compile_args(jq, program, jv_copy(program_arguments));
|
||||
}
|
||||
if (!compiled){
|
||||
ret = 3;
|
||||
goto out;
|
||||
}
|
||||
if (!compiled) return 1;
|
||||
|
||||
if (options & DUMP_DISASM) {
|
||||
jq_dump_disassembly(jq, 0);
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
if ((options & SEQ))
|
||||
parser_flags |= JV_PARSE_SEQ;
|
||||
|
||||
if ((options & RAW_INPUT))
|
||||
jq_util_input_set_parser(input_state, NULL, (options & SLURP) ? 1 : 0);
|
||||
else
|
||||
jq_util_input_set_parser(input_state, jv_parser_new(parser_flags), (options & SLURP) ? 1 : 0);
|
||||
|
||||
// Let jq program read from inputs
|
||||
jq_set_input_cb(jq, jq_util_input_next_input_cb, input_state);
|
||||
|
||||
// Let jq program call `debug` builtin and have that go somewhere
|
||||
jq_set_debug_cb(jq, debug_cb, &dumpopts);
|
||||
|
||||
if (nfiles == 0)
|
||||
jq_util_input_add_input(input_state, "-");
|
||||
|
||||
if (options & PROVIDE_NULL) {
|
||||
process(jq, jv_null(), jq_flags);
|
||||
ret = process(jq, jv_null(), jq_flags, dumpopts);
|
||||
} else {
|
||||
jv slurped;
|
||||
if (options & SLURP) {
|
||||
if (options & RAW_INPUT) {
|
||||
slurped = jv_string("");
|
||||
} else {
|
||||
slurped = jv_array();
|
||||
jv value;
|
||||
while (jq_util_input_errors(input_state) == 0 &&
|
||||
(jv_is_valid((value = jq_util_input_next_input(input_state))) || jv_invalid_has_msg(jv_copy(value)))) {
|
||||
if (jv_is_valid(value)) {
|
||||
ret = process(jq, value, jq_flags, dumpopts);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
struct jv_parser* parser = jv_parser_new();
|
||||
char buf[4096];
|
||||
while (read_more(buf, sizeof(buf))) {
|
||||
if (options & RAW_INPUT) {
|
||||
int len = strlen(buf);
|
||||
if (len > 0) {
|
||||
if (options & SLURP) {
|
||||
slurped = jv_string_concat(slurped, jv_string(buf));
|
||||
} else {
|
||||
if (buf[len-1] == '\n') buf[len-1] = 0;
|
||||
process(jq, jv_string(buf), jq_flags);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
jv_parser_set_buf(parser, buf, strlen(buf), !feof(stdin));
|
||||
jv value;
|
||||
while (jv_is_valid((value = jv_parser_next(parser)))) {
|
||||
if (options & SLURP) {
|
||||
slurped = jv_array_append(slurped, value);
|
||||
} else {
|
||||
process(jq, value, jq_flags);
|
||||
}
|
||||
}
|
||||
if (jv_invalid_has_msg(jv_copy(value))) {
|
||||
jv msg = jv_invalid_get_msg(value);
|
||||
fprintf(stderr, "parse error: %s\n", jv_string_value(msg));
|
||||
jv_free(msg);
|
||||
ret = 1;
|
||||
break;
|
||||
} else {
|
||||
jv_free(value);
|
||||
}
|
||||
|
||||
// Parse error
|
||||
jv msg = jv_invalid_get_msg(value);
|
||||
if (!(options & SEQ)) {
|
||||
// --seq -> errors are not fatal
|
||||
ret = 4;
|
||||
fprintf(stderr, "parse error: %s\n", jv_string_value(msg));
|
||||
jv_free(msg);
|
||||
break;
|
||||
}
|
||||
}
|
||||
jv_parser_free(parser);
|
||||
if (ret != 0)
|
||||
goto out;
|
||||
if (options & SLURP) {
|
||||
process(jq, slurped, jq_flags);
|
||||
fprintf(stderr, "ignoring parse error: %s\n", jv_string_value(msg));
|
||||
jv_free(msg);
|
||||
}
|
||||
}
|
||||
|
||||
if (jq_util_input_errors(input_state) != 0)
|
||||
ret = 2;
|
||||
|
||||
out:
|
||||
jv_mem_free(input_filenames);
|
||||
badwrite = ferror(stdout);
|
||||
if (fclose(stdout)!=0 || badwrite) {
|
||||
fprintf(stderr,"Error: writing output failed: %s\n", strerror(errno));
|
||||
ret = 2;
|
||||
}
|
||||
|
||||
jv_free(program_arguments);
|
||||
jq_util_input_free(&input_state);
|
||||
jq_teardown(&jq);
|
||||
if (ret >= 10 && (options & EXIT_STATUS))
|
||||
return ret - 10;
|
||||
if (ret >= 10)
|
||||
return 0;
|
||||
return ret;
|
||||
}
|
||||
@@ -1,13 +1,18 @@
|
||||
OP(LOADK, CONSTANT, 1, 1)
|
||||
OP(DUP, NONE, 1, 2)
|
||||
OP(DUPN, NONE, 1, 2)
|
||||
OP(DUP2, NONE, 2, 3)
|
||||
OP(POP, NONE, 1, 0)
|
||||
OP(LOADV, VARIABLE, 1, 1)
|
||||
OP(LOADVN, VARIABLE, 1, 1)
|
||||
OP(STOREV, VARIABLE, 1, 0)
|
||||
OP(STORE_GLOBAL, GLOBAL, 0, 0)
|
||||
OP(INDEX, NONE, 2, 1)
|
||||
OP(INDEX_OPT, NONE, 2, 1)
|
||||
OP(EACH, NONE, 1, 1)
|
||||
OP(EACH_OPT, NONE, 1, 1)
|
||||
OP(FORK, BRANCH, 0, 0)
|
||||
OP(FORK_OPT, BRANCH, 0, 0)
|
||||
OP(JUMP, BRANCH, 0, 0)
|
||||
OP(JUMP_F,BRANCH, 1, 0)
|
||||
OP(BACKTRACK, NONE, 0, 0)
|
||||
@@ -25,8 +30,15 @@ OP(CALL_BUILTIN, CFUNC, -1, 1)
|
||||
|
||||
OP(CALL_JQ, UFUNC, 1, 1)
|
||||
OP(RET, NONE, 1, 1)
|
||||
OP(TAIL_CALL_JQ, UFUNC, 1, 1)
|
||||
|
||||
OP(CLOSURE_PARAM, DEFINITION, 0, 0)
|
||||
OP(CLOSURE_REF, CLOSURE_REF_IMM, 0, 0)
|
||||
OP(CLOSURE_CREATE, DEFINITION, 0, 0)
|
||||
OP(CLOSURE_CREATE_C, DEFINITION, 0, 0)
|
||||
|
||||
OP(TOP, NONE, 0, 0)
|
||||
OP(CLOSURE_PARAM_REGULAR, DEFINITION, 0, 0)
|
||||
OP(DEPS, CONSTANT, 0, 0)
|
||||
OP(MODULEMETA, CONSTANT, 0, 0)
|
||||
OP(GENLABEL, NONE, 0, 1)
|
||||
@@ -0,0 +1,193 @@
|
||||
/* A Bison parser, made by GNU Bison 3.0.2. */
|
||||
|
||||
/* Bison interface for Yacc-like parsers in C
|
||||
|
||||
Copyright (C) 1984, 1989-1990, 2000-2013 Free Software Foundation, Inc.
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||
|
||||
/* As a special exception, you may create a larger work that contains
|
||||
part or all of the Bison parser skeleton and distribute that work
|
||||
under terms of your choice, so long as that work isn't itself a
|
||||
parser generator using the skeleton or a modified version thereof
|
||||
as a parser skeleton. Alternatively, if you modify or redistribute
|
||||
the parser skeleton itself, you may (at your option) remove this
|
||||
special exception, which will cause the skeleton and the resulting
|
||||
Bison output files to be licensed under the GNU General Public
|
||||
License without this special exception.
|
||||
|
||||
This special exception was added by the Free Software Foundation in
|
||||
version 2.2 of Bison. */
|
||||
|
||||
#ifndef YY_YY_PARSER_H_INCLUDED
|
||||
# define YY_YY_PARSER_H_INCLUDED
|
||||
/* Debug traces. */
|
||||
#ifndef YYDEBUG
|
||||
# define YYDEBUG 0
|
||||
#endif
|
||||
#if YYDEBUG
|
||||
extern int yydebug;
|
||||
#endif
|
||||
/* "%code requires" blocks. */
|
||||
#line 11 "parser.y" /* yacc.c:1909 */
|
||||
|
||||
#include "locfile.h"
|
||||
struct lexer_param;
|
||||
|
||||
#define YYLTYPE location
|
||||
#define YYLLOC_DEFAULT(Loc, Rhs, N) \
|
||||
do { \
|
||||
if (N) { \
|
||||
(Loc).start = YYRHSLOC(Rhs, 1).start; \
|
||||
(Loc).end = YYRHSLOC(Rhs, N).end; \
|
||||
} else { \
|
||||
(Loc).start = YYRHSLOC(Rhs, 0).end; \
|
||||
(Loc).end = YYRHSLOC(Rhs, 0).end; \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
#line 61 "parser.h" /* yacc.c:1909 */
|
||||
|
||||
/* Token type. */
|
||||
#ifndef YYTOKENTYPE
|
||||
# define YYTOKENTYPE
|
||||
enum yytokentype
|
||||
{
|
||||
INVALID_CHARACTER = 258,
|
||||
IDENT = 259,
|
||||
FIELD = 260,
|
||||
LITERAL = 261,
|
||||
FORMAT = 262,
|
||||
REC = 263,
|
||||
SETMOD = 264,
|
||||
EQ = 265,
|
||||
NEQ = 266,
|
||||
DEFINEDOR = 267,
|
||||
AS = 268,
|
||||
DEF = 269,
|
||||
MODULE = 270,
|
||||
IMPORT = 271,
|
||||
INCLUDE = 272,
|
||||
IF = 273,
|
||||
THEN = 274,
|
||||
ELSE = 275,
|
||||
ELSE_IF = 276,
|
||||
REDUCE = 277,
|
||||
FOREACH = 278,
|
||||
END = 279,
|
||||
AND = 280,
|
||||
OR = 281,
|
||||
TRY = 282,
|
||||
CATCH = 283,
|
||||
LABEL = 284,
|
||||
BREAK = 285,
|
||||
LOC = 286,
|
||||
SETPIPE = 287,
|
||||
SETPLUS = 288,
|
||||
SETMINUS = 289,
|
||||
SETMULT = 290,
|
||||
SETDIV = 291,
|
||||
SETDEFINEDOR = 292,
|
||||
LESSEQ = 293,
|
||||
GREATEREQ = 294,
|
||||
QQSTRING_START = 295,
|
||||
QQSTRING_TEXT = 296,
|
||||
QQSTRING_INTERP_START = 297,
|
||||
QQSTRING_INTERP_END = 298,
|
||||
QQSTRING_END = 299,
|
||||
FUNCDEF = 300,
|
||||
NONOPT = 301
|
||||
};
|
||||
#endif
|
||||
/* Tokens. */
|
||||
#define INVALID_CHARACTER 258
|
||||
#define IDENT 259
|
||||
#define FIELD 260
|
||||
#define LITERAL 261
|
||||
#define FORMAT 262
|
||||
#define REC 263
|
||||
#define SETMOD 264
|
||||
#define EQ 265
|
||||
#define NEQ 266
|
||||
#define DEFINEDOR 267
|
||||
#define AS 268
|
||||
#define DEF 269
|
||||
#define MODULE 270
|
||||
#define IMPORT 271
|
||||
#define INCLUDE 272
|
||||
#define IF 273
|
||||
#define THEN 274
|
||||
#define ELSE 275
|
||||
#define ELSE_IF 276
|
||||
#define REDUCE 277
|
||||
#define FOREACH 278
|
||||
#define END 279
|
||||
#define AND 280
|
||||
#define OR 281
|
||||
#define TRY 282
|
||||
#define CATCH 283
|
||||
#define LABEL 284
|
||||
#define BREAK 285
|
||||
#define LOC 286
|
||||
#define SETPIPE 287
|
||||
#define SETPLUS 288
|
||||
#define SETMINUS 289
|
||||
#define SETMULT 290
|
||||
#define SETDIV 291
|
||||
#define SETDEFINEDOR 292
|
||||
#define LESSEQ 293
|
||||
#define GREATEREQ 294
|
||||
#define QQSTRING_START 295
|
||||
#define QQSTRING_TEXT 296
|
||||
#define QQSTRING_INTERP_START 297
|
||||
#define QQSTRING_INTERP_END 298
|
||||
#define QQSTRING_END 299
|
||||
#define FUNCDEF 300
|
||||
#define NONOPT 301
|
||||
|
||||
/* Value type. */
|
||||
#if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED
|
||||
typedef union YYSTYPE YYSTYPE;
|
||||
union YYSTYPE
|
||||
{
|
||||
#line 31 "parser.y" /* yacc.c:1909 */
|
||||
|
||||
jv literal;
|
||||
block blk;
|
||||
|
||||
#line 170 "parser.h" /* yacc.c:1909 */
|
||||
};
|
||||
# define YYSTYPE_IS_TRIVIAL 1
|
||||
# define YYSTYPE_IS_DECLARED 1
|
||||
#endif
|
||||
|
||||
/* Location type. */
|
||||
#if ! defined YYLTYPE && ! defined YYLTYPE_IS_DECLARED
|
||||
typedef struct YYLTYPE YYLTYPE;
|
||||
struct YYLTYPE
|
||||
{
|
||||
int first_line;
|
||||
int first_column;
|
||||
int last_line;
|
||||
int last_column;
|
||||
};
|
||||
# define YYLTYPE_IS_DECLARED 1
|
||||
# define YYLTYPE_IS_TRIVIAL 1
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
int yyparse (block* answer, int* errors, struct locfile* locations, struct lexer_param* lexer_param_ptr);
|
||||
|
||||
#endif /* !YY_YY_PARSER_H_INCLUDED */
|
||||
@@ -1,7 +1,8 @@
|
||||
%{
|
||||
#include <assert.h>
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
#include "compile.h"
|
||||
#include "jv_alloc.h"
|
||||
#define YYMALLOC jv_mem_alloc
|
||||
@@ -22,7 +23,7 @@ struct lexer_param;
|
||||
(Loc).end = YYRHSLOC(Rhs, 0).end; \
|
||||
} \
|
||||
} while (0)
|
||||
}
|
||||
}
|
||||
|
||||
%locations
|
||||
%error-verbose
|
||||
@@ -50,19 +51,30 @@ struct lexer_param;
|
||||
%token <literal> FIELD
|
||||
%token <literal> LITERAL
|
||||
%token <literal> FORMAT
|
||||
%token REC ".."
|
||||
%token SETMOD "%="
|
||||
%token EQ "=="
|
||||
%token NEQ "!="
|
||||
%token DEFINEDOR "//"
|
||||
%token AS "as"
|
||||
%token DEF "def"
|
||||
%token MODULE "module"
|
||||
%token IMPORT "import"
|
||||
%token INCLUDE "include"
|
||||
%token IF "if"
|
||||
%token THEN "then"
|
||||
%token ELSE "else"
|
||||
%token ELSE_IF "elif"
|
||||
%token REDUCE "reduce"
|
||||
%token FOREACH "foreach"
|
||||
%token END "end"
|
||||
%token AND "and"
|
||||
%token OR "or"
|
||||
%token TRY "try"
|
||||
%token CATCH "catch"
|
||||
%token LABEL "label"
|
||||
%token BREAK "break"
|
||||
%token LOC "__loc__"
|
||||
%token SETPIPE "|="
|
||||
%token SETPLUS "+="
|
||||
%token SETMINUS "-="
|
||||
@@ -78,20 +90,31 @@ struct lexer_param;
|
||||
%token QQSTRING_INTERP_END
|
||||
%token QQSTRING_END
|
||||
|
||||
/* revolting hack */
|
||||
%left ';'
|
||||
/* Instead of raising this, find a way to use precedence to resolve
|
||||
* shift-reduce conflicts. */
|
||||
%expect 0
|
||||
|
||||
%precedence FUNCDEF
|
||||
%right '|'
|
||||
%left ','
|
||||
%right "//"
|
||||
%nonassoc '=' SETPIPE SETPLUS SETMINUS SETMULT SETDIV SETDEFINEDOR
|
||||
%nonassoc '=' SETPIPE SETPLUS SETMINUS SETMULT SETDIV SETMOD SETDEFINEDOR
|
||||
%left OR
|
||||
%left AND
|
||||
%nonassoc NEQ EQ '<' '>' LESSEQ GREATEREQ
|
||||
%left '+' '-'
|
||||
%left '*' '/'
|
||||
%left '*' '/' '%'
|
||||
%precedence NONOPT /* non-optional; rules for which a specialized
|
||||
'?' rule should be preferred over Exp '?' */
|
||||
%precedence '?'
|
||||
%precedence "try"
|
||||
%precedence "catch"
|
||||
|
||||
|
||||
%type <blk> Exp Term MkDict MkDictPair ExpD ElseBody QQString FuncDef FuncDefs String
|
||||
%type <blk> Exp Term MkDict MkDictPair ExpD ElseBody QQString
|
||||
%type <blk> FuncDef FuncDefs String Import Imports Param Params
|
||||
%type <blk> Arg Args Module Pattern ArrayPats ObjPats ObjPat
|
||||
%type <literal> Keyword
|
||||
%{
|
||||
#include "lexer.h"
|
||||
struct lexer_param {
|
||||
@@ -104,13 +127,21 @@ struct lexer_param {
|
||||
/*YYERROR*/; \
|
||||
} while (0)
|
||||
|
||||
void yyerror(YYLTYPE* loc, block* answer, int* errors,
|
||||
void yyerror(YYLTYPE* loc, block* answer, int* errors,
|
||||
struct locfile* locations, struct lexer_param* lexer_param_ptr, const char *s){
|
||||
(*errors)++;
|
||||
locfile_locate(locations, *loc, "error: %s", s);
|
||||
if (strstr(s, "unexpected")) {
|
||||
#ifdef WIN32
|
||||
locfile_locate(locations, *loc, "jq: error: %s (Windows cmd shell quoting issues?)", s);
|
||||
#else
|
||||
locfile_locate(locations, *loc, "jq: error: %s (Unix shell quoting issues?)", s);
|
||||
#endif
|
||||
} else {
|
||||
locfile_locate(locations, *loc, "jq: error: %s", s);
|
||||
}
|
||||
}
|
||||
|
||||
int yylex(YYSTYPE* yylval, YYLTYPE* yylloc, block* answer, int* errors,
|
||||
int yylex(YYSTYPE* yylval, YYLTYPE* yylloc, block* answer, int* errors,
|
||||
struct locfile* locations, struct lexer_param* lexer_param_ptr) {
|
||||
yyscan_t lexer = lexer_param_ptr->lexer;
|
||||
int tok = jq_yylex(yylval, yylloc, lexer);
|
||||
@@ -136,7 +167,11 @@ static block gen_index(block obj, block key) {
|
||||
return BLOCK(gen_subexp(key), obj, gen_op_simple(INDEX));
|
||||
}
|
||||
|
||||
static block gen_slice_index(block obj, block start, block end) {
|
||||
static block gen_index_opt(block obj, block key) {
|
||||
return BLOCK(gen_subexp(key), obj, gen_op_simple(INDEX_OPT));
|
||||
}
|
||||
|
||||
static block gen_slice_index(block obj, block start, block end, opcode idx_op) {
|
||||
block key = BLOCK(gen_subexp(gen_const(jv_object())),
|
||||
gen_subexp(gen_const(jv_string("start"))),
|
||||
gen_subexp(start),
|
||||
@@ -144,16 +179,60 @@ static block gen_slice_index(block obj, block start, block end) {
|
||||
gen_subexp(gen_const(jv_string("end"))),
|
||||
gen_subexp(end),
|
||||
gen_op_simple(INSERT));
|
||||
return BLOCK(key, obj, gen_op_simple(INDEX));
|
||||
return BLOCK(key, obj, gen_op_simple(idx_op));
|
||||
}
|
||||
|
||||
static block constant_fold(block a, block b, int op) {
|
||||
if (!block_is_single(a) || !block_is_const(a) ||
|
||||
!block_is_single(b) || !block_is_const(b))
|
||||
return gen_noop();
|
||||
if (block_const_kind(a) != block_const_kind(b))
|
||||
return gen_noop();
|
||||
|
||||
jv res = jv_invalid();
|
||||
|
||||
if (block_const_kind(a) == JV_KIND_NUMBER) {
|
||||
double na = jv_number_value(block_const(a));
|
||||
double nb = jv_number_value(block_const(b));
|
||||
switch (op) {
|
||||
case '+': res = jv_number(na + nb); break;
|
||||
case '-': res = jv_number(na - nb); break;
|
||||
case '*': res = jv_number(na * nb); break;
|
||||
case '/': res = jv_number(na / nb); break;
|
||||
case EQ: res = (na == nb ? jv_true() : jv_false()); break;
|
||||
case NEQ: res = (na != nb ? jv_true() : jv_false()); break;
|
||||
case '<': res = (na < nb ? jv_true() : jv_false()); break;
|
||||
case '>': res = (na > nb ? jv_true() : jv_false()); break;
|
||||
case LESSEQ: res = (na <= nb ? jv_true() : jv_false()); break;
|
||||
case GREATEREQ: res = (na >= nb ? jv_true() : jv_false()); break;
|
||||
default: break;
|
||||
}
|
||||
} else if (op == '+' && block_const_kind(a) == JV_KIND_STRING) {
|
||||
res = jv_string_concat(block_const(a), block_const(b));
|
||||
} else {
|
||||
return gen_noop();
|
||||
}
|
||||
|
||||
if (jv_get_kind(res) == JV_KIND_INVALID)
|
||||
return gen_noop();
|
||||
|
||||
block_free(a);
|
||||
block_free(b);
|
||||
return gen_const(res);
|
||||
}
|
||||
|
||||
static block gen_binop(block a, block b, int op) {
|
||||
block folded = constant_fold(a, b, op);
|
||||
if (!block_is_noop(folded))
|
||||
return folded;
|
||||
|
||||
const char* funcname = 0;
|
||||
switch (op) {
|
||||
case '+': funcname = "_plus"; break;
|
||||
case '-': funcname = "_minus"; break;
|
||||
case '*': funcname = "_multiply"; break;
|
||||
case '/': funcname = "_divide"; break;
|
||||
case '%': funcname = "_mod"; break;
|
||||
case EQ: funcname = "_equal"; break;
|
||||
case NEQ: funcname = "_notequal"; break;
|
||||
case '<': funcname = "_less"; break;
|
||||
@@ -175,16 +254,16 @@ static block gen_definedor_assign(block object, block val) {
|
||||
return BLOCK(gen_op_simple(DUP),
|
||||
val, tmp,
|
||||
gen_call("_modify", BLOCK(gen_lambda(object),
|
||||
gen_lambda(gen_definedor(gen_noop(),
|
||||
gen_lambda(gen_definedor(gen_noop(),
|
||||
gen_op_bound(LOADV, tmp))))));
|
||||
}
|
||||
|
||||
|
||||
static block gen_update(block object, block val, int optype) {
|
||||
block tmp = gen_op_var_fresh(STOREV, "tmp");
|
||||
return BLOCK(gen_op_simple(DUP),
|
||||
val,
|
||||
tmp,
|
||||
gen_call("_modify", BLOCK(gen_lambda(object),
|
||||
gen_call("_modify", BLOCK(gen_lambda(object),
|
||||
gen_lambda(gen_binop(gen_noop(),
|
||||
gen_op_bound(LOADV, tmp),
|
||||
optype)))));
|
||||
@@ -194,51 +273,102 @@ static block gen_update(block object, block val, int optype) {
|
||||
|
||||
%%
|
||||
TopLevel:
|
||||
Exp {
|
||||
*answer = $1;
|
||||
Module Imports Exp {
|
||||
*answer = BLOCK($1, $2, gen_op_simple(TOP), $3);
|
||||
} |
|
||||
FuncDefs {
|
||||
*answer = $1;
|
||||
}
|
||||
Module Imports FuncDefs {
|
||||
*answer = BLOCK($1, $2, $3);
|
||||
}
|
||||
|
||||
Module:
|
||||
%empty {
|
||||
$$ = gen_noop();
|
||||
} |
|
||||
"module" Exp ';' {
|
||||
if (!block_is_const($2)) {
|
||||
FAIL(@$, "Module metadata must be constant.");
|
||||
$$ = gen_noop();
|
||||
} else {
|
||||
$$ = gen_module($2);
|
||||
}
|
||||
}
|
||||
|
||||
Imports:
|
||||
%empty {
|
||||
$$ = gen_noop();
|
||||
} |
|
||||
Import Imports {
|
||||
$$ = BLOCK($1, $2);
|
||||
}
|
||||
|
||||
FuncDefs:
|
||||
/* empty */ {
|
||||
%empty {
|
||||
$$ = gen_noop();
|
||||
} |
|
||||
FuncDef FuncDefs {
|
||||
$$ = block_join($1, $2);
|
||||
$$ = block_bind($1, $2, OP_IS_CALL_PSEUDO);
|
||||
}
|
||||
|
||||
Exp:
|
||||
FuncDef Exp %prec ';' {
|
||||
$$ = block_bind($1, $2, OP_IS_CALL_PSEUDO);
|
||||
FuncDef Exp %prec FUNCDEF {
|
||||
$$ = block_bind_referenced($1, $2, OP_IS_CALL_PSEUDO);
|
||||
} |
|
||||
|
||||
Term "as" '$' IDENT '|' Exp {
|
||||
$$ = gen_var_binding($1, jv_string_value($4), $6);
|
||||
jv_free($4);
|
||||
Term "as" Pattern '|' Exp {
|
||||
$$ = gen_destructure($1, $3, $5);
|
||||
} |
|
||||
|
||||
"reduce" Term "as" '$' IDENT '(' Exp ';' Exp ')' {
|
||||
$$ = gen_reduce(jv_string_value($5), $2, $7, $9);
|
||||
jv_free($5);
|
||||
"reduce" Term "as" Pattern '(' Exp ';' Exp ')' {
|
||||
$$ = gen_reduce($2, $4, $6, $8);
|
||||
} |
|
||||
|
||||
"foreach" Term "as" Pattern '(' Exp ';' Exp ';' Exp ')' {
|
||||
$$ = gen_foreach($2, $4, $6, $8, $10);
|
||||
} |
|
||||
|
||||
"foreach" Term "as" Pattern '(' Exp ';' Exp ')' {
|
||||
$$ = gen_foreach($2, $4, $6, $8, gen_noop());
|
||||
} |
|
||||
|
||||
"if" Exp "then" Exp ElseBody {
|
||||
$$ = gen_cond($2, $4, $5);
|
||||
} |
|
||||
"if" Exp "then" error {
|
||||
FAIL(@$, "Possibly unterminated 'if' statment");
|
||||
FAIL(@$, "Possibly unterminated 'if' statement");
|
||||
$$ = $2;
|
||||
} |
|
||||
|
||||
"try" Exp "catch" Exp {
|
||||
//$$ = BLOCK(gen_op_target(FORK_OPT, $2), $2, $4);
|
||||
$$ = gen_try($2, gen_try_handler($4));
|
||||
} |
|
||||
"try" Exp {
|
||||
//$$ = BLOCK(gen_op_target(FORK_OPT, $2), $2, gen_op_simple(BACKTRACK));
|
||||
$$ = gen_try($2, gen_op_simple(BACKTRACK));
|
||||
} |
|
||||
"try" Exp "catch" error {
|
||||
FAIL(@$, "Possibly unterminated 'try' statement");
|
||||
$$ = $2;
|
||||
} |
|
||||
|
||||
"label" '$' IDENT '|' Exp {
|
||||
jv v = jv_string_fmt("*label-%s", jv_string_value($3));
|
||||
$$ = gen_location(@$, locations, gen_label(jv_string_value(v), $5));
|
||||
jv_free($3);
|
||||
jv_free(v);
|
||||
} |
|
||||
|
||||
Exp '?' {
|
||||
$$ = gen_try($1, gen_op_simple(BACKTRACK));
|
||||
} |
|
||||
|
||||
Exp '=' Exp {
|
||||
$$ = gen_call("_assign", BLOCK(gen_lambda($1), gen_lambda($3)));
|
||||
} |
|
||||
|
||||
Exp "or" Exp {
|
||||
$$ = gen_or($1, $3);
|
||||
} |
|
||||
} |
|
||||
|
||||
Exp "and" Exp {
|
||||
$$ = gen_and($1, $3);
|
||||
@@ -256,12 +386,12 @@ Exp "|=" Exp {
|
||||
$$ = gen_call("_modify", BLOCK(gen_lambda($1), gen_lambda($3)));
|
||||
} |
|
||||
|
||||
Exp '|' Exp {
|
||||
$$ = block_join($1, $3);
|
||||
Exp '|' Exp {
|
||||
$$ = block_join($1, $3);
|
||||
} |
|
||||
|
||||
Exp ',' Exp {
|
||||
$$ = gen_both($1, $3);
|
||||
Exp ',' Exp {
|
||||
$$ = gen_both($1, $3);
|
||||
} |
|
||||
|
||||
Exp '+' Exp {
|
||||
@@ -294,12 +424,24 @@ Exp "*=" Exp {
|
||||
|
||||
Exp '/' Exp {
|
||||
$$ = gen_binop($1, $3, '/');
|
||||
if (block_is_const_inf($$))
|
||||
FAIL(@$, "Division by zero?");
|
||||
} |
|
||||
|
||||
Exp '%' Exp {
|
||||
$$ = gen_binop($1, $3, '%');
|
||||
if (block_is_const_inf($$))
|
||||
FAIL(@$, "Remainder by zero?");
|
||||
} |
|
||||
|
||||
Exp "/=" Exp {
|
||||
$$ = gen_update($1, $3, '/');
|
||||
} |
|
||||
|
||||
Exp SETMOD Exp {
|
||||
$$ = gen_update($1, $3, '%');
|
||||
} |
|
||||
|
||||
Exp "==" Exp {
|
||||
$$ = gen_binop($1, $3, EQ);
|
||||
} |
|
||||
@@ -324,8 +466,67 @@ Exp ">=" Exp {
|
||||
$$ = gen_binop($1, $3, GREATEREQ);
|
||||
} |
|
||||
|
||||
Term {
|
||||
$$ = $1;
|
||||
Term {
|
||||
$$ = $1;
|
||||
}
|
||||
|
||||
Import:
|
||||
"import" String "as" '$' IDENT ';' {
|
||||
jv v = block_const($2);
|
||||
// XXX Make gen_import take only blocks and the int is_data so we
|
||||
// don't have to free so much stuff here
|
||||
$$ = gen_import(jv_string_value(v), gen_noop(), jv_string_value($5), 1);
|
||||
block_free($2);
|
||||
jv_free($5);
|
||||
jv_free(v);
|
||||
} |
|
||||
"import" String "as" IDENT ';' {
|
||||
jv v = block_const($2);
|
||||
$$ = gen_import(jv_string_value(v), gen_noop(), jv_string_value($4), 0);
|
||||
block_free($2);
|
||||
jv_free($4);
|
||||
jv_free(v);
|
||||
} |
|
||||
"include" String ';' {
|
||||
jv v = block_const($2);
|
||||
$$ = gen_import(jv_string_value(v), gen_noop(), NULL, 0);
|
||||
block_free($2);
|
||||
jv_free(v);
|
||||
} |
|
||||
"import" String "as" IDENT Exp ';' {
|
||||
if (!block_is_const($5)) {
|
||||
FAIL(@$, "Module metadata must be constant.");
|
||||
$$ = gen_noop();
|
||||
} else {
|
||||
jv v = block_const($2);
|
||||
$$ = gen_import(jv_string_value(v), $5, jv_string_value($4), 0);
|
||||
jv_free(v);
|
||||
}
|
||||
block_free($2);
|
||||
jv_free($4);
|
||||
} |
|
||||
"include" String Exp ';' {
|
||||
if (!block_is_const($3)) {
|
||||
FAIL(@$, "Module metadata must be constant.");
|
||||
$$ = gen_noop();
|
||||
} else {
|
||||
jv v = block_const($2);
|
||||
$$ = gen_import(jv_string_value(v), $3, NULL, 0);
|
||||
jv_free(v);
|
||||
}
|
||||
block_free($2);
|
||||
} |
|
||||
"import" String "as" '$' IDENT Exp ';' {
|
||||
if (!block_is_const($6)) {
|
||||
FAIL(@$, "Module metadata must be constant.");
|
||||
$$ = gen_noop();
|
||||
} else {
|
||||
jv v = block_const($2);
|
||||
$$ = gen_import(jv_string_value(v), $6, jv_string_value($5), 1);
|
||||
jv_free(v);
|
||||
}
|
||||
block_free($2);
|
||||
jv_free($5);
|
||||
}
|
||||
|
||||
FuncDef:
|
||||
@@ -334,24 +535,29 @@ FuncDef:
|
||||
jv_free($2);
|
||||
} |
|
||||
|
||||
"def" IDENT '(' IDENT ')' ':' Exp ';' {
|
||||
$$ = gen_function(jv_string_value($2),
|
||||
gen_param(jv_string_value($4)),
|
||||
$7);
|
||||
"def" IDENT '(' Params ')' ':' Exp ';' {
|
||||
$$ = gen_function(jv_string_value($2), $4, $7);
|
||||
jv_free($2);
|
||||
jv_free($4);
|
||||
} |
|
||||
|
||||
"def" IDENT '(' IDENT ';' IDENT ')' ':' Exp ';' {
|
||||
$$ = gen_function(jv_string_value($2),
|
||||
BLOCK(gen_param(jv_string_value($4)),
|
||||
gen_param(jv_string_value($6))),
|
||||
$9);
|
||||
jv_free($2);
|
||||
jv_free($4);
|
||||
jv_free($6);
|
||||
}
|
||||
|
||||
Params:
|
||||
Param {
|
||||
$$ = $1;
|
||||
} |
|
||||
Params ';' Param {
|
||||
$$ = BLOCK($1, $3);
|
||||
}
|
||||
|
||||
Param:
|
||||
'$' IDENT {
|
||||
$$ = gen_param_regular(jv_string_value($2));
|
||||
jv_free($2);
|
||||
} |
|
||||
|
||||
IDENT {
|
||||
$$ = gen_param(jv_string_value($1));
|
||||
jv_free($1);
|
||||
}
|
||||
|
||||
|
||||
String:
|
||||
@@ -366,7 +572,7 @@ FORMAT QQSTRING_START { $<literal>$ = $1; } QQString QQSTRING_END {
|
||||
|
||||
|
||||
QQString:
|
||||
/* empty */ {
|
||||
%empty {
|
||||
$$ = gen_const(jv_string(""));
|
||||
} |
|
||||
QQString QQSTRING_TEXT {
|
||||
@@ -386,10 +592,12 @@ ElseBody:
|
||||
}
|
||||
|
||||
ExpD:
|
||||
ExpD '|' ExpD {
|
||||
ExpD '|' ExpD {
|
||||
$$ = block_join($1, $3);
|
||||
} |
|
||||
|
||||
'-' ExpD {
|
||||
$$ = BLOCK($2, gen_call("_negate", gen_noop()));
|
||||
} |
|
||||
Term {
|
||||
$$ = $1;
|
||||
}
|
||||
@@ -397,18 +605,45 @@ Term {
|
||||
|
||||
Term:
|
||||
'.' {
|
||||
$$ = gen_noop();
|
||||
$$ = gen_noop();
|
||||
} |
|
||||
Term FIELD {
|
||||
REC {
|
||||
$$ = gen_call("recurse", gen_noop());
|
||||
} |
|
||||
BREAK '$' IDENT {
|
||||
jv v = jv_string_fmt("*label-%s", jv_string_value($3)); // impossible symbol
|
||||
$$ = gen_location(@$, locations,
|
||||
BLOCK(gen_op_unbound(LOADV, jv_string_value(v)),
|
||||
gen_call("error", gen_noop())));
|
||||
jv_free(v);
|
||||
jv_free($3);
|
||||
} |
|
||||
BREAK error {
|
||||
FAIL(@$, "break requires a label to break to");
|
||||
$$ = gen_noop();
|
||||
} |
|
||||
Term FIELD '?' {
|
||||
$$ = gen_index_opt($1, gen_const($2));
|
||||
} |
|
||||
FIELD '?' {
|
||||
$$ = gen_index_opt(gen_noop(), gen_const($1));
|
||||
} |
|
||||
Term '.' String '?' {
|
||||
$$ = gen_index_opt($1, $3);
|
||||
} |
|
||||
'.' String '?' {
|
||||
$$ = gen_index_opt(gen_noop(), $2);
|
||||
} |
|
||||
Term FIELD %prec NONOPT {
|
||||
$$ = gen_index($1, gen_const($2));
|
||||
} |
|
||||
FIELD {
|
||||
$$ = gen_index(gen_noop(), gen_const($1));
|
||||
FIELD %prec NONOPT {
|
||||
$$ = gen_index(gen_noop(), gen_const($1));
|
||||
} |
|
||||
Term '.' String {
|
||||
Term '.' String %prec NONOPT {
|
||||
$$ = gen_index($1, $3);
|
||||
} |
|
||||
'.' String {
|
||||
'.' String %prec NONOPT {
|
||||
$$ = gen_index(gen_noop(), $2);
|
||||
} |
|
||||
'.' error {
|
||||
@@ -419,25 +654,40 @@ Term '.' String {
|
||||
jv_free($2);
|
||||
FAIL(@$, "try .[\"field\"] instead of .field for unusually named fields");
|
||||
$$ = gen_noop();
|
||||
} |
|
||||
} |
|
||||
/* FIXME: string literals */
|
||||
Term '[' Exp ']' {
|
||||
$$ = gen_index($1, $3);
|
||||
Term '[' Exp ']' '?' {
|
||||
$$ = gen_index_opt($1, $3);
|
||||
} |
|
||||
Term '[' ']' {
|
||||
$$ = block_join($1, gen_op_simple(EACH));
|
||||
Term '[' Exp ']' %prec NONOPT {
|
||||
$$ = gen_index($1, $3);
|
||||
} |
|
||||
Term '[' Exp ':' Exp ']' {
|
||||
$$ = gen_slice_index($1, $3, $5);
|
||||
Term '[' ']' '?' {
|
||||
$$ = block_join($1, gen_op_simple(EACH_OPT));
|
||||
} |
|
||||
Term '[' Exp ':' ']' {
|
||||
$$ = gen_slice_index($1, $3, gen_const(jv_null()));
|
||||
Term '[' ']' %prec NONOPT {
|
||||
$$ = block_join($1, gen_op_simple(EACH));
|
||||
} |
|
||||
Term '[' ':' Exp ']' {
|
||||
$$ = gen_slice_index($1, gen_const(jv_null()), $4);
|
||||
Term '[' Exp ':' Exp ']' '?' {
|
||||
$$ = gen_slice_index($1, $3, $5, INDEX_OPT);
|
||||
} |
|
||||
Term '[' Exp ':' ']' '?' {
|
||||
$$ = gen_slice_index($1, $3, gen_const(jv_null()), INDEX_OPT);
|
||||
} |
|
||||
Term '[' ':' Exp ']' '?' {
|
||||
$$ = gen_slice_index($1, gen_const(jv_null()), $4, INDEX_OPT);
|
||||
} |
|
||||
Term '[' Exp ':' Exp ']' %prec NONOPT {
|
||||
$$ = gen_slice_index($1, $3, $5, INDEX);
|
||||
} |
|
||||
Term '[' Exp ':' ']' %prec NONOPT {
|
||||
$$ = gen_slice_index($1, $3, gen_const(jv_null()), INDEX);
|
||||
} |
|
||||
Term '[' ':' Exp ']' %prec NONOPT {
|
||||
$$ = gen_slice_index($1, gen_const(jv_null()), $4, INDEX);
|
||||
} |
|
||||
LITERAL {
|
||||
$$ = gen_const($1);
|
||||
$$ = gen_const($1);
|
||||
} |
|
||||
String {
|
||||
$$ = $1;
|
||||
@@ -445,34 +695,45 @@ String {
|
||||
FORMAT {
|
||||
$$ = gen_format(gen_noop(), $1);
|
||||
} |
|
||||
'(' Exp ')' {
|
||||
$$ = $2;
|
||||
} |
|
||||
'[' Exp ']' {
|
||||
$$ = gen_collect($2);
|
||||
'(' Exp ')' {
|
||||
$$ = $2;
|
||||
} |
|
||||
'[' ']' {
|
||||
$$ = gen_const(jv_array());
|
||||
'[' Exp ']' {
|
||||
$$ = gen_collect($2);
|
||||
} |
|
||||
'{' MkDict '}' {
|
||||
$$ = BLOCK(gen_subexp(gen_const(jv_object())), $2, gen_op_simple(POP));
|
||||
'[' ']' {
|
||||
$$ = gen_const(jv_array());
|
||||
} |
|
||||
'{' MkDict '}' {
|
||||
block o = gen_const_object($2);
|
||||
if (o.first != NULL)
|
||||
$$ = o;
|
||||
else
|
||||
$$ = BLOCK(gen_subexp(gen_const(jv_object())), $2, gen_op_simple(POP));
|
||||
} |
|
||||
'$' LOC {
|
||||
$$ = gen_const(JV_OBJECT(jv_string("file"), jv_copy(locations->fname),
|
||||
jv_string("line"), jv_number(locfile_get_line(locations, @$.start) + 1)));
|
||||
} |
|
||||
'$' IDENT {
|
||||
$$ = gen_location(@$, gen_op_unbound(LOADV, jv_string_value($2)));
|
||||
$$ = gen_location(@$, locations, gen_op_unbound(LOADV, jv_string_value($2)));
|
||||
jv_free($2);
|
||||
} |
|
||||
} |
|
||||
IDENT {
|
||||
$$ = gen_location(@$, gen_call(jv_string_value($1), gen_noop()));
|
||||
const char *s = jv_string_value($1);
|
||||
if (strcmp(s, "false") == 0)
|
||||
$$ = gen_const(jv_false());
|
||||
else if (strcmp(s, "true") == 0)
|
||||
$$ = gen_const(jv_true());
|
||||
else if (strcmp(s, "null") == 0)
|
||||
$$ = gen_const(jv_null());
|
||||
else
|
||||
$$ = gen_location(@$, locations, gen_call(s, gen_noop()));
|
||||
jv_free($1);
|
||||
} |
|
||||
IDENT '(' Exp ')' {
|
||||
$$ = gen_call(jv_string_value($1), gen_lambda($3));
|
||||
$$ = gen_location(@1, $$);
|
||||
jv_free($1);
|
||||
} |
|
||||
IDENT '(' Exp ';' Exp ')' {
|
||||
$$ = gen_call(jv_string_value($1), BLOCK(gen_lambda($3), gen_lambda($5)));
|
||||
$$ = gen_location(@1, $$);
|
||||
IDENT '(' Args ')' {
|
||||
$$ = gen_call(jv_string_value($1), $3);
|
||||
$$ = gen_location(@1, locations, $$);
|
||||
jv_free($1);
|
||||
} |
|
||||
'(' error ')' { $$ = gen_noop(); } |
|
||||
@@ -480,18 +741,138 @@ IDENT '(' Exp ';' Exp ')' {
|
||||
Term '[' error ']' { $$ = $1; } |
|
||||
'{' error '}' { $$ = gen_noop(); }
|
||||
|
||||
Args:
|
||||
Arg {
|
||||
$$ = $1;
|
||||
} |
|
||||
Args ';' Arg {
|
||||
$$ = BLOCK($1, $3);
|
||||
}
|
||||
|
||||
Arg:
|
||||
Exp {
|
||||
$$ = gen_lambda($1);
|
||||
}
|
||||
|
||||
Pattern:
|
||||
'$' IDENT {
|
||||
$$ = gen_op_unbound(STOREV, jv_string_value($2));
|
||||
jv_free($2);
|
||||
} |
|
||||
'[' ArrayPats ']' {
|
||||
$$ = BLOCK($2, gen_op_simple(POP));
|
||||
} |
|
||||
'{' ObjPats '}' {
|
||||
$$ = BLOCK($2, gen_op_simple(POP));
|
||||
}
|
||||
|
||||
ArrayPats:
|
||||
Pattern {
|
||||
$$ = gen_array_matcher(gen_noop(), $1);
|
||||
} |
|
||||
ArrayPats ',' Pattern {
|
||||
$$ = gen_array_matcher($1, $3);
|
||||
}
|
||||
|
||||
ObjPats:
|
||||
ObjPat {
|
||||
$$ = $1;
|
||||
} |
|
||||
ObjPats ',' ObjPat {
|
||||
$$ = BLOCK($1, $3);
|
||||
}
|
||||
|
||||
ObjPat:
|
||||
'$' IDENT {
|
||||
$$ = gen_object_matcher(gen_const($2), gen_op_unbound(STOREV, jv_string_value($2)));
|
||||
} |
|
||||
IDENT ':' Pattern {
|
||||
$$ = gen_object_matcher(gen_const($1), $3);
|
||||
} |
|
||||
Keyword ':' Pattern {
|
||||
$$ = gen_object_matcher(gen_const($1), $3);
|
||||
} |
|
||||
String ':' Pattern {
|
||||
$$ = gen_object_matcher($1, $3);
|
||||
} |
|
||||
'(' Exp ')' ':' Pattern {
|
||||
$$ = gen_object_matcher($2, $5);
|
||||
}
|
||||
|
||||
Keyword:
|
||||
"as" {
|
||||
$$ = jv_string("as");
|
||||
} |
|
||||
"def" {
|
||||
$$ = jv_string("def");
|
||||
} |
|
||||
"module" {
|
||||
$$ = jv_string("module");
|
||||
} |
|
||||
"import" {
|
||||
$$ = jv_string("import");
|
||||
} |
|
||||
"include" {
|
||||
$$ = jv_string("include");
|
||||
} |
|
||||
"if" {
|
||||
$$ = jv_string("if");
|
||||
} |
|
||||
"then" {
|
||||
$$ = jv_string("then");
|
||||
} |
|
||||
"else" {
|
||||
$$ = jv_string("else");
|
||||
} |
|
||||
"elif" {
|
||||
$$ = jv_string("elif");
|
||||
} |
|
||||
"reduce" {
|
||||
$$ = jv_string("reduce");
|
||||
} |
|
||||
"foreach" {
|
||||
$$ = jv_string("foreach");
|
||||
} |
|
||||
"end" {
|
||||
$$ = jv_string("end");
|
||||
} |
|
||||
"and" {
|
||||
$$ = jv_string("and");
|
||||
} |
|
||||
"or" {
|
||||
$$ = jv_string("or");
|
||||
} |
|
||||
"try" {
|
||||
$$ = jv_string("try");
|
||||
} |
|
||||
"catch" {
|
||||
$$ = jv_string("catch");
|
||||
} |
|
||||
"label" {
|
||||
$$ = jv_string("label");
|
||||
} |
|
||||
"break" {
|
||||
$$ = jv_string("break");
|
||||
} |
|
||||
"__loc__" {
|
||||
$$ = jv_string("__loc__");
|
||||
}
|
||||
|
||||
MkDict:
|
||||
{
|
||||
$$=gen_noop();
|
||||
%empty {
|
||||
$$=gen_noop();
|
||||
} |
|
||||
MkDictPair { $$ = $1; }
|
||||
| MkDictPair ',' MkDict { $$=block_join($1, $3); }
|
||||
| error ',' MkDict { $$ = $3; }
|
||||
|
||||
MkDictPair
|
||||
: IDENT ':' ExpD {
|
||||
MkDictPair:
|
||||
IDENT ':' ExpD {
|
||||
$$ = gen_dictpair(gen_const($1), $3);
|
||||
}
|
||||
| Keyword ':' ExpD {
|
||||
$$ = gen_dictpair(gen_const($1), $3);
|
||||
}
|
||||
| String ':' ExpD {
|
||||
$$ = gen_dictpair($1, $3);
|
||||
}
|
||||
@@ -499,6 +880,10 @@ MkDictPair
|
||||
$$ = gen_dictpair($1, BLOCK(gen_op_simple(POP), gen_op_simple(DUP2),
|
||||
gen_op_simple(DUP2), gen_op_simple(INDEX)));
|
||||
}
|
||||
| '$' IDENT {
|
||||
$$ = gen_dictpair(gen_const($2),
|
||||
gen_location(@$, locations, gen_op_unbound(LOADV, jv_string_value($2))));
|
||||
}
|
||||
| IDENT {
|
||||
$$ = gen_dictpair(gen_const(jv_copy($1)),
|
||||
gen_index(gen_noop(), gen_const($1)));
|
||||
@@ -529,9 +914,10 @@ int jq_parse(struct locfile* locations, block* answer) {
|
||||
int jq_parse_library(struct locfile* locations, block* answer) {
|
||||
int errs = jq_parse(locations, answer);
|
||||
if (errs) return errs;
|
||||
if (!block_has_only_binders(*answer, OP_IS_CALL_PSEUDO)) {
|
||||
locfile_locate(locations, UNKNOWN_LOCATION, "error: library should only have function definitions, not a main expression");
|
||||
if (block_has_main(*answer)) {
|
||||
locfile_locate(locations, UNKNOWN_LOCATION, "jq: error: library should only have function definitions, not a main expression");
|
||||
return 1;
|
||||
}
|
||||
assert(block_has_only_binders_and_imports(*answer, OP_IS_CALL_PSEUDO));
|
||||
return 0;
|
||||
}
|
||||
+19
-8
@@ -13,19 +13,30 @@
|
||||
set -e
|
||||
cd `dirname "$0"`/../build
|
||||
|
||||
jobs=-j4
|
||||
case "X$1" in
|
||||
X-j*) jobs=$1; shift;;
|
||||
esac
|
||||
|
||||
plat="$1"
|
||||
[ -z "$plat" ] && exit 1
|
||||
shift
|
||||
|
||||
case "$plat" in
|
||||
*/*) echo "platform name must not be a path"; exit 1;;
|
||||
*..*) echo "platform name must not be a path"; exit 1;;
|
||||
*) plat=$PWD/$plat;;
|
||||
esac
|
||||
|
||||
[ -d "$plat" ] || mkdir "$plat"
|
||||
rm -rf "$plat/tmp"
|
||||
mkdir "$plat/tmp"
|
||||
cd "$plat/tmp"
|
||||
mkdir install_other
|
||||
../../../configure \
|
||||
--prefix="`pwd`/install_other" \
|
||||
--bindir="`pwd`/.." \
|
||||
"$@"
|
||||
make install-binaries
|
||||
cd ..
|
||||
rm -rf tmp
|
||||
../../../configure "$@"
|
||||
make "$jobs" DESTDIR=$plat install
|
||||
set -x
|
||||
for jq in `find . -type f \( -name jq -o -name jq.exe \) -print`; do
|
||||
cp "$jq" ..
|
||||
done
|
||||
rm -rf tmp
|
||||
|
||||
@@ -6,13 +6,12 @@ mask = lambda n: (1 << n) - 1
|
||||
|
||||
|
||||
def print_table(type, name, t):
|
||||
assert len(t) == 256
|
||||
print("static const %s %s[] =" % (type, name))
|
||||
first = True
|
||||
for i in range(0,len(t),16):
|
||||
print ((" {" if i == 0 else " ") +
|
||||
", ".join("0x%02x"%n for n in t[i:i+16]) +
|
||||
("," if i + 16 < 256 else "};"))
|
||||
("," if i + 16 < len(t) else "};"))
|
||||
|
||||
|
||||
def utf8info(c):
|
||||
@@ -30,3 +29,4 @@ print("#define UTF8_CONTINUATION_BYTE ((unsigned char)255)")
|
||||
|
||||
print_table("unsigned char", "utf8_coding_length", table(0))
|
||||
print_table("unsigned char", "utf8_coding_bits", table(1))
|
||||
print_table("int", "utf8_first_codepoint", [0, 0x0, 0x80, 0x800, 0x10000])
|
||||
+1
-1
@@ -2,4 +2,4 @@
|
||||
set -e
|
||||
cd `dirname "$0"`
|
||||
git rev-parse --verify -q jq-1.0 > /dev/null 2>&1
|
||||
git describe --always --match 'jq-*' --dirty | sed 's/^jq-//'
|
||||
git describe --tags --match 'jq-*' --dirty | sed 's/^jq-//'
|
||||
@@ -9,12 +9,15 @@
|
||||
|
||||
if [ "clean" == "$1" ]; then
|
||||
make distclean || echo "continuing..."
|
||||
rm -rf tmp autom4te.cache config
|
||||
rm -f INSTALL Makefile.in aclocal.m4 configure config.h.in ChangeLog
|
||||
rm -f jv_utf8_tables.gen.h lexer.c lexer.h parser.c parser.h
|
||||
find config/m4 -type f \( -name libtool\* -o -name lt\* \) | xargs rm -f
|
||||
find config -name m4 -prune \
|
||||
-o -name .gitignore -prune \
|
||||
-o -type f -print | xargs rm -f
|
||||
rm -rf autom4te.cache
|
||||
rm -f Makefile.in aclocal.m4 configure
|
||||
elif [ "superclean" == "$1" ]; then
|
||||
# if autoconf errors during distcheck, it leaves files that need chmod'ing
|
||||
ver=$(tr -d '\n' <VERSION)
|
||||
ver=$(scripts/version|tr -d '\n')
|
||||
if [ "x${ver}" != "x" ]; then
|
||||
if [ -d jq-${ver} ]; then
|
||||
chmod -R u+w jq-${ver}
|
||||
@@ -24,10 +27,7 @@ elif [ "superclean" == "$1" ]; then
|
||||
fi
|
||||
else
|
||||
autoreconf --install
|
||||
./configure --prefix=/opt/junk
|
||||
make check
|
||||
[ -d tmp ] && mv tmp tmp-
|
||||
mkdir tmp
|
||||
make DESTDIR=./tmp install
|
||||
make distcheck
|
||||
./configure --disable-valgrind
|
||||
make -j8
|
||||
make check -j4
|
||||
fi
|
||||
-606
@@ -1,606 +0,0 @@
|
||||
# Tests are groups of three lines: program, input, expected output
|
||||
# Blank lines and lines starting with # are ignored
|
||||
|
||||
#
|
||||
# Simple value tests to check parser. Input is irrelevant
|
||||
#
|
||||
|
||||
true
|
||||
null
|
||||
true
|
||||
|
||||
false
|
||||
null
|
||||
false
|
||||
|
||||
null
|
||||
42
|
||||
null
|
||||
|
||||
1
|
||||
null
|
||||
1
|
||||
|
||||
|
||||
-1
|
||||
null
|
||||
-1
|
||||
|
||||
# FIXME: much more number testing needed
|
||||
|
||||
{}
|
||||
null
|
||||
{}
|
||||
|
||||
[]
|
||||
null
|
||||
[]
|
||||
|
||||
# The input line starts with a 0xFEFF (byte order mark) codepoint
|
||||
# No, there is no reason to have a byte order mark in UTF8 text.
|
||||
# But apparently people do, so jq shouldn't break on it.
|
||||
.
|
||||
"byte order mark"
|
||||
"byte order mark"
|
||||
|
||||
# We test escapes by matching them against Unicode codepoints
|
||||
# FIXME: more tests needed for weird unicode stuff (e.g. utf16 pairs)
|
||||
"Aa\r\n\t\b\f\u03bc"
|
||||
null
|
||||
"Aa\u000d\u000a\u0009\u0008\u000c\u03bc"
|
||||
|
||||
.
|
||||
"Aa\r\n\t\b\f\u03bc"
|
||||
"Aa\u000d\u000a\u0009\u0008\u000c\u03bc"
|
||||
|
||||
"inter\("pol" + "ation")"
|
||||
null
|
||||
"interpolation"
|
||||
|
||||
@text,@json,([1,.] | @csv),@html,@uri,@sh,@base64
|
||||
"<>&'\""
|
||||
"<>&'\""
|
||||
"\"<>&'\\\"\""
|
||||
"1,\"<>&'\"\"\""
|
||||
"<>&'""
|
||||
"%3c%3e%26'%22"
|
||||
"'<>&'\\''\"'"
|
||||
"PD4mJyI="
|
||||
|
||||
@uri
|
||||
"\u03bc"
|
||||
"%ce%bc"
|
||||
|
||||
@html "<b>\(.)</b>"
|
||||
"<script>hax</script>"
|
||||
"<b><script>hax</script></b>"
|
||||
|
||||
#
|
||||
# Dictionary construction syntax
|
||||
#
|
||||
|
||||
{a: 1}
|
||||
null
|
||||
{"a":1}
|
||||
|
||||
{a,b,(.d):.a,e:.b}
|
||||
{"a":1, "b":2, "c":3, "d":"c"}
|
||||
{"a":1, "b":2, "c":1, "e":2}
|
||||
|
||||
{"a",b,"a$\(1+1)"}
|
||||
{"a":1, "b":2, "c":3, "a$2":4}
|
||||
{"a":1, "b":2, "a$2":4}
|
||||
|
||||
#
|
||||
# Field access, piping
|
||||
#
|
||||
|
||||
.foo
|
||||
{"foo": 42, "bar": 43}
|
||||
42
|
||||
|
||||
.foo | .bar
|
||||
{"foo": {"bar": 42}, "bar": "badvalue"}
|
||||
42
|
||||
|
||||
.foo.bar
|
||||
{"foo": {"bar": 42}, "bar": "badvalue"}
|
||||
42
|
||||
|
||||
.foo_bar
|
||||
{"foo_bar": 2}
|
||||
2
|
||||
|
||||
.["foo"].bar
|
||||
{"foo": {"bar": 42}, "bar": "badvalue"}
|
||||
42
|
||||
|
||||
."foo"."bar"
|
||||
{"foo": {"bar": 20}}
|
||||
20
|
||||
|
||||
|
||||
#
|
||||
# Multiple outputs, iteration
|
||||
#
|
||||
|
||||
.[]
|
||||
[1,2,3]
|
||||
1
|
||||
2
|
||||
3
|
||||
|
||||
1,1
|
||||
[]
|
||||
1
|
||||
1
|
||||
|
||||
1,.
|
||||
[]
|
||||
1
|
||||
[]
|
||||
|
||||
[.]
|
||||
[2]
|
||||
[[2]]
|
||||
|
||||
[[2]]
|
||||
[3]
|
||||
[[2]]
|
||||
|
||||
[{}]
|
||||
[2]
|
||||
[{}]
|
||||
|
||||
[.[]]
|
||||
["a"]
|
||||
["a"]
|
||||
|
||||
[(.,1),((.,.[]),(2,3))]
|
||||
["a","b"]
|
||||
[["a","b"],1,["a","b"],"a","b",2,3]
|
||||
|
||||
[([5,5][]),.,.[]]
|
||||
[1,2,3]
|
||||
[5,5,[1,2,3],1,2,3]
|
||||
|
||||
{x: (1,2)},{x:3} | .x
|
||||
null
|
||||
1
|
||||
2
|
||||
3
|
||||
|
||||
#
|
||||
# Slices
|
||||
#
|
||||
|
||||
[.[3:2], .[-5:4], .[:-2], .[-2:], .[3:3][1:], .[10:]]
|
||||
[0,1,2,3,4,5,6]
|
||||
[[], [2,3], [0,1,2,3,4], [5,6], [], []]
|
||||
|
||||
del(.[2:4],.[0],.[-2:])
|
||||
[0,1,2,3,4,5,6,7]
|
||||
[1,4,5]
|
||||
|
||||
.[2:4] = ([], ["a","b"], ["a","b","c"])
|
||||
[0,1,2,3,4,5,6,7]
|
||||
[0,1,4,5,6,7]
|
||||
[0,1,"a","b",4,5,6,7]
|
||||
[0,1,"a","b","c",4,5,6,7]
|
||||
|
||||
|
||||
#
|
||||
# Variables
|
||||
#
|
||||
|
||||
1 as $x | 2 as $y | [$x,$y,$x]
|
||||
null
|
||||
[1,2,1]
|
||||
|
||||
[1,2,3][] as $x | [[4,5,6,7][$x]]
|
||||
null
|
||||
[5]
|
||||
[6]
|
||||
[7]
|
||||
|
||||
42 as $x | . | . | . + 432 | $x + 1
|
||||
34324
|
||||
43
|
||||
|
||||
1 as $x | [$x,$x,$x as $x | $x]
|
||||
null
|
||||
[1,1,1]
|
||||
|
||||
# [.,(.[] | {x:.},.),.,.[]]
|
||||
|
||||
#
|
||||
# Builtin functions
|
||||
#
|
||||
|
||||
1+1
|
||||
null
|
||||
2
|
||||
|
||||
1+1
|
||||
"wtasdf"
|
||||
2.0
|
||||
|
||||
2-1
|
||||
null
|
||||
1
|
||||
|
||||
2-(-1)
|
||||
null
|
||||
3
|
||||
|
||||
1e+0+0.001e3
|
||||
"I wonder what this will be?"
|
||||
20e-1
|
||||
|
||||
.+4
|
||||
15
|
||||
19.0
|
||||
|
||||
.+null
|
||||
{"a":42}
|
||||
{"a":42}
|
||||
|
||||
null+.
|
||||
null
|
||||
null
|
||||
|
||||
.a+.b
|
||||
{"a":42}
|
||||
42
|
||||
|
||||
[1,2,3] + [.]
|
||||
null
|
||||
[1,2,3,null]
|
||||
|
||||
{"a":1} + {"b":2} + {"c":3}
|
||||
"asdfasdf"
|
||||
{"a":1, "b":2, "c":3}
|
||||
|
||||
"asdf" + "jkl;" + . + . + .
|
||||
"some string"
|
||||
"asdfjkl;some stringsome stringsome string"
|
||||
|
||||
"\u0000\u0020\u0000" + .
|
||||
"\u0000\u0020\u0000"
|
||||
"\u0000 \u0000\u0000 \u0000"
|
||||
|
||||
42 - .
|
||||
11
|
||||
31
|
||||
|
||||
[1,2,3,4,1] - [.,3]
|
||||
1
|
||||
[2,4]
|
||||
|
||||
[10 * 20, 20 / .]
|
||||
4
|
||||
[200, 5]
|
||||
|
||||
1 + 2 * 2 + 10 / 2
|
||||
null
|
||||
10
|
||||
|
||||
[16 / 4 / 2, 16 / 4 * 2, 16 - 4 - 2, 16 - 4 + 2]
|
||||
null
|
||||
[2, 8, 10, 14]
|
||||
|
||||
1 + tonumber + ("10" | tonumber)
|
||||
4
|
||||
15
|
||||
|
||||
[{"a":42},.object,10,.num,false,true,null,"b",[1,4]] | .[] as $x | [$x == .[]]
|
||||
{"object": {"a":42}, "num":10.0}
|
||||
[true, true, false, false, false, false, false, false, false]
|
||||
[true, true, false, false, false, false, false, false, false]
|
||||
[false, false, true, true, false, false, false, false, false]
|
||||
[false, false, true, true, false, false, false, false, false]
|
||||
[false, false, false, false, true, false, false, false, false]
|
||||
[false, false, false, false, false, true, false, false, false]
|
||||
[false, false, false, false, false, false, true, false, false]
|
||||
[false, false, false, false, false, false, false, true, false]
|
||||
[false, false, false, false, false, false, false, false, true ]
|
||||
|
||||
[.[] | length]
|
||||
[[], {}, [1,2], {"a":42}, "asdf", "\u03bc"]
|
||||
[0, 0, 2, 1, 4, 1]
|
||||
|
||||
map(keys)
|
||||
[{}, {"abcd":1,"abc":2,"abcde":3}, {"x":1, "z": 3, "y":2}]
|
||||
[[], ["abc","abcd","abcde"], ["x","y","z"]]
|
||||
|
||||
[1,2,empty,3,empty,4]
|
||||
null
|
||||
[1,2,3,4]
|
||||
|
||||
map(add)
|
||||
[[], [1,2,3], ["a","b","c"], [[3],[4,5],[6]], [{"a":1}, {"b":2}, {"a":3}]]
|
||||
[null, 6, "abc", [3,4,5,6], {"a":3, "b": 2}]
|
||||
|
||||
#
|
||||
# User-defined functions
|
||||
# Oh god.
|
||||
#
|
||||
|
||||
def f: . + 1; def g: def g: . + 100; f | g | f; (f | g), g
|
||||
3.0
|
||||
106.0
|
||||
105.0
|
||||
|
||||
def f: (1000,2000); f
|
||||
123412345
|
||||
1000
|
||||
2000
|
||||
|
||||
([1,2] + [4,5])
|
||||
[1,2,3]
|
||||
[1,2,4,5]
|
||||
|
||||
true
|
||||
[1]
|
||||
true
|
||||
|
||||
null,1,null
|
||||
"hello"
|
||||
null
|
||||
1
|
||||
null
|
||||
|
||||
[1,2,3]
|
||||
[5,6]
|
||||
[1,2,3]
|
||||
|
||||
def f(x): x | x; f([.], . + [42])
|
||||
[1,2,3]
|
||||
[[[1,2,3]]]
|
||||
[[1,2,3],42]
|
||||
[[1,2,3,42]]
|
||||
[1,2,3,42,42]
|
||||
|
||||
# test closures and lexical scoping
|
||||
def id(x):x; 2000 as $x | def f(x):1 as $x | id([$x, x, x]); def g(x): 100 as $x | f($x,$x+x); g($x)
|
||||
"more testing"
|
||||
[1,100,2100.0,100,2100.0]
|
||||
|
||||
# test backtracking through function calls and returns
|
||||
# this test is *evil*
|
||||
[[20,10][1,0] as $x | def f: (100,200) as $y | def g: [$x + $y, .]; . + $x | g; f[0] | [f][0][1] | f]
|
||||
999999999
|
||||
[[110.0, 130.0], [210.0, 130.0], [110.0, 230.0], [210.0, 230.0], [120.0, 160.0], [220.0, 160.0], [120.0, 260.0], [220.0, 260.0]]
|
||||
|
||||
# test recursion
|
||||
def fac: if . == 1 then 1 else . * (. - 1 | fac) end; [.[] | fac]
|
||||
[1,2,3,4]
|
||||
[1,2,6,24]
|
||||
|
||||
# test stack overflow and reallocation
|
||||
# this test is disabled for now, it takes a realllllly long time.
|
||||
# def f: if length > 1000 then . else .+[1]|f end; f | length
|
||||
# []
|
||||
# 1001
|
||||
|
||||
reduce .[] as $x (0; . + $x)
|
||||
[1,2,4]
|
||||
7
|
||||
|
||||
#
|
||||
# Paths
|
||||
#
|
||||
|
||||
path(.foo[0,1])
|
||||
null
|
||||
["foo", 0]
|
||||
["foo", 1]
|
||||
|
||||
path(.[] | select(.>3))
|
||||
[1,5,3]
|
||||
[1]
|
||||
|
||||
path(.)
|
||||
42
|
||||
[]
|
||||
|
||||
["foo",1] as $p | getpath($p), setpath($p; 20), delpaths([$p])
|
||||
{"bar": 42, "foo": ["a", "b", "c", "d"]}
|
||||
"b"
|
||||
{"bar": 42, "foo": ["a", 20, "c", "d"]}
|
||||
{"bar": 42, "foo": ["a", "c", "d"]}
|
||||
|
||||
map(getpath([2])), map(setpath([2]; 42)), map(delpaths([[2]]))
|
||||
[[0], [0,1], [0,1,2]]
|
||||
[null, null, 2]
|
||||
[[0,null,42], [0,1,42], [0,1,42]]
|
||||
[[0], [0,1], [0,1]]
|
||||
|
||||
map(delpaths([[0,"foo"]]))
|
||||
[[{"foo":2, "x":1}], [{"bar":2}]]
|
||||
[[{"x":1}], [{"bar":2}]]
|
||||
|
||||
["foo",1] as $p | getpath($p), setpath($p; 20), delpaths([$p])
|
||||
{"bar":false}
|
||||
null
|
||||
{"bar":false, "foo": [null, 20]}
|
||||
{"bar":false}
|
||||
|
||||
delpaths([[-200]])
|
||||
[1,2,3]
|
||||
[1,2,3]
|
||||
|
||||
del(.), del(empty), del((.foo,.bar,.baz) | .[2,3,0]), del(.foo[0], .bar[0], .foo, .baz.bar[0].x)
|
||||
{"foo": [0,1,2,3,4], "bar": [0,1]}
|
||||
null
|
||||
{"foo": [0,1,2,3,4], "bar": [0,1]}
|
||||
{"foo": [1,4], "bar": [1]}
|
||||
{"bar": [1]}
|
||||
|
||||
#
|
||||
# Assignment
|
||||
#
|
||||
.message = "goodbye"
|
||||
{"message": "hello"}
|
||||
{"message": "goodbye"}
|
||||
|
||||
.foo = .bar
|
||||
{"bar":42}
|
||||
{"foo":42, "bar":42}
|
||||
|
||||
.foo |= .+1
|
||||
{"foo": 42}
|
||||
{"foo": 43}
|
||||
|
||||
.[] += 2, .[] *= 2, .[] -= 2, .[] /= 2
|
||||
[1,3,5]
|
||||
[3,5,7]
|
||||
[2,6,10]
|
||||
[-1,1,3]
|
||||
[0.5, 1.5, 2.5]
|
||||
|
||||
.foo += .foo
|
||||
{"foo":2}
|
||||
{"foo":4}
|
||||
|
||||
.[0].a |= {"old":., "new":(.+1)}
|
||||
[{"a":1,"b":2}]
|
||||
[{"a":{"old":1, "new":2},"b":2}]
|
||||
|
||||
def inc(x): x |= .+1; inc(.[].a)
|
||||
[{"a":1,"b":2},{"a":2,"b":4},{"a":7,"b":8}]
|
||||
[{"a":2,"b":2},{"a":3,"b":4},{"a":8,"b":8}]
|
||||
|
||||
.[2][3] = 1
|
||||
[4]
|
||||
[4, null, [null, null, null, 1]]
|
||||
|
||||
.foo[2].bar = 1
|
||||
{"foo":[11], "bar":42}
|
||||
{"foo":[11,null,{"bar":1}], "bar":42}
|
||||
|
||||
#
|
||||
# Conditionals
|
||||
#
|
||||
|
||||
[.[] | if .foo then "yep" else "nope" end]
|
||||
[{"foo":0},{"foo":1},{"foo":[]},{"foo":true},{"foo":false},{"foo":null},{"foo":"foo"},{}]
|
||||
["yep","yep","yep","yep","nope","nope","yep","nope"]
|
||||
|
||||
[.[] | if .baz then "strange" elif .foo then "yep" else "nope" end]
|
||||
[{"foo":0},{"foo":1},{"foo":[]},{"foo":true},{"foo":false},{"foo":null},{"foo":"foo"},{}]
|
||||
["yep","yep","yep","yep","nope","nope","yep","nope"]
|
||||
|
||||
|
||||
# FIXME: define/test behaviour of 'if (.foo,.bar) then A else B end'
|
||||
|
||||
[.[] | [.foo[] // .bar]]
|
||||
[{"foo":[1,2], "bar": 42}, {"foo":[1], "bar": null}, {"foo":[null,false,3], "bar": 18}, {"foo":[], "bar":42}, {"foo": [null,false,null], "bar": 41}]
|
||||
[[1,2], [1], [3], [42], [41]]
|
||||
|
||||
.[] //= .[0]
|
||||
["hello",true,false,[false],null]
|
||||
["hello",true,"hello",[false],"hello"]
|
||||
|
||||
.[] | [.[0] and .[1], .[0] or .[1]]
|
||||
[[true,[]], [false,1], [42,null], [null,false]]
|
||||
[true,true]
|
||||
[false,true]
|
||||
[false,true]
|
||||
[false,false]
|
||||
|
||||
[.[] | not]
|
||||
[1,0,false,null,true,"hello"]
|
||||
[false,false,true,true,false,false]
|
||||
|
||||
# Check numeric comparison binops
|
||||
[10 > 0, 10 > 10, 10 > 20, 10 < 0, 10 < 10, 10 < 20]
|
||||
{}
|
||||
[true,false,false,false,false,true]
|
||||
|
||||
[10 >= 0, 10 >= 10, 10 >= 20, 10 <= 0, 10 <= 10, 10 <= 20]
|
||||
{}
|
||||
[true,true,false,false,true,true]
|
||||
|
||||
# And some in/equality tests
|
||||
[ 10 == 10, 10 != 10, 10 != 11, 10 == 11]
|
||||
{}
|
||||
[true,false,true,false]
|
||||
|
||||
["hello" == "hello", "hello" != "hello", "hello" == "world", "hello" != "world" ]
|
||||
{}
|
||||
[true,false,false,true]
|
||||
|
||||
[[1,2,3] == [1,2,3], [1,2,3] != [1,2,3], [1,2,3] == [4,5,6], [1,2,3] != [4,5,6]]
|
||||
{}
|
||||
[true,false,false,true]
|
||||
|
||||
[{"foo":42} == {"foo":42},{"foo":42} != {"foo":42}, {"foo":42} != {"bar":42}, {"foo":42} == {"bar":42}]
|
||||
{}
|
||||
[true,false,true,false]
|
||||
|
||||
# ugly complicated thing
|
||||
[{"foo":[1,2,{"bar":18},"world"]} == {"foo":[1,2,{"bar":18},"world"]},{"foo":[1,2,{"bar":18},"world"]} == {"foo":[1,2,{"bar":19},"world"]}]
|
||||
{}
|
||||
[true,false]
|
||||
|
||||
# containment operator
|
||||
[("foo" | contains("foo")), ("foobar" | contains("foo")), ("foo" | contains("foobar"))]
|
||||
{}
|
||||
[true, true, false]
|
||||
|
||||
map(.[1] as $needle | .[0] | contains($needle))
|
||||
[[[],[]], [[1,2,3], [1,2]], [[1,2,3], [3,1]], [[1,2,3], [4]], [[1,2,3], [1,4]]]
|
||||
[true, true, true, false, false]
|
||||
|
||||
map(.[1] as $needle | .[0] | contains($needle))
|
||||
[[["foobar", "foobaz"], ["baz", "bar"]], [["foobar", "foobaz"], ["foo"]], [["foobar", "foobaz"], ["blap"]]]
|
||||
[true, true, false]
|
||||
|
||||
[({foo: 12, bar:13} | contains({foo: 12})), ({foo: 12} | contains({})), ({foo: 12, bar:13} | contains({baz:14}))]
|
||||
{}
|
||||
[true, true, false]
|
||||
|
||||
{foo: {baz: 12, blap: {bar: 13}}, bar: 14} | contains({bar: 14, foo: {blap: {}}})
|
||||
{}
|
||||
true
|
||||
|
||||
{foo: {baz: 12, blap: {bar: 13}}, bar: 14} | contains({bar: 14, foo: {blap: {bar: 14}}})
|
||||
{}
|
||||
false
|
||||
|
||||
sort
|
||||
[42,[2,5,3,11],10,{"a":42,"b":2},{"a":42},true,2,[2,6],"hello",null,[2,5,6],{"a":[],"b":1},"abc","ab",[3,10],{},false,"abcd",null]
|
||||
[null,null,false,true,2,10,42,"ab","abc","abcd","hello",[2,5,3,11],[2,5,6],[2,6],[3,10],{},{"a":42},{"a":42,"b":2},{"a":[],"b":1}]
|
||||
|
||||
(sort_by(.b) | sort_by(.a)), sort_by(.a, .b), sort_by(.b, .c), group_by(.b), group_by(.a + .b - .c == 2)
|
||||
[{"a": 1, "b": 4, "c": 14}, {"a": 4, "b": 1, "c": 3}, {"a": 1, "b": 4, "c": 3}, {"a": 0, "b": 2, "c": 43}]
|
||||
[{"a": 0, "b": 2, "c": 43}, {"a": 1, "b": 4, "c": 14}, {"a": 1, "b": 4, "c": 3}, {"a": 4, "b": 1, "c": 3}]
|
||||
[{"a": 0, "b": 2, "c": 43}, {"a": 1, "b": 4, "c": 14}, {"a": 1, "b": 4, "c": 3}, {"a": 4, "b": 1, "c": 3}]
|
||||
[{"a": 4, "b": 1, "c": 3}, {"a": 0, "b": 2, "c": 43}, {"a": 1, "b": 4, "c": 3}, {"a": 1, "b": 4, "c": 14}]
|
||||
[[{"a": 4, "b": 1, "c": 3}], [{"a": 0, "b": 2, "c": 43}], [{"a": 1, "b": 4, "c": 14}, {"a": 1, "b": 4, "c": 3}]]
|
||||
[[{"a": 1, "b": 4, "c": 14}, {"a": 0, "b": 2, "c": 43}], [{"a": 4, "b": 1, "c": 3}, {"a": 1, "b": 4, "c": 3}]]
|
||||
|
||||
unique
|
||||
[1,2,5,3,5,3,1,3]
|
||||
[1,2,3,5]
|
||||
|
||||
[min, max, min_by(.[1]), max_by(.[1]), min_by(.[2]), max_by(.[2])]
|
||||
[[4,2,"a"],[3,1,"a"],[2,4,"a"],[1,3,"a"]]
|
||||
[[1,3,"a"],[4,2,"a"],[3,1,"a"],[2,4,"a"],[4,2,"a"],[1,3,"a"]]
|
||||
|
||||
[min,max,min_by(.),max_by(.)]
|
||||
[]
|
||||
[null,null,null,null]
|
||||
|
||||
.foo[.baz]
|
||||
{"foo":{"bar":4},"baz":"bar"}
|
||||
4
|
||||
|
||||
.[] | .error = "no, it's OK"
|
||||
[{"error":true}]
|
||||
{"error": "no, it's OK"}
|
||||
|
||||
[{a:1}] | .[] | .a=999
|
||||
null
|
||||
{"a": 999}
|
||||
+1231
File diff suppressed because it is too large.
Load diff
Executable
+5
@@ -0,0 +1,5 @@
|
||||
#!/bin/sh
|
||||
|
||||
. "${0%/*}/setup"
|
||||
|
||||
$VALGRIND $Q $JQ -L "$mods" --run-tests $JQTESTDIR/jq.test
|
||||
Executable
+7
@@ -0,0 +1,7 @@
|
||||
#!/bin/sh
|
||||
|
||||
. "${0%/*}/setup"
|
||||
|
||||
# We set PAGER because there's a mantest for `env` that uses it.
|
||||
(cd $JQBASEDIR/docs && rake mantests) |
|
||||
env PAGER=less $VALGRIND $Q $JQ -L "$mods" --run-tests
|
||||
@@ -0,0 +1,5 @@
|
||||
def foo: "baz";
|
||||
def f: "wat";
|
||||
def f: "foo";
|
||||
def g: "bar";
|
||||
def fg: f+g;
|
||||
@@ -0,0 +1,2 @@
|
||||
module {version:1.7};
|
||||
def a: "a";
|
||||
@@ -0,0 +1,2 @@
|
||||
def a: "b";
|
||||
def b: "c";
|
||||
@@ -0,0 +1,16 @@
|
||||
module {whatever:null};
|
||||
import "a" as foo;
|
||||
import "d" as d {search:"./"};
|
||||
import "d" as d2{search:"./"};
|
||||
import "e" as e {search:"./../lib/jq"};
|
||||
import "f" as f {search:"./../lib/jq"};
|
||||
import "data" as $d;
|
||||
def a: 0;
|
||||
def c:
|
||||
if $d::d[0] != {this:"is a test",that:"is too"} then error("data import is busted")
|
||||
elif d2::meh != d::meh then error("import twice doesn't work")
|
||||
elif foo::a != "a" then error("foo::a didn't work as expected")
|
||||
elif d::meh != "meh" then error("d::meh didn't work as expected")
|
||||
elif e::bah != "bah" then error("e::bah didn't work as expected")
|
||||
elif f::f != "f is here" then error("f::f didn't work as expected")
|
||||
else foo::a + "c" + d::meh + e::bah end;
|
||||
@@ -0,0 +1 @@
|
||||
def meh: "meh";
|
||||
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"this": "is a test",
|
||||
"that": "is too"
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
def bah: "bah";
|
||||
@@ -0,0 +1 @@
|
||||
def f: "f is here";
|
||||
@@ -0,0 +1 @@
|
||||
wat;
|
||||
@@ -0,0 +1,4 @@
|
||||
import "test_bind_order0" as t;
|
||||
import "test_bind_order1" as t;
|
||||
import "test_bind_order2" as t;
|
||||
def check: if [t::sym0,t::sym1,t::sym2] == [0,1,2] then true else false end;
|
||||
@@ -0,0 +1 @@
|
||||
def sym0: 0;
|
||||
@@ -0,0 +1,2 @@
|
||||
def sym0: 1;
|
||||
def sym1: 1;
|
||||
@@ -0,0 +1,2 @@
|
||||
def sym1: 2;
|
||||
def sym2: 2;
|
||||
@@ -0,0 +1,21 @@
|
||||
{
|
||||
onig node recycling
|
||||
Memcheck:Leak
|
||||
...
|
||||
fun:onig_parse_make_tree
|
||||
...
|
||||
}
|
||||
{
|
||||
onig unicode case insensitivity 1
|
||||
Memcheck:Leak
|
||||
...
|
||||
fun:setup_tree
|
||||
...
|
||||
}
|
||||
{
|
||||
onig unicode case insensitivity 2
|
||||
Memcheck:Leak
|
||||
...
|
||||
fun:onig*unicode*
|
||||
...
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
# match builtin
|
||||
[match("( )*"; "g")]
|
||||
"abc"
|
||||
[{"offset":0, "length":0, "string":"", "captures":[]},{"offset":1, "length":0, "string":"", "captures":[]},{"offset":2, "length":0, "string":"", "captures":[]}]
|
||||
|
||||
[match("( )*"; "gn")]
|
||||
"abc"
|
||||
[]
|
||||
|
||||
[match("a"; "gi")]
|
||||
"āáàä"
|
||||
[]
|
||||
|
||||
[match(["(bar)"])]
|
||||
"foo bar"
|
||||
[{"offset": 4, "length": 3, "string": "bar", "captures":[{"offset": 4, "length": 3, "string": "bar", "name": null}]}]
|
||||
|
||||
# offsets account for combining codepoints and multi-byte UTF-8
|
||||
[match("bar")]
|
||||
"ā bar with a combining codepoint U+0304"
|
||||
[{"offset": 3, "length": 3, "string": "bar", "captures":[]}]
|
||||
|
||||
# matches with combining codepoints still count them in their length
|
||||
[match("bār")]
|
||||
"a bār"
|
||||
[{"offset": 2, "length": 4, "string": "bār", "captures":[]}]
|
||||
|
||||
[match(".+?\\b")]
|
||||
"ā two-codepoint grapheme"
|
||||
[{"offset": 0, "length": 2, "string": "ā", "captures":[]}]
|
||||
|
||||
[match(["foo (?<bar123>bar)? foo", "ig"])]
|
||||
"foo bar foo foo foo"
|
||||
[{"offset": 0, "length": 11, "string": "foo bar foo", "captures":[{"offset": 4, "length": 3, "string": "bar", "name": "bar123"}]},{"offset":12, "length": 8, "string": "foo foo", "captures":[{"offset": -1, "length": 0, "string": null, "name": "bar123"}]}]
|
||||
|
||||
#test builtin
|
||||
[test("( )*"; "gn")]
|
||||
"abc"
|
||||
[false]
|
||||
|
||||
[test("ā")]
|
||||
"ā"
|
||||
[true]
|
||||
|
||||
capture("(?<a>[a-z]+)-(?<n>[0-9]+)")
|
||||
"xyzzy-14"
|
||||
{"a":"xyzzy","n":"14"}
|
||||
|
||||
|
||||
# jq-coded utilities built on match:
|
||||
#
|
||||
# The second element in these tests' inputs tests the case where the
|
||||
# fromstring matches both the head and tail of the string
|
||||
[.[] | sub(", "; ":")]
|
||||
["a,b, c, d, e,f", ", a,b, c, d, e,f, "]
|
||||
["a,b:c, d, e,f",":a,b, c, d, e,f, "]
|
||||
|
||||
sub("^(?<head>.)"; "Head=\(.head) Tail=")
|
||||
"abcdef"
|
||||
"Head=a Tail=bcdef"
|
||||
|
||||
[.[] | gsub(", "; ":")]
|
||||
["a,b, c, d, e,f",", a,b, c, d, e,f, "]
|
||||
["a,b:c:d:e,f",":a,b:c:d:e,f:"]
|
||||
|
||||
gsub("(?<d>\\d)"; ":\(.d);")
|
||||
"a1b2"
|
||||
"a:1;b:2;"
|
||||
|
||||
[.[] | scan(", ")]
|
||||
["a,b, c, d, e,f",", a,b, c, d, e,f, "]
|
||||
[", ",", ",", ",", ",", ",", ",", ",", "]
|
||||
|
||||
[.[]|[[sub(", *";":")], [gsub(", *";":")], [scan(", *")]]]
|
||||
["a,b, c, d, e,f",", a,b, c, d, e,f, "]
|
||||
[[["a:b, c, d, e,f"],["a:b:c:d:e:f"],[",",", ",", ",", ",","]],[[":a,b, c, d, e,f, "],[":a:b:c:d:e:f:"],[", ",",",", ",", ",", ",",",", "]]]
|
||||
|
||||
[.[]|[[sub(", +";":")], [gsub(", +";":")], [scan(", +")]]]
|
||||
["a,b, c, d, e,f",", a,b, c, d, e,f, "]
|
||||
[[["a,b:c, d, e,f"],["a,b:c:d:e,f"],[", ",", ",", "]],[[":a,b, c, d, e,f, "],[":a,b:c:d:e,f:"],[", ",", ",", ",", ",", "]]]
|
||||
|
||||
# reference to named captures
|
||||
gsub("(?<x>.)[^a]*"; "+\(.x)-")
|
||||
"Abcabc"
|
||||
"+A-+a-"
|
||||
Executable
+5
@@ -0,0 +1,5 @@
|
||||
#!/bin/sh
|
||||
|
||||
. "${0%/*}/setup"
|
||||
|
||||
$VALGRIND $Q $JQ -L "$mods" --run-tests $JQTESTDIR/onig.test
|
||||
@@ -1,3 +0,0 @@
|
||||
#!/bin/sh
|
||||
|
||||
cat $@ | valgrind --error-exitcode=1 -q --leak-check=full ./jq --run-tests
|
||||
Executable
+36
@@ -0,0 +1,36 @@
|
||||
#!/bin/sh
|
||||
|
||||
# This is meant to be included by each test's shell script driver.
|
||||
|
||||
set -eu
|
||||
|
||||
JQTESTDIR=$(cd "$(dirname "$0")" && pwd)
|
||||
JQBASEDIR=$JQTESTDIR/..
|
||||
JQ=$JQBASEDIR/jq
|
||||
|
||||
if [ -z "${NO_VALGRIND-}" ] && which valgrind > /dev/null; then
|
||||
VALGRIND="valgrind --error-exitcode=1 --leak-check=full \
|
||||
--suppressions=$JQTESTDIR/onig.supp"
|
||||
Q=-q
|
||||
else
|
||||
VALGRIND=
|
||||
Q=
|
||||
fi
|
||||
|
||||
mods=$JQTESTDIR/modules
|
||||
|
||||
clean=true
|
||||
d=
|
||||
clean () {
|
||||
if ! $clean; then
|
||||
echo "See temp files in $d!"
|
||||
elif [ -n "$d" ]; then
|
||||
rm -rf "$d"
|
||||
fi
|
||||
}
|
||||
trap clean EXIT
|
||||
d=`mktemp -d -t || true`
|
||||
if [ -z "$d" ]; then
|
||||
echo "Your OS does not support mktemp(1) -d" 1>&2
|
||||
exit 0
|
||||
fi
|
||||
Executable
+193
@@ -0,0 +1,193 @@
|
||||
#!/bin/sh
|
||||
|
||||
. "${0%/*}/setup"
|
||||
|
||||
if [ -f "$JQBASEDIR/.libs/libinject_errors.so" ]; then
|
||||
# Do some simple error injection tests to check that we're handling
|
||||
# I/O errors correctly.
|
||||
(
|
||||
libinject=$JQBASEDIR/.libs/libinject_errors.so
|
||||
cd $d
|
||||
LD_PRELOAD=$libinject $JQ . /dev/null
|
||||
touch fail_read
|
||||
LD_PRELOAD=$libinject $JQ . fail_read && exit 2
|
||||
touch fail_close
|
||||
LD_PRELOAD=$libinject $JQ . fail_close && exit 2
|
||||
true
|
||||
)
|
||||
fi
|
||||
|
||||
printf 'a\0b\nc\0d\ne' > $d/input
|
||||
$VALGRIND $Q $JQ -Rse '. == "a\u0000b\nc\u0000d\ne"' $d/input
|
||||
$VALGRIND $Q $JQ -Rne '[inputs] == ["a\u0000b", "c\u0000d", "e"]' $d/input
|
||||
|
||||
## Test constant folding
|
||||
|
||||
## XXX If we add a builtin to list the program's disassembly then we can
|
||||
## move all of these into tests/all.test
|
||||
|
||||
# String constant folding (addition only)
|
||||
n=`$VALGRIND $Q $JQ -n --debug-dump-disasm '"foo"' | wc -l`
|
||||
if [ $n -ne 5 ]; then
|
||||
echo "Constant expression folding for strings didn't work"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Numeric constant folding (not all ops yet)
|
||||
n=`$VALGRIND $Q $JQ -n --debug-dump-disasm '1+1' | wc -l`
|
||||
if [ $n -ne 5 ]; then
|
||||
echo "Constant expression folding for strings didn't work"
|
||||
exit 1
|
||||
fi
|
||||
n=`$VALGRIND $Q $JQ -n --debug-dump-disasm '1-1' | wc -l`
|
||||
if [ $n -ne 5 ]; then
|
||||
echo "Constant expression folding for strings didn't work"
|
||||
exit 1
|
||||
fi
|
||||
n=`$VALGRIND $Q $JQ -n --debug-dump-disasm '2*3' | wc -l`
|
||||
if [ $n -ne 5 ]; then
|
||||
echo "Constant expression folding for strings didn't work"
|
||||
exit 1
|
||||
fi
|
||||
n=`$VALGRIND $Q $JQ -n --debug-dump-disasm '9/3' | wc -l`
|
||||
if [ $n -ne 5 ]; then
|
||||
echo "Constant expression folding for strings didn't work"
|
||||
exit 1
|
||||
fi
|
||||
n=`$VALGRIND $Q $JQ -n --debug-dump-disasm '9==3' | wc -l`
|
||||
if [ $n -ne 5 ]; then
|
||||
echo "Constant expression folding for strings didn't work"
|
||||
exit 1
|
||||
fi
|
||||
n=`$VALGRIND $Q $JQ -n --debug-dump-disasm '9!=3' | wc -l`
|
||||
if [ $n -ne 5 ]; then
|
||||
echo "Constant expression folding for strings didn't work"
|
||||
exit 1
|
||||
fi
|
||||
n=`$VALGRIND $Q $JQ -n --debug-dump-disasm '9<=3' | wc -l`
|
||||
if [ $n -ne 5 ]; then
|
||||
echo "Constant expression folding for strings didn't work"
|
||||
exit 1
|
||||
fi
|
||||
n=`$VALGRIND $Q $JQ -n --debug-dump-disasm '9>=3' | wc -l`
|
||||
if [ $n -ne 5 ]; then
|
||||
echo "Constant expression folding for strings didn't work"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
## Test JSON sequence support
|
||||
|
||||
cat > $d/expected <<EOF
|
||||
ignoring parse error: Truncated value at line 2, column 5
|
||||
ignoring parse error: Truncated value at line 2, column 25
|
||||
ignoring parse error: Truncated value at line 2, column 41
|
||||
EOF
|
||||
printf '1\0362 3\n[0,1\036[4,5]true"ab"{"c":4\036{}{"d":5,"e":6"\036false\n'|$VALGRIND $Q $JQ -ces --seq '. == [2,3,[4,5],true,"ab",{},false]' > /dev/null 2> $d/out
|
||||
cmp $d/out $d/expected
|
||||
|
||||
cat > $d/expected <<EOF
|
||||
ignoring parse error: Truncated value at line 2, column 5
|
||||
ignoring parse error: Truncated value at line 2, column 25
|
||||
ignoring parse error: Truncated value at line 3, column 1
|
||||
EOF
|
||||
printf '1\0362 3\n[0,1\036[4,5]true"ab"{"c":4\036{}{"d":5,"e":6"false\n\036null'|$VALGRIND $Q $JQ -ces --seq '. == [2,3,[4,5],true,"ab",{},null]' > /dev/null 2> $d/out
|
||||
cmp $d/out $d/expected
|
||||
|
||||
# Note that here jq sees no inputs at all but it still succeeds because
|
||||
# --seq ignores parse errors
|
||||
cat > $d/expected <<EOF
|
||||
ignoring parse error: Unfinished abandoned text at EOF at line 1, column 4
|
||||
EOF
|
||||
printf '"foo' | $JQ -ce --seq . > $d/out 2>&1
|
||||
cmp $d/out $d/expected
|
||||
|
||||
# Numeric values truncated by EOF are ignored
|
||||
cat > $d/expected <<EOF
|
||||
ignoring parse error: Unfinished abandoned text at EOF at line 1, column 1
|
||||
EOF
|
||||
printf '1' | $JQ -ce --seq . > $d/out 2>&1
|
||||
cmp $d/out $d/expected
|
||||
|
||||
cat > $d/expected <<EOF
|
||||
EOF
|
||||
printf '1\n' | $JQ -cen --seq '[inputs] == []' >/dev/null 2> $d/out
|
||||
cmp $d/out $d/expected
|
||||
|
||||
## Test streaming parser
|
||||
|
||||
## If we add an option to stream to the `import ... as $symbol;` directive
|
||||
## then we can move these tests into tests/all.test.
|
||||
$VALGRIND $Q $JQ -c '. as $d|path(..) as $p|$d|getpath($p)|scalars_or_empty|[$p,.]' < "$JQTESTDIR/torture/input0.json" > $d/out0
|
||||
$VALGRIND $Q $JQ --stream -c '.|select(length==2)' < "$JQTESTDIR/torture/input0.json" > $d/out1
|
||||
diff $d/out0 $d/out1
|
||||
|
||||
## XXX This test can be moved to tests/all.test _now_
|
||||
clean=false
|
||||
if which seq > /dev/null 2>&1; then
|
||||
# XXX We should try every prefix of input0.json, but that makes this
|
||||
# test very, very slow when run with valgrind, and the whole point
|
||||
# is to run it with valgrind.
|
||||
#
|
||||
#len=`wc -c < "$JQTESTDIR/torture/input0.json"`
|
||||
if [ -z "$VALGRIND" ]; then
|
||||
start=1
|
||||
end=`wc -c < "$JQTESTDIR/torture/input0.json"`
|
||||
else
|
||||
start=120
|
||||
end=151
|
||||
fi
|
||||
for i in `seq $start $end`; do
|
||||
dd "if=tests/torture/input0.json" bs=$i count=1 2>/dev/null |
|
||||
$VALGRIND $JQ -c . > $d/out0 2>$d/err || true
|
||||
if [ -n "$VALGRIND" ]; then
|
||||
grep '^==[0-9][0-9]*== ERROR SUMMARY: 0 errors' $d/err > /dev/null
|
||||
else
|
||||
tail -1 $d/err | egrep -i 'assert|abort|core' && false
|
||||
fi
|
||||
|
||||
dd "if=tests/torture/input0.json" bs=$i count=1 2>/dev/null |
|
||||
$VALGRIND $JQ -cn --stream 'fromstream(inputs)' > $d/out1 2>$d/err || true
|
||||
if [ -n "$VALGRIND" ]; then
|
||||
grep '^==[0-9][0-9]*== ERROR SUMMARY: 0 errors' $d/err > /dev/null
|
||||
else
|
||||
tail -1 $d/err | egrep -i 'assert|abort|core' && false
|
||||
fi
|
||||
|
||||
diff $d/out0 $d/out1
|
||||
done
|
||||
else
|
||||
echo "Not doing torture tests"
|
||||
fi
|
||||
|
||||
## Fuzz parser
|
||||
|
||||
## XXX With a `urandom` builtin we could move this test into tests/all.test
|
||||
clean=false
|
||||
if dd if=/dev/urandom bs=16 count=1024 > $d/rand 2>/dev/null; then
|
||||
# Have a /dev/urandom, good
|
||||
$VALGRIND $Q $JQ --seq . $d/rand >/dev/null 2>&1
|
||||
$VALGRIND $Q $JQ --seq --stream . $d/rand >/dev/null 2>&1
|
||||
dd if=/dev/urandom bs=16 count=1024 > $d/rand 2>/dev/null
|
||||
$VALGRIND $Q $JQ --seq . $d/rand >/dev/null 2>&1
|
||||
$VALGRIND $Q $JQ --seq --stream . $d/rand >/dev/null 2>&1
|
||||
dd if=/dev/urandom bs=16 count=1024 > $d/rand 2>/dev/null
|
||||
$VALGRIND $Q $JQ --seq . $d/rand >/dev/null 2>&1
|
||||
$VALGRIND $Q $JQ --seq --stream . $d/rand >/dev/null 2>&1
|
||||
fi
|
||||
clean=true
|
||||
|
||||
## Test library/module system
|
||||
|
||||
# Check handling of ~/.jq; these can't move into jq_test.c yet because
|
||||
# they depend on $HOME
|
||||
if [ "`HOME="$mods" $VALGRIND $Q $JQ -nr fg`" != foobar ]; then
|
||||
echo "Bug #479 appears to be back" 1>&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [ `HOME="$mods" $VALGRIND $Q $JQ --debug-dump-disasm -n fg | grep '^[a-z]' | wc -l` -gt 3 ]; then
|
||||
echo "Binding too many defs into program" 1>&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
exit 0
|
||||
@@ -0,0 +1,7 @@
|
||||
0 12 true false null [] {} [{}] [[]]
|
||||
[0,01,[12,22,[34,[45,56],7]],[]]
|
||||
{"a":[1]}
|
||||
{"a":[{}]}
|
||||
{"a":[{},[],[[]]]}
|
||||
{"a":[{"b":[]}]}
|
||||
{"a":[{"b":[]},{},[2]]}
|
||||
@@ -0,0 +1,462 @@
|
||||
|
||||
#ifdef HAVE_MEMMEM
|
||||
#define _GNU_SOURCE
|
||||
#endif
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <limits.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <stddef.h>
|
||||
#ifdef HAVE_ALLOCA_H
|
||||
# include <alloca.h>
|
||||
#elif !defined alloca
|
||||
# ifdef __GNUC__
|
||||
# define alloca __builtin_alloca
|
||||
# elif defined _MSC_VER
|
||||
# include <malloc.h>
|
||||
# define alloca _alloca
|
||||
# elif !defined HAVE_ALLOCA
|
||||
# ifdef __cplusplus
|
||||
extern "C"
|
||||
# endif
|
||||
void *alloca (size_t);
|
||||
# endif
|
||||
#endif
|
||||
#ifndef WIN32
|
||||
#include <pwd.h>
|
||||
#endif
|
||||
|
||||
#ifdef WIN32
|
||||
#include <windows.h>
|
||||
#include <processenv.h>
|
||||
#include <shellapi.h>
|
||||
#include <wchar.h>
|
||||
#include <wtypes.h>
|
||||
#endif
|
||||
|
||||
|
||||
#include "util.h"
|
||||
#include "jq.h"
|
||||
#include "jv_alloc.h"
|
||||
|
||||
#ifdef WIN32
|
||||
FILE *fopen(const char *fname, const char *mode) {
|
||||
size_t sz = MultiByteToWideChar(CP_UTF8, 0, fname, -1, NULL, 0);
|
||||
wchar_t *wfname = alloca(sz);
|
||||
MultiByteToWideChar(CP_UTF8, 0, fname, -1, wfname, sz);
|
||||
|
||||
sz = MultiByteToWideChar(CP_UTF8, 0, mode, -1, NULL, 0);
|
||||
wchar_t *wmode = alloca(sz);
|
||||
MultiByteToWideChar(CP_UTF8, 0, mode, -1, wmode, sz);
|
||||
return _wfopen(wfname, wmode);
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifndef HAVE_MKSTEMP
|
||||
int mkstemp(char *template) {
|
||||
size_t len = strlen(template);
|
||||
int tries=5;
|
||||
int fd;
|
||||
|
||||
// mktemp() truncates template when it fails
|
||||
char *s = alloca(len + 1);
|
||||
assert(s != NULL);
|
||||
strcpy(s, template);
|
||||
|
||||
do {
|
||||
// Restore template
|
||||
strcpy(template, s);
|
||||
(void) mktemp(template);
|
||||
fd = open(template, O_CREAT | O_EXCL | O_RDWR, 0600);
|
||||
} while (fd == -1 && tries-- > 0);
|
||||
return fd;
|
||||
}
|
||||
#endif
|
||||
|
||||
jv expand_path(jv path) {
|
||||
assert(jv_get_kind(path) == JV_KIND_STRING);
|
||||
const char *pstr = jv_string_value(path);
|
||||
jv ret = path;
|
||||
if (jv_string_length_bytes(jv_copy(path)) > 1 && pstr[0] == '~' && pstr[1] == '/') {
|
||||
jv home = get_home();
|
||||
if (jv_is_valid(home)) {
|
||||
ret = jv_string_fmt("%s/%s",jv_string_value(home),pstr+2);
|
||||
jv_free(home);
|
||||
} else {
|
||||
jv emsg = jv_invalid_get_msg(home);
|
||||
ret = jv_invalid_with_msg(jv_string_fmt("Could not expand %s. (%s)", pstr, jv_string_value(emsg)));
|
||||
jv_free(emsg);
|
||||
}
|
||||
jv_free(path);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
jv get_home() {
|
||||
jv ret;
|
||||
char *home = getenv("HOME");
|
||||
if (!home) {
|
||||
#ifndef WIN32
|
||||
struct passwd* pwd = getpwuid(getuid());
|
||||
if (pwd)
|
||||
ret = jv_string(pwd->pw_dir);
|
||||
else
|
||||
ret = jv_invalid_with_msg(jv_string("Could not find home directory."));
|
||||
#else
|
||||
home = getenv("USERPROFILE");
|
||||
if (!home) {
|
||||
char *hd = getenv("HOMEDRIVE");
|
||||
if (!hd) hd = "";
|
||||
home = getenv("HOMEPATH");
|
||||
if (!home) {
|
||||
ret = jv_invalid_with_msg(jv_string("Could not find home directory."));
|
||||
} else {
|
||||
ret = jv_string_fmt("%s%s",hd,home);
|
||||
}
|
||||
} else {
|
||||
ret = jv_string(home);
|
||||
}
|
||||
#endif
|
||||
} else {
|
||||
ret = jv_string(home);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
jv jq_realpath(jv path) {
|
||||
int path_max;
|
||||
char *buf = NULL;
|
||||
#ifdef _PC_PATH_MAX
|
||||
path_max = pathconf(jv_string_value(path),_PC_PATH_MAX);
|
||||
#else
|
||||
path_max = PATH_MAX;
|
||||
#endif
|
||||
if (path_max > 0) {
|
||||
buf = malloc(sizeof(char) * path_max);
|
||||
}
|
||||
#ifdef WIN32
|
||||
char *tmp = _fullpath(buf, jv_string_value(path), path_max);
|
||||
#else
|
||||
char *tmp = realpath(jv_string_value(path), buf);
|
||||
#endif
|
||||
if (tmp == NULL) {
|
||||
free(buf);
|
||||
return path;
|
||||
}
|
||||
jv_free(path);
|
||||
path = jv_string(tmp);
|
||||
free(tmp);
|
||||
return path;
|
||||
}
|
||||
|
||||
const void *_jq_memmem(const void *haystack, size_t haystacklen,
|
||||
const void *needle, size_t needlelen) {
|
||||
#ifdef HAVE_MEMMEM
|
||||
return (const void*)memmem(haystack, haystacklen, needle, needlelen);
|
||||
#else
|
||||
const char *h = haystack;
|
||||
const char *n = needle;
|
||||
size_t hi, hi2, ni;
|
||||
|
||||
if (haystacklen < needlelen || haystacklen == 0)
|
||||
return NULL;
|
||||
for (hi = 0; hi < (haystacklen - needlelen + 1); hi++) {
|
||||
for (ni = 0, hi2 = hi; ni < needlelen; ni++, hi2++) {
|
||||
if (h[hi2] != n[ni])
|
||||
goto not_this;
|
||||
}
|
||||
|
||||
return &h[hi];
|
||||
|
||||
not_this:
|
||||
continue;
|
||||
}
|
||||
return NULL;
|
||||
#endif /* !HAVE_MEMMEM */
|
||||
}
|
||||
|
||||
struct jq_util_input_state {
|
||||
jq_util_msg_cb err_cb;
|
||||
void *err_cb_data;
|
||||
jv_parser *parser;
|
||||
FILE* current_input;
|
||||
char **files;
|
||||
int nfiles;
|
||||
int curr_file;
|
||||
int failures;
|
||||
jv slurped;
|
||||
char buf[4096];
|
||||
size_t buf_valid_len;
|
||||
jv current_filename;
|
||||
size_t current_line;
|
||||
};
|
||||
|
||||
static void fprinter(void *data, const char *fname) {
|
||||
fprintf((FILE *)data, "jq: error: Could not open file %s: %s\n", fname, strerror(errno));
|
||||
}
|
||||
|
||||
// If parser == NULL -> RAW
|
||||
jq_util_input_state *jq_util_input_init(jq_util_msg_cb err_cb, void *err_cb_data) {
|
||||
if (err_cb == NULL) {
|
||||
err_cb = fprinter;
|
||||
err_cb_data = stderr;
|
||||
}
|
||||
jq_util_input_state *new_state = jv_mem_alloc(sizeof(*new_state));
|
||||
memset(new_state, 0, sizeof(*new_state));
|
||||
new_state->err_cb = err_cb;
|
||||
new_state->err_cb_data = err_cb_data;
|
||||
new_state->parser = NULL;
|
||||
new_state->current_input = NULL;
|
||||
new_state->files = NULL;
|
||||
new_state->nfiles = 0;
|
||||
new_state->curr_file = 0;
|
||||
new_state->slurped = jv_invalid();
|
||||
new_state->buf[0] = 0;
|
||||
new_state->buf_valid_len = 0;
|
||||
new_state->current_filename = jv_invalid();
|
||||
new_state->current_line = 0;
|
||||
|
||||
return new_state;
|
||||
}
|
||||
|
||||
void jq_util_input_set_parser(jq_util_input_state *state, jv_parser *parser, int slurp) {
|
||||
assert(!jv_is_valid(state->slurped));
|
||||
state->parser = parser;
|
||||
|
||||
if (parser == NULL && slurp)
|
||||
state->slurped = jv_string("");
|
||||
else if (slurp)
|
||||
state->slurped = jv_array();
|
||||
else
|
||||
state->slurped = jv_invalid();
|
||||
}
|
||||
|
||||
void jq_util_input_free(jq_util_input_state **state) {
|
||||
jq_util_input_state *old_state = *state;
|
||||
*state = NULL;
|
||||
if (old_state == NULL)
|
||||
return;
|
||||
|
||||
if (old_state->parser != NULL)
|
||||
jv_parser_free(old_state->parser);
|
||||
for (int i = 0; i < old_state->nfiles; i++)
|
||||
free(old_state->files[i]);
|
||||
free(old_state->files);
|
||||
jv_free(old_state->slurped);
|
||||
jv_free(old_state->current_filename);
|
||||
jv_mem_free(old_state);
|
||||
}
|
||||
|
||||
void jq_util_input_add_input(jq_util_input_state *state, const char *fname) {
|
||||
state->files = jv_mem_realloc(state->files, (state->nfiles + 1) * sizeof(state->files[0]));
|
||||
state->files[state->nfiles++] = jv_mem_strdup(fname);
|
||||
}
|
||||
|
||||
int jq_util_input_errors(jq_util_input_state *state) {
|
||||
return state->failures;
|
||||
}
|
||||
|
||||
static const char *next_file(jq_util_input_state *state) {
|
||||
if (state->curr_file < state->nfiles)
|
||||
return state->files[state->curr_file++];
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static int jq_util_input_read_more(jq_util_input_state *state) {
|
||||
if (!state->current_input || feof(state->current_input) || ferror(state->current_input)) {
|
||||
if (state->current_input && ferror(state->current_input)) {
|
||||
// System-level input error on the stream. It will be closed (below).
|
||||
// TODO: report it. Can't use 'state->err_cb()' as it is hard-coded for
|
||||
// 'open' related problems.
|
||||
fprintf(stderr,"Input error: %s\n", strerror(errno));
|
||||
}
|
||||
if (state->current_input) {
|
||||
if (state->current_input == stdin) {
|
||||
clearerr(stdin); // perhaps we can read again; anyways, we don't fclose(stdin)
|
||||
} else {
|
||||
fclose(state->current_input);
|
||||
}
|
||||
state->current_input = NULL;
|
||||
jv_free(state->current_filename);
|
||||
state->current_filename = jv_invalid();
|
||||
state->current_line = 0 ;
|
||||
}
|
||||
const char *f = next_file(state);
|
||||
if (f != NULL) {
|
||||
if (!strcmp(f, "-")) {
|
||||
state->current_input = stdin;
|
||||
state->current_filename = jv_string("<stdin>");
|
||||
} else {
|
||||
state->current_input = fopen(f, "r");
|
||||
state->current_filename = jv_string(f);
|
||||
if (!state->current_input) {
|
||||
state->err_cb(state->err_cb_data, f);
|
||||
state->failures++;
|
||||
}
|
||||
}
|
||||
state->current_line = 0;
|
||||
}
|
||||
}
|
||||
|
||||
state->buf[0] = 0;
|
||||
state->buf_valid_len = 0;
|
||||
if (state->current_input) {
|
||||
char *res;
|
||||
memset(state->buf, 0, sizeof(state->buf));
|
||||
|
||||
while (!(res = fgets(state->buf, sizeof(state->buf), state->current_input)) &&
|
||||
ferror(state->current_input) && errno == EINTR)
|
||||
clearerr(state->current_input);
|
||||
if (res == NULL) {
|
||||
state->buf[0] = 0;
|
||||
if (ferror(state->current_input))
|
||||
state->failures++;
|
||||
} else {
|
||||
const char *p = memchr(state->buf, '\n', sizeof(state->buf));
|
||||
|
||||
if (p != NULL)
|
||||
state->current_line++;
|
||||
|
||||
if (p == NULL && state->parser != NULL) {
|
||||
/*
|
||||
* There should be no NULs in JSON texts (but JSON text
|
||||
* sequences are another story).
|
||||
*/
|
||||
state->buf_valid_len = strlen(state->buf);
|
||||
} else if (p == NULL && feof(state->current_input)) {
|
||||
size_t i;
|
||||
|
||||
/*
|
||||
* XXX We can't know how many bytes we've read!
|
||||
*
|
||||
* We can't use getline() because there need not be any newlines
|
||||
* in the input. The only entirely correct choices are: use
|
||||
* fgetc() or fread(). Using fread() will complicate buffer
|
||||
* management here.
|
||||
*
|
||||
* For now we guess how much fgets() read.
|
||||
*/
|
||||
for (p = state->buf, i = 0; i < sizeof(state->buf); i++) {
|
||||
if (state->buf[i] != '\0')
|
||||
p = &state->buf[i];
|
||||
}
|
||||
state->buf_valid_len = p - state->buf + 1;
|
||||
} else if (p == NULL) {
|
||||
state->buf_valid_len = sizeof(state->buf) - 1;
|
||||
} else {
|
||||
state->buf_valid_len = (p - state->buf) + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
return state->curr_file == state->nfiles &&
|
||||
(!state->current_input || feof(state->current_input) || ferror(state->current_input));
|
||||
}
|
||||
|
||||
jv jq_util_input_next_input_cb(jq_state *jq, void *data) {
|
||||
return jq_util_input_next_input((jq_util_input_state *)data);
|
||||
}
|
||||
|
||||
// Return the current_filename:current_line
|
||||
jv jq_util_input_get_position(jq_state *jq) {
|
||||
jq_input_cb cb = NULL;
|
||||
void *cb_data = NULL;
|
||||
jq_get_input_cb(jq, &cb, &cb_data);
|
||||
assert(cb == jq_util_input_next_input_cb);
|
||||
if (cb != jq_util_input_next_input_cb)
|
||||
return jv_invalid_with_msg(jv_string("Invalid jq_util_input API usage"));
|
||||
jq_util_input_state *s = (jq_util_input_state *)cb_data;
|
||||
|
||||
// We can't assert that current_filename is a string because if
|
||||
// the error was a JSON parser error then we may not have set
|
||||
// current_filename yet.
|
||||
if (jv_get_kind(s->current_filename) != JV_KIND_STRING)
|
||||
return jv_string("<unknown>");
|
||||
|
||||
jv v = jv_string_fmt("%s:%lu", jv_string_value(s->current_filename), (unsigned long)s->current_line);
|
||||
return v;
|
||||
}
|
||||
|
||||
jv jq_util_input_get_current_filename(jq_state* jq) {
|
||||
jq_input_cb cb=NULL;
|
||||
void *cb_data=NULL;
|
||||
jq_get_input_cb(jq, &cb, &cb_data);
|
||||
if (cb != jq_util_input_next_input_cb)
|
||||
return jv_invalid_with_msg(jv_string("Unknown input filename"));
|
||||
jq_util_input_state *s = (jq_util_input_state *)cb_data;
|
||||
jv v = jv_copy(s->current_filename);
|
||||
return v;
|
||||
}
|
||||
|
||||
jv jq_util_input_get_current_line(jq_state* jq) {
|
||||
jq_input_cb cb=NULL;
|
||||
void *cb_data=NULL;
|
||||
jq_get_input_cb(jq, &cb, &cb_data);
|
||||
if (cb != jq_util_input_next_input_cb)
|
||||
return jv_invalid_with_msg(jv_string("Unknown input line number"));
|
||||
jq_util_input_state *s = (jq_util_input_state *)cb_data;
|
||||
jv v = jv_number(s->current_line);
|
||||
return v;
|
||||
}
|
||||
|
||||
|
||||
// Blocks to read one more input from stdin and/or given files
|
||||
// When slurping, it returns just one value
|
||||
jv jq_util_input_next_input(jq_util_input_state *state) {
|
||||
int is_last = 0;
|
||||
jv value = jv_invalid(); // need more input
|
||||
do {
|
||||
if (state->parser == NULL) {
|
||||
// Raw input
|
||||
is_last = jq_util_input_read_more(state);
|
||||
if (state->buf_valid_len == 0)
|
||||
continue;
|
||||
if (jv_is_valid(state->slurped)) {
|
||||
// Slurped raw input
|
||||
state->slurped = jv_string_concat(state->slurped, jv_string_sized(state->buf, state->buf_valid_len));
|
||||
} else {
|
||||
if (!jv_is_valid(value))
|
||||
value = jv_string("");
|
||||
if (state->buf[state->buf_valid_len-1] == '\n') {
|
||||
// whole line
|
||||
state->buf[state->buf_valid_len-1] = 0;
|
||||
return jv_string_concat(value, jv_string_sized(state->buf, state->buf_valid_len-1));
|
||||
}
|
||||
value = jv_string_concat(value, jv_string_sized(state->buf, state->buf_valid_len));
|
||||
state->buf[0] = '\0';
|
||||
state->buf_valid_len = 0;
|
||||
}
|
||||
} else {
|
||||
if (jv_parser_remaining(state->parser) == 0) {
|
||||
is_last = jq_util_input_read_more(state);
|
||||
if (is_last && state->buf_valid_len == 0) {
|
||||
value = jv_invalid();
|
||||
break;
|
||||
}
|
||||
jv_parser_set_buf(state->parser, state->buf, state->buf_valid_len, !is_last);
|
||||
}
|
||||
value = jv_parser_next(state->parser);
|
||||
if (jv_is_valid(state->slurped)) {
|
||||
if (jv_is_valid(value)) {
|
||||
state->slurped = jv_array_append(state->slurped, value);
|
||||
value = jv_invalid();
|
||||
} else if (jv_invalid_has_msg(jv_copy(value)))
|
||||
return value; // Not slurped parsed input
|
||||
} else if (jv_is_valid(value) || jv_invalid_has_msg(jv_copy(value))) {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
} while (!is_last);
|
||||
|
||||
if (jv_is_valid(state->slurped)) {
|
||||
value = state->slurped;
|
||||
state->slurped = jv_invalid();
|
||||
}
|
||||
return value;
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
#ifndef UTIL_H
|
||||
#define UTIL_H
|
||||
|
||||
#ifdef WIN32
|
||||
/* For WriteFile() below */
|
||||
#include <windows.h>
|
||||
#include <io.h>
|
||||
#include <processenv.h>
|
||||
#include <shellapi.h>
|
||||
#include <wchar.h>
|
||||
#include <wtypes.h>
|
||||
#endif
|
||||
|
||||
#include "jv.h"
|
||||
|
||||
#ifndef HAVE_MKSTEMP
|
||||
int mkstemp(char *template);
|
||||
#endif
|
||||
|
||||
jv expand_path(jv);
|
||||
jv get_home(void);
|
||||
jv jq_realpath(jv);
|
||||
|
||||
/*
|
||||
* The Windows CRT and console are something else. In order for the
|
||||
* console to get UTF-8 written to it correctly we have to bypass stdio
|
||||
* completely. No amount of fflush()ing helps. If the first byte of a
|
||||
* buffer being written with fwrite() is non-ASCII UTF-8 then the
|
||||
* console misinterprets the byte sequence. But one must not
|
||||
* WriteFile() if stdout is a file!1!!
|
||||
*
|
||||
* We carry knowledge of whether the FILE * is a tty everywhere we
|
||||
* output to it just so we can write with WriteFile() if stdout is a
|
||||
* console on WIN32.
|
||||
*/
|
||||
|
||||
static void priv_fwrite(const char *s, size_t len, FILE *fout, int is_tty) {
|
||||
#ifdef WIN32
|
||||
if (is_tty)
|
||||
WriteFile((HANDLE)_get_osfhandle(fileno(fout)), s, len, NULL, NULL);
|
||||
else
|
||||
fwrite(s, 1, len, fout);
|
||||
#else
|
||||
fwrite(s, 1, len, fout);
|
||||
#endif
|
||||
}
|
||||
|
||||
const void *_jq_memmem(const void *haystack, size_t haystacklen,
|
||||
const void *needle, size_t needlelen);
|
||||
|
||||
#ifndef MIN
|
||||
#define MIN(a,b) \
|
||||
({ __typeof__ (a) _a = (a); \
|
||||
__typeof__ (b) _b = (b); \
|
||||
_a < _b ? _a : _b; })
|
||||
#endif
|
||||
#ifndef MAX
|
||||
#define MAX(a,b) \
|
||||
({ __typeof__ (a) _a = (a); \
|
||||
__typeof__ (b) _b = (b); \
|
||||
_a > _b ? _a : _b; })
|
||||
#endif
|
||||
|
||||
#endif /* UTIL_H */
|
||||
Reference in new issue
Block a user