diff --git a/find-useless-parentheses.sh b/find-useless-parentheses.sh index c2b0283..7fd0538 100644 --- a/find-useless-parentheses.sh +++ b/find-useless-parentheses.sh @@ -36,18 +36,22 @@ compute() { //block[ (: except not one of these ... :) - (: do not flag "a ( b | c )* d" or with other operator :) + (: do not flag "a ( b | c )* d" or with other operator :) not(./parent::ebnf/blockSuffix and ./altList/OR) and - (: do not flag "(a ( b | c )* )?" because it is not the same as the '*?'-operator. :) + (: do not flag "(a ( b | c )* )?" because it is not the same as the '*?'-operator. :) not(./parent::ebnf/blockSuffix/ebnfSuffix/QUESTION and ./altList[count(./*) = 1]/alternative[count(./*) = 1]/element[count(./*) = 1]/ebnf[./block and ./blockSuffix/ebnfSuffix/*]) and + (: do not flag blocks that contain a lot of elements like "(a b c)*" :) not(./parent::ebnf/blockSuffix and count(./altList/alternative/element) > 1) and + + (: do not flag if there are alts *and* it is preceed or followed by an element, + e.g., "a (b | c d)" or "(a | b) c". :) not(./altList/OR and ../../following-sibling::element) and not(./altList/OR and ../../preceding-sibling::element) and - (: do not flag "a ( v += b )* c" or with other operator :) - not(./altList/alternative/element/labeledElement/(ASSIGN or PLUS_ASSIGN) and ./parent::ebnf/blockSuffix) and + (: do not flag "a ( v += b )* c" or with other operator :) + not(./altList/alternative/element/labeledElement/(ASSIGN or PLUS_ASSIGN) and ./parent::ebnf/blockSuffix) and not(./parent::labeledElement/(ASSIGN or PLUS_ASSIGN)) ]' | trcaret -- -H > up-output.txt @@ -72,7 +76,7 @@ compute() { if [ -s up-output.txt ] then echo Found useless parentheses in grammars... 1>&2 - found=1 + found=1 cat up-output.txt 1>&2 fi rm -f up-output.txt diff --git a/tests/grammars/Up4.g4 b/tests/grammars/Up4.g4 new file mode 100644 index 0000000..267855f --- /dev/null +++ b/tests/grammars/Up4.g4 @@ -0,0 +1,2 @@ +grammar Up4; +start: (a b c)*; diff --git a/tests/grammars/Up5.g4 b/tests/grammars/Up5.g4 new file mode 100644 index 0000000..9d63010 --- /dev/null +++ b/tests/grammars/Up5.g4 @@ -0,0 +1,2 @@ +grammar Up5; +start: a (b | c); diff --git a/tests/grammars/Up6.g4 b/tests/grammars/Up6.g4 new file mode 100644 index 0000000..0bfbf13 --- /dev/null +++ b/tests/grammars/Up6.g4 @@ -0,0 +1,2 @@ +grammar Up6; +start: (b | c) d;