From 872ff25bb86484dbac6dd9a6236c446cb59bdc3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Vehlow?= Date: Tue, 12 Dec 2017 22:09:21 +0100 Subject: [PATCH 1/3] Updated moo definitions for moo 0.4.3 --- types/moo/index.d.ts | 73 +++++++++++++++++++++++++----------------- types/moo/moo-tests.ts | 67 +++++++++++++++++++++++++++++++------- 2 files changed, 99 insertions(+), 41 deletions(-) diff --git a/types/moo/index.d.ts b/types/moo/index.d.ts index 8bdc583cc9..7b6938ad11 100644 --- a/types/moo/index.d.ts +++ b/types/moo/index.d.ts @@ -1,6 +1,7 @@ -// Type definitions for moo 0.3 +// Type definitions for moo 0.4.3 // Project: https://github.com/tjvr/moo#readme // Definitions by: Nikita Litvin +// Jörg Vehlow // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped export as namespace moo; @@ -14,32 +15,42 @@ export function compile(rules: Rules): Lexer; export function states(states: {[x: string]: Rules}, start?: string): Lexer; +export interface Rule { + match?: RegExp | string | string[], + /** + * Moo tracks detailed information about the input for you. + * It will track line numbers, as long as you apply the `lineBreaks: true` + * option to any tokens which might contain newlines. Moo will try to warn you if you forget to do this. + */ + lineBreaks?: boolean, + /** + * Moves the lexer to a new state, and pushes the old state onto the stack. + */ + push?: string, + /** + * Returns to a previous state, by removing one or more states from the stack. + */ + pop?: number, + /** + * Moves to a new state, but does not affect the stack. + */ + next?: string, + /** + * You can have a token type that both matches tokens and contains error values. + */ + error?: true, + /** + * Moo doesn't allow capturing groups, but you can supply a transform function, value(), + * which will be called on the value before storing it in the Token object. + */ + value?: (x: string) => string, + + keywords?: { + [x: string]: string | string[] + } +} export interface Rules { - [x: string]: RegExp | string | string[] | { - match: RegExp | string | string[], - /** - * Moo tracks detailed information about the input for you. - * It will track line numbers, as long as you apply the `lineBreaks: true` - * option to any tokens which might contain newlines. Moo will try to warn you if you forget to do this. - */ - lineBreaks?: boolean, - /** - * Moves the lexer to a new state, and pushes the old state onto the stack. - */ - push?: string, - /** - * Returns to a previous state, by removing one or more states from the stack. - */ - pop?: number, - /** - * Moves to a new state, but does not affect the stack. - */ - next?: string, - /** - * You can have a token type that both matches tokens and contains error values. - */ - error?: true - }; + [x: string]: RegExp | string | string[] | Rule; } export interface Lexer { @@ -59,12 +70,14 @@ export interface Lexer { /** * Empty the internal buffer of the lexer, and set the line, column, and offset counts back to their initial value. */ - reset(chunk: string, state?: LexerState): void; + reset(chunk?: string, state?: LexerState): void; /** * Returns current state, which you can later pass it as the second argument * to reset() to explicitly control the internal state of the lexer. */ save(): LexerState; + + [Symbol.iterator]: () => Iterator; } export interface Token { @@ -77,7 +90,7 @@ export interface Token { */ type?: string; /** - * The contents of the capturing group (or the whole match, if the token RegExp doesn't define a capture). + * The match contents. */ value: string; /** @@ -85,9 +98,9 @@ export interface Token { */ offset: number; /** - * The total length of the match (value may be shorter if you have capturing groups). + * The complete match. */ - size: number; + text: string; /** * The number of line breaks found in the match. (Always zero if this rule has lineBreaks: false.) */ diff --git a/types/moo/moo-tests.ts b/types/moo/moo-tests.ts index 52defb99ce..43f9a9f86b 100644 --- a/types/moo/moo-tests.ts +++ b/types/moo/moo-tests.ts @@ -2,28 +2,73 @@ import * as moo from 'moo'; let lexer = moo.compile({ lparen: '(', - word: /[a-z]+/, + word: /[a-z]+/, rparen: ')', keyword: ['while', 'if', 'else', 'moo', 'cows'] }); +lexer = moo.compile({ + WS: /[ \t]+/, + comment: /\/\/.*?$/, + number: /0|[1-9][0-9]*/, + string: /"(?:\\["\\]|[^\n"\\])*"/, + lparen: '(', + rparen: ')', + keyword: ['while', 'if', 'else', 'moo', 'cows'], + NL: { match: /\n/, lineBreaks: true }, +}); + +lexer = moo.compile({ + IDEN: { + match: /[a-zA-Z]+/, keywords: { + KW: ['while', 'if', 'else', 'moo', 'reloacows'] + } + }, + SPACE: { match: /\s+/, lineBreaks: true } +}); + +lexer = moo.compile({ + name: { + match: /[a-zA-Z]+/, keywords: { + 'kw-class': 'class', + 'kw-def': 'def', + 'kw-if': 'if', + } + } +}); + lexer = moo.states({ main: { - strstart: {match: '`', push: 'lit'}, - ident: /\w+/, - lbrace: {match: '{', push: 'main'}, - rbrace: {match: '}', pop: 1}, - colon: ':', - space: {match: /\s+/, lineBreaks: true}, + strstart: { match: '`', push: 'lit' }, + ident: /\w+/, + lbrace: { match: '{', push: 'main' }, + rbrace: { match: '}', pop: 1 }, + colon: ':', + space: { match: /\s+/, lineBreaks: true }, }, lit: { - interp: {match: '${', push: 'main'}, // tslint:disable-line no-invalid-template-strings - escape: /\\./, - strend: {match: '`', pop: 1}, - const: {match: /(?:[^$`]|\$(?!\{))+/, lineBreaks: true}, + interp: { match: '${', push: 'main' }, // tslint:disable-line no-invalid-template-strings + escape: /\\./, + strend: { match: '`', pop: 1 }, + const: { match: /(?:[^$`]|\$(?!\{))+/, lineBreaks: true }, }, }); +moo.compile({ + myError: moo.error +}); + +moo.compile({ + myError: { match: /[\$?`]/, error: true } +}); + +for (const here of lexer) { + const t: string = here.type; + const v: string = here.value; +} + +const tokens: moo.Token[] = Array.from(lexer); + lexer.reset('some line\n'); const info = lexer.save(); lexer.next(); From 2946765dcfe6655be3f97288db6e1d666067225b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Vehlow?= Date: Tue, 12 Dec 2017 22:33:45 +0100 Subject: [PATCH 2/3] fixed tslint errors --- types/moo/index.d.ts | 20 ++++++++++---------- types/moo/moo-tests.ts | 2 +- types/moo/tsconfig.json | 1 + 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/types/moo/index.d.ts b/types/moo/index.d.ts index 7b6938ad11..dbb6d81f5b 100644 --- a/types/moo/index.d.ts +++ b/types/moo/index.d.ts @@ -1,4 +1,4 @@ -// Type definitions for moo 0.4.3 +// Type definitions for moo 0.4 // Project: https://github.com/tjvr/moo#readme // Definitions by: Nikita Litvin // Jörg Vehlow @@ -16,38 +16,38 @@ export function compile(rules: Rules): Lexer; export function states(states: {[x: string]: Rules}, start?: string): Lexer; export interface Rule { - match?: RegExp | string | string[], + match?: RegExp | string | string[]; /** * Moo tracks detailed information about the input for you. * It will track line numbers, as long as you apply the `lineBreaks: true` * option to any tokens which might contain newlines. Moo will try to warn you if you forget to do this. */ - lineBreaks?: boolean, + lineBreaks?: boolean; /** * Moves the lexer to a new state, and pushes the old state onto the stack. */ - push?: string, + push?: string; /** * Returns to a previous state, by removing one or more states from the stack. */ - pop?: number, + pop?: number; /** * Moves to a new state, but does not affect the stack. */ - next?: string, + next?: string; /** * You can have a token type that both matches tokens and contains error values. */ - error?: true, + error?: true; /** * Moo doesn't allow capturing groups, but you can supply a transform function, value(), * which will be called on the value before storing it in the Token object. */ - value?: (x: string) => string, + value?: (x: string) => string; keywords?: { [x: string]: string | string[] - } + }; } export interface Rules { [x: string]: RegExp | string | string[] | Rule; @@ -77,7 +77,7 @@ export interface Lexer { */ save(): LexerState; - [Symbol.iterator]: () => Iterator; + [Symbol.iterator](): Iterator; } export interface Token { diff --git a/types/moo/moo-tests.ts b/types/moo/moo-tests.ts index 43f9a9f86b..6f0c18ae2d 100644 --- a/types/moo/moo-tests.ts +++ b/types/moo/moo-tests.ts @@ -63,7 +63,7 @@ moo.compile({ }); for (const here of lexer) { - const t: string = here.type; + const t: string | undefined = here.type; const v: string = here.value; } diff --git a/types/moo/tsconfig.json b/types/moo/tsconfig.json index 895fed83d5..e2659ef342 100644 --- a/types/moo/tsconfig.json +++ b/types/moo/tsconfig.json @@ -5,6 +5,7 @@ "es6", "dom" ], + "target": "es6", "noImplicitAny": true, "noImplicitThis": true, "strictNullChecks": true, From 439f9efd78a748d8cdf01963ef54011cfe82cba7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Vehlow?= Date: Tue, 12 Dec 2017 22:40:07 +0100 Subject: [PATCH 3/3] fixed last lint error --- types/moo/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/moo/index.d.ts b/types/moo/index.d.ts index dbb6d81f5b..9cf67c3299 100644 --- a/types/moo/index.d.ts +++ b/types/moo/index.d.ts @@ -40,7 +40,7 @@ export interface Rule { */ error?: true; /** - * Moo doesn't allow capturing groups, but you can supply a transform function, value(), + * Moo doesn't allow capturing groups, but you can supply a transform function, value(), * which will be called on the value before storing it in the Token object. */ value?: (x: string) => string;