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 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
|
.. highlight:: javascript
Mode Reference
==============
Types
-----
Types of attributes values in this reference:
+------------+-------------------------------------------------------------------------------------+
| identifier | String suitable to be used as a JavaScript variable and CSS class name |
| | (i.e. mostly ``/[A-Za-z0-9_]+/``) |
+------------+-------------------------------------------------------------------------------------+
| regexp | String representing a JavaScript regexp. |
| | Note that since it's not a literal regexp all back-slashes should be repeated twice |
+------------+-------------------------------------------------------------------------------------+
| boolean | JavaScript boolean: ``true`` or ``false`` |
+------------+-------------------------------------------------------------------------------------+
| number | JavaScript number |
+------------+-------------------------------------------------------------------------------------+
| object | JavaScript object: ``{ ... }`` |
+------------+-------------------------------------------------------------------------------------+
| array | JavaScript array: ``[ ... ]`` |
+------------+-------------------------------------------------------------------------------------+
Language Only Attributes
------------------------
These attributes are only valid at the language level (ie, they many only exist on the top-most language object and have no meaning if specified in children modes).
name
^^^^
- **type**: string
The canonical name of this language, ie "JavaScript", etc.
case_insensitive
^^^^^^^^^^^^^^^^
- **type**: boolean
Case insensitivity of language keywords and regexps. Used only on the top-level mode.
aliases
^^^^^^^
- **type**: array
A list of additional names (besides the canonical one given by the filename) that can be used to identify a language in HTML classes and in a call to :ref:`getLanguage <getLanguage>`.
classNameAliases
^^^^^^^^^^^^^^^^
- **type**: object
A mapping table of any custom class names your grammar uses and their supported equivalencies. Perhaps your language has a concept of "slots" that roughly correspond to variables in other languages. This allows you to write grammar code like:
::
{
classNameAliases: {
slot: "variable",
"message-name": "string"
},
contains: [
{
className: "slot",
begin: // ...
}
]
}
The final HTML output will render slots with the CSS class as ``hljs-variable``. This feature exists to make it easier for grammar maintainers to think in their own language when maintaining a grammar.
For a list of all supported class names please see the :doc:`CSS class reference
</css-classes-reference>`.
disableAutodetect
^^^^^^^^^^^^^^^^^
- **type**: boolean
Disables autodetection for this language.
compilerExtensions (USE WITH CAUTION)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- **type**: an array of compiler extensions ie: ``(mode, parentMode) -> {} ``
This allows grammars to extend the mode compiler to add their own syntactic
sugar to make reading and writing grammars easier. **Note: This is heavily
dependent upon compiler internals and may NOT be stable from minor release to
minor release.** *It is currently recommended only for 1st party grammars.* The
intention is that we use grammars to "test" out new compiler extensions and if
they perform well promote them into the core library.
mode
The incoming mode object
parentMode
The parent mode of the mode (null for the top level language mode)
For example lets look at a tiny well behaved extension to allow us to write
``match`` as sugar to better express the intent to "match a single thing, then
end mode".
::
compilerExtensions: [
(mode, _parentMode) => {
// first some quick sanity checks
if (!mode.match) return;
// then check for users doing things that would make no sense
if (mode.begin || mode.end) throw new Error("begin & end are not supported with match");
// copy the match regex into begin
mode.begin = mode.match;
// cleanup: delete our syntactic construct
delete mode.match;
}
]
Compiler extension functions return nothing. They are expected to mutate the
mode itself.
Mode Attributes
---------------
className
^^^^^^^^^
- **type**: identifier
The name of the mode. It is used as a class name in HTML markup.
Multiple modes can have the same name. This is useful when a language has multiple variants of syntax
for one thing like string in single or double quotes.
begin
^^^^^
- **type**: regexp
Regular expression starting a mode. For example a single quote for strings or two forward slashes for C-style comments.
If absent, ``begin`` defaults to a regexp that matches anything, so the mode starts immediately.
match
^^^^^
- **type**: regexp
This is simply syntactic sugar for a ``begin`` when no ``end`` expression is
necessary. It may not be used with ``begin`` or ``end`` keys (that would make
no sense). It exists simply to help make grammars more readable.
::
{
className: "title",
match: /Fish/
}
on:begin
^^^^^^^^
- **type**: callback (matchData, response)
This callback is triggered the moment a begin match is detected. ``matchData`` includes the typical regex match data; the full match, match groups, etc. The ``response`` object is used to tell the parser how it should handle the match. It can be also used to temporarily store data.
- ``response.data`` - a simple object data store. Can be used for building more complex rules where the end rule is dependent on the content of begin, etc.
- ``response.ignoreMatch()`` - pretend as if this match never happened. The mode is not entered. Continues trying subsequent modes in the current mode's ``contains`` list
For an example of usage see ``END_SAME_AS_BEGIN`` in ``modes.js``.
end
^^^
- **type**: regexp
Regular expression ending a mode. For example a single quote for strings or "$" (end of line) for one-line comments.
It's often the case that a beginning regular expression defines the entire mode and doesn't need any special ending.
For example a number can be defined with ``begin: "\\b\\d+"`` which spans all the digits.
If absent, ``end`` defaults to a regexp that matches anything, so the mode ends immediately (after possibly
matching any ``contains`` sub-modes).
Sometimes a mode can end not by itself but implicitly with its containing (parent) mode.
This is achieved with :ref:`endsWithParent <endsWithParent>` attribute.
on:end
^^^^^^
- **type**: callback (matchData, response)
This callback is triggered the moment an end match is detected. ``matchData`` includes the typical regex match data; the full match, match groups, etc. The ``response`` object is used to tell the parser how it should handle the match. It can also be used to retrieve data stored from a `begin` callback.
- ``response.data`` - a simple object data store. Can be used for building more complex rules where the end rule is dependent on the content of begin, etc.
- ``response.ignoreMatch()`` - pretend as if this match never happened. The mode is not entered. Continues trying subsequent modes in the current mode's ``contains`` list
For an example of usage see ``END_SAME_AS_BEGIN`` in ``modes.js``.
beginKeywords
^^^^^^^^^^^^^
- **type**: string
Used instead of ``begin`` for modes starting with keywords to avoid needless repetition:
::
{
begin: '\\b(class|interface)\\b',
keywords: 'class interface'
}
… can often be shortened to:
::
{
beginKeywords: 'class interface'
}
Unlike the :ref:`keywords <keywords>` attribute, this one allows only a simple list of space separated keywords.
If you do need additional features of ``keywords`` or you just need more keywords for this mode you may include ``keywords`` along with ``beginKeywords``.
Note: ``beginKeywords`` also checks for a ``.`` before or after the keywords and will fail to match if one is found.
This is to avoid false positives for method calls or property accesses.
Ex. ``class A { ... }`` would match while ``A.class == B.class`` would not.
.. _endsWithParent:
endsWithParent
^^^^^^^^^^^^^^
- **type**: boolean
A flag showing that a mode ends when its parent ends.
This is best demonstrated by example. In CSS syntax a selector has a set of rules contained within symbols "{" and "}".
Individual rules separated by ";" but the last one in a set can omit the terminating semicolon:
::
p {
width: 100%; color: red
}
This is when ``endsWithParent`` comes into play:
::
{
className: 'rules', begin: /\{/, end: /\}/,
contains: [
{className: 'rule', /* ... */ end: ';', endsWithParent: true}
]
}
.. _endsParent:
endsParent
^^^^^^^^^^^^^^
- **type**: boolean
Forces closing of the parent mode right after the current mode is closed.
This is used for modes that don't have an easily expressible ending lexeme but
instead could be closed after the last interesting sub-mode is found.
Here's an example with two ways of defining functions in Elixir, one using a
keyword ``do`` and another using a comma:
::
def foo :clear, list do
:ok
end
def foo, do: IO.puts "hello world"
Note that in the first case the parameter list after the function title may also
include a comma. And if we're only interested in highlighting a title we can
tell it to end the function definition after itself:
::
{
className: 'function',
beginKeywords: 'def', end: /\B\b/,
contains: [
{
className: 'title',
begin: hljs.IDENT_RE, endsParent: true
}
]
}
(The ``end: /\B\b/`` regex tells function to never end by itself.)
.. _endSameAsBegin:
endSameAsBegin (deprecated as of 10.1)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
**Deprecated:** *This attribute has been deprecated.* You should instead use the
``END_SAME_AS_BEGIN`` mode or use the ``on:begin`` and ``on:end`` attributes to
build more complex paired matchers.
- **type**: boolean
Acts as ``end`` matching exactly the same string that was found by the
corresponding ``begin`` regexp.
For example, in PostgreSQL string constants can use "dollar quotes",
consisting of a dollar sign, an optional tag of zero or more characters,
and another dollar sign. String constant must be ended with the same
construct using the same tag. It is possible to nest dollar-quoted string
constants by choosing different tags at each nesting level:
::
$foo$
...
$bar$ nested $bar$
...
$foo$
In this case you can't simply specify the same regexp for ``begin`` and
``end`` (say, ``"\\$[a-z]\\$"``), but you can use ``begin: "\\$[a-z]\\$"``
and ``endSameAsBegin: true``.
.. _lexemes:
lexemes (now keywords.$pattern)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- **type**: regexp
A regular expression that extracts individual "words" from the code to compare
against :ref:`keywords <keywords>`. The default value is ``\w+`` which works for
many languages.
Note: It's now recommmended that you use ``keywords.$pattern`` instead of
``lexemes``, as this makes it easier to keep your keyword pattern associated
with your keywords themselves, particularly if your keyword configuration is a
constant that you repeat multiple times within different modes of your grammar.
.. _keywords:
keywords
^^^^^^^^
- **type**: object / string / array
Keyword definition comes in three forms:
* ``'for while if|0 else weird_voodoo|10 ... '`` -- a string of space-separated keywords with an optional relevance over a pipe
* ``{keyword: ' ... ', literal: ' ... ', $pattern: /\w+/ }`` -- an object that describes multiple sets of keywords and the pattern used to find them
* ``["for", "while", "if|0", ...]`` -- an array of keywords (with optional relevance via ``|``)
For detailed explanation see :doc:`Language definition guide </language-guide>`.
illegal
^^^^^^^
- **type**: regexp or array
A regular expression or array that defines symbols illegal for the mode.
When the parser finds a match for illegal expression it immediately drops parsing the whole language altogether.
::
{
illegal: /%/,
// or using an array
illegal: [ /%/, /cookies/ ]
}
excludeBegin, excludeEnd
^^^^^^^^^^^^^^^^^^^^^^^^
- **type**: boolean
Exclude beginning or ending lexemes out of mode's generated markup. For example in CSS syntax a rule ends with a semicolon.
However visually it's better not to color it as the rule contents. Having ``excludeEnd: true`` forces a ``<span>`` element for the rule to close before the semicolon.
returnBegin
^^^^^^^^^^^
- **type**: boolean
Returns just found beginning lexeme back into parser. This is used when beginning of a sub-mode is a complex expression
that should not only be found within a parent mode but also parsed according to the rules of a sub-mode.
Since the parser is effectively goes back it's quite possible to create a infinite loop here so use with caution!
returnEnd
^^^^^^^^^
- **type**: boolean
Returns just found ending lexeme back into parser. This is used for example to parse JavaScript embedded into HTML.
A JavaScript block ends with the HTML closing tag ``</script>`` that cannot be parsed with JavaScript rules.
So it is returned back into its parent HTML mode that knows what to do with it.
Since the parser is effectively goes back it's quite possible to create a infinite loop here so use with caution!
contains
^^^^^^^^
- **type**: array
The list of sub-modes that can be found inside the mode. For detailed explanation see :doc:`Language definition guide </language-guide>`.
starts
^^^^^^
- **type**: identifier
The name of the mode that will start right after the current mode ends. The new mode won't be contained within the current one.
Currently this attribute is used to highlight JavaScript and CSS contained within HTML.
Tags ``<script>`` and ``<style>`` start sub-modes that use another language definition to parse their contents (see :ref:`subLanguage`).
variants
^^^^^^^^
- **type**: array
Modification to the main definitions of the mode, effectively expanding it into several similar modes
each having all the attributes from the main definition augmented or overridden by the variants::
{
className: 'string',
contains: ['self', hljs.BACKSLASH_ESCAPE],
relevance: 0,
variants: [
{begin: /"/, end: /"/},
{begin: /'/, end: /'/, relevance: 1}
]
}
Note: ``variants`` has very specific behavior with regards to ``contains: ['self']``.
Lets consider the example above. While you might think this would allow you to
embed any type of string (double or single quoted) within any other string, it
does not allow for this.
The variants are compiled into to two *discrete* modes::
{ className: 'string', begin: /"/, contains: ['self', ... ] }
{ className: 'string', begin: /'/, contains: ['self', ... ] }
Each mode's ``self`` refers only to the new expanded mode, not the original mode
with variants (which no longer exists after compiling).
Further info: https://github.com/highlightjs/highlight.js/issues/826
.. _subLanguage:
subLanguage
^^^^^^^^^^^
- **type**: string or array
Highlights the entire contents of the mode with another language.
When using this attribute there's no point to define internal parsing rules like :ref:`lexemes` or :ref:`keywords`. Also it is recommended to skip ``className`` attribute since the sublanguage will wrap the text in its own ``<span class="language-name">``.
The value of the attribute controls which language or languages will be used for highlighting:
* language name: explicit highlighting with the specified language
* empty array: auto detection with all the languages available
* array of language names: auto detection constrained to the specified set
skip
^^^^
- **type**: boolean
Skips any markup processing for the mode ensuring that it remains a part of its
parent buffer along with the starting and the ending lexemes. This works in
conjunction with the parent's :ref:`subLanguage` when it requires complex
parsing.
Consider parsing PHP inside HTML::
<p><? echo 'PHP'; /* ?> */ ?></p>
The ``?>`` inside the comment should **not** end the PHP part, so we have to
handle pairs of ``/* .. */`` to correctly find the ending ``?>``::
{
begin: /<\?/, end: /\?>/,
subLanguage: 'php',
contains: [{begin: '/\\*', end: '\\*/', skip: true}]
}
Without ``skip: true`` every comment would cause the parser to drop out back
into the HTML mode.
|