Skip to content

Commit 6c9df11

Browse files
committed
chore: clean up language lexer
1 parent 2dfa563 commit 6c9df11

File tree

1 file changed

+24
-24
lines changed

1 file changed

+24
-24
lines changed

src/main/kotlin/com/github/lppedd/cc/language/lexer/conventionalCommit.flex

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.github.lppedd.cc.language.lexer;
22

3-
import org.jetbrains.annotations.NotNull;
4-
53
import com.intellij.psi.PlainTextTokenTypes;
64
import com.intellij.psi.TokenType;
75
import com.intellij.psi.tree.IElementType;
@@ -30,7 +28,6 @@ import com.intellij.psi.tree.IElementType;
3028
this.isEof = isEof;
3129
}
3230

33-
@NotNull
3431
private IElementType getFooterType() {
3532
final String text = yytext().toString().trim();
3633
return "BREAKING CHANGE".equals(text) || "BREAKING-CHANGE".equals(text)
@@ -40,8 +37,9 @@ import com.intellij.psi.tree.IElementType;
4037

4138
%}
4239

43-
NewLine = \r\n | \r | \n
44-
Space = [ \t]
40+
NL = \r\n | \r | \n
41+
WS = [ \t]
42+
4543
FooterType = [^:\s]+ | BREAKING\ CHANGE
4644

4745
%state TYPE
@@ -63,16 +61,16 @@ FooterType = [^:\s]+ | BREAKING\ CHANGE
6361
}
6462

6563
<TYPE, SCOPE, SUMMARY_SEPARATOR, SUBJECT> {
66-
{NewLine} {
64+
{NL} {
6765
yybegin(BODY_OR_FOOTERS);
6866
return TokenType.WHITE_SPACE;
6967
}
7068

71-
\! / : {
69+
"!" / ":" {
7270
return ConventionalCommitTokenType.BREAKING_CHANGE;
7371
}
7472

75-
: {
73+
":" {
7674
yybegin(SUBJECT);
7775
return ConventionalCommitTokenType.SEPARATOR;
7876
}
@@ -92,11 +90,11 @@ FooterType = [^:\s]+ | BREAKING\ CHANGE
9290
// We lex it as BREAKING_CHANGE so that we do not fail lexing
9391
// and then let the parser or inspections reason about it.
9492
// Note that this rule must have lower priority than '\! / :'.
95-
\! {
93+
"!" {
9694
return ConventionalCommitTokenType.BREAKING_CHANGE;
9795
}
9896

99-
\( {
97+
"(" {
10098
yybegin(SCOPE);
10199
return ConventionalCommitTokenType.SCOPE_OPEN_PAREN;
102100
}
@@ -107,13 +105,14 @@ FooterType = [^:\s]+ | BREAKING\ CHANGE
107105
return ConventionalCommitTokenType.SCOPE;
108106
}
109107

110-
\) {
108+
")" {
111109
yybegin(SUMMARY_SEPARATOR);
112110
return ConventionalCommitTokenType.SCOPE_CLOSE_PAREN;
113111
}
114112
}
115113

116114
<SUBJECT> {
115+
// A subject is any text up to a newline character
117116
[^\r\n]+ {
118117
yybegin(TYPE);
119118
return ConventionalCommitTokenType.SUBJECT;
@@ -122,25 +121,25 @@ FooterType = [^:\s]+ | BREAKING\ CHANGE
122121

123122
<BODY_OR_FOOTERS> {
124123
// Closes: #16
125-
^{FooterType}{Space}*: {
124+
^{FooterType}{WS}*: {
126125
// The ':' char should not be part of the footer type
127126
yypushback(1);
128127
yybegin(FOOTERS);
129128
return getFooterType();
130129
}
131130

132131
// Closes #16
133-
^{FooterType}{Space}+ / #.* {
132+
^{FooterType}{WS}+ / #.* {
134133
yybegin(FOOTER_VALUE);
135134
return getFooterType();
136135
}
137136

138137
// Skip all blank lines after the summary
139-
^{Space}*{NewLine} {
138+
^{WS}*{NL} {
140139
return TokenType.WHITE_SPACE;
141140
}
142141

143-
^{Space}+ {
142+
^{WS}+ {
144143
yypushback(yylength());
145144
yybegin(BODY);
146145
}
@@ -152,16 +151,17 @@ FooterType = [^:\s]+ | BREAKING\ CHANGE
152151
}
153152

154153
<BODY> {
155-
{NewLine}{Space}*{NewLine} ({FooterType}{Space}*: | {FooterType}{Space}+#) {
154+
{NL}{WS}*{NL} ({FooterType}{WS}*: | {FooterType}{WS}+#) {
156155
yypushback(yylength());
157156
yybegin(FOOTERS);
158157
return ConventionalCommitTokenType.BODY;
159158
}
160159

161-
[^] | {Space}+ {
160+
[^] | {WS}+ {
162161
// Skip
163162
}
164163

164+
// Match body until EOF
165165
<<EOF>> {
166166
if (isEof()) {
167167
return null;
@@ -174,18 +174,18 @@ FooterType = [^:\s]+ | BREAKING\ CHANGE
174174

175175
<FOOTERS> {
176176
// Closes: #16
177-
^{FooterType}{Space}*: {
177+
^{FooterType}{WS}*: {
178178
yypushback(1);
179179
return getFooterType();
180180
}
181181

182182
// Closes #16
183-
^{FooterType}{Space}+ / #.* {
183+
^{FooterType}{WS}+ / #.* {
184184
yybegin(FOOTER_VALUE);
185185
return getFooterType();
186186
}
187187

188-
: {
188+
":" {
189189
yybegin(FOOTER_VALUE);
190190
return ConventionalCommitTokenType.FOOTER_SEPARATOR;
191191
}
@@ -195,21 +195,21 @@ FooterType = [^:\s]+ | BREAKING\ CHANGE
195195
// Closes #16 | .+
196196
// multiline footer | ({NewLine}{Space}+([^\s]+{Space}*)+)*
197197
// value | ({NewLine}{Space}+([^\s]+{Space}*)+)*
198-
.+ ({NewLine}{Space}+([^\s]+{Space}*)+)* {
198+
.+ ({NL}{WS}+([^\s]+{WS}*)+)* {
199199
return ConventionalCommitTokenType.FOOTER_VALUE;
200200
}
201201

202-
{NewLine} {
202+
{NL} {
203203
yybegin(FOOTERS);
204204
return TokenType.WHITE_SPACE;
205205
}
206206
}
207207

208-
{Space} {
208+
{WS} {
209209
return TokenType.WHITE_SPACE;
210210
}
211211

212-
{NewLine} {
212+
{NL} {
213213
return TokenType.WHITE_SPACE;
214214
}
215215

0 commit comments

Comments
 (0)