aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMu Qiao <qiaomuf@gentoo.org>2011-07-20 22:49:00 +0800
committerMu Qiao <qiaomuf@gentoo.org>2011-07-20 23:15:45 +0800
commita918ae2d53a03689480738e7bfb59b91c35d14d8 (patch)
tree6cffae368a9ed93cc1d52b1bd0855989d2e5fde1
parentParser: fix delete expansion (diff)
downloadlibbash-a918ae2d53a03689480738e7bfb59b91c35d14d8.tar.gz
libbash-a918ae2d53a03689480738e7bfb59b91c35d14d8.tar.bz2
libbash-a918ae2d53a03689480738e7bfb59b91c35d14d8.zip
Parser: fix builtin/keyword test
Now more tests are uncommented or added.
-rw-r--r--Makefile.am2
-rw-r--r--bashast/bashast.g41
-rw-r--r--bashast/gunit/compound.gunit14
-rw-r--r--bashast/gunit/cond_main.gunit4
-rw-r--r--bashast/gunit/expansions.gunit2
-rw-r--r--bashast/gunit/fname.gunit42
-rw-r--r--bashast/gunit/list.gunit2
-rwxr-xr-xutils/isolated-functions.sh30
8 files changed, 72 insertions, 65 deletions
diff --git a/Makefile.am b/Makefile.am
index 92ecdfe..6a059a2 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -52,6 +52,8 @@ BASH_TESTS = scripts/var_def.bash \
scripts/function_def.bash \
scripts/arithmetic_assignment.bash \
scripts/var_expansion.bash \
+ scripts/test_expr.bash \
+ scripts/isolated_functions.bash \
scripts/binary_arithmetic.bash
EBUILD_LOG_COMPILER = $(srcdir)/test/ebuild_compiler.sh
diff --git a/bashast/bashast.g b/bashast/bashast.g
index 9537a0e..65cc493 100644
--- a/bashast/bashast.g
+++ b/bashast/bashast.g
@@ -214,7 +214,7 @@ redirection
: redirection_atom+;
redirection_atom
: redirection_operator BLANK? redirection_destination -> ^(REDIR redirection_operator redirection_destination)
- | BLANK? process_substitution;
+ | BLANK!? process_substitution;
process_substitution
: (dir=LESS_THAN|dir=GREATER_THAN)LPAREN BLANK* command_list BLANK* RPAREN
@@ -511,28 +511,37 @@ keyword_negation_primary
: BANG BLANK keyword_condition_primary -> ^(NEGATION keyword_condition_primary);
keyword_condition_primary
: LPAREN! BLANK!? keyword_condition BLANK!? RPAREN!
- | keyword_condition_binary
- | (unary_operator) => keyword_condition_unary;
+ | (unary_operator) => keyword_condition_unary
+ | keyword_condition_binary;
keyword_condition_unary
: unary_operator^ BLANK! condition_part;
keyword_condition_binary
: condition_part
(
- (BLANK? EQUALS TILDE) => BLANK? EQUALS TILDE BLANK? bash_pattern_part
+ (BLANK EQUALS TILDE) => BLANK EQUALS TILDE BLANK bash_pattern_part
-> ^(MATCH_REGULAR_EXPRESSION condition_part ^(STRING bash_pattern_part))
- | BLANK? keyword_binary_string_operator BLANK? right=condition_part
+ | BLANK keyword_binary_string_operator BLANK right=condition_part
-> ^(keyword_binary_string_operator condition_part $right)
- | BLANK? (BANG EQUALS) BLANK? extended_pattern_match+
- -> ^(NOT_MATCH_PATTERN condition_part extended_pattern_match+)
- | BLANK? (EQUALS EQUALS) BLANK? extended_pattern_match+
- -> ^(MATCH_PATTERN condition_part extended_pattern_match+)
- )?;
+ | BLANK (BANG EQUALS) BLANK extended_pattern_match+
+ -> ^(NOT_MATCH_PATTERN condition_part ^(STRING extended_pattern_match+))
+ | BLANK (EQUALS EQUALS) BLANK extended_pattern_match+
+ -> ^(MATCH_PATTERN condition_part ^(STRING extended_pattern_match+))
+ | -> condition_part
+ );
//TODO improve this rule
bash_pattern_part
+scope {
+ int parens;
+}
+@init {
+ $bash_pattern_part::parens = 0;
+}
:(
(ESC BLANK) => ESC BLANK
| (ESC RSQUARE) => ESC RSQUARE
- | ~(BLANK|RSQUARE|EOL|LOGICAND|LOGICOR|RPAREN)
+ | LPAREN { if(LA(-2) != ESC) $bash_pattern_part::parens++; }
+ | {$bash_pattern_part::parens != 0}? => RPAREN { if(LA(-2) != ESC) $bash_pattern_part::parens--; }
+ | ~(BLANK|RSQUARE|EOL|LOGICAND|LOGICOR|LPAREN|RPAREN)
)+;
keyword_binary_string_operator
: binary_operator
@@ -542,17 +551,17 @@ keyword_binary_string_operator
builtin_condition
: ((BANG) => builtin_negation_primary|builtin_keyword_condition_primary)
- (BLANK!? builtin_logic_operator^ BLANK!? builtin_condition)?;
+ (BLANK! builtin_logic_operator^ BLANK! builtin_condition)?;
builtin_negation_primary
: BANG BLANK builtin_keyword_condition_primary -> ^(NEGATION builtin_keyword_condition_primary);
builtin_keyword_condition_primary
: LPAREN! BLANK!? builtin_condition BLANK!? RPAREN!
- | builtin_condition_binary
- | builtin_condition_unary;
+ | (unary_operator) => builtin_condition_unary
+ | builtin_condition_binary;
builtin_condition_unary
: unary_operator^ BLANK! condition_part;
builtin_condition_binary
- : condition_part (BLANK!? builtin_binary_string_operator^ BLANK!? condition_part)?;
+ : condition_part (BLANK! builtin_binary_string_operator^ BLANK! condition_part)?;
builtin_binary_string_operator
: binary_operator
| (EQUALS EQUALS) => EQUALS EQUALS -> EQUALS
@@ -570,7 +579,7 @@ unary_operator
// TODO support brace expansion
condition_part
- : name -> ^(STRING name);
+ : string_expr;
name
: NAME | LETTER | UNDERSCORE;
diff --git a/bashast/gunit/compound.gunit b/bashast/gunit/compound.gunit
index ff1b9ce..dba0b60 100644
--- a/bashast/gunit/compound.gunit
+++ b/bashast/gunit/compound.gunit
@@ -19,18 +19,18 @@
gunit java_libbash;
condition_comparison:
-//"[[ -a this/is.afile ]]" -> (COMPOUND_COND (KEYWORD_TEST (a (STRING this / is . afile))))
-//"[[ -a this/is.afile]]" FAIL
-//"[[-a this/is.afile ]]" FAIL
-//"[[
-//-a this/is.afile ]]" -> (COMPOUND_COND (KEYWORD_TEST (a (STRING this / is . afile))))
-//"test ! -a this/is.afile" -> (COMPOUND_COND (BUILTIN_TEST (NEGATION (a (STRING this / is . afile)))))
+"[[ -a this/is.afile ]]" -> (COMPOUND_COND (KEYWORD_TEST (a (STRING this / is . afile))))
+"[[ -a this/is.afile]]" FAIL
+"[[-a this/is.afile ]]" FAIL
+"[[
+-a this/is.afile ]]" -> (COMPOUND_COND (KEYWORD_TEST (a (STRING this / is . afile))))
+"test ! -a this/is.afile" -> (COMPOUND_COND (BUILTIN_TEST (NEGATION (a (STRING this / is . afile)))))
"[[ -a afile ]]" -> (COMPOUND_COND (KEYWORD_TEST (a (STRING afile))))
"[[ -a afile]]" FAIL
"[[-a afile ]]" FAIL
"[[
-a afile ]]" -> (COMPOUND_COND (KEYWORD_TEST (a (STRING afile))))
-//"test ! -a this/is.afile" -> (COMPOUND_COND (BUILTIN_TEST (NEGATION (a (STRING this / is . afile)))))
+"test ! -a this/is.afile" -> (COMPOUND_COND (BUILTIN_TEST (NEGATION (a (STRING this / is . afile)))))
"[[ asdf > qwert ]]" -> (COMPOUND_COND (KEYWORD_TEST (> (STRING asdf) (STRING qwert))))
"[ asdf \> qwert ]" -> (COMPOUND_COND (BUILTIN_TEST (\> (STRING asdf) (STRING qwert))))
diff --git a/bashast/gunit/cond_main.gunit b/bashast/gunit/cond_main.gunit
index 6b31a2d..4068c51 100644
--- a/bashast/gunit/cond_main.gunit
+++ b/bashast/gunit/cond_main.gunit
@@ -18,8 +18,7 @@
*/
gunit java_libbash;
-/*
-cond_expr:
+condition_expr:
"[[ -a this/is.afile ]]" -> (KEYWORD_TEST (a (STRING this / is . afile)))
"[ -n \"yar53\" ]" -> (BUILTIN_TEST (n (STRING (DOUBLE_QUOTED_STRING yar53))))
"test 5 -eq 6" -> (BUILTIN_TEST (eq (STRING 5) (STRING 6)))
@@ -34,4 +33,3 @@ cond_expr:
"[[ \"${DISTUTILS_SRC_TEST}\" =~ ^(setup\.py|nosetests|py\.test|trial(\ .*)?)$ ]]" -> (KEYWORD_TEST (MATCH_REGULAR_EXPRESSION (STRING (DOUBLE_QUOTED_STRING (VAR_REF DISTUTILS_SRC_TEST))) (STRING ^ ( setup \ . py | nosetests | py \ . test | trial ( \ . * ) ? ) $)))
"[ -n "$FROM_LANG" -a -n "$TO_LANG" ]" -> (BUILTIN_TEST (BUILTIN_LOGIC a (n (STRING (DOUBLE_QUOTED_STRING (VAR_REF FROM_LANG)))) (n (STRING (DOUBLE_QUOTED_STRING (VAR_REF TO_LANG))))))
"[ -n "$FROM_LANG" -o -n "$TO_LANG" ]" -> (BUILTIN_TEST (BUILTIN_LOGIC o (n (STRING (DOUBLE_QUOTED_STRING (VAR_REF FROM_LANG)))) (n (STRING (DOUBLE_QUOTED_STRING (VAR_REF TO_LANG))))))
-*/
diff --git a/bashast/gunit/expansions.gunit b/bashast/gunit/expansions.gunit
index 4608994..a1733e8 100644
--- a/bashast/gunit/expansions.gunit
+++ b/bashast/gunit/expansions.gunit
@@ -18,7 +18,6 @@
*/
gunit java_libbash;
-/*
command_list:
"echo a{b,c,d}" -> (LIST (COMMAND (STRING echo) (STRING a (BRACE_EXP (STRING b) (STRING c) (STRING d)))))
"((5+5))" -> (LIST (COMMAND (ARITHMETIC_EXPRESSION (+ 5 5))))
@@ -29,7 +28,6 @@ command_list:
echo $each
done" -> (LIST (COMMAND (for each (STRING (COMMAND_SUB `ls |grep output`)) (LIST (COMMAND (STRING echo) (STRING (VAR_REF each)))))))
"wc <(cat /usr/share/dict/linux.words)" -> (LIST (COMMAND (STRING wc) (PROCESS_SUBSTITUTION < (LIST (COMMAND (STRING cat) (STRING / usr / share / dict / linux . words))))))
-*/
all_expansions:
"abc $(ab) ${ab} $((ab)) `ab` \"ab\" 'ab'" -> (STRING abc (COMMAND_SUB $(ab)) (VAR_REF ab) (ARITHMETIC_EXPRESSION (VAR_REF ab)) (COMMAND_SUB `ab`) (DOUBLE_QUOTED_STRING ab) 'ab')
diff --git a/bashast/gunit/fname.gunit b/bashast/gunit/fname.gunit
index b77ff50..5f5c9fd 100644
--- a/bashast/gunit/fname.gunit
+++ b/bashast/gunit/fname.gunit
@@ -50,31 +50,31 @@ string_expr:
condition_expr:
// bracket patterns
-"[[ x == abc[def] ]]" -> (KEYWORD_TEST (MATCH_PATTERN (STRING x) abc (MATCH_ANY def)))
-"[[ x == abc[d${more}] ]]" -> (KEYWORD_TEST (MATCH_PATTERN (STRING x) abc (MATCH_ANY d (VAR_REF more))))
-"[[ x==abc[#d] ]]" -> (KEYWORD_TEST (MATCH_PATTERN (STRING x) abc (MATCH_ANY # d)))
-"[[ x==abc[d#] ]]" -> (KEYWORD_TEST (MATCH_PATTERN (STRING x) abc (MATCH_ANY d #)))
-"[[ x==ab[d-h] ]]" -> (KEYWORD_TEST (MATCH_PATTERN (STRING x) ab (MATCH_ANY d - h)))
-"[[ x==ab[!d-h] ]]" -> (KEYWORD_TEST (MATCH_PATTERN (STRING x) ab (MATCH_ANY_EXCEPT d - h)))
-"[[ x==ab[^d-h] ]]" -> (KEYWORD_TEST (MATCH_PATTERN (STRING x) ab (MATCH_ANY_EXCEPT d - h)))
+"[[ x == abc[def] ]]" -> (KEYWORD_TEST (MATCH_PATTERN (STRING x) (STRING abc (MATCH_ANY def))))
+"[[ x == abc[d${more}] ]]" -> (KEYWORD_TEST (MATCH_PATTERN (STRING x) (STRING abc (MATCH_ANY d (VAR_REF more)))))
+"[[ x == abc[#d] ]]" -> (KEYWORD_TEST (MATCH_PATTERN (STRING x) (STRING abc (MATCH_ANY # d))))
+"[[ x == abc[d#] ]]" -> (KEYWORD_TEST (MATCH_PATTERN (STRING x) (STRING abc (MATCH_ANY d #))))
+"[[ x == ab[d-h] ]]" -> (KEYWORD_TEST (MATCH_PATTERN (STRING x) (STRING ab (MATCH_ANY d - h))))
+"[[ x == ab[!d-h] ]]" -> (KEYWORD_TEST (MATCH_PATTERN (STRING x) (STRING ab (MATCH_ANY_EXCEPT d - h))))
+"[[ x == ab[^d-h] ]]" -> (KEYWORD_TEST (MATCH_PATTERN (STRING x) (STRING ab (MATCH_ANY_EXCEPT d - h))))
//"ab[]c]" -> (STRING ab (MATCH_ANY ] c))
//"ab[]!]" -> (STRING ab (MATCH_ANY ] !))
-"[[ x==ab[:alpha:] ]]" -> (KEYWORD_TEST (MATCH_PATTERN (STRING x) ab (MATCH_ANY : alpha :)))
-"[[ x==ab[=c=] ]]" -> (KEYWORD_TEST (MATCH_PATTERN (STRING x) ab (MATCH_ANY = c =)))
-"[[ x==ab[.c.] ]]" -> (KEYWORD_TEST (MATCH_PATTERN (STRING x) ab (MATCH_ANY . c .)))
-"[[ x==ab[[:alpha:]] ]]" -> (KEYWORD_TEST (MATCH_PATTERN (STRING x) ab (MATCH_ANY (CHARACTER_CLASS alpha))))
-"[[ x==ab[[:alpha:][:digit:]] ]]" -> (KEYWORD_TEST (MATCH_PATTERN (STRING x) ab (MATCH_ANY (CHARACTER_CLASS alpha) (CHARACTER_CLASS digit))))
-"[[ x==ab[^[:alpha:]] ]]" -> (KEYWORD_TEST (MATCH_PATTERN (STRING x) ab (MATCH_ANY_EXCEPT (CHARACTER_CLASS alpha))))
-"[[ x==ab[[=c=]] ]]" -> (KEYWORD_TEST (MATCH_PATTERN (STRING x) ab (MATCH_ANY (EQUIVALENCE_CLASS c))))
-"[[ x==ab[[.backslash.]] ]]" -> (KEYWORD_TEST (MATCH_PATTERN (STRING x) ab (MATCH_ANY (COLLATING_SYMBOL backslash))))
-"[[ x==ab[12[:alpha:]] ]]" -> (KEYWORD_TEST (MATCH_PATTERN (STRING x) ab (MATCH_ANY 12 (CHARACTER_CLASS alpha))))
+"[[ x == ab[:alpha:] ]]" -> (KEYWORD_TEST (MATCH_PATTERN (STRING x) (STRING ab (MATCH_ANY : alpha :))))
+"[[ x == ab[=c=] ]]" -> (KEYWORD_TEST (MATCH_PATTERN (STRING x) (STRING ab (MATCH_ANY = c =))))
+"[[ x == ab[.c.] ]]" -> (KEYWORD_TEST (MATCH_PATTERN (STRING x) (STRING ab (MATCH_ANY . c .))))
+"[[ x == ab[[:alpha:]] ]]" -> (KEYWORD_TEST (MATCH_PATTERN (STRING x) (STRING ab (MATCH_ANY (CHARACTER_CLASS alpha)))))
+"[[ x == ab[[:alpha:][:digit:]] ]]" -> (KEYWORD_TEST (MATCH_PATTERN (STRING x) (STRING ab (MATCH_ANY (CHARACTER_CLASS alpha) (CHARACTER_CLASS digit)))))
+"[[ x == ab[^[:alpha:]] ]]" -> (KEYWORD_TEST (MATCH_PATTERN (STRING x) (STRING ab (MATCH_ANY_EXCEPT (CHARACTER_CLASS alpha)))))
+"[[ x == ab[[=c=]] ]]" -> (KEYWORD_TEST (MATCH_PATTERN (STRING x) (STRING ab (MATCH_ANY (EQUIVALENCE_CLASS c)))))
+"[[ x == ab[[.backslash.]] ]]" -> (KEYWORD_TEST (MATCH_PATTERN (STRING x) (STRING ab (MATCH_ANY (COLLATING_SYMBOL backslash)))))
+"[[ x == ab[12[:alpha:]] ]]" -> (KEYWORD_TEST (MATCH_PATTERN (STRING x) (STRING ab (MATCH_ANY 12 (CHARACTER_CLASS alpha)))))
// extended patterns
-"[[ x==ab?(g|h) ]]" -> (KEYWORD_TEST (MATCH_PATTERN (STRING x) ab (EXTENDED_MATCH_AT_MOST_ONE (BRANCH g) (BRANCH h))))
-"[[ x==ab*(gh|i) ]]" -> (KEYWORD_TEST (MATCH_PATTERN (STRING x) ab (EXTENDED_MATCH_ANY (BRANCH gh) (BRANCH i))))
-"[[ x==ab+(gh|i) ]]" -> (KEYWORD_TEST (MATCH_PATTERN (STRING x) ab (EXTENDED_MATCH_AT_LEAST_ONE (BRANCH gh) (BRANCH i))))
-"[[ x==ab@(gh|i) ]]" -> (KEYWORD_TEST (MATCH_PATTERN (STRING x) ab (EXTENDED_MATCH_EXACTLY_ONE (BRANCH gh) (BRANCH i))))
-"[[ x==ab!(gh|i) ]]" -> (KEYWORD_TEST (MATCH_PATTERN (STRING x) ab (EXTENDED_MATCH_NONE (BRANCH gh) (BRANCH i))))
+"[[ x == ab?(g|h) ]]" -> (KEYWORD_TEST (MATCH_PATTERN (STRING x) (STRING ab (EXTENDED_MATCH_AT_MOST_ONE (BRANCH g) (BRANCH h)))))
+"[[ x == ab*(gh|i) ]]" -> (KEYWORD_TEST (MATCH_PATTERN (STRING x) (STRING ab (EXTENDED_MATCH_ANY (BRANCH gh) (BRANCH i)))))
+"[[ x == ab+(gh|i) ]]" -> (KEYWORD_TEST (MATCH_PATTERN (STRING x) (STRING ab (EXTENDED_MATCH_AT_LEAST_ONE (BRANCH gh) (BRANCH i)))))
+"[[ x == ab@(gh|i) ]]" -> (KEYWORD_TEST (MATCH_PATTERN (STRING x) (STRING ab (EXTENDED_MATCH_EXACTLY_ONE (BRANCH gh) (BRANCH i)))))
+"[[ x == ab!(gh|i) ]]" -> (KEYWORD_TEST (MATCH_PATTERN (STRING x) (STRING ab (EXTENDED_MATCH_NONE (BRANCH gh) (BRANCH i)))))
double_quoted_string:
"\"\\\\\"\$\`\"" -> (DOUBLE_QUOTED_STRING \ " \$ \`)
diff --git a/bashast/gunit/list.gunit b/bashast/gunit/list.gunit
index bfe29fc..552788a 100644
--- a/bashast/gunit/list.gunit
+++ b/bashast/gunit/list.gunit
@@ -43,4 +43,4 @@ echo \"a b\"" -> (LIST (COMMAND (VARIABLE_DEFINITIONS (= a (STRING asdf)))) (COM
true" -> (LIST (COMMAND (STRING true)) (COMMAND (STRING true)))
"(echo hi > /dev/null) >> 1" -> (LIST (COMMAND (SUBSHELL (LIST (COMMAND (STRING echo) (STRING hi) (REDIR > (STRING / dev / null))))) (REDIR >> (FILE_DESCRIPTOR 1))))
"{ echo hi > /dev/null; } >> 1" -> (LIST (COMMAND (CURRENT_SHELL (LIST (COMMAND (STRING echo) (STRING hi) (REDIR > (STRING / dev / null))))) (REDIR >> (FILE_DESCRIPTOR 1))))
-//"test 1 -gt 0 || return 0" -> (LIST (|| (COMMAND (COMPOUND_COND (BUILTIN_TEST (gt (STRING 1) (STRING 0))))) (COMMAND (STRING return) (STRING 0))))
+"test 1 -gt 0 || return 0" -> (LIST (|| (COMMAND (COMPOUND_COND (BUILTIN_TEST (gt (STRING 1) (STRING 0))))) (COMMAND (STRING return) (STRING 0))))
diff --git a/utils/isolated-functions.sh b/utils/isolated-functions.sh
index 5bb157f..9d189ed 100755
--- a/utils/isolated-functions.sh
+++ b/utils/isolated-functions.sh
@@ -6,21 +6,21 @@ has() {
hasq $*
}
-#hasq() {
-# for item in ${*:2}
-# do
-# [[ $item == $1 ]] && return 0
-# done
-# return 1
-#}
-
-#EXPORT_FUNCTIONS() {
-# if [ -z "$ECLASS" ]; then
-# die "EXPORT_FUNCTIONS without a defined ECLASS"
-# return 1
-# fi
-# __export_funcs_var="$__export_funcs_var $*"
-#}
+hasq() {
+ for item in ${*:2}
+ do
+ [[ $item == $1 ]] && return 0
+ done
+ return 1
+}
+
+EXPORT_FUNCTIONS() {
+ if [ -z "$ECLASS" ]; then
+ die "EXPORT_FUNCTIONS without a defined ECLASS"
+ return 1
+ fi
+ __export_funcs_var="$__export_funcs_var $*"
+}
use() {
echo "use shouldn't be called"