-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathshexc.js
252 lines (237 loc) · 8.06 KB
/
shexc.js
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
/*
Language: ShExC
Description: Shape Expressions Compact Syntax, a schema language for graphs, may be good for Turtle
Author: Eric Prud'hommeaux <eric+github@w3.org>
Category: misc
*/
/**
* highlight.js ShExC syntax highlighting definition
*
* @see https://github.com/ericprud/highlightjs-shexc
*
* @package: highlightjs-shexc
* @author: Eric Prud'hommeaux <eric@w3.org>
* @since: 2019-21-12
*
*
* Maintenance notes:
*
* This module is largely transliterated from the ShExC grammar.
* <http://shex.io/shex-semantics/index.html#shexc> For terminals, this acts as
* a validator. If e.g. your URL doesn't show up with syntax highlighting, it's
* probably malformed.
*
* Anything with begin: iris_RE gets relevance: 0 in order not to assume
* anything with URLs is ShExC.
*
* TODO, in decreasing priority:
* annotations <http://shex.io/shex-semantics/index.html#prod-annotation>
* semacts <http://shex.io/shex-semantics/index.html#prod-semanticAction>
* strings <http://shex.io/shex-semantics/index.html#prod-string>
* comments in odd places.
*
* Limitations: There is no way (afaik) in highlight.js to assign classes based
* on position. As a result, a TripleConstraint with a predicate and a datatype
* will have the same class applied to both.
*/
module.exports = function (hljs, opts = {}) {
/** terminals from <http://shex.io/shex-semantics/index.html#term-IRIREF>
* <IRIREF> ::= "<" ([^#0000- <>\"{}|^`\\] | UCHAR)* ">"
* <PNAME_NS> ::= PN_PREFIX? ":"
* <PNAME_LN> ::= PNAME_NS PN_LOCAL
* ... (see link for the rest)
*/
const HEX_RE = '[0-9a-fA-F]'
const UCHAR_RE = '\\\\(?:u' + HEX_RE + '{4}|U' + HEX_RE + '{8})'
const IRIREF_RE = '<([^<>"{}|^`\\\\]|' + UCHAR_RE + ')*>'
const PN_CHARS_BASE_RE = '[a-zA-Z]'
const PN_CHARS_U_RE = [PN_CHARS_BASE_RE, '_'].join('|')
const PN_CHARS_RE = [PN_CHARS_U_RE, '-', '[0-9]'].join('|')
const PN_PREFIX_RE = PN_CHARS_BASE_RE + '((' + PN_CHARS_RE + '|\\.)*' + PN_CHARS_RE + ')?'
const PNAME_NS_RE = '(' + PN_PREFIX_RE + ')?:'
const PN_LOCAL_ESC_RE = '\\\\[_~.!$&\'()*+,;=/?#@%-]'
const PERCENT_RE = '%' + HEX_RE + HEX_RE
const PLX_RE = [PERCENT_RE, PN_LOCAL_ESC_RE].join('|')
const PN_LOCAL_RE = '(' + [PN_CHARS_U_RE, ':', '[0-9]', PLX_RE].join('|') + ')'
+ '(' + '(' + [PN_CHARS_RE, '\\.', ':', PLX_RE].join('|') + ')' + ')*'
const PNAME_LN_RE = PNAME_NS_RE + PN_LOCAL_RE
/** IRI forms from <https://shexspec.github.io/spec/#prod-iri>
* iri ::= IRIREF | prefixedName
* prefixedName ::= PNAME_LN | PNAME_NS
* ... (see link for the rest)
*/
const prefixedName_RE = PNAME_LN_RE + '|' + PNAME_NS_RE
const iris_RE = '(' + [prefixedName_RE, IRIREF_RE].join('|') + ')'
const PERCENT = { className: 'meta-keyword', begin: PERCENT_RE }
const UCHAR = { className: 'meta-keyword', begin: UCHAR_RE }
const PN_LOCAL_ESC = { className: 'meta-keyword', begin: PN_LOCAL_ESC_RE }
const productions = {}
productions.IRIREF = {
className: 'symbol',
begin: /</, end: />/, // can't use begin: IRIREF_RE because of contains.
contains: [ PERCENT, UCHAR ]
}
productions.prefixedName = {
begin: prefixedName_RE,
returnBegin: true,
contains: [
{
className: "type",
begin: PNAME_NS_RE,
},
{
className: "variable",
begin: PN_LOCAL_RE,
endsWithParent: true,
contains: [PN_LOCAL_ESC], // doesn't work
},
]
}
/** Special regexp which consumes rest of string. */
const EndOfDocument = /\B\b/
/** directives from <https://shexspec.github.io/spec/#prod-directive>
* baseDecl ::= "BASE" IRIREF
* prefixDecl ::= "PREFIX" PNAME_NS IRIREF
* importDecl ::= "IMPORT" IRIREF
*/
productions.prefix = {
beginKeywords: "prefix",
// begin: "prefix",
end: EndOfDocument,
returnBegin: true,
contains: [
// { // not needed if using beginKeywords in parent
// className: "keyword",
// beginKeywords: 'prefix',
// },
{
className: "type",
begin: PNAME_NS_RE,
},
Object.assign({ endsParent: true }, productions.IRIREF),
]
}
productions.base = {
beginKeywords: "base",
end: EndOfDocument,
returnBegin: true,
contains: [
Object.assign({ endsParent: true }, productions.IRIREF),
]
}
productions._import = { // Need a leading '_' because "import" is a js keyword.
beginKeywords: "import",
end: EndOfDocument,
returnBegin: true,
contains: [
Object.assign({ endsParent: true }, productions.IRIREF),
]
}
/** shape expressions from <http://shex.io/shex-semantics/index.html#prod-shapeExpression>
*/
productions.shape = {
begin: /{/, end: /}/,
relevance: 0
// Add .contains (below) after constructing its contents.
}
const shapeExprContentModel = [
hljs.HASH_COMMENT_MODE,
hljs.C_BLOCK_COMMENT_MODE,
/** <http://shex.io/shex-semantics/index.html#prod-shapeExprLabel>
* shapeExprDecl ::= shapeExprLabel (shapeExpression | "EXTERNAL")
*/
{
className: 'title',
begin: iris_RE,
contains: [ PERCENT, UCHAR ],
relevance: 0
},
/** <http://shex.io/shex-semantics/index.html#prod-shapeRef>
* shapeRef ::= ATPNAME_LN | ATPNAME_NS | '@' shapeExprLabel
*/
{
className: 'name',
begin: '@' + iris_RE,
contains: [ PERCENT, UCHAR ],
relevance: 10
},
/**
* shapeDefinition ::= (extraPropertySet | "CLOSED")* '{' tripleExpression? '}'
* annotation* semanticActions
*/
{
beginKeywords: 'extra closed', end: /{/,
returnEnd: true,
contains: [productions.IRIREF, productions.prefixedName],
relevance: 10
},
/** simplified form of <http://shex.io/shex-semantics/index.html#term-REGEXP>
<REGEXP> ::= '/' ([^/\\\n\r]
| '\\' [nrt\\|.?*+(){}$-\[\]^/]
| UCHAR
)+ '/' [smix]*
*/
{
className: 'regexp',
begin: /\/([^\\/]|\\.)*\//,
contains: [
hljs.REGEXP_MODE,
],
},
productions.shape,
]
const shapeExpression_keywords = 'and or not closed abstract extends restricts iri bnode literal nonliteran'
+ ' length minlength maxlength'
+ ' mininclusive minexclusive maxinclusive maxexclusive'
productions.shapeExpression = {
begin: iris_RE,
end: EndOfDocument,
returnBegin: true,
keywords: shapeExpression_keywords,
contains: shapeExprContentModel,
relevance: 0
}
/** shape expressions from <http://shex.io/shex-semantics/index.html#prod-unaryTripleExpr>
*/
productions.tripleExpression = {
begin: iris_RE,
end: EndOfDocument,
returnBegin: true,
endsWithParent: true,
keywords: shapeExpression_keywords,
contains: [productions.IRIREF, productions.prefixedName].concat(shapeExprContentModel),
relevance: 0
}
productions.tripleExprLabel = {
className: 'name',
begin: '$' + iris_RE,
contains: [ PERCENT, UCHAR ],
relevance: 10
}
productions.inclusion = {
className: 'name',
begin: '&' + iris_RE,
contains: [ PERCENT, UCHAR ],
relevance: 10
}
// The root language is called "shexDoc" <http://shex.io/shex-semantics/index.html#prod-shexDoc>
productions.shexDoc = {
case_insensitive: true,
contains: [
hljs.HASH_COMMENT_MODE,
hljs.C_BLOCK_COMMENT_MODE,
productions.prefix,
productions.base,
productions._import,
productions.shapeExpression,
],
relevance: 10
}
// Add last component in this cycle:
// shape ➜ tripleExpression ➜ shapeExpression ➜ shape
productions.shape.contains = [productions.tripleExprLabel, productions.inclusion, productions.tripleExpression]
const startingProduction = opts.startingProduction || 'shexDoc'
if (!(startingProduction in productions))
throw Error(`starting production ${startingProduction} not found in ${Object.keys(productions).join(', ')}}`)
return productions[startingProduction]
}