diff options
author | Jannick <unknown> | 2018-12-12 08:38:19 +1100 |
---|---|---|
committer | Jannick <unknown> | 2018-12-12 08:38:19 +1100 |
commit | 3f3eb0ea971edddb3983bb6498decbb171b17e0f (patch) | |
tree | c5f86fc7cda92ac118ea413425d4b30d69bb9a47 | |
parent | 2c4a82892183b5cf7eaa12f1b5f9048d034a13e4 (diff) | |
download | scintilla-mirror-3f3eb0ea971edddb3983bb6498decbb171b17e0f.tar.gz |
Bug [#2069]. LexCPP: fix bug in arithmetic calculation by adding precedence levels
The precedence for the implemented arithmetic operators +,-,%,*,/
is added, such that the calculations produce the correct results
honoring the standard precedence levels.
* Replace characterset setArithmeticOp by setAddOp and setMultOp.
* Replace precedence precArithmetic by precMult and precAdd
* (EvaluateTokens): Apply new precedences.
This fixes the bug in the arithmetic calculation:
// lines with 'false' should not be highlighted,
// those with 'true' should be.
#if 1 + 2 * 3 == 9
false
#endif
#if (1 + 2) * 3 == 9
true
#endif
#if 1 + 2 * 3 == 7
true
#endif
#if 1 == 5 % 2
true
#endif
#if 6 - 7 == -1
true
#endif
#if 25 / 5 * 5 == 25
true
#endif
#if 1 + 2 * 3 % 2 == 1
true
#endif
#if 1 + 2 * 3 % 2 == 2 + 1
false
#endif
-rw-r--r-- | doc/ScintillaHistory.html | 5 | ||||
-rw-r--r-- | lexers/LexCPP.cxx | 14 |
2 files changed, 14 insertions, 5 deletions
diff --git a/doc/ScintillaHistory.html b/doc/ScintillaHistory.html index 0908fcc08..3965ac8e9 100644 --- a/doc/ScintillaHistory.html +++ b/doc/ScintillaHistory.html @@ -582,6 +582,11 @@ <a href="https://sourceforge.net/p/scintilla/bugs/2062/">Bug #2062</a>. </li> <li> + The C++ lexer interprets preprocessor arithmetic expressions containing multiplicative and additive + operators correctly by following operator precedence rules. + <a href="https://sourceforge.net/p/scintilla/bugs/2069/">Bug #2069</a>. + </li> + <li> For SciTE's Find in Files, allow case-sensitivity and whole-word options when running a user defined command. <a href="https://sourceforge.net/p/scintilla/bugs/2053/">Bug #2053</a>. diff --git a/lexers/LexCPP.cxx b/lexers/LexCPP.cxx index 4ca182b3a..fb3d47d8e 100644 --- a/lexers/LexCPP.cxx +++ b/lexers/LexCPP.cxx @@ -484,7 +484,8 @@ class LexerCPP : public ILexer4 { bool caseSensitive; CharacterSet setWord; CharacterSet setNegationOp; - CharacterSet setArithmethicOp; + CharacterSet setAddOp; + CharacterSet setMultOp; CharacterSet setRelOp; CharacterSet setLogicalOp; CharacterSet setWordStart; @@ -525,7 +526,8 @@ public: caseSensitive(caseSensitive_), setWord(CharacterSet::setAlphaNum, "._", 0x80, true), setNegationOp(CharacterSet::setNone, "!"), - setArithmethicOp(CharacterSet::setNone, "+-/*%"), + setAddOp(CharacterSet::setNone, "+-"), + setMultOp(CharacterSet::setNone, "*/%"), setRelOp(CharacterSet::setNone, "=!<>"), setLogicalOp(CharacterSet::setNone, "|&"), subStyles(styleSubable, 0x80, 0x40, activeFlag) { @@ -1628,13 +1630,15 @@ void LexerCPP::EvaluateTokens(std::vector<std::string> &tokens, const SymbolTabl } // Evaluate expressions in precedence order - enum precedence { precArithmetic, precRelative, precLogical }; - for (int prec=precArithmetic; prec <= precLogical; prec++) { + enum precedence { precMult, precAdd, precRelative + , precLogical, /* end marker */ precLast }; + for (int prec = precMult; prec < precLast; prec++) { // Looking at 3 tokens at a time so end at 2 before end for (size_t k=0; (k+2)<tokens.size();) { const char chOp = tokens[k+1][0]; if ( - ((prec==precArithmetic) && setArithmethicOp.Contains(chOp)) || + ((prec==precMult) && setMultOp.Contains(chOp)) || + ((prec==precAdd) && setAddOp.Contains(chOp)) || ((prec==precRelative) && setRelOp.Contains(chOp)) || ((prec==precLogical) && setLogicalOp.Contains(chOp)) ) { |