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

Commit 2c5fc70

Browse files
committed
Fix bobbylight#212: Add yaml syntax highlighting
1 parent e8cff64 commit 2c5fc70

File tree

5 files changed

+1085
-0
lines changed

5 files changed

+1085
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ protected void initTokenMakerMap() {
7575
putMapping(SYNTAX_STYLE_VISUAL_BASIC, pkg + "VisualBasicTokenMaker");
7676
putMapping(SYNTAX_STYLE_WINDOWS_BATCH, pkg + "WindowsBatchTokenMaker");
7777
putMapping(SYNTAX_STYLE_XML, pkg + "XMLTokenMaker");
78+
putMapping(SYNTAX_STYLE_YAML, pkg + "YamlTokenMaker");
7879

7980
}
8081

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,4 +305,10 @@ public interface SyntaxConstants {
305305
String SYNTAX_STYLE_XML = "text/xml";
306306

307307

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

0 commit comments

Comments
 (0)