-
Notifications
You must be signed in to change notification settings - Fork 1
/
ShExDoc.g4
313 lines (301 loc) · 12 KB
/
ShExDoc.g4
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
// ANTLR4 Equivalent of accompanying bnf, developed in
// http://www.w3.org/2005/01/yacker/uploads/ShEx3
// Updated to Jul 27 AM ShEx3
// Updated to Aug 23 AM ShEx3 (last change was EGP 20150820)
// Sept 21 AM disallow single internal unary (e.g. {(:p .{2}){3}}
// Change (non-standard) "codeLabel" to "productionName"
// Oct 26 - change annotation predicate to include rdftype (how did this slip in to the production rules?
// Dec 30 - update to match http://www.w3.org/2005/01/yacker/uploads/ShEx2/bnf with last change "EGP 20151120"
// May 23, 2016 - Update to match http://www.w3.org/2005/01/yacker/uploads/ShEx2/bnf with last change "EGP20160520" AND ';' separator and '//' for annotations
// May 24, 2016 - EGP20150424
// Aug 11, 2016 - EGP20160708
// Sep 14, 2016 - Revised to match Eric's latest reshuffle
// Sep 24, 2016 - Switched to TT grammar (vs inner and outer shapes)
// Sep 26, 2016 - Refactored to match https://raw.githubusercontent.com/shexSpec/shex.js/7eb770fe2b5bab9edfe9558dc07bb6f6dcdf5d23/doc/bnf
// Oct 27, 2016 - Added comments to '*', '*' and '?' to facilitate parsing
// Oct 27, 2016 - Added qualifier rule to be reused by shapeDefinition and inlineShapeDefinition
// Oct 27, 2016 - Added negation rule
// Mar 03, 2017 - removed ^^-style facet arguments per shex#41
// Mar 03, 2017 - switch to ~/regexp/
// Apr 09, 2017 - removed WS fragment (unused)
// Apr 09, 2017 - revise REGEXP definition
// Apr 09, 2017 - factor out REGEXP_FLAGS so we don't have to parse them out
// Apr 09, 2017 - literalRange / languageRange additions
// Apr 09, 2017 - factor out shapeRef to match spec
// Apr 09, 2017 - update repeatRange to allow differentiation of {INTEGER} and {INTEGER,}
// Apr 09, 2017 - add STEM_MARK and UNBOUNDED tokens to eliminate lex token parsingf
// Apr 17, 2018 - add 2.1 rules -- extensions, restrictions and ABSTRACT
// Aug 13, 2018 - Re-order rules to approximate Yakker BNF
// Aug 13, 2018 - Remove '!' for NOT
// Aug 13, 2018 - Add annotations and semanticActions to nodeconstraint
grammar ShExDoc;
shExDoc : directive* ((notStartAction | startActions) statement*)? EOF; // leading CODE
directive : baseDecl
| prefixDecl
| importDecl
;
baseDecl : KW_BASE IRIREF ;
prefixDecl : KW_PREFIX PNAME_NS IRIREF ;
importDecl : KW_IMPORT IRIREF ;
notStartAction : start | shapeExprDecl ;
start : KW_START '=' shapeExpression ;
startActions : semanticAction+ ;
statement : directive | notStartAction ;
shapeExprDecl : KW_ABSTRACT? shapeExprLabel restrictions* (shapeExpression | KW_EXTERNAL) ;
shapeExpression : shapeOr ;
inlineShapeExpression : inlineShapeOr ;
shapeOr : shapeAnd (KW_OR shapeAnd)* ;
inlineShapeOr : inlineShapeAnd (KW_OR inlineShapeAnd)* ;
shapeAnd : shapeNot (KW_AND shapeNot)* ;
inlineShapeAnd : inlineShapeNot (KW_AND inlineShapeNot)* ;
shapeNot : KW_NOT? shapeAtom ;
inlineShapeNot : KW_NOT? inlineShapeAtom ;
shapeAtom : nonLitNodeConstraint shapeOrRef? # shapeAtomNonLitNodeConstraint
| litNodeConstraint # shapeAtomLitNodeConstraint
| shapeOrRef nonLitNodeConstraint? # shapeAtomShapeOrRef
| '(' shapeExpression ')' # shapeAtomShapeExpression
| '.' # shapeAtomAny // no constraint
;
inlineShapeAtom : inlineNonLitNodeConstraint inlineShapeOrRef? # inlineShapeAtomNonLitNodeConstraint
| inlineLitNodeConstraint # inlineShapeAtomLitNodeConstraint
| inlineShapeOrRef inlineNonLitNodeConstraint? # inlineShapeAtomShapeOrRef
| '(' shapeExpression ')' # inlineShapeAtomShapeExpression
| '.' # inlineShapeAtomAny // no constraint
;
shapeOrRef : shapeDefinition
| shapeRef
;
inlineShapeOrRef : inlineShapeDefinition
| shapeRef
;
shapeRef : ATPNAME_LN
| ATPNAME_NS
| '@' shapeExprLabel
;
inlineLitNodeConstraint : KW_LITERAL xsFacet* # nodeConstraintLiteral
| nonLiteralKind stringFacet* # nodeConstraintNonLiteral
| datatype xsFacet* # nodeConstraintDatatype
| valueSet xsFacet* # nodeConstraintValueSet
| numericFacet+ # nodeConstraintNumericFacet
;
litNodeConstraint : inlineLitNodeConstraint annotation* semanticAction* ;
inlineNonLitNodeConstraint : nonLiteralKind stringFacet* # litNodeConstraintLiteral
| stringFacet+ # litNodeConstraintStringFacet
;
nonLitNodeConstraint : inlineNonLitNodeConstraint annotation* semanticAction* ;
nonLiteralKind : KW_IRI
| KW_BNODE
| KW_NONLITERAL
;
xsFacet : stringFacet
| numericFacet
;
stringFacet : stringLength INTEGER
| REGEXP REGEXP_FLAGS?
;
stringLength : KW_LENGTH
| KW_MINLENGTH
| KW_MAXLENGTH
;
numericFacet : numericRange rawNumeric
| numericLength INTEGER
;
numericRange : KW_MININCLUSIVE
| KW_MINEXCLUSIVE
| KW_MAXINCLUSIVE
| KW_MAXEXCLUSIVE
;
numericLength : KW_TOTALDIGITS
| KW_FRACTIONDIGITS
;
// rawNumeric is like numericLiteral but returns a JSON integer or float
rawNumeric : INTEGER
| DECIMAL
| DOUBLE
;
shapeDefinition : inlineShapeDefinition annotation* semanticAction* ;
inlineShapeDefinition : qualifier* '{' tripleExpression? '}' ;
qualifier : extension | extraPropertySet | KW_CLOSED ;
extraPropertySet : KW_EXTRA predicate+ ;
tripleExpression : oneOfTripleExpr ;
oneOfTripleExpr : groupTripleExpr
| multiElementOneOf
;
multiElementOneOf : groupTripleExpr ( '|' groupTripleExpr )+ ;
groupTripleExpr : singleElementGroup
| multiElementGroup
;
singleElementGroup : unaryTripleExpr ';'? ;
multiElementGroup : unaryTripleExpr (';' unaryTripleExpr)+ ';'? ;
unaryTripleExpr : ('$' tripleExprLabel)? (tripleConstraint | bracketedTripleExpr)
| include
;
bracketedTripleExpr : '(' tripleExpression ')' cardinality? annotation* semanticAction* ;
tripleConstraint : senseFlags? predicate inlineShapeExpression cardinality? annotation* semanticAction* ;
cardinality : '*' # starCardinality
| '+' # plusCardinality
| '?' # optionalCardinality
| repeatRange # repeatCardinality
;
// BNF: REPEAT_RANGE ::= '{' INTEGER (',' (INTEGER | '*')?)? '}'
repeatRange : '{' INTEGER '}' # exactRange
| '{' INTEGER ',' (INTEGER | UNBOUNDED)? '}' # minMaxRange
;
senseFlags : '^' ;
valueSet : '[' valueSetValue* ']' ;
valueSetValue : iriRange
| literalRange
| languageRange
| '.' (iriExclusion+ | literalExclusion+ | languageExclusion+)
;
iriRange : iri (STEM_MARK iriExclusion*)? ;
iriExclusion : '-' iri STEM_MARK? ;
literalRange : literal (STEM_MARK literalExclusion*)? ;
literalExclusion : '-' literal STEM_MARK? ;
languageRange : LANGTAG (STEM_MARK languageExclusion*)? # languageRangeFull
| '@' STEM_MARK languageExclusion* # languageRangeAt
;
languageExclusion : '-' LANGTAG STEM_MARK? ;
include : '&' tripleExprLabel ;
annotation : '//' predicate (iri | literal) ;
semanticAction : '%' iri (CODE | '%') ;
literal : rdfLiteral
| numericLiteral
| booleanLiteral
;
// BNF: predicate ::= iri | RDF_TYPE
predicate : iri
| rdfType
;
rdfType : RDF_TYPE ;
datatype : iri ;
shapeExprLabel : iri
| blankNode
;
tripleExprLabel : iri
| blankNode
;
numericLiteral : INTEGER
| DECIMAL
| DOUBLE
;
rdfLiteral : string (LANGTAG | '^^' datatype)? ;
booleanLiteral : KW_TRUE
| KW_FALSE
;
string : STRING_LITERAL_LONG1
| STRING_LITERAL_LONG2
| STRING_LITERAL1
| STRING_LITERAL2
;
iri : IRIREF
| prefixedName
;
prefixedName : PNAME_LN
| PNAME_NS
;
blankNode : BLANK_NODE_LABEL ;
extension : KW_EXTENDS shapeExprLabel
| '&' shapeExprLabel
;
/* Not implemented yet, but reserved for future enhancement */
restrictions : KW_RESTRICTS shapeExprLabel
| '-' shapeExprLabel
;
// Keywords
KW_ABSTRACT : A B S T R A C T ;
KW_BASE : B A S E ;
KW_EXTENDS : E X T E N D S ;
KW_IMPORT : I M P O R T ;
KW_RESTRICTS : R E S T R I C T S ;
KW_EXTERNAL : E X T E R N A L ;
KW_PREFIX : P R E F I X ;
KW_START : S T A R T ;
KW_VIRTUAL : V I R T U A L ;
KW_CLOSED : C L O S E D ;
KW_EXTRA : E X T R A ;
KW_LITERAL : L I T E R A L ;
KW_IRI : I R I ;
KW_NONLITERAL : N O N L I T E R A L ;
KW_BNODE : B N O D E ;
KW_AND : A N D ;
KW_OR : O R ;
KW_MININCLUSIVE : M I N I N C L U S I V E ;
KW_MINEXCLUSIVE : M I N E X C L U S I V E ;
KW_MAXINCLUSIVE : M A X I N C L U S I V E ;
KW_MAXEXCLUSIVE : M A X E X C L U S I V E ;
KW_LENGTH : L E N G T H ;
KW_MINLENGTH : M I N L E N G T H ;
KW_MAXLENGTH : M A X L E N G T H ;
KW_TOTALDIGITS : T O T A L D I G I T S ;
KW_FRACTIONDIGITS : F R A C T I O N D I G I T S ;
KW_NOT : N O T ;
KW_TRUE : 'true' ;
KW_FALSE : 'false' ;
// terminals
PASS : [ \t\r\n]+ -> skip;
COMMENT : ('#' ~[\r\n]*
| '/*' (~[*] | '*' ('\\/' | ~[/]))* '*/') -> skip;
CODE : '{' (~[%\\] | '\\' [%\\] | UCHAR)* '%' '}' ;
RDF_TYPE : 'a' ;
IRIREF : '<' (~[\u0000-\u0020=<>"{}|^`\\] | UCHAR)* '>' ; /* #x00=NULL #01-#x1F=control codes #x20=space */
PNAME_NS : PN_PREFIX? ':' ;
PNAME_LN : PNAME_NS PN_LOCAL ;
ATPNAME_NS : '@' PN_PREFIX? ':' ;
ATPNAME_LN : '@' PNAME_NS PN_LOCAL ;
REGEXP : '/' (~[/\n\r\\] | '\\' [/nrt\\|.?*+(){}[\]$^-] | UCHAR)+ '/' ;
REGEXP_FLAGS : [smix]+ ;
BLANK_NODE_LABEL : '_:' (PN_CHARS_U | [0-9]) ((PN_CHARS | '.')* PN_CHARS)? ;
LANGTAG : '@' [a-zA-Z]+ ('-' [a-zA-Z0-9]+)* ;
INTEGER : [+-]? [0-9]+ ;
DECIMAL : [+-]? [0-9]* '.' [0-9]+ ;
DOUBLE : [+-]? ([0-9]+ '.' [0-9]* EXPONENT | '.'? [0-9]+ EXPONENT) ;
STEM_MARK : '~' ;
UNBOUNDED : '*' ;
fragment EXPONENT : [eE] [+-]? [0-9]+ ;
STRING_LITERAL1 : '\'' (~[\u0027\u005C\u000A\u000D] | ECHAR | UCHAR)* '\'' ; /* #x27=' #x5C=\ #xA=new line #xD=carriage return */
STRING_LITERAL2 : '"' (~[\u0022\u005C\u000A\u000D] | ECHAR | UCHAR)* '"' ; /* #x22=" #x5C=\ #xA=new line #xD=carriage return */
STRING_LITERAL_LONG1 : '\'\'\'' (('\'' | '\'\'')? (~['\\] | ECHAR | UCHAR))* '\'\'\'' ;
STRING_LITERAL_LONG2 : '"""' (('"' | '""')? (~["\\] | ECHAR | UCHAR))* '"""' ;
fragment UCHAR : '\\u' HEX HEX HEX HEX | '\\U' HEX HEX HEX HEX HEX HEX HEX HEX ;
fragment ECHAR : '\\' [tbnrf\\"'] ;
fragment PN_CHARS_BASE : [A-Z] | [a-z] | [\u00C0-\u00D6] | [\u00D8-\u00F6] | [\u00F8-\u02FF] | [\u0370-\u037D]
| [\u037F-\u1FFF] | [\u200C-\u200D] | [\u2070-\u218F] | [\u2C00-\u2FEF] | [\u3001-\uD7FF]
| [\uF900-\uFDCF] | [\uFDF0-\uFFFD]
| [\u{10000}-\u{EFFFD}]
;
fragment PN_CHARS_U : PN_CHARS_BASE | '_' ;
fragment PN_CHARS : PN_CHARS_U | '-' | [0-9] | [\u00B7] | [\u0300-\u036F] | [\u203F-\u2040] ;
fragment PN_PREFIX : PN_CHARS_BASE ((PN_CHARS | '.')* PN_CHARS)? ;
fragment PN_LOCAL : (PN_CHARS_U | ':' | [0-9] | PLX) ((PN_CHARS | '.' | ':' | PLX)* (PN_CHARS | ':' | PLX))? ;
fragment PLX : PERCENT | PN_LOCAL_ESC ;
fragment PERCENT : '%' HEX HEX ;
fragment HEX : [0-9] | [A-F] | [a-f] ;
fragment PN_LOCAL_ESC : '\\' ('_' | '~' | '.' | '-' | '!' | '$' | '&' | '\'' | '(' | ')' | '*' | '+' | ','
| ';' | '=' | '/' | '?' | '#' | '@' | '%') ;
fragment A:('a'|'A');
fragment B:('b'|'B');
fragment C:('c'|'C');
fragment D:('d'|'D');
fragment E:('e'|'E');
fragment F:('f'|'F');
fragment G:('g'|'G');
fragment H:('h'|'H');
fragment I:('i'|'I');
fragment J:('j'|'J');
fragment K:('k'|'K');
fragment L:('l'|'L');
fragment M:('m'|'M');
fragment N:('n'|'N');
fragment O:('o'|'O');
fragment P:('p'|'P');
fragment Q:('q'|'Q');
fragment R:('r'|'R');
fragment S:('s'|'S');
fragment T:('t'|'T');
fragment U:('u'|'U');
fragment V:('v'|'V');
fragment W:('w'|'W');
fragment X:('x'|'X');
fragment Y:('y'|'Y');
fragment Z:('z'|'Z');