Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#184 Strict special parameter highlighting. #186

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions lexers/LexBash.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ enum class QuoteStyle {
};

#define BASH_QUOTE_STACK_MAX 7
#define BASH_SPECIAL_PARAMETER "*@#?-$!"

constexpr int commandSubstitutionFlag = 0x40;
constexpr int MaskCommand(int state) noexcept {
Expand Down Expand Up @@ -180,6 +181,7 @@ struct OptionsBash {
bool stylingInsideParameter = false;
bool stylingInsideHeredoc = false;
int commandSubstitution = static_cast<int>(CommandSubstitution::Backtick);
std::string specialParameter = BASH_SPECIAL_PARAMETER;

[[nodiscard]] bool stylingInside(int state) const noexcept {
switch (state) {
Expand Down Expand Up @@ -228,6 +230,9 @@ struct OptionSetBash : public OptionSet<OptionsBash> {
"1 highlighted inside. "
"2 highlighted inside with extra scope tracking.");

DefineProperty("lexer.bash.special.parameter", &OptionsBash::specialParameter,
"Set shell (default is Bash) special parameters.");

DefineWordListSets(bashWordListDesc);
}
};
Expand Down Expand Up @@ -266,6 +271,8 @@ class QuoteStackCls { // Class to manage quote pairs that nest
int insideCommand = 0;
QuoteCls Current;
QuoteCls Stack[BASH_QUOTE_STACK_MAX];
const CharacterSet &setParamStart;
QuoteStackCls(const CharacterSet &setParamStart_) noexcept : setParamStart{setParamStart_} {}
[[nodiscard]] bool Empty() const noexcept {
return Current.Up == '\0';
}
Expand Down Expand Up @@ -365,6 +372,9 @@ class QuoteStackCls { // Class to manage quote pairs that nest
sc.ChangeState(SCE_SH_BACKTICKS);
} else {
// scalar has no delimiter pair
if (!setParamStart.Contains(sc.ch)) {
stylingInside = false; // not scalar
}
}
if (!stylingInside) {
sc.ChangeState(state);
Expand Down Expand Up @@ -408,11 +418,13 @@ class LexerBash final : public DefaultLexer {
WordList testOperator;
OptionsBash options;
OptionSetBash osBash;
CharacterSet setParamStart;
enum { ssIdentifier, ssScalar };
SubStyles subStyles;
public:
LexerBash() :
DefaultLexer("bash", SCLEX_BASH, lexicalClasses, ELEMENTS(lexicalClasses)),
setParamStart(CharacterSet::setAlphaNum, "_" BASH_SPECIAL_PARAMETER),
subStyles(styleSubable, 0x80, 0x40, 0) {
cmdDelimiter.Set("| || |& & && ; ;; ( ) { }");
bashStruct.Set("if elif fi while until else then do done esac eval");
Expand Down Expand Up @@ -490,6 +502,10 @@ class LexerBash final : public DefaultLexer {

Sci_Position SCI_METHOD LexerBash::PropertySet(const char *key, const char *val) {
if (osBash.PropertySet(&options, key, val)) {
if (strcmp(key, "lexer.bash.special.parameter") == 0) {
setParamStart = CharacterSet(CharacterSet::setAlphaNum, "_");
setParamStart.AddString(options.specialParameter.empty() ? BASH_SPECIAL_PARAMETER : options.specialParameter.c_str());
}
return 0;
}
return -1;
Expand Down Expand Up @@ -543,7 +559,7 @@ void SCI_METHOD LexerBash::Lex(Sci_PositionU startPos, Sci_Position length, int
};
HereDocCls HereDoc;

QuoteStackCls QuoteStack;
QuoteStackCls QuoteStack(setParamStart);
QuoteStack.commandSubstitution = static_cast<CommandSubstitution>(options.commandSubstitution);

const WordClassifier &classifierIdentifiers = subStyles.Classifier(SCE_SH_IDENTIFIER);
Expand Down Expand Up @@ -818,7 +834,7 @@ void SCI_METHOD LexerBash::Lex(Sci_PositionU startPos, Sci_Position length, int
sc.ChangeState(subStyle | insideCommand);
}
if (sc.LengthCurrent() == 1) {
// Special variable: $(, $_ etc.
// Special variable
sc.Forward();
}
sc.SetState(QuoteStack.State | insideCommand);
Expand Down
11 changes: 11 additions & 0 deletions test/examples/bash/Issue184.bsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
echo $*
echo $@
echo $?
echo $-
echo $$
echo $!
echo $_
echo $%
echo $<

ifeth=$(ls /sys/class/net | grep ^"$intf" | grep "$intf"$)
12 changes: 12 additions & 0 deletions test/examples/bash/Issue184.bsh.folded
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
0 400 0 echo $*
0 400 0 echo $@
0 400 0 echo $?
0 400 0 echo $-
0 400 0 echo $$
0 400 0 echo $!
0 400 0 echo $_
0 400 0 echo $%
0 400 0 echo $<
1 400 0
0 400 0 ifeth=$(ls /sys/class/net | grep ^"$intf" | grep "$intf"$)
0 400 0
11 changes: 11 additions & 0 deletions test/examples/bash/Issue184.bsh.styled
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{4}echo{0} {9}$*{0}
{4}echo{0} {9}$@{0}
{4}echo{0} {9}$?{0}
{4}echo{0} {9}$-{0}
{4}echo{0} {9}$${0}
{4}echo{0} {9}$!{0}
{4}echo{0} {9}$_{0}
{4}echo{0} ${7}%{0}
{4}echo{0} ${7}<{0}

{8}ifeth{7}=$({8}ls{0} {7}/{8}sys{7}/{8}class{7}/{8}net{0} {7}|{0} {8}grep{0} {7}^{5}"{9}$intf{5}"{0} {7}|{0} {8}grep{0} {5}"{9}$intf{5}"{0}${7}){0}
11 changes: 11 additions & 0 deletions test/examples/bash/Issue184Copy.bsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
echo $*
echo $@
echo $?
echo $-
echo $$
echo $!
echo $_
echo $%
echo $<

ifeth=$(ls /sys/class/net | grep ^"$intf" | grep "$intf"$)
12 changes: 12 additions & 0 deletions test/examples/bash/Issue184Copy.bsh.folded
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
0 400 0 echo $*
0 400 0 echo $@
0 400 0 echo $?
0 400 0 echo $-
0 400 0 echo $$
0 400 0 echo $!
0 400 0 echo $_
0 400 0 echo $%
0 400 0 echo $<
1 400 0
0 400 0 ifeth=$(ls /sys/class/net | grep ^"$intf" | grep "$intf"$)
0 400 0
11 changes: 11 additions & 0 deletions test/examples/bash/Issue184Copy.bsh.styled
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{4}echo{0} {9}$*{0}
{4}echo{0} {9}$@{0}
{4}echo{0} {9}$?{0}
{4}echo{0} {9}$-{0}
{4}echo{0} {9}$${0}
{4}echo{0} {9}$!{0}
{4}echo{0} {9}$_{0}
{4}echo{0} {9}$%{0}
{4}echo{0} {9}$<{0}

{8}ifeth{7}=$({8}ls{0} {7}/{8}sys{7}/{8}class{7}/{8}net{0} {7}|{0} {8}grep{0} {7}^{5}"{9}$intf{5}"{0} {7}|{0} {8}grep{0} {5}"{9}$intf{5}"{0}${7}){0}
9 changes: 9 additions & 0 deletions test/examples/bash/SciTE.properties
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ match Issue180.bsh
match Issue182.bsh
lexer.bash.styling.inside.string=1

match Issue184.bsh
lexer.bash.styling.inside.string=1
lexer.bash.command.substitution=1

match Issue184Copy.bsh
lexer.bash.styling.inside.string=1
lexer.bash.command.substitution=1
lexer.bash.special.parameter=*@#?-$!%<

match NestedStyledInside.bsh
lexer.bash.styling.inside.string=1
lexer.bash.styling.inside.backticks=1
Expand Down