diff options
author | Nicolas Williams <nico@cryptonector.com> | 2014-12-30 13:13:30 -0600 |
---|---|---|
committer | Nicolas Williams <nico@cryptonector.com> | 2014-12-31 20:09:53 -0600 |
commit | ae7f8d6ab9d29bd72f462fdafa4d7a9270706d3b (patch) | |
tree | 3be6c09d87619b65f87e13e4bee4cdf24b064639 /parser.y | |
parent | 7dc34b92aff7a38e16c0ef608238d03e1ac3d213 (diff) |
Further module system revamp (fix #659)
To import a module now use:
# Import module.jq file:
import "relative/path/to/module" as foo;
# Use the module's defs as foo::<def-name>
To import a JSON file:
# Read file.json:
import "relative/path/to/file" as $foo;
#
# Use as $foo::foo
Using `-L` now drops the builtin library path and appends the requested
path to the empty array (or the result of an earlier `-L`).
Support for the `$JQ_LIBRARY_PATH` environment variable has been
removed.
Diffstat (limited to 'parser.y')
-rw-r--r-- | parser.y | 51 |
1 files changed, 31 insertions, 20 deletions
@@ -280,14 +280,13 @@ Module: %empty { $$ = gen_noop(); } | -"module" IDENT Exp ';' { - if (!block_is_const($3)) { +"module" Exp ';' { + if (!block_is_const($2)) { FAIL(@$, "Module metadata must be constant."); $$ = gen_noop(); } else { - $$ = gen_module(jv_string_value($2), $3); + $$ = gen_module($2); } - jv_free($2); } Imports: @@ -479,33 +478,45 @@ Term { } Import: -"import" IDENT ';' { - $$ = gen_import(jv_string_value($2), gen_noop(), NULL); - jv_free($2); +"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" IDENT Exp ';' { - if (!block_is_const($3)) { +"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); +} | +"import" String "as" IDENT Exp ';' { + if (!block_is_const($5)) { FAIL(@$, "Module metadata must be constant."); $$ = gen_noop(); } else { - $$ = gen_import(jv_string_value($2), $3, NULL); + jv v = block_const($2); + $$ = gen_import(jv_string_value(v), $5, jv_string_value($4), 0); + jv_free(v); } - jv_free($2); -} | -"import" IDENT "as" IDENT ';' { - $$ = gen_import(jv_string_value($2), gen_noop(), jv_string_value($4)); - jv_free($2); + block_free($2); jv_free($4); } | -"import" IDENT "as" IDENT Exp ';' { - if (!block_is_const($5)) { +"import" String "as" '$' IDENT Exp ';' { + if (!block_is_const($6)) { FAIL(@$, "Module metadata must be constant."); $$ = gen_noop(); } else { - $$ = gen_import(jv_string_value($2), $5, jv_string_value($4)); + jv v = block_const($2); + $$ = gen_import(jv_string_value(v), $6, jv_string_value($5), 1); + jv_free(v); } - jv_free($2); - jv_free($4); + block_free($2); + jv_free($5); } FuncDef: |