Support "if" without "else" clause

Defaults the else clause to the identity filter "." if an else clause is
missing.

The following two jq programs are considered equivalent:

```
if .foo then
    .foo = "bar"
else
    .
end
```

```
if .foo then
    .foo = "bar"
end
```
This commit is contained in:
Chance Zibolski authored and Nico Williams committed 2019-02-20 20:03:26 -06:00
1 parent 1ef90acb38
commit 4f6045a94e
4 files changed
+1092 -1057

No files matched your search

+1070 -1051
View File
File diff suppressed because it is too large. Load diff
+6 -6
View File
@@ -1,8 +1,8 @@
/* A Bison parser, made by GNU Bison 3.0.4. */ /* A Bison parser, made by GNU Bison 3.0.5. */
/* Bison interface for Yacc-like parsers in C /* Bison interface for Yacc-like parsers in C
Copyright (C) 1984, 1989-1990, 2000-2015 Free Software Foundation, Inc. Copyright (C) 1984, 1989-1990, 2000-2015, 2018 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU General Public License as published by
@@ -40,7 +40,7 @@
extern int yydebug; extern int yydebug;
#endif #endif
/* "%code requires" blocks. */ /* "%code requires" blocks. */
#line 11 "src/parser.y" /* yacc.c:1909 */ #line 11 "src/parser.y" /* yacc.c:1910 */
#include "locfile.h" #include "locfile.h"
struct lexer_param; struct lexer_param;
@@ -57,7 +57,7 @@ struct lexer_param;
} \ } \
} while (0) } while (0)
#line 61 "src/parser.h" /* yacc.c:1909 */ #line 61 "src/parser.h" /* yacc.c:1910 */
/* Token type. */ /* Token type. */
#ifndef YYTOKENTYPE #ifndef YYTOKENTYPE
@@ -163,12 +163,12 @@ struct lexer_param;
union YYSTYPE union YYSTYPE
{ {
#line 31 "src/parser.y" /* yacc.c:1909 */ #line 31 "src/parser.y" /* yacc.c:1910 */
jv literal; jv literal;
block blk; block blk;
#line 172 "src/parser.h" /* yacc.c:1909 */ #line 172 "src/parser.h" /* yacc.c:1910 */
}; };
typedef union YYSTYPE YYSTYPE; typedef union YYSTYPE YYSTYPE;
+3
View File
@@ -357,6 +357,9 @@ Term "as" Patterns '|' Exp {
"if" Exp "then" Exp ElseBody { "if" Exp "then" Exp ElseBody {
$$ = gen_cond($2, $4, $5); $$ = gen_cond($2, $4, $5);
} | } |
"if" Exp "then" Exp "end" {
$$ = gen_cond($2, $4, gen_noop());
} |
"if" Exp "then" error { "if" Exp "then" error {
FAIL(@$, "Possibly unterminated 'if' statement"); FAIL(@$, "Possibly unterminated 'if' statement");
$$ = $2; $$ = $2;
+13
View File
@@ -1036,6 +1036,19 @@ null
null null
[5,6] [5,6]
[if true then 3 end]
7
[3]
[if false then 3 end]
7
[7]
[if false then 3 else . end]
7
[7]
[.[] | [.foo[] // .bar]] [.[] | [.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}] [{"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]] [[1,2], [1], [3], [42], [41]]