Skip to content

Commit 4dc08da

Browse files
authored
Updated antlr highlighter to better reflect language (#239)
1 parent 85ff1c9 commit 4dc08da

File tree

2 files changed

+101
-58
lines changed

2 files changed

+101
-58
lines changed

docfx/templates/Ubiquity/public/antlr.js

Lines changed: 97 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -4,58 +4,108 @@ Simple antlr syntax highlighting for Highlight.JS
44

55
export function antlr(hljs)
66
{
7-
return {
8-
aliases: ['antlr'],
7+
// lexer rules always begin with uppercase character
8+
const lexerIdent_RE = '\\b[A-Z]' + hljs.UNDERSCORE_IDENT_RE + '\\b';
9+
const lexerIdent = {
10+
scope: 'LexerRuleName',
11+
begin: lexerIdent_RE,
12+
relevance: 0
13+
};
14+
15+
// grammar rules always begin with a lowercase character
16+
const grammarIdent_RE = '\\b[a-z]' + hljs.UNDERSCORE_IDENT_RE + '\\b';
17+
const grammarIdent = {
18+
scope: 'GrammarRuleName',
19+
begin: grammarIdent_RE,
20+
relevance: 0
21+
};
22+
23+
const semanticPredicate = {
24+
scope: 'SemanticPredicate',
25+
begin: '{',
26+
end: /}\?/,
27+
};
28+
29+
const lexerCommand = {
30+
scope: 'LexerCommand',
31+
begin: '->',
32+
beginScope: 'punctuation',
33+
end: ';',
34+
endScope: 'punctuation',
35+
endsParent: true,
936
keywords: {
10-
keyword: 'grammar fragment'
37+
built_in: 'skip more popMode mode pushMode type channel'
38+
}
39+
};
40+
41+
const lexerRule = {
42+
scope: 'LexerRule',
43+
begin: [lexerIdent_RE, '\\s*', ':', '\\s*'],
44+
beginScope: {
45+
1: 'LexerRuleName',
46+
3: 'punctuation',
1147
},
48+
end: ';',
49+
endScope: 'punctuation',
50+
relevance: 10,
51+
contains: [
52+
semanticPredicate,
53+
lexerCommand,
54+
lexerIdent,
55+
hljs.C_LINE_COMMENT_MODE,
56+
hljs.APOS_STRING_MODE,
57+
hljs.QUOTE_STRING_MODE,
58+
hljs.BACKSLASH_ESCAPE,
59+
]
60+
};
61+
62+
const grammarAltName = {
63+
begin: "#",
64+
end: /\s*$/,
1265
contains: [
13-
hljs.COMMENT('//', '\\n', { relevance: 0 }),
14-
{
15-
scope: 'regexp',
16-
variants: [
17-
{ begin: '\\[', end: '\\]' }
18-
],
19-
relevance: 0
20-
},
21-
{
22-
scope: 'string',
23-
variants: [
24-
{ begin: '\'', end: '\'' },
25-
{ begin: '"', end: '"' }
26-
],
27-
relevance: 0
28-
},
29-
{
30-
scope: 'keyword',
31-
variants: [
32-
{ begin: 'i\\d+' }
33-
],
34-
relevance: 0
35-
},
36-
{
37-
scope: 'meta',
38-
variants: [
39-
{ begin: '{', end: '}' },
40-
{ begin: '{', end: '}\?' }
41-
],
42-
relevance: 0
43-
},
44-
{
45-
scope: 'number',
46-
variants: [
47-
{ begin: '0[xX][a-fA-F0-9]+' },
48-
{ begin: '-?\\d+(?:[.]\\d+)?(?:[eE][-+]?\\d+(?:[.]\\d+)?)?' }
49-
],
50-
relevance: 0
51-
},
5266
{
53-
scope: 'symbol',
54-
variants: [
55-
{ begin: '([A-Z][\\w\\-$.]*)' }
56-
],
57-
relevance: 0
67+
scope: 'GrammarAltName',
68+
begin: hljs.UNDERSCORE_IDENT_RE,
5869
}
5970
]
6071
};
72+
73+
const grammarRule = {
74+
scope: 'GrammarRule',
75+
begin: [grammarIdent_RE, '\\s*', ':', '\\s*'],
76+
beginScope: {
77+
1: "GrammarRuleName",
78+
3: "punctuation",
79+
},
80+
end: ';',
81+
endScope: 'punctuation',
82+
relevance: 10,
83+
contains: [
84+
grammarAltName,
85+
semanticPredicate,
86+
lexerIdent,
87+
grammarIdent,
88+
hljs.C_LINE_COMMENT_MODE,
89+
hljs.APOS_STRING_MODE,
90+
hljs.QUOTE_STRING_MODE,
91+
hljs.C_NUMBER_MODE,
92+
]
93+
};
94+
95+
return {
96+
name: "ANTLR V4 (.g4)",
97+
aliases: ['antlr'],
98+
keywords: ['grammar', 'fragment', 'skip'],
99+
classNameAliases: {
100+
LexerRuleName: 'type',
101+
GrammarRuleName: 'type',
102+
GrammarAltName: 'function',
103+
// TODO: Fill these in once tagging is complete.
104+
},
105+
contains: [
106+
hljs.C_LINE_COMMENT_MODE,
107+
lexerRule,
108+
grammarRule,
109+
]
110+
};
61111
}

docfx/templates/Ubiquity/readme.md

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,8 @@
11
# Ubiquity DOCFX template
2-
This template adds support to the syntx highlighting provided by [HightlightJS](https://highlightjs.readthedocs.io/en/latest/supported-languages.html).
3-
The languages added are for ANTLR^1^ (Which seems bizarre it isn't already covered given the
2+
This template adds support to the syntax highlighting provided by [HightlightJS](https://highlightjs.readthedocs.io/en/latest/supported-languages.html).
3+
The languages added are for ANTLR (Which seems bizarre it isn't already covered given the
44
esoteric nature of some of the supported languages...) and of course the `Kaleidoscope`
55
language, which was made up entirely for the purposes of LLVM tutorials. (No surprise that one
6-
isn't supported)
6+
isn't supported) [Though it oddly IS supported directly in the [Local MarkDig
7+
based editor](https://github.com/MadsKristensen/MarkdownEditor2022) used to edit these files...
78

8-
^1^The support for ANTLR is not present. DOCFX officially documents that Highlight JS
9-
extensibility is supported by this. It even has an example. However, that example uses
10-
a language that is built-in to Highlight JS already. It doesn't seem to actually use
11-
this extention point as the Kaleidoscope highlighting never happens ([Local MarkDig
12-
based editor](https://github.com/MadsKristensen/MarkdownEditor2022) will render the
13-
ANTLR with syntax highlighting [That seems to be using PRISM for highlighting though].
14-
But the final docs hosted by DOCFX do not. Thus, this functionality is currently
15-
broken.

0 commit comments

Comments
 (0)