From 328c4a13f1d63b02cbc7afe8e46a16ef4f896f07 Mon Sep 17 00:00:00 2001 From: Stephen Dolan Date: Tue, 4 Sep 2012 16:05:24 +0100 Subject: [PATCH] Add "elif" to if-then-else constructs. --- c/lexer.l | 1 + c/parser.y | 15 ++++++++++++--- c/testdata | 5 +++++ 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/c/lexer.l b/c/lexer.l index e3c140b6..98365f1f 100644 --- a/c/lexer.l +++ b/c/lexer.l @@ -16,6 +16,7 @@ "if" { return IF; } "then" { return THEN; } "else" { return ELSE; } +"elif" { return ELSE_IF; } "end" { return END; } "//" { return DEFINEDOR; } "."|"="|";"|"["|"]"|","|":"|"("|")"|"{"|"}"|"|"|"+"|"\$" { return yytext[0];} diff --git a/c/parser.y b/c/parser.y index d4e8d8f4..165cdb42 100644 --- a/c/parser.y +++ b/c/parser.y @@ -32,6 +32,7 @@ %token IF "if" %token THEN "then" %token ELSE "else" +%token ELSE_IF "elif" %token END "end" %right "//" %nonassoc '=' SETPIPE @@ -39,7 +40,7 @@ %left '+' -%type Exp Term MkDict MkDictPair ExpD +%type Exp Term MkDict MkDictPair ExpD ElseBody %{ #include "lexer.yy.h" @@ -85,8 +86,8 @@ Term "as" '$' IDENT '|' Exp { jv_free($4); } | -"if" Exp "then" Exp "else" Exp "end" { - $$ = gen_cond($2, $4, $6); +"if" Exp "then" Exp ElseBody { + $$ = gen_cond($2, $4, $5); } | Exp '=' Exp { @@ -128,6 +129,14 @@ Term { $$ = $1; } +ElseBody: +"elif" Exp "then" Exp ElseBody { + $$ = gen_cond($2, $4, $5); +} | +"else" Exp "end" { + $$ = $2; +} + ExpD: ExpD '|' ExpD { diff --git a/c/testdata b/c/testdata index 888d8b74..3fe231f5 100644 --- a/c/testdata +++ b/c/testdata @@ -243,6 +243,11 @@ def inc(x): x |= .+1; inc(.[].a) [{"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]]