Skip to content

Commit 6e1e6dd

Browse files
authored
Add custom BNF highlighter: code, CSS, example usage (#338)
1 parent 0f96c6d commit 6e1e6dd

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

preview-src/code.adoc

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,3 +210,31 @@ var expandCollapseBlock = function (e) {
210210
}
211211
}
212212
----
213+
214+
== BNF syntax example
215+
216+
=== Single-line rules
217+
218+
[source, gql-bnf]
219+
----
220+
<simple match statement> ::= "MATCH" <graph pattern>
221+
<path pattern> ::= [ <identifier> "=" ] [ <path pattern prefix> ] <path pattern expression>
222+
----
223+
224+
=== Multi-line rules
225+
226+
[source, gql-bnf]
227+
----
228+
<simple match statement> ::=
229+
"MATCH" <graph pattern> "{}"
230+
231+
<graph pattern> ::=
232+
[ <match mode> ] <path pattern> [ { "," <path pattern> }... ] [ <graph pattern where clause> ]
233+
234+
<match mode> ::=
235+
"REPEATABLE" { "ELEMENT" [ "BINDINGS" ] | "ELEMENTS" }
236+
| "DIFFERENT" { "RELATIONSHIP" [ "BINDINGS" ] | "RELATIONSHIPS" }
237+
238+
<path pattern> ::=
239+
[ <identifier> "=" ] [ <path pattern prefix> ] <path pattern expression>
240+
----

src/css/highlight.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@
5454
color: rgba(var(--colors-forest-40));
5555
}
5656

57+
.hljs-operator {
58+
color: rgba(var(--colors-neutral-50));
59+
}
60+
5761
.hljs-symbol,
5862
.hljs-bullet {
5963
color: var(--color-indigo-800);

src/js/vendor/highlight.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,34 @@
128128
]
129129
}
130130
})
131+
// Custom flavor of BNF syntax defined in GQL standard docs
132+
hljs.registerLanguage('gql-bnf',
133+
function gql_bnf(hljs) {
134+
return {
135+
name: 'Custom BNF syntax for GQL',
136+
contains: [
137+
{
138+
// Use "attribute" for nonterminals as the included BNF/ABNF/EBNF do
139+
className: 'attribute',
140+
begin: /</,
141+
end: />/
142+
},
143+
{
144+
// It should be enough to treat syntax extension symbols just as simple operators.
145+
// Symbols include:
146+
// - Square brackets (optional elements)
147+
// - Braces (element group)
148+
// - Vertical bar (alternative operator)
149+
// - Ellipsis (element repetition)
150+
className: 'operator',
151+
begin: /[[\]{}|]|\.\.\./
152+
},
153+
// Double quote-delimited strings
154+
hljs.QUOTE_STRING_MODE,
155+
hljs.C_LINE_COMMENT_MODE
156+
]
157+
};
158+
})
131159
hljs.highlightAll()
132160

133161
// Apply line highlighting to lines marked with `// marked-line`

0 commit comments

Comments
 (0)