11import ITokenMatcher from './ITokenMatcher'
22import Dialect from './Dialect'
3- import { Token , TokenType } from './Parser'
3+ import { Token , TokenType } from './Parser'
44import DIALECTS from './gherkin-languages.json'
5- import { Item } from './IToken'
5+ import { Item } from './IToken'
66import * as messages from '@cucumber/messages'
7- import { NoSuchLanguageException } from './Errors'
7+ import { NoSuchLanguageException } from './Errors'
8+ import { KeywordPrefixes } from "./flavors/KeywordPrefixes" ;
89
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 = / ^ ( ` ` ` [ ` ] * ) ( .* ) /
1112
1213function addKeywordTypeMappings ( h : { [ key : string ] : messages . StepKeywordType [ ] } , keywords : readonly string [ ] , keywordType : messages . StepKeywordType ) {
1314 for ( const k of keywords ) {
@@ -19,17 +20,23 @@ function addKeywordTypeMappings(h: { [key: string]: messages.StepKeywordType[] }
1920}
2021
2122export 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 [ ]
2526 private readonly stepRegexp : RegExp
2627 private readonly headerRegexp : RegExp
2728 private activeDocStringSeparator : RegExp
2829 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+ }
3036 private keywordTypesMap : { [ key : string ] : messages . StepKeywordType [ ] }
3137
32- constructor ( private readonly defaultDialectName : string = 'en' ) {
38+ constructor ( private readonly defaultDialectName : string = 'en' , prefixes ?: KeywordPrefixes ) {
39+ prefixes ? this . prefixes = prefixes : null ;
3340 this . dialect = DIALECT_DICT [ defaultDialectName ]
3441 this . nonStarStepKeywords = [ ]
3542 . concat ( this . dialect . given )
@@ -41,7 +48,7 @@ export default class GherkinInMarkdownTokenMatcher implements ITokenMatcher<Toke
4148 this . initializeKeywordTypes ( )
4249
4350 this . stepRegexp = new RegExp (
44- `${ KeywordPrefix . BULLET } (${ this . nonStarStepKeywords . map ( escapeRegExp ) . join ( '|' ) } )`
51+ `${ this . prefixes . BULLET } (${ this . nonStarStepKeywords . map ( escapeRegExp ) . join ( '|' ) } )`
4552 )
4653
4754 const headerKeywords = [ ]
@@ -54,7 +61,7 @@ export default class GherkinInMarkdownTokenMatcher implements ITokenMatcher<Toke
5461 . filter ( ( value , index , self ) => self . indexOf ( value ) === index )
5562
5663 this . headerRegexp = new RegExp (
57- `${ KeywordPrefix . HEADER } (${ headerKeywords . map ( escapeRegExp ) . join ( '|' ) } )`
64+ `${ this . prefixes . HEADER } (${ headerKeywords . map ( escapeRegExp ) . join ( '|' ) } )`
5865 )
5966
6067 this . reset ( )
@@ -171,7 +178,7 @@ export default class GherkinInMarkdownTokenMatcher implements ITokenMatcher<Toke
171178 }
172179 // We first try to match "# Feature: blah"
173180 let result = this . matchTitleLine (
174- KeywordPrefix . HEADER ,
181+ this . prefixes . HEADER ,
175182 this . dialect . feature ,
176183 ':' ,
177184 token ,
@@ -191,7 +198,7 @@ export default class GherkinInMarkdownTokenMatcher implements ITokenMatcher<Toke
191198
192199 match_BackgroundLine ( token : Token ) : boolean {
193200 return this . matchTitleLine (
194- KeywordPrefix . HEADER ,
201+ this . prefixes . HEADER ,
195202 this . dialect . background ,
196203 ':' ,
197204 token ,
@@ -201,7 +208,7 @@ export default class GherkinInMarkdownTokenMatcher implements ITokenMatcher<Toke
201208
202209 match_RuleLine ( token : Token ) : boolean {
203210 return this . matchTitleLine (
204- KeywordPrefix . HEADER ,
211+ this . prefixes . HEADER ,
205212 this . dialect . rule ,
206213 ':' ,
207214 token ,
@@ -212,14 +219,14 @@ export default class GherkinInMarkdownTokenMatcher implements ITokenMatcher<Toke
212219 match_ScenarioLine ( token : Token ) : boolean {
213220 return (
214221 this . matchTitleLine (
215- KeywordPrefix . HEADER ,
222+ this . prefixes . HEADER ,
216223 this . dialect . scenario ,
217224 ':' ,
218225 token ,
219226 TokenType . ScenarioLine
220227 ) ||
221228 this . matchTitleLine (
222- KeywordPrefix . HEADER ,
229+ this . prefixes . HEADER ,
223230 this . dialect . scenarioOutline ,
224231 ':' ,
225232 token ,
@@ -230,7 +237,7 @@ export default class GherkinInMarkdownTokenMatcher implements ITokenMatcher<Toke
230237
231238 match_ExamplesLine ( token : Token ) : boolean {
232239 return this . matchTitleLine (
233- KeywordPrefix . HEADER ,
240+ this . prefixes . HEADER ,
234241 this . dialect . examples ,
235242 ':' ,
236243 token ,
@@ -240,7 +247,7 @@ export default class GherkinInMarkdownTokenMatcher implements ITokenMatcher<Toke
240247
241248 match_StepLine ( token : Token ) : boolean {
242249 return this . matchTitleLine (
243- KeywordPrefix . BULLET ,
250+ this . prefixes . BULLET ,
244251 this . nonStarStepKeywords ,
245252 '' ,
246253 token ,
@@ -249,7 +256,7 @@ export default class GherkinInMarkdownTokenMatcher implements ITokenMatcher<Toke
249256 }
250257
251258 matchTitleLine (
252- prefix : KeywordPrefix ,
259+ prefix : string ,
253260 keywords : readonly string [ ] ,
254261 keywordSuffix : ':' | '' ,
255262 token : Token ,
@@ -337,12 +344,6 @@ export default class GherkinInMarkdownTokenMatcher implements ITokenMatcher<Toke
337344 }
338345}
339346
340- enum KeywordPrefix {
341- // https://spec.commonmark.org/0.29/#bullet-list-marker
342- BULLET = '^(\\s*[*+-]\\s*)' ,
343- HEADER = '^(#{1,6}\\s)' ,
344- }
345-
346347// https://stackoverflow.com/questions/3115150/how-to-escape-regular-expression-special-characters-using-javascript
347348function escapeRegExp ( text : string ) {
348349 return text . replace ( / [ - [ \] { } ( ) * + ? . , \\ ^ $ | # \s ] / g, '\\$&' )
0 commit comments