Skip to content

Commit

Permalink
#288 Fix unstable lexing with substyled keyword and unterminated string.
Browse files Browse the repository at this point in the history
Merge backtracking loops and only check line end styles - don't exit from backtracking
when non-string state found.
Respect isPHPScript if backtracking reaches start.
Remove redundant styler.StartAt(startPos);
  • Loading branch information
zufuliu authored and nyamatongwe committed Nov 23, 2024
1 parent 942ac3f commit 8a6e5e7
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 14 deletions.
4 changes: 4 additions & 0 deletions doc/LexillaHistory.html
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,10 @@ <h3>
<a href="https://github.com/ScintillaOrg/lexilla/issues/286">Issue #286</a>.
</li>
<li>
PHP: Fix unstable lexing with substyled keyword and unterminated string.
<a href="https://github.com/ScintillaOrg/lexilla/issues/288">Issue #288</a>.
</li>
<li>
TOML: Don't treat keys without values as errors.
<a href="https://github.com/ScintillaOrg/lexilla/pull/283">Pull request #283</a>.
</li>
Expand Down
24 changes: 11 additions & 13 deletions lexers/LexHTML.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,10 @@ constexpr bool isPHPStringState(int state) noexcept {
(state == SCE_HPHP_COMPLEX_VARIABLE);
}

constexpr bool StyleNeedsBacktrack(int state) noexcept {
return InTagState(state) || isPHPStringState(state);
}

enum class AllowPHP : int {
None, // No PHP
PHP, // <?php and <?=
Expand Down Expand Up @@ -1190,7 +1194,6 @@ void SCI_METHOD LexerHTML::Lex(Sci_PositionU startPos, Sci_Position length, int
if (isPHPScript && (startPos == 0)) {
initStyle = SCE_HPHP_DEFAULT;
}
styler.StartAt(startPos);
std::string lastTag;
std::string prevWord;
PhpNumberState phpNumber;
Expand All @@ -1201,23 +1204,18 @@ void SCI_METHOD LexerHTML::Lex(Sci_PositionU startPos, Sci_Position length, int
int makoComment = 0;
std::string djangoBlockType;
// If inside a tag, it may be a script tag, so reread from the start of line starting tag to ensure any language tags are seen
if (InTagState(state)) {
while ((startPos > 0) && (InTagState(styler.StyleIndexAt(startPos - 1)))) {
// PHP string can be heredoc, must find a delimiter first. Reread from beginning of line containing the string, to get the correct lineState
if (StyleNeedsBacktrack(state)) {
while ((startPos > 0) && (StyleNeedsBacktrack(styler.StyleIndexAt(startPos - 1)))) {
const Sci_Position backLineStart = styler.LineStart(styler.GetLine(startPos-1));
length += startPos - backLineStart;
startPos = backLineStart;
}
state = (startPos > 0) ? styler.StyleIndexAt(startPos - 1) : SCE_H_DEFAULT;
}
// String can be heredoc, must find a delimiter first. Reread from beginning of line containing the string, to get the correct lineState
if (isPHPStringState(state)) {
while (startPos > 0 && (isPHPStringState(state) || !isLineEnd(styler[startPos - 1]))) {
startPos--;
length++;
state = styler.StyleIndexAt(startPos);
if (startPos > 0) {
state = styler.StyleIndexAt(startPos - 1);
} else {
state = isPHPScript ? SCE_HPHP_DEFAULT : SCE_H_DEFAULT;
}
if (startPos == 0)
state = SCE_H_DEFAULT;
}
styler.StartAt(startPos);

Expand Down
4 changes: 4 additions & 0 deletions test/examples/hypertext/Issue288.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?
nl2br("\n); // unterminated string
?>
")
5 changes: 5 additions & 0 deletions test/examples/hypertext/Issue288.php.folded
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
2 400 0 + <?
0 401 0 | nl2br("\n); // unterminated string
0 401 0 | ?>
0 401 0 | ")
0 401 0 |
4 changes: 4 additions & 0 deletions test/examples/hypertext/Issue288.php.styled
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{18}<?{118}
{198}nl2br{127}({119}"\n); // unterminated string
?>
"{127}){118}
2 changes: 1 addition & 1 deletion test/examples/hypertext/SciTE.properties
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ substyles.hypertext.96=1
substylewords.96.1.*=parse
# PHP
substyles.hypertext.121=1
substylewords.121.1.*=decrypt
substylewords.121.1.*=decrypt nl2br

fold=1
fold.html=1
Expand Down

0 comments on commit 8a6e5e7

Please sign in to comment.