From 1ddc7d4e7908caab71279ef5fba0a63c22ffdd15 Mon Sep 17 00:00:00 2001 From: Alex Dima Date: Thu, 15 Jun 2017 16:20:11 +0200 Subject: [PATCH] Fix TS 2.4 monarch issues --- .../common/modes/monarch/monarchCommon.ts | 26 ++++++++++++++++--- .../common/modes/monarch/monarchCompile.ts | 8 +++--- .../common/modes/monarch/monarchLexer.ts | 12 ++++----- 3 files changed, 32 insertions(+), 14 deletions(-) diff --git a/src/vs/editor/common/modes/monarch/monarchCommon.ts b/src/vs/editor/common/modes/monarch/monarchCommon.ts index fd72d1d0cad..070d3572d34 100644 --- a/src/vs/editor/common/modes/monarch/monarchCommon.ts +++ b/src/vs/editor/common/modes/monarch/monarchCommon.ts @@ -46,19 +46,37 @@ export interface IBracket { close: string; } +export type FuzzyAction = IAction | string; + +export function isFuzzyActionArr(what: FuzzyAction | FuzzyAction[]): what is FuzzyAction[] { + return (Array.isArray(what)); +} + +export function isFuzzyAction(what: FuzzyAction | FuzzyAction[]): what is FuzzyAction { + return !isFuzzyActionArr(what); +} + +export function isString(what: FuzzyAction): what is string { + return (typeof what === 'string'); +} + +export function isIAction(what: FuzzyAction): what is IAction { + return !isString(what); +} + export interface IRule { regex: RegExp; - action: IAction; + action: FuzzyAction; matchOnlyAtLineStart: boolean; name: string; } export interface IAction { // an action is either a group of actions - group?: IAction[]; + group?: FuzzyAction[]; // or a function that returns a fresh action - test?: (id: string, matches: string[], state: string, eos: boolean) => IAction; + test?: (id: string, matches: string[], state: string, eos: boolean) => FuzzyAction; // or it is a declarative action with a token value and various other attributes token?: string; @@ -74,7 +92,7 @@ export interface IAction { export interface IBranch { name: string; - value: IAction; + value: FuzzyAction; test: (id: string, matches: string[], state: string, eos: boolean) => boolean; } diff --git a/src/vs/editor/common/modes/monarch/monarchCompile.ts b/src/vs/editor/common/modes/monarch/monarchCompile.ts index 7f5fc6ebf60..7a41ee1a0be 100644 --- a/src/vs/editor/common/modes/monarch/monarchCompile.ts +++ b/src/vs/editor/common/modes/monarch/monarchCompile.ts @@ -118,7 +118,7 @@ function selectScrutinee(id: string, matches: string[], state: string, num: numb return null; } -function createGuard(lexer: monarchCommon.ILexerMin, ruleName: string, tkey: string, val: monarchCommon.IAction): monarchCommon.IBranch { +function createGuard(lexer: monarchCommon.ILexerMin, ruleName: string, tkey: string, val: monarchCommon.FuzzyAction): monarchCommon.IBranch { // get the scrutinee and pattern var scrut = -1; // -1: $!, 0-99: $n, 100+n: $Sn var oppat = tkey; @@ -222,7 +222,7 @@ function createGuard(lexer: monarchCommon.ILexerMin, ruleName: string, tkey: str * contains user functions as actions (which is usually not allowed), then this * may be called during lexing. It is important therefore to compile common cases efficiently */ -function compileAction(lexer: monarchCommon.ILexerMin, ruleName: string, action: any): monarchCommon.IAction { +function compileAction(lexer: monarchCommon.ILexerMin, ruleName: string, action: any): monarchCommon.FuzzyAction { if (!action) { return { token: '' }; } @@ -285,7 +285,7 @@ function compileAction(lexer: monarchCommon.ILexerMin, ruleName: string, action: } } else if (Array.isArray(action)) { - var results = []; + var results: monarchCommon.FuzzyAction[] = []; var idx: string; for (idx in action) { if (action.hasOwnProperty(idx)) { @@ -345,7 +345,7 @@ function compileAction(lexer: monarchCommon.ILexerMin, ruleName: string, action: */ class Rule implements monarchCommon.IRule { public regex: RegExp = new RegExp(''); - public action: monarchCommon.IAction = { token: '' }; + public action: monarchCommon.FuzzyAction = { token: '' }; public matchOnlyAtLineStart: boolean = false; public name: string = ''; diff --git a/src/vs/editor/common/modes/monarch/monarchLexer.ts b/src/vs/editor/common/modes/monarch/monarchLexer.ts index bc6f6381138..daf73aae631 100644 --- a/src/vs/editor/common/modes/monarch/monarchLexer.ts +++ b/src/vs/editor/common/modes/monarch/monarchLexer.ts @@ -459,7 +459,7 @@ export class MonarchTokenizer implements modes.ITokenizationSupport { continue; } let rule: monarchCommon.IRule = rules[idx]; - if (rule.action.nextEmbedded !== '@pop') { + if (monarchCommon.isIAction(rule.action) && rule.action.nextEmbedded !== '@pop') { continue; } hasEmbeddedPopRule = true; @@ -518,7 +518,7 @@ export class MonarchTokenizer implements modes.ITokenizationSupport { // regular expression group matching // these never need cloning or equality since they are only used within a line match - let groupActions: monarchCommon.IAction[] = null; + let groupActions: monarchCommon.FuzzyAction[] = null; let groupMatches: string[] = null; let groupMatched: string[] = null; let groupRule: monarchCommon.IRule = null; @@ -531,7 +531,7 @@ export class MonarchTokenizer implements modes.ITokenizationSupport { let matches: string[] = null; let matched: string = null; - let action: monarchCommon.IAction = null; + let action: monarchCommon.FuzzyAction | monarchCommon.FuzzyAction[] = null; let rule: monarchCommon.IRule = null; let enteringEmbeddedMode: string = null; @@ -604,11 +604,11 @@ export class MonarchTokenizer implements modes.ITokenizationSupport { pos += matched.length; // maybe call action function (used for 'cases') - while (action.test) { + while (monarchCommon.isFuzzyAction(action) && monarchCommon.isIAction(action) && action.test) { action = action.test(matched, matches, state, pos === lineLength); } - let result: string | monarchCommon.IAction[] = null; + let result: monarchCommon.FuzzyAction | monarchCommon.FuzzyAction[] = null; // set the result: either a string or an array of actions if (typeof action === 'string' || Array.isArray(action)) { result = action; @@ -739,7 +739,7 @@ export class MonarchTokenizer implements modes.ITokenizationSupport { // return the result (and check for brace matching) // todo: for efficiency we could pre-sanitize tokenPostfix and substitutions let tokenType: string = null; - if (result.indexOf('@brackets') === 0) { + if (monarchCommon.isString(result) && result.indexOf('@brackets') === 0) { let rest = result.substr('@brackets'.length); let bracket = findBracket(this._lexer, matched); if (!bracket) { -- GitLab