Skip to content

Commit

Permalink
Fix the error when a single quote is escaped in the Latte template ap…
Browse files Browse the repository at this point in the history
…ache#5862

- apache#5862
- https://latte.nette.org/en/tags#toc-translation
- Fix the lexer (Fix the regex for the single quote string)
- Fix the braces matcher
- Add unit tests
  • Loading branch information
junichi11 committed Apr 25, 2023
1 parent 92080c7 commit 87092c4
Show file tree
Hide file tree
Showing 15 changed files with 459 additions and 239 deletions.
48 changes: 48 additions & 0 deletions php/php.latte/build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,53 @@
</target>
<target name="gen-markuplexer" >
<jflex file="tools/LatteMarkupColoringLexer.flex" destdir="src" skel="tools/skeleton.netbeans" />
<replace file="src/org/netbeans/modules/php/latte/lexer/LatteMarkupColoringLexer.java">
<replacetoken><![CDATA[
if (zzCurrentPosL < zzEndReadL)
zzInput = zzBufferL[zzCurrentPosL++];
else if (zzAtEOF) {
zzInput = YYEOF;
break zzForAction;
}
else {
// store back cached positions
zzCurrentPos = zzCurrentPosL;
zzMarkedPos = zzMarkedPosL;
boolean eof = zzRefill();
// get translated positions and possibly new buffer
zzCurrentPosL = zzCurrentPos;
zzMarkedPosL = zzMarkedPos;
zzBufferL = zzBuffer;
zzEndReadL = zzEndRead;
if (eof) {
zzInput = YYEOF;
break zzForAction;
}
else {
zzInput = zzBufferL[zzCurrentPosL++];
}
}]]></replacetoken>
<replacevalue></replacevalue>
</replace>
<replace file="src/org/netbeans/modules/php/latte/lexer/LatteMarkupColoringLexer.java">
<replacetoken><![CDATA[
yychar+= zzMarkedPosL-zzStartRead;
]]></replacetoken>
<replacevalue></replacevalue>
</replace>
<replace file="src/org/netbeans/modules/php/latte/lexer/LatteMarkupColoringLexer.java">
<replacetoken><![CDATA[
public LatteMarkupTokenId findNextToken() throws java.io.IOException {
]]></replacetoken>
<replacevalue><![CDATA[
@SuppressWarnings("fallthrough")
public LatteMarkupTokenId findNextToken() throws java.io.IOException {
]]></replacevalue>
</replace>
<replaceregexp match="[ \t]+$" replace="" byline="true" >
<fileset dir="src/org/netbeans/modules/php/latte/lexer">
<include name="LatteMarkupColoringLexer.java"/>
</fileset>
</replaceregexp>
</target>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import javax.swing.text.BadLocationException;
import org.netbeans.api.lexer.Token;
import org.netbeans.api.lexer.TokenSequence;
import org.netbeans.api.lexer.TokenUtilities;
import org.netbeans.editor.BaseDocument;
import org.netbeans.modules.csl.api.OffsetRange;
import org.netbeans.modules.php.latte.lexer.LatteMarkupTokenId;
Expand Down Expand Up @@ -57,6 +58,7 @@ public class LatteBracesMatcher implements BracesMatcher {
MATCHERS.add(new StartEndMacroMatcher("form")); //NOI18N
MATCHERS.add(new StartEndMacroMatcher("label")); //NOI18N
MATCHERS.add(new StartEndMacroMatcher("snippet")); //NOI18N
MATCHERS.add(new StartEndMacroMatcher("translate")); //NOI18N
MATCHERS.add(new IfMatcher());
MATCHERS.add(new IfsetMatcher());
MATCHERS.add(new EndIfMatcher());
Expand Down Expand Up @@ -100,14 +102,40 @@ private int[] findOriginInSequence(TokenSequence<? extends LatteMarkupTokenId> t
Token<? extends LatteMarkupTokenId> currentToken = ts.token();
if (currentToken != null) {
LatteMarkupTokenId currentTokenId = currentToken.id();
if (currentTokenId == LatteMarkupTokenId.T_MACRO_START || currentTokenId == LatteMarkupTokenId.T_MACRO_END) {
if ((currentTokenId == LatteMarkupTokenId.T_MACRO_START || currentTokenId == LatteMarkupTokenId.T_MACRO_END)
&& !isUnderscoreTranslationTag(ts)) {
result = new int[] {ts.offset(), ts.offset() + currentToken.length()};
}
}
}
return result;
}

private boolean isUnderscoreTranslationTag(TokenSequence<? extends LatteMarkupTokenId> ts) {
// check {_'text'}
// see: https://latte.nette.org/en/tags#toc-translation
int originalOffset = ts.offset();
boolean isTranslationTag = false;
if (TokenUtilities.textEquals("_", ts.token().text())) { // NOI18N
while(ts.moveNext()) {
Token<? extends LatteMarkupTokenId> token = ts.token();
if (token == null) {
break;
}
if (token.id() == LatteMarkupTokenId.T_WHITESPACE) {
continue;
}
if (token.id() == LatteMarkupTokenId.T_STRING) {
isTranslationTag = true;
}
break;
}
}
ts.move(originalOffset);
ts.moveNext();
return isTranslationTag;
}

@Override
public int[] findMatches() throws InterruptedException, BadLocationException {
int[] result = null;
Expand Down
Loading

0 comments on commit 87092c4

Please sign in to comment.