Skip to content

Commit 3134f40

Browse files
committed
Fix dangling Python span by subclassing JFlexXrefSimple
Also: - Fix to remove matches of end-quote or end- apostrophe followed by space and another quote or apostrophe (resp.) that could have mis-identified when the trailing quote or apostrophe was actually beginning a Python longstring. - Make PythonXref case-sensitive, revising {File} to match any case. - Escape all HTML special characters in PythonXref. - Use Common.xref in PythonSymbolTokenizer.
1 parent f82788d commit 3134f40

File tree

3 files changed

+94
-65
lines changed

3 files changed

+94
-65
lines changed

src/org/opensolaris/opengrok/analysis/python/PythonSymbolTokenizer.lex

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ super(in);
4545

4646
%state STRING LSTRING SCOMMENT QSTRING LQSTRING
4747

48+
%include Common.lexh
4849
%include Python.lexh
4950
%%
5051

@@ -67,25 +68,30 @@ super(in);
6768
}
6869

6970
<STRING> {
71+
\\[\"\\] {}
7072
\" { yybegin(YYINITIAL); }
71-
\n { yybegin(YYINITIAL); }
72-
}
73-
74-
<LSTRING> {
75-
\"\"\" { yybegin(YYINITIAL); }
73+
{EOL} { yybegin(YYINITIAL); }
7674
}
7775

7876
<QSTRING> {
77+
\\[\'\\] {}
7978
\' { yybegin(YYINITIAL); }
80-
\n { yybegin(YYINITIAL); }
79+
{EOL} { yybegin(YYINITIAL); }
80+
}
81+
82+
<LSTRING> {
83+
\\[\"\\] {}
84+
\"\"\" { yybegin(YYINITIAL); }
8185
}
8286

8387
<LQSTRING> {
88+
\\[\'\\] {}
8489
\'\'\' { yybegin(YYINITIAL); }
8590
}
8691

8792
<SCOMMENT> {
88-
\n { yybegin(YYINITIAL);}
93+
{WhiteSpace} {}
94+
{EOL} { yybegin(YYINITIAL);}
8995
}
9096

9197
<YYINITIAL, STRING, LSTRING, SCOMMENT, QSTRING , LQSTRING> {

src/org/opensolaris/opengrok/analysis/python/PythonXref.lex

Lines changed: 56 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,18 @@
2727
*/
2828

2929
package org.opensolaris.opengrok.analysis.python;
30-
import org.opensolaris.opengrok.analysis.JFlexXref;
30+
import org.opensolaris.opengrok.analysis.JFlexXrefSimple;
3131
import java.io.IOException;
3232
import java.io.Writer;
3333
import java.io.Reader;
34+
import org.opensolaris.opengrok.web.HtmlConsts;
3435
import org.opensolaris.opengrok.web.Util;
3536

3637
%%
3738
%public
3839
%class PythonXref
39-
%extends JFlexXref
40+
%extends JFlexXrefSimple
4041
%unicode
41-
%ignorecase
4242
%int
4343
%include CommonXref.lexh
4444
%{
@@ -49,7 +49,9 @@ import org.opensolaris.opengrok.web.Util;
4949
protected void setLineNumber(int x) { yyline = x; }
5050
%}
5151

52-
File = [a-zA-Z]{FNameChar}* "." ("py"|"pm"|"conf"|"txt"|"htm"|"html"|"xml"|"ini"|"diff"|"patch")
52+
File = [a-zA-Z]{FNameChar}* "." ([Pp][Yy] | [Pp][Mm] | [Cc][Oo][Nn][Ff] |
53+
[Tt][Xx][Tt] | [Hh][Tt][Mm][Ll]? | [Xx][Mm][Ll] | [Ii][Nn][Ii] |
54+
[Dd][Ii][Ff][Ff] | [Pp][Aa][Tt][Cc][Hh])
5355

5456
%state STRING LSTRING SCOMMENT QSTRING LQSTRING
5557

@@ -83,62 +85,83 @@ File = [a-zA-Z]{FNameChar}* "." ("py"|"pm"|"conf"|"txt"|"htm"|"html"|"xml"|"ini"
8385
out.write("&gt;");
8486
}
8587

86-
{Number} { out.write("<span class=\"n\">"); out.write(yytext()); out.write("</span>"); }
87-
88-
\" { yybegin(STRING);out.write("<span class=\"s\">\"");}
89-
\"\"\" { yybegin(LSTRING);out.write("<span class=\"s\">\"\"\"");}
90-
\' { yybegin(QSTRING);out.write("<span class=\"s\">\'");}
91-
\'\'\' { yybegin(LQSTRING);out.write("<span class=\"s\">\'\'\'");}
92-
"#" { yybegin(SCOMMENT);out.write("<span class=\"c\">#");}
88+
{Number} {
89+
disjointSpan(HtmlConsts.NUMBER_CLASS);
90+
out.write(yytext());
91+
disjointSpan(null);
92+
}
93+
94+
\" {
95+
pushSpan(STRING, HtmlConsts.STRING_CLASS);
96+
out.write(htmlize(yytext()));
97+
}
98+
\"\"\" {
99+
pushSpan(LSTRING, HtmlConsts.STRING_CLASS);
100+
out.write(htmlize(yytext()));
101+
}
102+
\' {
103+
pushSpan(QSTRING, HtmlConsts.STRING_CLASS);
104+
out.write(htmlize(yytext()));
105+
}
106+
\'\'\' {
107+
pushSpan(LQSTRING, HtmlConsts.STRING_CLASS);
108+
out.write(htmlize(yytext()));
109+
}
110+
"#" {
111+
pushSpan(SCOMMENT, HtmlConsts.COMMENT_CLASS);
112+
out.write(yytext());
113+
}
93114
}
94115

95116
<STRING> {
96-
\" { yybegin(YYINITIAL); out.write("\"</span>"); }
97-
\\\\ { out.write("\\\\"); }
98-
\\\" { out.write("\\\""); }
117+
\\[\"\\] { out.write(htmlize(yytext())); }
118+
\" {
119+
out.write(htmlize(yytext()));
120+
yypop();
121+
}
99122
{WhspChar}*{EOL} {
100-
yybegin(YYINITIAL); out.write("</span>");
123+
yypop();
101124
startNewLine();
102125
}
103126
}
104127

105128
<QSTRING> {
106-
"\\\\" { out.write("\\\\"); }
107-
"\\\'" { out.write("\\\'"); }
108-
\' {WhiteSpace} \' { out.write(yytext()); }
109-
\' { yybegin(YYINITIAL); out.write("'</span>"); }
129+
\\[\'\\] { out.write(htmlize(yytext())); }
130+
\' {
131+
out.write(htmlize(yytext()));
132+
yypop();
133+
}
110134
{WhspChar}*{EOL} {
111-
yybegin(YYINITIAL); out.write("</span>");
135+
yypop();
112136
startNewLine();
113137
}
114138
}
115139

116140
<LSTRING> {
117-
\" {WhiteSpace} \" { out.write(yytext());}
118-
\"\"\" { yybegin(YYINITIAL); out.write("\"\"\"</span>"); }
119-
\\\\ { out.write("\\\\"); }
120-
\\\" { out.write("\\\""); }
141+
\\[\"\\] { out.write(htmlize(yytext()));}
142+
\"\"\" {
143+
out.write(htmlize(yytext()));
144+
yypop();
145+
}
121146
}
122147

123148
<LQSTRING> {
124-
"\\\\" { out.write("\\\\"); }
125-
"\\\'" { out.write("\\\'"); }
126-
\' {WhiteSpace} \' { out.write(yytext()); }
127-
\'\'\' { yybegin(YYINITIAL); out.write("'''</span>"); }
149+
\\[\'\\] { out.write(htmlize(yytext())); }
150+
\'\'\' {
151+
out.write(htmlize(yytext()));
152+
yypop();
153+
}
128154
}
129155

130156
<SCOMMENT> {
131157
{WhspChar}*{EOL} {
132-
yybegin(YYINITIAL); out.write("</span>");
158+
yypop();
133159
startNewLine();
134160
}
135161
}
136162

137-
138163
<YYINITIAL, STRING, SCOMMENT, QSTRING , LSTRING, LQSTRING> {
139-
"&" {out.write( "&amp;");}
140-
"<" {out.write( "&lt;");}
141-
">" {out.write( "&gt;");}
164+
[&<>\'\"] { out.write(htmlize(yytext())); }
142165
{WhspChar}*{EOL} { startNewLine(); }
143166
{WhiteSpace} { out.write(yytext()); }
144167
[!-~] { out.write(yycharat(0)); }

0 commit comments

Comments
 (0)