-
-
Notifications
You must be signed in to change notification settings - Fork 34
/
naniscript.js
173 lines (168 loc) · 4.54 KB
/
naniscript.js
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// @ts-nocheck
naniscript.displayName = 'naniscript'
naniscript.aliases = ['nani']
/** @type {import('../core.js').Syntax} */
export default function naniscript(Prism) {
;(function (Prism) {
var expressionDef = /\{[^\r\n\[\]{}]*\}/
var params = {
'quoted-string': {
pattern: /"(?:[^"\\]|\\.)*"/,
alias: 'operator'
},
'command-param-id': {
pattern: /(\s)\w+:/,
lookbehind: true,
alias: 'property'
},
'command-param-value': [
{
pattern: expressionDef,
alias: 'selector'
},
{
pattern: /([\t ])\S+/,
lookbehind: true,
greedy: true,
alias: 'operator'
},
{
pattern: /\S(?:.*\S)?/,
alias: 'operator'
}
]
}
Prism.languages.naniscript = {
// ; ...
comment: {
pattern: /^([\t ]*);.*/m,
lookbehind: true
},
// > ...
// Define is a control line starting with '>' followed by a word, a space and a text.
define: {
pattern: /^>.+/m,
alias: 'tag',
inside: {
value: {
pattern: /(^>\w+[\t ]+)(?!\s)[^{}\r\n]+/,
lookbehind: true,
alias: 'operator'
},
key: {
pattern: /(^>)\w+/,
lookbehind: true
}
}
},
// # ...
label: {
pattern: /^([\t ]*)#[\t ]*\w+[\t ]*$/m,
lookbehind: true,
alias: 'regex'
},
command: {
pattern: /^([\t ]*)@\w+(?=[\t ]|$).*/m,
lookbehind: true,
alias: 'function',
inside: {
'command-name': /^@\w+/,
expression: {
pattern: expressionDef,
greedy: true,
alias: 'selector'
},
'command-params': {
pattern: /\s*\S[\s\S]*/,
inside: params
}
}
},
// Generic is any line that doesn't start with operators: ;>#@
'generic-text': {
pattern: /(^[ \t]*)[^#@>;\s].*/m,
lookbehind: true,
alias: 'punctuation',
inside: {
// \{ ... \} ... \[ ... \] ... \"
'escaped-char': /\\[{}\[\]"]/,
expression: {
pattern: expressionDef,
greedy: true,
alias: 'selector'
},
'inline-command': {
pattern: /\[[\t ]*\w[^\r\n\[\]]*\]/,
greedy: true,
alias: 'function',
inside: {
'command-params': {
pattern: /(^\[[\t ]*\w+\b)[\s\S]+(?=\]$)/,
lookbehind: true,
inside: params
},
'command-param-name': {
pattern: /^(\[[\t ]*)\w+/,
lookbehind: true,
alias: 'name'
},
'start-stop-char': /[\[\]]/
}
}
}
}
}
Prism.languages.nani = Prism.languages['naniscript']
/** @typedef {InstanceType<import("./prism-core")["Token"]>} Token */
/**
* This hook is used to validate generic-text tokens for balanced brackets.
* Mark token as bad-line when contains not balanced brackets: {},[]
*/
Prism.hooks.add('after-tokenize', function (env) {
/** @type {(Token | string)[]} */
var tokens = env.tokens
tokens.forEach(function (token) {
if (typeof token !== 'string' && token.type === 'generic-text') {
var content = getTextContent(token)
if (!isBracketsBalanced(content)) {
token.type = 'bad-line'
token.content = content
}
}
})
})
/**
* @param {string} input
* @returns {boolean}
*/
function isBracketsBalanced(input) {
var brackets = '[]{}'
var stack = []
for (var i = 0; i < input.length; i++) {
var bracket = input[i]
var bracketsIndex = brackets.indexOf(bracket)
if (bracketsIndex !== -1) {
if (bracketsIndex % 2 === 0) {
stack.push(bracketsIndex + 1)
} else if (stack.pop() !== bracketsIndex) {
return false
}
}
}
return stack.length === 0
}
/**
* @param {string | Token | (string | Token)[]} token
* @returns {string}
*/
function getTextContent(token) {
if (typeof token === 'string') {
return token
} else if (Array.isArray(token)) {
return token.map(getTextContent).join('')
} else {
return getTextContent(token.content)
}
}
})(Prism)
}