forked from microsoft/vscode-docker
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparser.ts
57 lines (44 loc) · 1.35 KB
/
parser.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*---------------------------------------------------------
* Copyright (C) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------*/
'use strict';
import {TextLine} from 'vscode';
export abstract class Parser {
_tokenParseRegex: RegExp;
constructor(parseTokenRegex: RegExp) {
this._tokenParseRegex = parseTokenRegex;
}
keyNameFromKeyToken(keyToken: string): string {
return keyToken.replace(this._tokenParseRegex, '');
}
tokenValue(line: string, token: IToken): string {
return line.substring(token.startIndex, token.endIndex);
}
tokensAtColumn(tokens: IToken[], charIndex: number): number[] {
for (var i = 0, len = tokens.length; i < len; i++) {
var token = tokens[i];
if (token.endIndex < charIndex) {
continue;
}
if (token.endIndex === charIndex && i + 1 < len) {
return [i, i + 1]
}
return [i];
}
// should not happen: no token found? => return the last one
return [tokens.length - 1];
}
abstract parseLine(textLine: TextLine): IToken[];
}
export enum TokenType {
Whitespace,
Text,
String,
Comment,
Key
}
export interface IToken {
startIndex: number;
endIndex: number;
type: TokenType;
}