Add "elif" to if-then-else constructs.

This commit is contained in:
Stephen Dolan committed 2012-09-04 16:05:24 +01:00
1 parent 5add2c2ceb
commit 328c4a13f1
3 files changed
+18 -3

No files matched your search

+1
View File
@@ -16,6 +16,7 @@
"if" { return IF; } "if" { return IF; }
"then" { return THEN; } "then" { return THEN; }
"else" { return ELSE; } "else" { return ELSE; }
"elif" { return ELSE_IF; }
"end" { return END; } "end" { return END; }
"//" { return DEFINEDOR; } "//" { return DEFINEDOR; }
"."|"="|";"|"["|"]"|","|":"|"("|")"|"{"|"}"|"|"|"+"|"\$" { return yytext[0];} "."|"="|";"|"["|"]"|","|":"|"("|")"|"{"|"}"|"|"|"+"|"\$" { return yytext[0];}
+12 -3
View File
@@ -32,6 +32,7 @@
%token IF "if" %token IF "if"
%token THEN "then" %token THEN "then"
%token ELSE "else" %token ELSE "else"
%token ELSE_IF "elif"
%token END "end" %token END "end"
%right "//" %right "//"
%nonassoc '=' SETPIPE %nonassoc '=' SETPIPE
@@ -39,7 +40,7 @@
%left '+' %left '+'
%type <blk> Exp Term MkDict MkDictPair ExpD %type <blk> Exp Term MkDict MkDictPair ExpD ElseBody
%{ %{
#include "lexer.yy.h" #include "lexer.yy.h"
@@ -85,8 +86,8 @@ Term "as" '$' IDENT '|' Exp {
jv_free($4); jv_free($4);
} | } |
"if" Exp "then" Exp "else" Exp "end" { "if" Exp "then" Exp ElseBody {
$$ = gen_cond($2, $4, $6); $$ = gen_cond($2, $4, $5);
} | } |
Exp '=' Exp { Exp '=' Exp {
@@ -128,6 +129,14 @@ Term {
$$ = $1; $$ = $1;
} }
ElseBody:
"elif" Exp "then" Exp ElseBody {
$$ = gen_cond($2, $4, $5);
} |
"else" Exp "end" {
$$ = $2;
}
ExpD: ExpD:
ExpD '|' ExpD { ExpD '|' ExpD {
+5
View File
@@ -243,6 +243,11 @@ def inc(x): x |= .+1; inc(.[].a)
[{"foo":0},{"foo":1},{"foo":[]},{"foo":true},{"foo":false},{"foo":null},{"foo":"foo"},{}] [{"foo":0},{"foo":1},{"foo":[]},{"foo":true},{"foo":false},{"foo":null},{"foo":"foo"},{}]
["yep","yep","yep","yep","nope","nope","yep","nope"] ["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' # FIXME: define/test behaviour of 'if (.foo,.bar) then A else B end'
[.[] | [.foo[] // .bar]] [.[] | [.foo[] // .bar]]