Skip to content
This repository was archived by the owner on Aug 10, 2022. It is now read-only.

Commit 7421b4d

Browse files
committed
Fix bobbylight#210: Adding .ini file syntax highlighting
1 parent 100abcb commit 7421b4d

File tree

6 files changed

+1022
-1
lines changed

6 files changed

+1022
-1
lines changed

.classpath

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
<attribute name="FROM_GRADLE_MODEL" value="true"/>
2121
</attributes>
2222
</classpathentry>
23-
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/jdk1.6.0_45 (32)"/>
2423
<classpathentry kind="con" path="org.eclipse.buildship.core.gradleclasspathcontainer"/>
24+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
2525
<classpathentry kind="output" path="bin"/>
2626
</classpath>

src/main/java/org/fife/ui/rsyntaxtextarea/DefaultTokenMakerFactory.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ protected void initTokenMakerMap() {
4848
putMapping(SYNTAX_STYLE_HOSTS, pkg + "HostsTokenMaker");
4949
putMapping(SYNTAX_STYLE_HTACCESS, pkg + "HtaccessTokenMaker");
5050
putMapping(SYNTAX_STYLE_HTML, pkg + "HTMLTokenMaker");
51+
putMapping(SYNTAX_STYLE_INI, pkg + "IniTokenMaker");
5152
putMapping(SYNTAX_STYLE_JAVA, pkg + "JavaTokenMaker");
5253
putMapping(SYNTAX_STYLE_JAVASCRIPT, pkg + "JavaScriptTokenMaker");
5354
putMapping(SYNTAX_STYLE_JSON_WITH_COMMENTS, pkg + "JshintrcTokenMaker");

src/main/java/org/fife/ui/rsyntaxtextarea/SyntaxConstants.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,12 @@ public interface SyntaxConstants {
142142
String SYNTAX_STYLE_HTML = "text/html";
143143

144144

145+
/**
146+
* Style for highlighting INI files.
147+
*/
148+
String SYNTAX_STYLE_INI = "text/ini";
149+
150+
145151
/**
146152
* Style for highlighting Java.
147153
*/
Lines changed: 225 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
1+
/*
2+
* 11/04/2016
3+
*
4+
* IniTokenMaker.java - Scanner for .ini files.
5+
*
6+
* This library is distributed under a modified BSD license. See the included
7+
* RSyntaxTextArea.License.txt file for details.
8+
*/
9+
package org.fife.ui.rsyntaxtextarea.modes;
10+
11+
import java.io.*;
12+
import javax.swing.text.Segment;
13+
14+
import org.fife.ui.rsyntaxtextarea.*;
15+
16+
17+
/**
18+
* This class splits up text into tokens representing a .ini file.<p>
19+
*
20+
* This implementation was created using
21+
* <a href="http://www.jflex.de/">JFlex</a> 1.4.1; however, the generated file
22+
* was modified for performance. Memory allocation needs to be almost
23+
* completely removed to be competitive with the handwritten lexers (subclasses
24+
* of <code>AbstractTokenMaker</code>, so this class has been modified so that
25+
* Strings are never allocated (via yytext()), and the scanner never has to
26+
* worry about refilling its buffer (needlessly copying chars around).
27+
* We can achieve this because RText always scans exactly 1 line of tokens at a
28+
* time, and hands the scanner this line as an array of characters (a Segment
29+
* really). Since tokens contain pointers to char arrays instead of Strings
30+
* holding their contents, there is no need for allocating new memory for
31+
* Strings.<p>
32+
*
33+
* The actual algorithm generated for scanning has, of course, not been
34+
* modified.<p>
35+
*
36+
* If you wish to regenerate this file yourself, keep in mind the following:
37+
* <ul>
38+
* <li>The generated <code>IniTokenMaker.java</code> file will
39+
* contain two definitions of both <code>zzRefill</code> and
40+
* <code>yyreset</code>. You should hand-delete the second of each
41+
* definition (the ones generated by the lexer), as these generated
42+
* methods modify the input buffer, which we'll never have to do.
43+
* <li>You should also change the declaration/definition of zzBuffer to NOT
44+
* be initialized. This is a needless memory allocation for us since we
45+
* will be pointing the array somewhere else anyway.
46+
* <li>You should NOT call <code>yylex()</code> on the generated scanner
47+
* directly; rather, you should use <code>getTokenList</code> as you would
48+
* with any other <code>TokenMaker</code> instance.
49+
* </ul>
50+
*
51+
* @author Robert Futrell
52+
* @version 0.4
53+
*
54+
*/
55+
%%
56+
57+
%public
58+
%class IniTokenMaker
59+
%extends AbstractJFlexTokenMaker
60+
%unicode
61+
%type org.fife.ui.rsyntaxtextarea.Token
62+
63+
64+
%{
65+
66+
67+
/**
68+
* Constructor. This must be here because JFlex does not generate a
69+
* no-parameter constructor.
70+
*/
71+
public IniTokenMaker() {
72+
super();
73+
}
74+
75+
76+
/**
77+
* Adds the token specified to the current linked list of tokens.
78+
*
79+
* @param tokenType The token's type.
80+
*/
81+
private void addToken(int tokenType) {
82+
addToken(zzStartRead, zzMarkedPos-1, tokenType);
83+
}
84+
85+
86+
/**
87+
* Adds the token specified to the current linked list of tokens.
88+
*
89+
* @param tokenType The token's type.
90+
*/
91+
private void addToken(int start, int end, int tokenType) {
92+
int so = start + offsetShift;
93+
addToken(zzBuffer, start,end, tokenType, so);
94+
}
95+
96+
97+
/**
98+
* Adds the token specified to the current linked list of tokens.
99+
*
100+
* @param array The character array.
101+
* @param start The starting offset in the array.
102+
* @param end The ending offset in the array.
103+
* @param tokenType The token's type.
104+
* @param startOffset The offset in the document at which this token
105+
* occurs.
106+
*/
107+
@Override
108+
public void addToken(char[] array, int start, int end, int tokenType, int startOffset) {
109+
super.addToken(array, start,end, tokenType, startOffset);
110+
zzStartRead = zzMarkedPos;
111+
}
112+
113+
114+
/**
115+
* {@inheritDoc}
116+
*/
117+
@Override
118+
public String[] getLineCommentStartAndEnd(int languageIndex) {
119+
return new String[] { ";", null };
120+
}
121+
122+
123+
/**
124+
* Returns the first token in the linked list of tokens generated
125+
* from <code>text</code>. This method must be implemented by
126+
* subclasses so they can correctly implement syntax highlighting.
127+
*
128+
* @param text The text from which to get tokens.
129+
* @param initialTokenType The token type we should start with.
130+
* @param startOffset The offset into the document at which
131+
* <code>text</code> starts.
132+
* @return The first <code>Token</code> in a linked list representing
133+
* the syntax highlighted text.
134+
*/
135+
@Override
136+
public Token getTokenList(Segment text, int initialTokenType, int startOffset) {
137+
138+
resetTokenList();
139+
this.offsetShift = -text.offset + startOffset;
140+
141+
// Start off in the proper state.
142+
int state = YYINITIAL;
143+
s = text;
144+
try {
145+
yyreset(zzReader);
146+
yybegin(state);
147+
return yylex();
148+
} catch (IOException ioe) {
149+
ioe.printStackTrace();
150+
return new TokenImpl();
151+
}
152+
153+
}
154+
155+
156+
/**
157+
* Refills the input buffer.
158+
*
159+
* @return <code>true</code> if EOF was reached, otherwise
160+
* <code>false</code>.
161+
*/
162+
private boolean zzRefill() {
163+
return zzCurrentPos>=s.offset+s.count;
164+
}
165+
166+
167+
/**
168+
* Resets the scanner to read from a new input stream.
169+
* Does not close the old reader.
170+
*
171+
* All internal variables are reset, the old input stream
172+
* <b>cannot</b> be reused (internal buffer is discarded and lost).
173+
* Lexical state is set to <tt>YY_INITIAL</tt>.
174+
*
175+
* @param reader the new input stream
176+
*/
177+
public final void yyreset(Reader reader) {
178+
// 's' has been updated.
179+
zzBuffer = s.array;
180+
/*
181+
* We replaced the line below with the two below it because zzRefill
182+
* no longer "refills" the buffer (since the way we do it, it's always
183+
* "full" the first time through, since it points to the segment's
184+
* array). So, we assign zzEndRead here.
185+
*/
186+
//zzStartRead = zzEndRead = s.offset;
187+
zzStartRead = s.offset;
188+
zzEndRead = zzStartRead + s.count - 1;
189+
zzCurrentPos = zzMarkedPos = zzPushbackPos = s.offset;
190+
zzLexicalState = YYINITIAL;
191+
zzReader = reader;
192+
zzAtBOL = true;
193+
zzAtEOF = false;
194+
}
195+
196+
197+
%}
198+
199+
Equals = ([=])
200+
Identifier = ([^ \t\n#;\[=]*)
201+
Whitespace = ([ \t]+)
202+
Comment = ([#;].*)
203+
Section = ([\[][^\]]*[\]]?)
204+
205+
%state VALUE
206+
207+
%%
208+
209+
<YYINITIAL> {
210+
{Identifier} { addToken(Token.DATA_TYPE); }
211+
{Equals} { start = zzMarkedPos; addToken(Token.OPERATOR); yybegin(VALUE); }
212+
{Whitespace} { addToken(Token.WHITESPACE); }
213+
{Comment} { addToken(Token.COMMENT_EOL); }
214+
{Section} { addToken(Token.PREPROCESSOR); }
215+
<<EOF>> { addNullToken(); return firstToken; }
216+
}
217+
218+
<VALUE> {
219+
{Identifier} { addToken(Token.IDENTIFIER); }
220+
{Equals} { start = zzMarkedPos; addToken(Token.OPERATOR); }
221+
{Whitespace} { addToken(Token.WHITESPACE); }
222+
{Comment} { addToken(Token.COMMENT_EOL); }
223+
{Section} { addToken(Token.PREPROCESSOR); }
224+
<<EOF>> { addNullToken(); return firstToken; }
225+
}

0 commit comments

Comments
 (0)