Skip to content

Commit

Permalink
#197 Treat += as operator and, inside arithmetic expressions, treat +…
Browse files Browse the repository at this point in the history
…+ and -- as operators.
  • Loading branch information
zufuliu authored and nyamatongwe committed Aug 17, 2023
1 parent 8a688e3 commit abc09e4
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 3 deletions.
4 changes: 4 additions & 0 deletions doc/LexillaHistory.html
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,10 @@ <h3>
<a href="https://github.com/ScintillaOrg/lexilla/issues/195">Issue #195</a>.
</li>
<li>
Bash: treat += as operator and, inside arithmetic expressions, treat ++ and -- as operators.
<a href="https://github.com/ScintillaOrg/lexilla/issues/197">Issue #197</a>.
</li>
<li>
HTML: fix invalid entity at line end and terminate invalid entity before invalid character.
<a href="https://github.com/ScintillaOrg/lexilla/issues/192">Issue #192</a>.
</li>
Expand Down
5 changes: 3 additions & 2 deletions lexers/LexBash.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,7 @@ void SCI_METHOD LexerBash::Lex(Sci_PositionU startPos, Sci_Position length, int
break;
case SCE_SH_WORD:
// "." never used in Bash variable names but used in file names
if (!setWord.Contains(sc.ch)) {
if (!setWord.Contains(sc.ch) || sc.Match('+', '=')) {
char s[500];
sc.GetCurrent(s, sizeof(s));
int identifierStyle = SCE_SH_IDENTIFIER | insideCommand;
Expand Down Expand Up @@ -996,7 +996,7 @@ void SCI_METHOD LexerBash::Lex(Sci_PositionU startPos, Sci_Position length, int
}
}
} else if (setWordStart.Contains(sc.ch)) {
sc.SetState(SCE_SH_WORD | insideCommand);
sc.SetState(((cmdState == CmdState::Arithmetic)? SCE_SH_IDENTIFIER : SCE_SH_WORD) | insideCommand);
} else if (sc.ch == '#') {
if (stylePrev != SCE_SH_WORD && stylePrev != SCE_SH_IDENTIFIER &&
(sc.currentPos == 0 || setMetaCharacter.Contains(sc.chPrev))) {
Expand Down Expand Up @@ -1043,6 +1043,7 @@ void SCI_METHOD LexerBash::Lex(Sci_PositionU startPos, Sci_Position length, int
HereDoc.Indent = false;
}
} else if (sc.ch == '-' && // test operator or short and long option
cmdState != CmdState::Arithmetic &&
(IsUpperOrLowerCase(sc.chNext) || sc.chNext == '-') &&
IsASpace(sc.chPrev)) {
sc.SetState(SCE_SH_WORD | insideCommand);
Expand Down
16 changes: 16 additions & 0 deletions test/examples/bash/197ArithmeticOperator.bsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
hello="hello, "
hello+=word
echo $hello

for ((i = 2; i > 0; i--)); do
echo postfix dec $i
done
for ((i = 2; i > 0; --i)); do
echo prefix dec $i
done
for ((i = 0; i < 2; i++)); do
echo postfix inc $i
done
for ((i = 0; i < 2; ++i)); do
echo prefix inc $i
done
17 changes: 17 additions & 0 deletions test/examples/bash/197ArithmeticOperator.bsh.folded
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
0 400 0 hello="hello, "
0 400 0 hello+=word
0 400 0 echo $hello
1 400 0
2 400 0 + for ((i = 2; i > 0; i--)); do
0 401 0 | echo postfix dec $i
0 401 0 | done
2 400 0 + for ((i = 2; i > 0; --i)); do
0 401 0 | echo prefix dec $i
0 401 0 | done
2 400 0 + for ((i = 0; i < 2; i++)); do
0 401 0 | echo postfix inc $i
0 401 0 | done
2 400 0 + for ((i = 0; i < 2; ++i)); do
0 401 0 | echo prefix inc $i
0 401 0 | done
0 400 0
16 changes: 16 additions & 0 deletions test/examples/bash/197ArithmeticOperator.bsh.styled
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{8}hello{7}={5}"hello, "{0}
{8}hello{7}+={8}word{0}
{4}echo{0} {9}$hello{0}

{4}for{0} {7}(({8}i{0} {7}={0} {3}2{7};{0} {8}i{0} {7}>{0} {3}0{7};{0} {8}i{7}--));{0} {4}do{0}
{4}echo{0} {8}postfix{0} {8}dec{0} {9}$i{0}
{4}done{0}
{4}for{0} {7}(({8}i{0} {7}={0} {3}2{7};{0} {8}i{0} {7}>{0} {3}0{7};{0} {7}--{8}i{7}));{0} {4}do{0}
{4}echo{0} {8}prefix{0} {8}dec{0} {9}$i{0}
{4}done{0}
{4}for{0} {7}(({8}i{0} {7}={0} {3}0{7};{0} {8}i{0} {7}<{0} {3}2{7};{0} {8}i{7}++));{0} {4}do{0}
{4}echo{0} {8}postfix{0} {8}inc{0} {9}$i{0}
{4}done{0}
{4}for{0} {7}(({8}i{0} {7}={0} {3}0{7};{0} {8}i{0} {7}<{0} {3}2{7};{0} {7}++{8}i{7}));{0} {4}do{0}
{4}echo{0} {8}prefix{0} {8}inc{0} {9}$i{0}
{4}done{0}
2 changes: 1 addition & 1 deletion test/examples/bash/hash.zsh.styled
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
{4}print{0} {7}$(({0} {8}##T{7}+{3}0{0} {7})){0}
{4}print{0} {7}$(({0} {8}##^G{7}+{3}0{0} {7})){0}
{2}# Failure: does not work when - included for bindkey syntax. \M-\C-x means Meta+Ctrl+x.{0}
{4}print{0} {7}$(({0} {8}##\M{7}-{8}\C{7}-{8}x+0{0} {7})){0}
{4}print{0} {7}$(({0} {8}##\M{7}-{8}\C{7}-{8}x{7}+{3}0{0} {7})){0}

{2}# Value of first character of variable in expression{0}
{8}var{7}={8}Tree{0}
Expand Down

0 comments on commit abc09e4

Please sign in to comment.