1 | 'use strict'
|
---|
2 |
|
---|
3 | module.exports = naniscript
|
---|
4 | naniscript.displayName = 'naniscript'
|
---|
5 | naniscript.aliases = []
|
---|
6 | function naniscript(Prism) {
|
---|
7 | ;(function (Prism) {
|
---|
8 | var expressionDef = /\{[^\r\n\[\]{}]*\}/
|
---|
9 | var params = {
|
---|
10 | 'quoted-string': {
|
---|
11 | pattern: /"(?:[^"\\]|\\.)*"/,
|
---|
12 | alias: 'operator'
|
---|
13 | },
|
---|
14 | 'command-param-id': {
|
---|
15 | pattern: /(\s)\w+:/,
|
---|
16 | lookbehind: true,
|
---|
17 | alias: 'property'
|
---|
18 | },
|
---|
19 | 'command-param-value': [
|
---|
20 | {
|
---|
21 | pattern: expressionDef,
|
---|
22 | alias: 'selector'
|
---|
23 | },
|
---|
24 | {
|
---|
25 | pattern: /([\t ])\S+/,
|
---|
26 | lookbehind: true,
|
---|
27 | greedy: true,
|
---|
28 | alias: 'operator'
|
---|
29 | },
|
---|
30 | {
|
---|
31 | pattern: /\S(?:.*\S)?/,
|
---|
32 | alias: 'operator'
|
---|
33 | }
|
---|
34 | ]
|
---|
35 | }
|
---|
36 | Prism.languages.naniscript = {
|
---|
37 | // ; ...
|
---|
38 | comment: {
|
---|
39 | pattern: /^([\t ]*);.*/m,
|
---|
40 | lookbehind: true
|
---|
41 | },
|
---|
42 | // > ...
|
---|
43 | // Define is a control line starting with '>' followed by a word, a space and a text.
|
---|
44 | define: {
|
---|
45 | pattern: /^>.+/m,
|
---|
46 | alias: 'tag',
|
---|
47 | inside: {
|
---|
48 | value: {
|
---|
49 | pattern: /(^>\w+[\t ]+)(?!\s)[^{}\r\n]+/,
|
---|
50 | lookbehind: true,
|
---|
51 | alias: 'operator'
|
---|
52 | },
|
---|
53 | key: {
|
---|
54 | pattern: /(^>)\w+/,
|
---|
55 | lookbehind: true
|
---|
56 | }
|
---|
57 | }
|
---|
58 | },
|
---|
59 | // # ...
|
---|
60 | label: {
|
---|
61 | pattern: /^([\t ]*)#[\t ]*\w+[\t ]*$/m,
|
---|
62 | lookbehind: true,
|
---|
63 | alias: 'regex'
|
---|
64 | },
|
---|
65 | command: {
|
---|
66 | pattern: /^([\t ]*)@\w+(?=[\t ]|$).*/m,
|
---|
67 | lookbehind: true,
|
---|
68 | alias: 'function',
|
---|
69 | inside: {
|
---|
70 | 'command-name': /^@\w+/,
|
---|
71 | expression: {
|
---|
72 | pattern: expressionDef,
|
---|
73 | greedy: true,
|
---|
74 | alias: 'selector'
|
---|
75 | },
|
---|
76 | 'command-params': {
|
---|
77 | pattern: /\s*\S[\s\S]*/,
|
---|
78 | inside: params
|
---|
79 | }
|
---|
80 | }
|
---|
81 | },
|
---|
82 | // Generic is any line that doesn't start with operators: ;>#@
|
---|
83 | 'generic-text': {
|
---|
84 | pattern: /(^[ \t]*)[^#@>;\s].*/m,
|
---|
85 | lookbehind: true,
|
---|
86 | alias: 'punctuation',
|
---|
87 | inside: {
|
---|
88 | // \{ ... \} ... \[ ... \] ... \"
|
---|
89 | 'escaped-char': /\\[{}\[\]"]/,
|
---|
90 | expression: {
|
---|
91 | pattern: expressionDef,
|
---|
92 | greedy: true,
|
---|
93 | alias: 'selector'
|
---|
94 | },
|
---|
95 | 'inline-command': {
|
---|
96 | pattern: /\[[\t ]*\w[^\r\n\[\]]*\]/,
|
---|
97 | greedy: true,
|
---|
98 | alias: 'function',
|
---|
99 | inside: {
|
---|
100 | 'command-params': {
|
---|
101 | pattern: /(^\[[\t ]*\w+\b)[\s\S]+(?=\]$)/,
|
---|
102 | lookbehind: true,
|
---|
103 | inside: params
|
---|
104 | },
|
---|
105 | 'command-param-name': {
|
---|
106 | pattern: /^(\[[\t ]*)\w+/,
|
---|
107 | lookbehind: true,
|
---|
108 | alias: 'name'
|
---|
109 | },
|
---|
110 | 'start-stop-char': /[\[\]]/
|
---|
111 | }
|
---|
112 | }
|
---|
113 | }
|
---|
114 | }
|
---|
115 | }
|
---|
116 | Prism.languages.nani = Prism.languages['naniscript']
|
---|
117 | /** @typedef {InstanceType<import("./prism-core")["Token"]>} Token */
|
---|
118 | /**
|
---|
119 | * This hook is used to validate generic-text tokens for balanced brackets.
|
---|
120 | * Mark token as bad-line when contains not balanced brackets: {},[]
|
---|
121 | */
|
---|
122 | Prism.hooks.add('after-tokenize', function (env) {
|
---|
123 | /** @type {(Token | string)[]} */
|
---|
124 | var tokens = env.tokens
|
---|
125 | tokens.forEach(function (token) {
|
---|
126 | if (typeof token !== 'string' && token.type === 'generic-text') {
|
---|
127 | var content = getTextContent(token)
|
---|
128 | if (!isBracketsBalanced(content)) {
|
---|
129 | token.type = 'bad-line'
|
---|
130 | token.content = content
|
---|
131 | }
|
---|
132 | }
|
---|
133 | })
|
---|
134 | })
|
---|
135 | /**
|
---|
136 | * @param {string} input
|
---|
137 | * @returns {boolean}
|
---|
138 | */
|
---|
139 | function isBracketsBalanced(input) {
|
---|
140 | var brackets = '[]{}'
|
---|
141 | var stack = []
|
---|
142 | for (var i = 0; i < input.length; i++) {
|
---|
143 | var bracket = input[i]
|
---|
144 | var bracketsIndex = brackets.indexOf(bracket)
|
---|
145 | if (bracketsIndex !== -1) {
|
---|
146 | if (bracketsIndex % 2 === 0) {
|
---|
147 | stack.push(bracketsIndex + 1)
|
---|
148 | } else if (stack.pop() !== bracketsIndex) {
|
---|
149 | return false
|
---|
150 | }
|
---|
151 | }
|
---|
152 | }
|
---|
153 | return stack.length === 0
|
---|
154 | }
|
---|
155 | /**
|
---|
156 | * @param {string | Token | (string | Token)[]} token
|
---|
157 | * @returns {string}
|
---|
158 | */
|
---|
159 | function getTextContent(token) {
|
---|
160 | if (typeof token === 'string') {
|
---|
161 | return token
|
---|
162 | } else if (Array.isArray(token)) {
|
---|
163 | return token.map(getTextContent).join('')
|
---|
164 | } else {
|
---|
165 | return getTextContent(token.content)
|
---|
166 | }
|
---|
167 | }
|
---|
168 | })(Prism)
|
---|
169 | }
|
---|