From b921511820ee4116b4f1f2746bb90e6b859ae1c8 Mon Sep 17 00:00:00 2001 From: Mu Qiao Date: Thu, 23 Feb 2012 11:24:05 +0800 Subject: Parser: respect operator precedence in keyword test --- bashast/bashast.g | 5 ++++- bashast/gunit/cond_main.gunit | 2 ++ scripts/test_expr.bash | 1 + 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/bashast/bashast.g b/bashast/bashast.g index b7e0d32..b383219 100644 --- a/bashast/bashast.g +++ b/bashast/bashast.g @@ -597,12 +597,15 @@ condition_expr | {LA(1) == NAME && LA(2) == BLANK && "test".equals(get_string(LT(1)))}? => NAME wspace? builtin_condition-> ^(BUILTIN_TEST builtin_condition); #endif +keyword_condition_and + : keyword_condition_primary (BLANK!? LOGICAND^ BLANK!? keyword_condition_primary)?; keyword_condition - : ((BANG) => keyword_negation_primary|keyword_condition_primary) (BLANK!? (LOGICOR^|LOGICAND^) BLANK!? keyword_condition)?; + : keyword_condition_and (BLANK!? LOGICOR^ BLANK!? keyword_condition_and)?; keyword_negation_primary : BANG BLANK keyword_condition_primary -> ^(NEGATION keyword_condition_primary); keyword_condition_primary : LPAREN! BLANK!? keyword_condition BLANK!? RPAREN! + | (BANG) => keyword_negation_primary | (unary_operator) => keyword_condition_unary | keyword_condition_binary; keyword_condition_unary diff --git a/bashast/gunit/cond_main.gunit b/bashast/gunit/cond_main.gunit index fbf785e..5ecd024 100644 --- a/bashast/gunit/cond_main.gunit +++ b/bashast/gunit/cond_main.gunit @@ -37,3 +37,5 @@ condition_expr: "[[ a (KEYWORD_TEST (< (STRING a) (STRING b))) "[[ a>b ]]" -> (KEYWORD_TEST (> (STRING a) (STRING b))) "[[ ${VIRTUALX_REQUIRED} == always || ${VIRTUALX_REQUIRED} == test ]]" -> (KEYWORD_TEST (|| (MATCH_PATTERN (STRING (VAR_REF VIRTUALX_REQUIRED)) (STRING always)) (MATCH_PATTERN (STRING (VAR_REF VIRTUALX_REQUIRED)) (STRING test)))) +"[[ a == b || c == d && e == f ]]" -> (KEYWORD_TEST (|| (MATCH_PATTERN (STRING a) (STRING b)) (&& (MATCH_PATTERN (STRING c) (STRING d)) (MATCH_PATTERN (STRING e) (STRING f))))) +"[[ a == b && c == d || e == f ]]" -> (KEYWORD_TEST (|| (&& (MATCH_PATTERN (STRING a) (STRING b)) (MATCH_PATTERN (STRING c) (STRING d))) (MATCH_PATTERN (STRING e) (STRING f)))) diff --git a/scripts/test_expr.bash b/scripts/test_expr.bash index 66ab24a..ccedb76 100644 --- a/scripts/test_expr.bash +++ b/scripts/test_expr.bash @@ -54,3 +54,4 @@ unset i [ abc = bcd -a abc = abc ] || echo true19 [[ =a <=b ]] [[ =a >=b ]] +[[ a == a || c == b && a == b ]] && echo true -- cgit v1.2.3-65-gdbad