1
1
import ITokenMatcher from './ITokenMatcher'
2
2
import Dialect from './Dialect'
3
- import { Token , TokenType } from './Parser'
3
+ import { Token , TokenType } from './Parser'
4
4
import DIALECTS from './gherkin-languages.json'
5
- import { Item } from './IToken'
5
+ import { Item } from './IToken'
6
6
import * as messages from '@cucumber/messages'
7
- import { NoSuchLanguageException } from './Errors'
7
+ import { NoSuchLanguageException } from './Errors'
8
+ import { KeywordPrefixes } from "./flavors/KeywordPrefixes" ;
8
9
9
- const DIALECT_DICT : { [ key : string ] : Dialect } = DIALECTS
10
- const DEFAULT_DOC_STRING_SEPARATOR = / ^ ( ` ` ` [ ` ] * ) ( .* ) /
10
+ export const DIALECT_DICT : { [ key : string ] : Dialect } = DIALECTS
11
+ export const DEFAULT_DOC_STRING_SEPARATOR = / ^ ( ` ` ` [ ` ] * ) ( .* ) /
11
12
12
13
function addKeywordTypeMappings ( h : { [ key : string ] : messages . StepKeywordType [ ] } , keywords : readonly string [ ] , keywordType : messages . StepKeywordType ) {
13
14
for ( const k of keywords ) {
@@ -19,17 +20,23 @@ function addKeywordTypeMappings(h: { [key: string]: messages.StepKeywordType[] }
19
20
}
20
21
21
22
export default class GherkinInMarkdownTokenMatcher implements ITokenMatcher < TokenType > {
22
- private dialect : Dialect
23
- private dialectName : string
24
- private readonly nonStarStepKeywords : string [ ]
23
+ dialect : Dialect
24
+ dialectName : string
25
+ readonly nonStarStepKeywords : string [ ]
25
26
private readonly stepRegexp : RegExp
26
27
private readonly headerRegexp : RegExp
27
28
private activeDocStringSeparator : RegExp
28
29
private indentToRemove : number
29
- private matchedFeatureLine : boolean
30
+ matchedFeatureLine : boolean
31
+ private prefixes : KeywordPrefixes = {
32
+ // https://spec.commonmark.org/0.29/#bullet-list-marker
33
+ BULLET : '^(\\s*[*+-]\\s*)' ,
34
+ HEADER : '^(#{1,6}\\s)' ,
35
+ }
30
36
private keywordTypesMap : { [ key : string ] : messages . StepKeywordType [ ] }
31
37
32
- constructor ( private readonly defaultDialectName : string = 'en' ) {
38
+ constructor ( private readonly defaultDialectName : string = 'en' , prefixes ?: KeywordPrefixes ) {
39
+ prefixes ? this . prefixes = prefixes : null ;
33
40
this . dialect = DIALECT_DICT [ defaultDialectName ]
34
41
this . nonStarStepKeywords = [ ]
35
42
. concat ( this . dialect . given )
@@ -41,7 +48,7 @@ export default class GherkinInMarkdownTokenMatcher implements ITokenMatcher<Toke
41
48
this . initializeKeywordTypes ( )
42
49
43
50
this . stepRegexp = new RegExp (
44
- `${ KeywordPrefix . BULLET } (${ this . nonStarStepKeywords . map ( escapeRegExp ) . join ( '|' ) } )`
51
+ `${ this . prefixes . BULLET } (${ this . nonStarStepKeywords . map ( escapeRegExp ) . join ( '|' ) } )`
45
52
)
46
53
47
54
const headerKeywords = [ ]
@@ -54,7 +61,7 @@ export default class GherkinInMarkdownTokenMatcher implements ITokenMatcher<Toke
54
61
. filter ( ( value , index , self ) => self . indexOf ( value ) === index )
55
62
56
63
this . headerRegexp = new RegExp (
57
- `${ KeywordPrefix . HEADER } (${ headerKeywords . map ( escapeRegExp ) . join ( '|' ) } )`
64
+ `${ this . prefixes . HEADER } (${ headerKeywords . map ( escapeRegExp ) . join ( '|' ) } )`
58
65
)
59
66
60
67
this . reset ( )
@@ -171,7 +178,7 @@ export default class GherkinInMarkdownTokenMatcher implements ITokenMatcher<Toke
171
178
}
172
179
// We first try to match "# Feature: blah"
173
180
let result = this . matchTitleLine (
174
- KeywordPrefix . HEADER ,
181
+ this . prefixes . HEADER ,
175
182
this . dialect . feature ,
176
183
':' ,
177
184
token ,
@@ -191,7 +198,7 @@ export default class GherkinInMarkdownTokenMatcher implements ITokenMatcher<Toke
191
198
192
199
match_BackgroundLine ( token : Token ) : boolean {
193
200
return this . matchTitleLine (
194
- KeywordPrefix . HEADER ,
201
+ this . prefixes . HEADER ,
195
202
this . dialect . background ,
196
203
':' ,
197
204
token ,
@@ -201,7 +208,7 @@ export default class GherkinInMarkdownTokenMatcher implements ITokenMatcher<Toke
201
208
202
209
match_RuleLine ( token : Token ) : boolean {
203
210
return this . matchTitleLine (
204
- KeywordPrefix . HEADER ,
211
+ this . prefixes . HEADER ,
205
212
this . dialect . rule ,
206
213
':' ,
207
214
token ,
@@ -212,14 +219,14 @@ export default class GherkinInMarkdownTokenMatcher implements ITokenMatcher<Toke
212
219
match_ScenarioLine ( token : Token ) : boolean {
213
220
return (
214
221
this . matchTitleLine (
215
- KeywordPrefix . HEADER ,
222
+ this . prefixes . HEADER ,
216
223
this . dialect . scenario ,
217
224
':' ,
218
225
token ,
219
226
TokenType . ScenarioLine
220
227
) ||
221
228
this . matchTitleLine (
222
- KeywordPrefix . HEADER ,
229
+ this . prefixes . HEADER ,
223
230
this . dialect . scenarioOutline ,
224
231
':' ,
225
232
token ,
@@ -230,7 +237,7 @@ export default class GherkinInMarkdownTokenMatcher implements ITokenMatcher<Toke
230
237
231
238
match_ExamplesLine ( token : Token ) : boolean {
232
239
return this . matchTitleLine (
233
- KeywordPrefix . HEADER ,
240
+ this . prefixes . HEADER ,
234
241
this . dialect . examples ,
235
242
':' ,
236
243
token ,
@@ -240,7 +247,7 @@ export default class GherkinInMarkdownTokenMatcher implements ITokenMatcher<Toke
240
247
241
248
match_StepLine ( token : Token ) : boolean {
242
249
return this . matchTitleLine (
243
- KeywordPrefix . BULLET ,
250
+ this . prefixes . BULLET ,
244
251
this . nonStarStepKeywords ,
245
252
'' ,
246
253
token ,
@@ -249,7 +256,7 @@ export default class GherkinInMarkdownTokenMatcher implements ITokenMatcher<Toke
249
256
}
250
257
251
258
matchTitleLine (
252
- prefix : KeywordPrefix ,
259
+ prefix : string ,
253
260
keywords : readonly string [ ] ,
254
261
keywordSuffix : ':' | '' ,
255
262
token : Token ,
@@ -337,12 +344,6 @@ export default class GherkinInMarkdownTokenMatcher implements ITokenMatcher<Toke
337
344
}
338
345
}
339
346
340
- enum KeywordPrefix {
341
- // https://spec.commonmark.org/0.29/#bullet-list-marker
342
- BULLET = '^(\\s*[*+-]\\s*)' ,
343
- HEADER = '^(#{1,6}\\s)' ,
344
- }
345
-
346
347
// https://stackoverflow.com/questions/3115150/how-to-escape-regular-expression-special-characters-using-javascript
347
348
function escapeRegExp ( text : string ) {
348
349
return text . replace ( / [ - [ \] { } ( ) * + ? . , \\ ^ $ | # \s ] / g, '\\$&' )
0 commit comments