Skip to content

Commit

Permalink
Add tests, and doc.
Browse files Browse the repository at this point in the history
  • Loading branch information
kaby76 committed May 15, 2024
1 parent df3da02 commit e28fbe8
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 5 deletions.
14 changes: 9 additions & 5 deletions find-useless-parentheses.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions tests/grammars/Up4.g4
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
grammar Up4;
start: (a b c)*;
2 changes: 2 additions & 0 deletions tests/grammars/Up5.g4
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
grammar Up5;
start: a (b | c);
2 changes: 2 additions & 0 deletions tests/grammars/Up6.g4
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
grammar Up6;
start: (b | c) d;

0 comments on commit e28fbe8

Please sign in to comment.