Step 2: Implementation
Implement the custom tokens
Use the defineToken function to implement the tokens. This function takes the name of the token and its implementation and returns the implemented token.
Constant tokens
Implement the
constantfunctionthisin the function is aRegExpTokenthat contains the expression preceding the custom tokenThe token can append to, wrap around, or modify
thisin any way
const severity = defineToken('severity', {
constant(this: RegExpToken) {
// Append a constant expression
return this.oneOf`error``warning``info``debug`;
},
});
const matchAll = defineToken('matchAll', {
constant(this: RegExpToken) {
// Wrap around the existing expression
return lineStart.match(this).lineEnd;
},
});Dynamic tokens
Implement the
dynamicfunctionthisin the function is aRegExpTokenthat contains the expression preceding the custom tokenToken arguments are passed to the
dynamicfunction as argumentsTemplate string arguments are converted to ordinary strings automatically
Mixed tokens
Implement both the
constantanddynamicfunctionsSame rules apply for both functions
If the custom token is called, the
dynamicfunction will handle the call. Otherwise, theconstantfunction will be used.
Last updated