From 0f3a3e3ece624cc267900771798069cc8e9339a5 Mon Sep 17 00:00:00 2001 From: BVK Chaitanya Date: Sun, 21 Mar 2010 10:27:21 +0530 Subject: [PATCH] added elif patch from Deepak Vankadaru --- conf/tests.rmk | 4 ++++ script/parser.y | 34 ++++++++++++++++++++++------------ tests/grub_script_if.in | 31 +++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 12 deletions(-) create mode 100644 tests/grub_script_if.in diff --git a/conf/tests.rmk b/conf/tests.rmk index 92f14797d..427720e82 100644 --- a/conf/tests.rmk +++ b/conf/tests.rmk @@ -53,6 +53,9 @@ grub_script_vars1_SOURCES = tests/grub_script_vars1.in check_SCRIPTS += grub_script_for1 grub_script_for1_SOURCES = tests/grub_script_for1.in +check_SCRIPTS += grub_script_if +grub_script_if_SOURCES = tests/grub_script_if.in + # List of tests to execute on "make check" # SCRIPTED_TESTS = example_scripted_test # SCRIPTED_TESTS += example_grub_script_test @@ -63,6 +66,7 @@ SCRIPTED_TESTS = grub_script_echo1 SCRIPTED_TESTS += grub_script_echo_keywords SCRIPTED_TESTS += grub_script_vars1 SCRIPTED_TESTS += grub_script_for1 +SCRIPTED_TESTS += grub_script_if # dependencies between tests and testing-tools $(SCRIPTED_TESTS): grub-shell grub-shell-tester diff --git a/script/parser.y b/script/parser.y index caba0f433..4ed58b4f2 100644 --- a/script/parser.y +++ b/script/parser.y @@ -74,7 +74,7 @@ %token GRUB_PARSER_TOKEN_WORD "word" %type word argument arguments0 arguments1 -%type script_init script grubcmd ifcmd forcmd command +%type script_init script grubcmd ifclause ifcmd forcmd command %type commands1 menuentry statement %pure-parser @@ -224,18 +224,28 @@ menuentry: "menuentry" } ; -if: "if" { grub_script_lexer_ref (state->lexerstate); } +ifcmd: "if" + { + grub_script_lexer_ref (state->lexerstate); + } + ifclause "fi" + { + $$ = $3; + grub_script_lexer_deref (state->lexerstate); + } ; -ifcmd: if commands1 delimiters1 "then" commands1 delimiters1 "fi" - { - $$ = grub_script_create_cmdif (state, $2, $5, 0); - grub_script_lexer_deref (state->lexerstate); - } - | if commands1 delimiters1 "then" commands1 delimiters1 "else" commands1 delimiters1 "fi" - { - $$ = grub_script_create_cmdif (state, $2, $5, $8); - grub_script_lexer_deref (state->lexerstate); - } +ifclause: commands1 delimiters1 "then" commands1 delimiters1 + { + $$ = grub_script_create_cmdif (state, $1, $4, 0); + } + | commands1 delimiters1 "then" commands1 delimiters1 "else" commands1 delimiters1 + { + $$ = grub_script_create_cmdif (state, $1, $4, $7); + } + | commands1 delimiters1 "then" commands1 delimiters1 "elif" ifclause + { + $$ = grub_script_create_cmdif (state, $1, $4, $7); + } ; forcmd: "for" "name" diff --git a/tests/grub_script_if.in b/tests/grub_script_if.in new file mode 100644 index 000000000..fb17eaf70 --- /dev/null +++ b/tests/grub_script_if.in @@ -0,0 +1,31 @@ +#! @builddir@/grub-shell-tester + +#basic if, execute +if true; then echo yes; fi + +#basic if, no execution +if false; then echo no; fi + +#if else, execute if path +if true; then echo yes; else echo no; fi + +#if else, execute else path +if false; then echo no; else echo yes; fi + +#if elif, execute elif +if false; then echo no; elif true; then echo yes; fi + +#if elif else, execute else +if false; then echo no; elif false; then echo no; else echo yes; fi + +#if elif(1) elif(2), execute elif(2) +if false; then echo no; elif false; then echo no; elif true; then echo yes; fi + +#if elif(1) elif(2) else, execute else +if false; then echo no; elif false; then echo no; elif false; then echo no; else echo yes; fi + +#if {if elif else}, execute elif +if true; then if false; then echo no; elif true; then echo yes; else echo no; fi; fi + +#if {if elif} else, execute elif. ofcourse no dangling-else problem due to "fi" +if true; then if false; then echo no; elif true; then echo yes; fi; else echo no; fi