-
Notifications
You must be signed in to change notification settings - Fork 727
/
bicep.ts
117 lines (98 loc) · 3.16 KB
/
bicep.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
import { languages } from 'monaco-editor-core';
const bounded = (text: string) => `\\b${text}\\b`;
const after = (regex: string) => `(?<=${regex})`;
const notAfter = (regex: string) => `(?<!${regex})`;
const before = (regex: string) => `(?=${regex})`;
const notBefore = (regex: string) => `(?!${regex})`;
const identifierStart = "[_a-zA-Z]";
const identifierContinue = "[_a-zA-Z0-9]";
const identifier = bounded(`${identifierStart}${identifierContinue}*`);
const directive = bounded(`[_a-zA-Z-0-9]+`);
const keywords = [
'metadata',
'targetScope',
'resource',
'module',
'param',
'var',
'output',
'for',
'in',
'if',
'existing',
'import',
'as',
'type',
'with',
'using',
'func',
'assert',
'provider',
];
const namedLiterals = [
'true',
'false',
'null'
];
const nonCommentWs = `[ \\t\\r\\n]`;
const numericLiteral = `[0-9]+`;
export const BicepLanguage: languages.IMonarchLanguage = {
defaultToken: '',
tokenPostfix: '.bicep',
brackets: [
{ open: '{', close: '}', token: 'delimiter.curly' },
{ open: '[', close: ']', token: 'delimiter.square' },
{ open: '(', close: ')', token: 'delimiter.parenthesis' },
],
symbols: /[=><!~?:&|+\-*/^%]+/,
keywords,
namedLiterals,
escapes: `\\\\(u{[0-9A-Fa-f]+}|n|r|t|\\\\|'|\\\${)`,
tokenizer: {
root: [
{ include: '@expression' },
{ include: '@whitespace' },
],
stringVerbatim: [
{ regex: `(|'|'')[^']`, action: { token: 'string' } },
{ regex: `'''${notBefore(`'`)}`, action: { token: 'string.quote', next: '@pop' } },
],
stringLiteral: [
{ regex: `\\\${`, action: { token: 'delimiter.bracket', next: '@bracketCounting' } },
{ regex: `[^\\\\'$]+`, action: { token: 'string' } },
{ regex: '@escapes', action: { token: 'string.escape' } },
{ regex: `\\\\.`, action: { token: 'string.escape.invalid' } },
{ regex: `'`, action: { token: 'string', next: '@pop' } },
],
bracketCounting: [
{ regex: `{`, action: { token: 'delimiter.bracket', next: '@bracketCounting' } },
{ regex: `}`, action: { token: 'delimiter.bracket', next: '@pop' } },
{ include: '@expression' }
],
comment: [
{ regex: `[^\\*]+`, action: { token: 'comment' } },
{ regex: `\\*\\/`, action: { token: 'comment', next: '@pop' } },
{ regex: `[\\/*]`, action: { token: 'comment' } },
],
whitespace: [
{ regex: nonCommentWs, },
{ regex: `\\/\\*`, action: { token: 'comment', next: '@comment' } },
{ regex: `\\/\\/.*$`, action: { token: 'comment' } },
],
expression: [
{ regex: `'''`, action: { token: 'string.quote', next: '@stringVerbatim' } },
{ regex: `'${notBefore(`''`)}`, action: { token: 'string.quote', next: '@stringLiteral' } },
{ regex: numericLiteral, action: { token: 'number' } },
{ regex: identifier, action: {
cases: {
'@keywords': { token: 'keyword' },
'@namedLiterals': { token: 'keyword' },
'@default': { token: 'identifier' },
}
} },
{ regex: `#${directive}`, action: { token: 'directive' } },
],
},
};