Merge pull request #32574 from akshaykarthik/moo-0.5

[moo 0.5] Add type definitions for keywords value transform
This commit is contained in:
Pranav Senthilnathan
2019-01-29 11:10:23 -08:00
committed by GitHub
2 changed files with 20 additions and 8 deletions
+15 -4
View File
@@ -1,4 +1,4 @@
// Type definitions for moo 0.4
// Type definitions for moo 0.5
// Project: https://github.com/tjvr/moo#readme
// Definitions by: Nikita Litvin <https://github.com/deltaidea>
// Jörg Vehlow <https://github.com/MofX>
@@ -11,6 +11,15 @@ export as namespace moo;
*/
export const error: { error: true };
/**
* Reserved token for indicating a fallback rule.
*/
export const fallback: { fallback: true };
export type TypeMapper = (x: string) => string;
export function keywords(kws: {[k: string]: string | string[]}): TypeMapper;
export function compile(rules: Rules): Lexer;
export function states(states: {[x: string]: Rules}, start?: string): Lexer;
@@ -45,9 +54,11 @@ export interface Rule {
*/
value?: (x: string) => string;
keywords?: {
[x: string]: string | string[]
};
/**
* Used for mapping one set of types to another.
* See https://github.com/no-context/moo#keywords for an example
*/
type?: TypeMapper;
}
export interface Rules {
[x: string]: RegExp | string | string[] | Rule | Rule[];
+5 -4
View File
@@ -20,20 +20,21 @@ lexer = moo.compile({
lexer = moo.compile({
IDEN: {
match: /[a-zA-Z]+/, keywords: {
match: /[a-zA-Z]+/,
type: moo.keywords({
KW: ['while', 'if', 'else', 'moo', 'reloacows']
}
})
},
SPACE: { match: /\s+/, lineBreaks: true }
});
lexer = moo.compile({
name: {
match: /[a-zA-Z]+/, keywords: {
match: /[a-zA-Z]+/, type: moo.keywords({
'kw-class': 'class',
'kw-def': 'def',
'kw-if': 'if',
}
})
}
});