@scalar/highlight 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (166) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/README.md +324 -0
  3. package/dist/all.d.ts +28 -0
  4. package/dist/all.d.ts.map +1 -0
  5. package/dist/all.js +105 -0
  6. package/dist/compat/hljs.d.ts +24 -0
  7. package/dist/compat/hljs.d.ts.map +1 -0
  8. package/dist/compat/hljs.js +95 -0
  9. package/dist/compat/index.d.ts +24 -0
  10. package/dist/compat/index.d.ts.map +1 -0
  11. package/dist/compat/index.js +21 -0
  12. package/dist/compat/languages.d.ts +46 -0
  13. package/dist/compat/languages.d.ts.map +1 -0
  14. package/dist/compat/languages.js +236 -0
  15. package/dist/compat/syntax-highlight.d.ts +37 -0
  16. package/dist/compat/syntax-highlight.d.ts.map +1 -0
  17. package/dist/compat/syntax-highlight.js +133 -0
  18. package/dist/core/compile.d.ts +3 -0
  19. package/dist/core/compile.d.ts.map +1 -0
  20. package/dist/core/compile.js +142 -0
  21. package/dist/core/index.d.ts +9 -0
  22. package/dist/core/index.d.ts.map +1 -0
  23. package/dist/core/index.js +5 -0
  24. package/dist/core/registry.d.ts +14 -0
  25. package/dist/core/registry.d.ts.map +1 -0
  26. package/dist/core/registry.js +77 -0
  27. package/dist/core/render.d.ts +67 -0
  28. package/dist/core/render.d.ts.map +1 -0
  29. package/dist/core/render.js +216 -0
  30. package/dist/core/scopes.d.ts +94 -0
  31. package/dist/core/scopes.d.ts.map +1 -0
  32. package/dist/core/scopes.js +113 -0
  33. package/dist/core/tokenize.d.ts +18 -0
  34. package/dist/core/tokenize.d.ts.map +1 -0
  35. package/dist/core/tokenize.js +131 -0
  36. package/dist/core/types.d.ts +99 -0
  37. package/dist/core/types.d.ts.map +1 -0
  38. package/dist/core/types.js +10 -0
  39. package/dist/index.d.ts +19 -0
  40. package/dist/index.d.ts.map +1 -0
  41. package/dist/index.js +20 -0
  42. package/dist/langs/bash.d.ts +4 -0
  43. package/dist/langs/bash.d.ts.map +1 -0
  44. package/dist/langs/bash.js +123 -0
  45. package/dist/langs/c.d.ts +4 -0
  46. package/dist/langs/c.d.ts.map +1 -0
  47. package/dist/langs/c.js +196 -0
  48. package/dist/langs/clojure.d.ts +4 -0
  49. package/dist/langs/clojure.d.ts.map +1 -0
  50. package/dist/langs/clojure.js +213 -0
  51. package/dist/langs/cpp.d.ts +4 -0
  52. package/dist/langs/cpp.d.ts.map +1 -0
  53. package/dist/langs/cpp.js +391 -0
  54. package/dist/langs/csharp.d.ts +4 -0
  55. package/dist/langs/csharp.d.ts.map +1 -0
  56. package/dist/langs/csharp.js +307 -0
  57. package/dist/langs/css.d.ts +4 -0
  58. package/dist/langs/css.d.ts.map +1 -0
  59. package/dist/langs/css.js +94 -0
  60. package/dist/langs/dart.d.ts +4 -0
  61. package/dist/langs/dart.d.ts.map +1 -0
  62. package/dist/langs/dart.js +262 -0
  63. package/dist/langs/diff.d.ts +10 -0
  64. package/dist/langs/diff.d.ts.map +1 -0
  65. package/dist/langs/diff.js +26 -0
  66. package/dist/langs/dockerfile.d.ts +4 -0
  67. package/dist/langs/dockerfile.d.ts.map +1 -0
  68. package/dist/langs/dockerfile.js +308 -0
  69. package/dist/langs/elixir.d.ts +4 -0
  70. package/dist/langs/elixir.d.ts.map +1 -0
  71. package/dist/langs/elixir.js +343 -0
  72. package/dist/langs/fsharp.d.ts +4 -0
  73. package/dist/langs/fsharp.d.ts.map +1 -0
  74. package/dist/langs/fsharp.js +330 -0
  75. package/dist/langs/go.d.ts +5 -0
  76. package/dist/langs/go.d.ts.map +1 -0
  77. package/dist/langs/go.js +90 -0
  78. package/dist/langs/graphql.d.ts +4 -0
  79. package/dist/langs/graphql.d.ts.map +1 -0
  80. package/dist/langs/graphql.js +185 -0
  81. package/dist/langs/haskell.d.ts +4 -0
  82. package/dist/langs/haskell.d.ts.map +1 -0
  83. package/dist/langs/haskell.js +375 -0
  84. package/dist/langs/html.d.ts +14 -0
  85. package/dist/langs/html.d.ts.map +1 -0
  86. package/dist/langs/html.js +71 -0
  87. package/dist/langs/http.d.ts +4 -0
  88. package/dist/langs/http.d.ts.map +1 -0
  89. package/dist/langs/http.js +185 -0
  90. package/dist/langs/ini.d.ts +4 -0
  91. package/dist/langs/ini.d.ts.map +1 -0
  92. package/dist/langs/ini.js +227 -0
  93. package/dist/langs/java.d.ts +4 -0
  94. package/dist/langs/java.d.ts.map +1 -0
  95. package/dist/langs/java.js +178 -0
  96. package/dist/langs/javascript.d.ts +4 -0
  97. package/dist/langs/javascript.d.ts.map +1 -0
  98. package/dist/langs/javascript.js +226 -0
  99. package/dist/langs/json.d.ts +10 -0
  100. package/dist/langs/json.d.ts.map +1 -0
  101. package/dist/langs/json.js +46 -0
  102. package/dist/langs/kotlin.d.ts +4 -0
  103. package/dist/langs/kotlin.d.ts.map +1 -0
  104. package/dist/langs/kotlin.js +220 -0
  105. package/dist/langs/lua.d.ts +4 -0
  106. package/dist/langs/lua.d.ts.map +1 -0
  107. package/dist/langs/lua.js +330 -0
  108. package/dist/langs/makefile.d.ts +4 -0
  109. package/dist/langs/makefile.d.ts.map +1 -0
  110. package/dist/langs/makefile.js +266 -0
  111. package/dist/langs/markdown.d.ts +11 -0
  112. package/dist/langs/markdown.d.ts.map +1 -0
  113. package/dist/langs/markdown.js +71 -0
  114. package/dist/langs/matlab.d.ts +4 -0
  115. package/dist/langs/matlab.d.ts.map +1 -0
  116. package/dist/langs/matlab.js +352 -0
  117. package/dist/langs/mojo.d.ts +4 -0
  118. package/dist/langs/mojo.d.ts.map +1 -0
  119. package/dist/langs/mojo.js +337 -0
  120. package/dist/langs/nginx.d.ts +4 -0
  121. package/dist/langs/nginx.d.ts.map +1 -0
  122. package/dist/langs/nginx.js +168 -0
  123. package/dist/langs/objectivec.d.ts +4 -0
  124. package/dist/langs/objectivec.d.ts.map +1 -0
  125. package/dist/langs/objectivec.js +287 -0
  126. package/dist/langs/ocaml.d.ts +4 -0
  127. package/dist/langs/ocaml.d.ts.map +1 -0
  128. package/dist/langs/ocaml.js +309 -0
  129. package/dist/langs/perl.d.ts +4 -0
  130. package/dist/langs/perl.d.ts.map +1 -0
  131. package/dist/langs/perl.js +435 -0
  132. package/dist/langs/php.d.ts +4 -0
  133. package/dist/langs/php.d.ts.map +1 -0
  134. package/dist/langs/php.js +325 -0
  135. package/dist/langs/powershell.d.ts +4 -0
  136. package/dist/langs/powershell.d.ts.map +1 -0
  137. package/dist/langs/powershell.js +308 -0
  138. package/dist/langs/python.d.ts +4 -0
  139. package/dist/langs/python.d.ts.map +1 -0
  140. package/dist/langs/python.js +350 -0
  141. package/dist/langs/r.d.ts +4 -0
  142. package/dist/langs/r.d.ts.map +1 -0
  143. package/dist/langs/r.js +265 -0
  144. package/dist/langs/ruby.d.ts +4 -0
  145. package/dist/langs/ruby.d.ts.map +1 -0
  146. package/dist/langs/ruby.js +365 -0
  147. package/dist/langs/rust.d.ts +10 -0
  148. package/dist/langs/rust.d.ts.map +1 -0
  149. package/dist/langs/rust.js +116 -0
  150. package/dist/langs/scala.d.ts +4 -0
  151. package/dist/langs/scala.d.ts.map +1 -0
  152. package/dist/langs/scala.js +308 -0
  153. package/dist/langs/sql.d.ts +5 -0
  154. package/dist/langs/sql.d.ts.map +1 -0
  155. package/dist/langs/sql.js +57 -0
  156. package/dist/langs/swift.d.ts +33 -0
  157. package/dist/langs/swift.d.ts.map +1 -0
  158. package/dist/langs/swift.js +256 -0
  159. package/dist/langs/yaml.d.ts +11 -0
  160. package/dist/langs/yaml.d.ts.map +1 -0
  161. package/dist/langs/yaml.js +123 -0
  162. package/dist/lazy.d.ts +69 -0
  163. package/dist/lazy.d.ts.map +1 -0
  164. package/dist/lazy.js +200 -0
  165. package/dist/style.css +308 -0
  166. package/package.json +86 -0
@@ -0,0 +1,325 @@
1
+ /**
2
+ * PHP.
3
+ *
4
+ * The shape of the grammar follows the shape of the language: a PHP file is
5
+ * markup until `<?php` opens a code region, so `root` is the HTML mode and the
6
+ * code lives in a pushed state that `?>` pops. That is also what makes template
7
+ * files work — `<a href="<?= $url ?>">` switches modes twice inside one tag.
8
+ *
9
+ * Things worth knowing about:
10
+ *
11
+ * - Heredocs and nowdocs cannot match their own terminator (that needs a
12
+ * backreference, which the compiler rejects), so any identifier alone on a
13
+ * line closes them. See the `heredoc-end` note.
14
+ * - `#` starts a comment but `#[` starts an attribute, so the attribute rule
15
+ * runs first and lets the argument list tokenize as ordinary PHP.
16
+ * - A line comment stops at `?>`, because PHP itself leaves code mode there.
17
+ * - Interpolation is real: `"{$order->total()}"` tokenizes as an expression,
18
+ * while the same text in single quotes stays one string.
19
+ */
20
+ /** Identifier. PHP allows the high-byte range in names, same as Python. */
21
+ const ID = '[A-Za-z_\\u0080-\\uFFFF][\\w\\u0080-\\uFFFF]*';
22
+ /** A possibly-qualified name: `Invoice`, `App\Billing\Invoice`, `\RuntimeException`. */
23
+ const NAME = `\\\\?${ID}(?:\\\\${ID})*`;
24
+ /** An HTML tag or attribute name, for the inline-markup mode. */
25
+ const TAG = '[A-Za-z_][\\w:.-]*';
26
+ const php = {
27
+ name: 'php',
28
+ aliases: ['php3', 'php4', 'php5', 'php8'],
29
+ states: {
30
+ // ---- inline markup -----------------------------------------------------
31
+ // Outside `<?php … ?>` a PHP file is HTML. It is scoped rather than
32
+ // highlighted with the HTML grammar — cross-language embedding is not a
33
+ // thing the engine does yet — so this covers tags, attributes and comments
34
+ // and stops there.
35
+ root: {
36
+ ignoreCase: true,
37
+ rules: [
38
+ { match: '<\\?(?:php\\b|=)', scope: 'tag', push: 'php' },
39
+ { match: '<!--', scope: 'comment', push: 'html-comment' },
40
+ {
41
+ match: `(</?)(${TAG})`,
42
+ scope: ['punctuation.bracket', 'tag'],
43
+ push: 'html-tag',
44
+ },
45
+ ],
46
+ },
47
+ /** Markup and PHP block comments. Neither form nests. */
48
+ 'html-comment': {
49
+ default: 'comment',
50
+ rules: [{ match: '-->', pop: true }],
51
+ },
52
+ 'block-comment': {
53
+ default: 'comment',
54
+ rules: [{ match: '\\*/', pop: true }],
55
+ },
56
+ 'doc-comment': {
57
+ default: 'comment.doc',
58
+ rules: [{ match: '\\*/', pop: true }],
59
+ },
60
+ 'html-tag': {
61
+ ignoreCase: true,
62
+ rules: [
63
+ { match: '/?>', scope: 'punctuation.bracket', pop: true },
64
+ // `value="<?= $x ?>"` is everywhere in templates, so an open tag beats
65
+ // the attribute rules below.
66
+ { match: '<\\?(?:php\\b|=)', scope: 'tag', push: 'php' },
67
+ // A quoted value stops at `<` so it can never swallow an embedded PHP
68
+ // region; a value that holds one simply renders unscoped.
69
+ { match: '"[^"<]*"|\'[^\'<]*\'', scope: 'string' },
70
+ { match: `[@:#]?${TAG}`, scope: 'tag.attribute' },
71
+ { match: '=', scope: 'operator' },
72
+ ],
73
+ },
74
+ // ---- code --------------------------------------------------------------
75
+ php: {
76
+ rules: [{ match: '\\?>', scope: 'tag', pop: true }, { include: 'expression' }],
77
+ },
78
+ expression: {
79
+ rules: [
80
+ // Before the comment rules: `#[` opens an attribute, `#` a comment.
81
+ // Only the `#[Name` head is claimed so the arguments keep tokenizing as
82
+ // PHP, which is what makes `#[Route('/x', methods: ['GET'])]` read.
83
+ { match: `(#\\[)(${NAME})`, scope: ['decorator', 'decorator'] },
84
+ // States rather than a lazy `[\\s\\S]*?` scan: the lazy form runs to EOF
85
+ // and fails once per opener, which is O(n²) on a file with an
86
+ // unterminated `/*`.
87
+ { match: '/\\*\\*(?![/*])', scope: 'comment.doc', push: 'doc-comment' },
88
+ { match: '/\\*', scope: 'comment', push: 'block-comment' },
89
+ // A line comment ends at `?>` as well as at the newline, because that
90
+ // is where PHP leaves code mode — and a comment that swallowed the `?>`
91
+ // would take the rest of a template file with it.
92
+ //
93
+ // Written as an unrolled loop rather than the obvious
94
+ // `(?:[^?\n]|\?(?!>))*`: both are linear, but alternating per character
95
+ // inside the merged regex measured ~450x slower on a long comment,
96
+ // because every character leaves a backtracking state behind. Here the
97
+ // inner group can only start at a `?`, which the run before it cannot
98
+ // consume, so there is nothing to backtrack through.
99
+ { match: '(?://|#)[^?\\n]*(?:\\?(?!>)[^?\\n]*)*', scope: 'comment' },
100
+ { include: 'strings' },
101
+ { include: 'numbers' },
102
+ // Matched at the arrow, so `$logger->count` never reads as the builtin.
103
+ {
104
+ match: `(\\?->|->)([ \\t]*)(${ID})(?=[ \\t]*\\()`,
105
+ scope: ['operator', null, 'function.method'],
106
+ },
107
+ {
108
+ match: `(\\?->|->)([ \\t]*)(${ID})`,
109
+ scope: ['operator', null, 'variable.member'],
110
+ },
111
+ {
112
+ match: `(::)([ \\t]*)(${ID})(?=[ \\t]*\\()`,
113
+ scope: ['operator', null, 'function.method'],
114
+ },
115
+ // Consume the opening paren so parameters get their own state.
116
+ {
117
+ match: `(function)([ \\t]+)(&?)(${ID})([ \\t]*)(\\()`,
118
+ scope: ['keyword.declaration', null, 'operator', 'function', null, 'punctuation.bracket'],
119
+ push: 'params',
120
+ },
121
+ {
122
+ match: '\\b(function|fn)([ \\t]*)(&?)(\\()',
123
+ scope: ['keyword.declaration', null, 'operator', 'punctuation.bracket'],
124
+ push: 'params',
125
+ },
126
+ {
127
+ match: `\\b(class|interface|trait|enum)([ \\t]+)(${ID})`,
128
+ scope: ['keyword.declaration', null, 'class'],
129
+ },
130
+ {
131
+ match: `\\b(namespace|use)([ \\t]+)(${NAME})`,
132
+ scope: ['keyword.import', null, 'namespace'],
133
+ },
134
+ {
135
+ match: '\\b(?:if|else|elseif|while|do|for|foreach|switch|case|default|break|continue|return|goto|throw|try|catch|finally|yield|match|declare|end(?:if|while|for|foreach|switch|declare))\\b',
136
+ scope: 'keyword.control',
137
+ },
138
+ {
139
+ match: '\\b(?:abstract|final|public|private|protected|static|readonly|const|var|global|function|fn|class|interface|trait|enum|extends|implements)\\b',
140
+ scope: 'keyword.declaration',
141
+ },
142
+ {
143
+ match: '\\b(?:namespace|use|require_once|require|include_once|include)\\b',
144
+ scope: 'keyword.import',
145
+ },
146
+ {
147
+ match: '\\b(?:new|clone|instanceof|insteadof|as|and|or|xor)\\b',
148
+ scope: 'keyword.operator',
149
+ },
150
+ { match: '\\b(?:echo|print)\\b', scope: 'keyword' },
151
+ // Only the lowercase spellings. `TRUE`/`NULL` are just as legal and land
152
+ // on the SCREAMING_CASE rule below, which is not wrong — they are
153
+ // constants — and saves carrying every casing of every literal.
154
+ { match: '\\b(?:true|false)\\b', scope: 'boolean' },
155
+ // `__DIR__`, `__CLASS__` and friends. Magic *methods* (`__construct`)
156
+ // have no trailing underscores, so they are not caught here.
157
+ { match: '\\bnull\\b|\\b__[A-Z]+__\\b', scope: 'constant.builtin' },
158
+ { match: '\\$this\\b', scope: 'variable.builtin' },
159
+ {
160
+ match: '\\$(?:GLOBALS|_(?:GET|POST|REQUEST|SERVER|SESSION|COOKIE|FILES|ENV))\\b',
161
+ scope: 'variable.builtin',
162
+ },
163
+ // `$$name` is a variable variable — one name, so one token.
164
+ { match: `\\$\\$?${ID}`, scope: 'variable' },
165
+ {
166
+ match: '\\b(?:array_(?:map|filter|merge|keys|values|slice|sum)|in_array|count|is_(?:array|string|int|null|numeric)|str_(?:replace|repeat|contains|pad)|strlen|strtolower|strtoupper|substr|strpos|trim|sprintf|printf|number_format|implode|explode|json_(?:encode|decode)|preg_(?:match|replace|split)|var_dump|usort|isset|unset|empty|list|compact|exit|die)\\b',
167
+ scope: 'function.builtin',
168
+ },
169
+ {
170
+ match: '\\b(?:int|float|string|bool|array|object|mixed|void|never|callable|iterable|resource|self|parent)\\b',
171
+ scope: 'type.builtin',
172
+ },
173
+ // Before the name rules, so `App\Models\Invoice` reads as a path into a
174
+ // type rather than as three types.
175
+ { match: `\\b${ID}(?=\\\\)`, scope: 'namespace' },
176
+ // SCREAMING_CASE is a constant and CapWords a type. Between them these
177
+ // cover class names, enum cases and every `define()`d constant without
178
+ // shipping a name list.
179
+ { match: '\\b[A-Z][A-Z0-9_]+\\b', scope: 'constant' },
180
+ { match: '\\b[A-Z][A-Za-z0-9_]*\\b', scope: 'type' },
181
+ // PHP function names are conventionally lowercase, so a CapWords name
182
+ // before a paren stays a type — which is what `new Invoice(…)` wants.
183
+ { match: '\\b[a-z_]\\w*(?=[ \\t]*\\()', scope: 'function.call' },
184
+ {
185
+ match: '\\*\\*=?|\\?\\?=?|\\?->|<=>|<<=?|>>=?|===?|!==?|<>|=>|->|::|&&|\\|\\||\\+\\+|--|[-+*/%.&|^!]=?|[<>]=?|=|[?~@]',
186
+ scope: 'operator',
187
+ },
188
+ { match: '[()\\[\\]{}]', scope: 'punctuation.bracket' },
189
+ // The namespace separator is punctuation, not the escape it looks like.
190
+ { match: '[,;:]|\\\\', scope: 'punctuation.delimiter' },
191
+ ],
192
+ },
193
+ numbers: {
194
+ rules: [
195
+ { match: '\\b0[xX][0-9a-fA-F_]+\\b', scope: 'number' },
196
+ { match: '\\b0[bB][01_]+\\b', scope: 'number' },
197
+ { match: '\\b0[oO][0-7_]+\\b', scope: 'number' },
198
+ // Legacy `0755` octal needs no rule of its own: it reads as a decimal
199
+ // literal and renders identically.
200
+ {
201
+ match: '\\b\\d[\\d_]*\\.[\\d_]*(?:[eE][-+]?\\d+)?|\\.\\d[\\d_]*(?:[eE][-+]?\\d+)?|\\b\\d[\\d_]*(?:[eE][-+]?\\d+)?\\b',
202
+ scope: 'number',
203
+ },
204
+ ],
205
+ },
206
+ // ---- strings -----------------------------------------------------------
207
+ strings: {
208
+ rules: [
209
+ // Nowdoc before heredoc: they differ only by the quotes on the marker,
210
+ // and only the heredoc interpolates.
211
+ { match: `<<<[ \\t]*'${ID}'`, scope: 'string.special', push: 'nowdoc' },
212
+ {
213
+ match: `<<<[ \\t]*"?${ID}"?`,
214
+ scope: 'string.special',
215
+ push: 'heredoc',
216
+ },
217
+ { match: '"', scope: 'string', push: 'string-double' },
218
+ { match: "'", scope: 'string', push: 'string-single' },
219
+ { match: '`', scope: 'string', push: 'shell' },
220
+ ],
221
+ },
222
+ escapes: {
223
+ rules: [
224
+ {
225
+ match: '\\\\(?:x[0-9a-fA-F]{1,2}|u\\{[0-9a-fA-F]+\\}|[0-7]{1,3}|[nrtvef\\\\$"`])',
226
+ scope: 'string.escape',
227
+ },
228
+ ],
229
+ },
230
+ interpolation: {
231
+ rules: [
232
+ // `{$…}` is the complex form and takes full expression syntax. The
233
+ // lookahead is what keeps a literal `{` in a string from opening one.
234
+ { match: '\\{(?=\\$)', scope: 'interpolation', push: 'interp' },
235
+ // `${name}` — deprecated in 8.2, still all over older code.
236
+ { match: '\\$\\{[^}\\n]*\\}', scope: 'variable' },
237
+ // The simple form reaches exactly one `->prop` or one `[key]`, which is
238
+ // all PHP itself parses without braces. `[` is excluded from the
239
+ // subscript so the scan stops at the next one instead of at the newline.
240
+ {
241
+ match: `\\$${ID}(?:->${ID}|\\[[^\\[\\]\\n]*\\])?`,
242
+ scope: 'variable',
243
+ },
244
+ ],
245
+ },
246
+ /**
247
+ * Inside `{$…}`. A `{` here is left as punctuation rather than tracked,
248
+ * because the complex form holds a variable expression and PHP has no
249
+ * brace-shaped literal to nest inside one.
250
+ */
251
+ interp: {
252
+ rules: [{ match: '\\}', scope: 'interpolation', pop: true }, { include: 'expression' }],
253
+ },
254
+ // A PHP string is allowed to span lines, so an unterminated one really does
255
+ // run to the end of the file — no `$` escape hatch here, unlike Python.
256
+ 'string-double': {
257
+ default: 'string',
258
+ rules: [{ include: 'escapes' }, { include: 'interpolation' }, { match: '"', scope: 'string', pop: true }],
259
+ },
260
+ 'string-single': {
261
+ default: 'string',
262
+ rules: [
263
+ // Single quotes recognise exactly two escapes, and `\n` is not one.
264
+ { match: "\\\\['\\\\]", scope: 'string.escape' },
265
+ { match: "'", scope: 'string', pop: true },
266
+ ],
267
+ },
268
+ /** Backtick shell execution. Interpolates, exactly like a double quote. */
269
+ shell: {
270
+ default: 'string',
271
+ rules: [{ include: 'escapes' }, { include: 'interpolation' }, { match: '`', scope: 'string', pop: true }],
272
+ },
273
+ /**
274
+ * The closing marker of a heredoc or a nowdoc.
275
+ *
276
+ * It cannot be matched against the opening one — that is a backreference,
277
+ * and group numbers shift when rules merge — so any identifier alone on a
278
+ * line, optionally trailed by the punctuation a heredoc can end with
279
+ * (`;`, `,`, `)`, `]`), closes the body. A body line that is a single bare
280
+ * word therefore ends it early. That is rare in the SQL, HTML and shell
281
+ * text heredocs actually carry, and the alternative is not closing at all.
282
+ */
283
+ 'heredoc-end': {
284
+ rules: [
285
+ // The indent stays string-coloured, so only the marker itself stands
286
+ // out — PHP 7.3 lets the closing marker be indented with the body.
287
+ {
288
+ match: '^([ \\t]*)([A-Za-z_]\\w*)(?=[ \\t]*[;,)\\]]*[ \\t]*$)',
289
+ scope: [null, 'string.special'],
290
+ pop: true,
291
+ },
292
+ ],
293
+ },
294
+ heredoc: {
295
+ default: 'string',
296
+ rules: [{ include: 'heredoc-end' }, { include: 'escapes' }, { include: 'interpolation' }],
297
+ },
298
+ nowdoc: {
299
+ default: 'string',
300
+ rules: [{ include: 'heredoc-end' }],
301
+ },
302
+ // ---- parameters --------------------------------------------------------
303
+ /**
304
+ * A parameter list. Names are parameters rather than plain variables here,
305
+ * which also covers constructor property promotion (`private int $qty`).
306
+ */
307
+ params: {
308
+ rules: [
309
+ { match: '\\)', scope: 'punctuation.bracket', pop: true },
310
+ { match: '\\(', scope: 'punctuation.bracket', push: 'paren' },
311
+ { match: `\\$${ID}`, scope: 'variable.parameter' },
312
+ { include: 'expression' },
313
+ ],
314
+ },
315
+ /** Balanced parens, so a call inside a default value cannot end the list. */
316
+ paren: {
317
+ rules: [
318
+ { match: '\\)', scope: 'punctuation.bracket', pop: true },
319
+ { match: '\\(', scope: 'punctuation.bracket', push: 'paren' },
320
+ { include: 'expression' },
321
+ ],
322
+ },
323
+ },
324
+ };
325
+ export default php;
@@ -0,0 +1,4 @@
1
+ import type { Grammar } from '../core/types.js';
2
+ declare const powershell: Grammar;
3
+ export default powershell;
4
+ //# sourceMappingURL=powershell.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"powershell.d.ts","sourceRoot":"","sources":["../../src/langs/powershell.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AA8D5C,QAAA,MAAM,UAAU,EAAE,OAoRjB,CAAA;AAED,eAAe,UAAU,CAAA"}
@@ -0,0 +1,308 @@
1
+ /**
2
+ * PowerShell.
3
+ *
4
+ * Every state is case-insensitive because the language is: `ForEach`, `foreach`
5
+ * and `FOREACH` are one keyword, so the word lists are spelled in lowercase
6
+ * once instead of enumerating both cases. That is also why `[a-z_]` is enough
7
+ * to start an identifier here.
8
+ *
9
+ * The four things that make PowerShell awkward for a regex tokenizer, and what
10
+ * this grammar does about them:
11
+ *
12
+ * - A leading `-` is three different things. `Get-ChildItem` is a command name,
13
+ * `-eq` is an operator and `-Recurse` is a parameter. They are matched in
14
+ * that order, so the longest, most structural reading always wins.
15
+ * - Double-quoted strings interpolate `$var`, `${var}` and whole `$( ... )`
16
+ * expressions, and escape with a backtick rather than a backslash.
17
+ * - Here-strings (`@"` ... `"@`) run to a terminator that has to start a line.
18
+ * - `[Something]` is a type when it closes and an attribute when a `(` follows,
19
+ * which is the only difference between `[string]` and `[Parameter(...)]`.
20
+ *
21
+ * Where a regex cannot know, this takes the reading that is right on ordinary
22
+ * scripts and wrong on these: an unquoted hyphenated word in argument position
23
+ * reads as a command (`Content-Type = 'x'`), a class constructor reads as a
24
+ * call rather than a definition, a bare path picks up a member (`.\b.ps1`), and
25
+ * a type literal nested more than one level deep (`[List[List[int]]]`) falls
26
+ * back to plain brackets.
27
+ */
28
+ /** Identifiers are case-insensitive, so `[a-z_]` covers `[A-Za-z_]` here. */
29
+ const ID = '[a-z_]\\w*';
30
+ /**
31
+ * A dotted identifier such as `System.Collections`. Written as one quantifier
32
+ * rather than `${ID}[\\w.]*`: two adjacent word-class runs (`\\w*[\\w.]*`) let
33
+ * the engine split the same characters between them O(n) ways, so a `[` literal
34
+ * with no closing `]` or `(` would backtrack quadratically.
35
+ */
36
+ const DOTTED = '[a-z_][\\w.]*';
37
+ /**
38
+ * Word-shaped operators, including the `-c`/`-i` case-sensitivity prefixes.
39
+ * The trailing `\b` is what keeps `-not` from claiming the front of `-notlike`
40
+ * and lets `-Force` fall through to the parameter rule instead of matching the
41
+ * `-f` format operator.
42
+ */
43
+ const WORD_OPERATOR = '-[ci]?(?:eq|ne|gt|ge|lt|le|like|notlike|match|notmatch|contains|notcontains|in|notin|replace|split|join|isnot|is|as|and|or|xor|not|band|bor|bxor|bnot|shl|shr|f)\\b';
44
+ /** Type accelerators worth their own color; anything else in `[...]` is a user type. */
45
+ const BUILTIN_TYPE = 'string|char|byte|int|int32|int64|long|bool|boolean|double|float|decimal|object|array|hashtable|pscustomobject|psobject|scriptblock|switch|datetime|regex|xml|void|version';
46
+ /**
47
+ * The inside of `[...]`: a possibly dotted name, optionally followed by one
48
+ * bracketed suffix so `[string[]]` and `[List[string]]` read as one type. The
49
+ * suffix excludes both brackets, so a line of `[` cannot make it rescan.
50
+ * Deeper nesting (`[List[List[int]]]`) falls back to plain brackets.
51
+ */
52
+ const TYPE_NAME = `${DOTTED}(?:\\[[^\\][\\n]*\\])?`;
53
+ const powershell = {
54
+ name: 'powershell',
55
+ aliases: ['ps', 'ps1', 'pwsh', 'posh'],
56
+ states: {
57
+ root: {
58
+ ignoreCase: true,
59
+ rules: [
60
+ // ---- comments ------------------------------------------------------
61
+ { match: '<#', scope: 'comment', push: 'block-comment' },
62
+ // `#` only opens a comment at the start of a token, so `$a#b` and a
63
+ // bare path containing a `#` stay code.
64
+ // The separator is asserted, not consumed — consuming it scoped the
65
+ // `;` of `$a = 1;# note` and the `(` of `foo(#x` as comment text.
66
+ { match: '(?<=^|[ \\t;(])#[^\\n]*', scope: 'comment' },
67
+ // ---- strings -------------------------------------------------------
68
+ // A here-string opener has to be the last thing on its line, which is
69
+ // what tells `@"` apart from `@` followed by an ordinary string.
70
+ {
71
+ match: '@"[ \\t]*\\r?$',
72
+ scope: 'string.special',
73
+ push: 'heredoc-double',
74
+ },
75
+ {
76
+ match: "@'[ \\t]*\\r?$",
77
+ scope: 'string.special',
78
+ push: 'heredoc-single',
79
+ },
80
+ { match: '"', scope: 'string', push: 'string-double' },
81
+ { match: "'", scope: 'string', push: 'string-single' },
82
+ // Outside a string a backtick still escapes the next character — most
83
+ // often a newline, as a line continuation.
84
+ { match: '`[\\s\\S]', scope: 'string.escape' },
85
+ // ---- variables and subexpressions ----------------------------------
86
+ // `$(` and `@(` open a real expression: push so the matching `)` cannot
87
+ // be mistaken for the end of a `param(` list further out.
88
+ { match: '[$@]\\(', scope: 'punctuation.bracket', push: 'paren' },
89
+ { match: '@\\{', scope: 'punctuation.bracket' },
90
+ { include: 'variables' },
91
+ // Splatting: `@config` passes a hashtable as parameters.
92
+ { match: `@${ID}`, scope: 'variable' },
93
+ // ---- attributes and type literals ----------------------------------
94
+ // `[Parameter(...)]` is an attribute, `[string]` is a type. The only
95
+ // difference a regex can see is the `(`.
96
+ {
97
+ match: `(\\[)(${DOTTED})(?=\\()`,
98
+ scope: ['punctuation.bracket', 'decorator'],
99
+ },
100
+ {
101
+ match: `(\\[)((?:${BUILTIN_TYPE})(?:\\[[^\\][\\n]*\\])?)(\\])`,
102
+ scope: ['punctuation.bracket', 'type.builtin', 'punctuation.bracket'],
103
+ },
104
+ {
105
+ match: `(\\[)(${TYPE_NAME})(\\])`,
106
+ scope: ['punctuation.bracket', 'type', 'punctuation.bracket'],
107
+ },
108
+ // ---- member access -------------------------------------------------
109
+ {
110
+ match: `(::)(${ID})(?=[ \\t]*\\()`,
111
+ scope: ['operator', 'function.method'],
112
+ },
113
+ { match: `(::)(${ID})`, scope: ['operator', 'variable.member'] },
114
+ {
115
+ match: `(\\.)(${ID})(?=\\()`,
116
+ scope: ['punctuation', 'function.method'],
117
+ },
118
+ { match: `(\\.)(${ID})`, scope: ['punctuation', 'variable.member'] },
119
+ // ---- declarations --------------------------------------------------
120
+ // Command names may contain `-`, so the function name is `[\w-]*` where
121
+ // a class or enum name is not.
122
+ {
123
+ match: '\\b(function|filter|workflow)([ \\t]+)([a-z_][\\w-]*)([ \\t]*)(\\()',
124
+ scope: ['keyword.declaration', null, 'function', null, 'punctuation.bracket'],
125
+ push: 'params',
126
+ },
127
+ {
128
+ match: '\\b(function|filter|workflow)([ \\t]+)([a-z_][\\w-]*)',
129
+ scope: ['keyword.declaration', null, 'function'],
130
+ },
131
+ {
132
+ match: `\\b(class|enum)([ \\t]+)(${ID})`,
133
+ scope: ['keyword.declaration', null, 'class'],
134
+ },
135
+ {
136
+ match: '\\b(param)([ \\t]*)(\\()',
137
+ scope: ['keyword.declaration', null, 'punctuation.bracket'],
138
+ push: 'params',
139
+ },
140
+ {
141
+ match: '\\b(using)([ \\t]+)(namespace|module|assembly)([ \\t]+)([\\w.]+)',
142
+ scope: ['keyword.import', null, 'keyword.import', null, 'namespace'],
143
+ },
144
+ // ---- keywords ------------------------------------------------------
145
+ {
146
+ match: '\\b(?:if|elseif|else|switch|default|foreach|for|while|do|until|break|continue|return|throw|exit|try|catch|finally|trap)\\b',
147
+ scope: 'keyword.control',
148
+ },
149
+ {
150
+ match: '\\b(?:begin|process|end|clean|dynamicparam|param|hidden|static|data)\\b',
151
+ scope: 'keyword.declaration',
152
+ },
153
+ { match: '\\busing\\b', scope: 'keyword.import' },
154
+ { match: '\\bin\\b', scope: 'keyword.operator' },
155
+ // ---- commands ------------------------------------------------------
156
+ // `Verb-Noun` first, so `Import-Module` is one command rather than the
157
+ // word `Import` followed by something that looks like a parameter. The
158
+ // `\b` anchor matters: unanchored, this would rescan every word looking
159
+ // for a `-` it will not find.
160
+ { match: '\\b[a-z]\\w*-[a-z]\\w*\\b', scope: 'function.call' },
161
+ { match: WORD_OPERATOR, scope: 'keyword.operator' },
162
+ // A parameter starts a token: `$a-b` and `5-x` are arithmetic. This has
163
+ // to be a lookbehind rather than a consumed separator, because the
164
+ // merged regex picks the leftmost match before it picks the
165
+ // higher-priority rule — eating the space would make `-eq` a parameter.
166
+ { match: '(?<![\\w)\\]}\'"])-[a-z]\\w*', scope: 'variable.parameter' },
167
+ { match: `\\b${ID}(?=\\()`, scope: 'function.call' },
168
+ // ---- numbers -------------------------------------------------------
169
+ {
170
+ match: '\\b0x[0-9a-f]+(?:kb|mb|gb|tb|pb)?[ldu]?\\b',
171
+ scope: 'number',
172
+ },
173
+ { match: '\\b0b[01]+[ldu]?\\b', scope: 'number' },
174
+ {
175
+ match: '\\b\\d+(?:\\.\\d+)?(?:e[-+]?\\d+)?(?:kb|mb|gb|tb|pb)?[ldu]?\\b',
176
+ scope: 'number',
177
+ },
178
+ // ---- operators and punctuation -------------------------------------
179
+ {
180
+ match: '\\+\\+|--|\\.\\.|::|&&|\\|\\||\\?\\?=?|[-+*/%]=?|[!<>=]=?|[|&]',
181
+ scope: 'operator',
182
+ },
183
+ { match: '[()\\[\\]{}]', scope: 'punctuation.bracket' },
184
+ { match: '[,;:]', scope: 'punctuation.delimiter' },
185
+ ],
186
+ },
187
+ /**
188
+ * Variables that keep their meaning even where a `$name` is being declared,
189
+ * so `param([switch]$Force = $true)` does not read `$true` as a parameter.
190
+ */
191
+ 'special-variables': {
192
+ ignoreCase: true,
193
+ rules: [
194
+ { match: '\\$(?:true|false)\\b', scope: 'boolean' },
195
+ { match: '\\$null\\b', scope: 'constant.builtin' },
196
+ {
197
+ match: '\\$(?:_|this|args|input|matches|error|host|pwd|psitem|pscmdlet|psscriptroot|psboundparameters|psversiontable|lastexitcode|myinvocation|erroractionpreference)\\b',
198
+ scope: 'variable.builtin',
199
+ },
200
+ // `$$`, `$?` and `$^` — the last token, status and first token.
201
+ { match: '\\$[$?^]', scope: 'variable.builtin' },
202
+ {
203
+ match: `\\$((?:env|global|script|local|private|using|variable|function):)${ID}`,
204
+ scope: ['namespace'],
205
+ rest: 'variable',
206
+ },
207
+ ],
208
+ },
209
+ /**
210
+ * Shared by code and by interpolating strings, which is why the automatic
211
+ * and preference variables are recognised here rather than in `root`.
212
+ */
213
+ variables: {
214
+ ignoreCase: true,
215
+ rules: [
216
+ { include: 'special-variables' },
217
+ // `${any name}`. Both braces are excluded from the scan so a run of
218
+ // `${` cannot cost a line rescan each.
219
+ { match: '\\$\\{[^{}\\n]*\\}', scope: 'variable' },
220
+ { match: `\\$${ID}`, scope: 'variable' },
221
+ ],
222
+ },
223
+ /** Escapes and expansions shared by `"..."` and the `@"` here-string. */
224
+ interpolated: {
225
+ ignoreCase: true,
226
+ rules: [
227
+ { match: '`[\\s\\S]', scope: 'string.escape' },
228
+ { match: '\\$\\(', scope: 'interpolation', push: 'subexpression' },
229
+ { include: 'variables' },
230
+ ],
231
+ },
232
+ 'string-double': {
233
+ default: 'string',
234
+ ignoreCase: true,
235
+ rules: [
236
+ // `""` is the escaped quote; it has to beat the closing rule.
237
+ { match: '""', scope: 'string.escape' },
238
+ { include: 'interpolated' },
239
+ { match: '"', scope: 'string', pop: true },
240
+ ],
241
+ },
242
+ // Single quotes are literal: no backtick escapes, no expansion, and `''`
243
+ // is the only way to get a quote in.
244
+ 'string-single': {
245
+ default: 'string',
246
+ rules: [
247
+ { match: "''", scope: 'string.escape' },
248
+ { match: "'", scope: 'string', pop: true },
249
+ ],
250
+ },
251
+ // A here-string ends at its terminator on a line of its own. Leading
252
+ // whitespace is tolerated because indented terminators are everywhere,
253
+ // even though Windows PowerShell rejects them.
254
+ 'heredoc-double': {
255
+ default: 'string',
256
+ ignoreCase: true,
257
+ rules: [{ match: '^[ \\t]*"@', scope: 'string.special', pop: true }, { include: 'interpolated' }],
258
+ },
259
+ 'heredoc-single': {
260
+ default: 'string',
261
+ rules: [{ match: "^[ \\t]*'@", scope: 'string.special', pop: true }],
262
+ },
263
+ /** The body of `$( ... )` inside a string: ordinary code that ends at `)`. */
264
+ subexpression: {
265
+ ignoreCase: true,
266
+ rules: [
267
+ { match: '\\)', scope: 'interpolation', pop: true },
268
+ { match: '\\(', scope: 'punctuation.bracket', push: 'paren' },
269
+ { include: 'root' },
270
+ ],
271
+ },
272
+ /** Balanced parentheses, so an inner `)` never ends an outer construct. */
273
+ paren: {
274
+ ignoreCase: true,
275
+ rules: [
276
+ { match: '\\)', scope: 'punctuation.bracket', pop: true },
277
+ { match: '\\(', scope: 'punctuation.bracket', push: 'paren' },
278
+ { include: 'root' },
279
+ ],
280
+ },
281
+ /**
282
+ * A `param(...)` or `function name(...)` list, where a `$name` is being
283
+ * declared rather than used. `special-variables` comes first so a default
284
+ * value of `$true` or `$env:HOME` keeps the meaning it has everywhere else.
285
+ */
286
+ params: {
287
+ ignoreCase: true,
288
+ rules: [
289
+ { match: '\\)', scope: 'punctuation.bracket', pop: true },
290
+ { match: '\\(', scope: 'punctuation.bracket', push: 'paren' },
291
+ { include: 'special-variables' },
292
+ { match: `\\$${ID}`, scope: 'variable.parameter' },
293
+ { include: 'root' },
294
+ ],
295
+ },
296
+ 'block-comment': {
297
+ default: 'comment',
298
+ ignoreCase: true,
299
+ rules: [
300
+ { match: '#>', pop: true },
301
+ // Comment-based help: `.SYNOPSIS`, `.PARAMETER Name` and friends are
302
+ // the reason most block comments exist, so they read as documentation.
303
+ { match: '^([ \\t]*)(\\.[a-z]+)\\b', scope: [null, 'comment.doc'] },
304
+ ],
305
+ },
306
+ },
307
+ };
308
+ export default powershell;
@@ -0,0 +1,4 @@
1
+ import type { Grammar } from '../core/types.js';
2
+ declare const python: Grammar;
3
+ export default python;
4
+ //# sourceMappingURL=python.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"python.d.ts","sourceRoot":"","sources":["../../src/langs/python.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AA6B5C,QAAA,MAAM,MAAM,EAAE,OAwWb,CAAA;AAED,eAAe,MAAM,CAAA"}