-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRules.g4
156 lines (119 loc) · 4.21 KB
/
Rules.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
/* JSON-NLP to Graph / RDF Mapper Rules grammar
(C) 2020 by Semiring Inc, Damir Cavar, Anthony Meyer
The grammar definition for the mapping language from JSON-NLP
to RDF (and graphs) defines:
- Macros
- Rules
Macros combine matching expressions.
Rules describe Matching expressions and the output generated in
form of RDF triples, JSON-LD, or graph specifications.
*/
grammar Rules;
COLON : ':' ;
SEMICOLON : ';' ;
DOT : '.' ;
ARROW : ('-'|'=')+ '>' ;
EQUALS : '=' ;
NOTEQUALS : '!=' ;
LARGER : '>' ;
SMALLER : '<' ;
LARGEREQUAL : '>=' ;
SMALLEREQUAL : '<=' ;
COMMA : ',' ;
OPENBRACKET : '[' ;
CLOSEBRACKET : ']' ;
OPENSET : '(' ;
CLOSESET : ')' ;
INT : [0-9]+ ;
DOUBLE : [0-9]+'.'[0-9]+ ;
BOOLTRUE : 'true' | 'True' | 'TRUE' ;
BOOLFALSE : 'false' | 'False' | 'FALSE' ;
RULEKEYWORD : 'Rule'|'rule'|'RULE' ;
MACROKEYWORD : 'Macro' | 'macro' | 'MACRO' ;
STRINGB : '"' ;
ID : [a-zA-Z][a-zA-Z0-9_]* ; // match identifiers
WS : [ \t\r\n]+ -> skip ; // skip spaces, tabs, newlines
LINE_COMMENT : '//' ~[\r\n]* -> skip ;
COMMENT : '/*' .*? '*/' -> skip ;
// --------------------------------------------------------------------
// test: see test.dat
input : (ruleExp|aliasExp)+ // main input def, set of rules
;
ruleExp : RULEKEYWORD rulenameExp matchExpressions ARROW tripleExpressions SEMICOLON ;
rulenameExp : ruleName (OPENBRACKET ruleFeatureExp CLOSEBRACKET)? COLON // ruleName followed by :
;
ruleFeatureExp : ruleFeatureProp (COMMA ruleFeatureProp)* ;
ruleFeatureProp : rulePropertyExp (EQUALS ruleValueExp)? ;
rulePropertyExp : ID ;
ruleValueExp : STRINGB valueString STRINGB
| valueInt
| valueDouble
| valueBool
;
ruleName : ID ;
aliasExp : MACROKEYWORD aliasName COLON aliasProps SEMICOLON ;
aliasName : ID ;
aliasProps : (methodName|OPENBRACKET featureExp CLOSEBRACKET)+ ;
matchExpressions : matchExp+ ;
matchExp : matchVariable (DOT methodName)* (OPENBRACKET featureExp CLOSEBRACKET)? ;
matchVariable : ID ;
methodName : getSubject
| getSubjectPhrase
| getSubjectCompound
| getObject
| getObjectPhrase
| getObjectCompound
;
getSubject : 'subject' ;
getSubjectPhrase : 'subjectPhrase' ;
getSubjectCompound : 'subjectCompound' ;
getObject : 'object' ;
getObjectPhrase : 'objectPhrase' ;
getObjectCompound : 'objectCompound' ;
// feature expression is attribute = value or for logical checks just attribute
featureExp : featureProp (COMMA featureProp)* ;
featureProp : propertyExp (EQUALS valueExp)? ;
// properties are ID type of strings
propertyExp : ID ;
// values can be strings with double quotes, integers, doubles, fals or true
valueExp : STRINGB valueString STRINGB
| valueInt
| valueDouble
| valueBool
;
valueString : ID ;
valueInt : INT ;
valueDouble : DOUBLE ;
valueBool : BOOLFALSE | BOOLTRUE ;
tripleExpressions : OPENSET tripleExp (COMMA tripleExp)* CLOSESET
| tripleExp
;
// a triple is:
// (X, Y, Z)
// where X, Y, and Z can be a variable (mathing concept), a string/URL ID
// a predicate or rconcept can be a value (string, int, double, boolean)
tripleExp : OPENSET lconcept COMMA predicateExp COMMA rconcept CLOSESET ;
lconcept : concept (DOT outputFunctions)? (OPENBRACKET lcFeatureExp CLOSEBRACKET)? ;
lcFeatureExp : lcFeatureProp (COMMA lcFeatureProp)* ;
// if not value, then set value to true
lcFeatureProp : lcPropertyExp (EQUALS valueExp)? ;
lcPropertyExp : ID ;
rconcept : ( concept (DOT outputFunctions)? |
INT |
DOUBLE |
STRINGB ID STRINGB |
BOOLFALSE |
BOOLTRUE )
(OPENBRACKET rcFeatureExp CLOSEBRACKET)? ;
rcFeatureExp : rcFeatureProp (COMMA rcFeatureProp)* ;
// if not value, then set value to true
rcFeatureProp : rcPropertyExp (EQUALS valueExp)? ;
rcPropertyExp : ID ;
predicateExp : predicate ;
predicate : ( ID (DOT outputFunctions)? | STRINGB ID STRINGB ) (OPENBRACKET pFeatureExp CLOSEBRACKET)? ;
pFeatureExp : pFeatureProp (COMMA pFeatureProp)* ;
// if not value, then set value to true
pFeatureProp : pPropertyExp (EQUALS valueExp)? ;
pPropertyExp : ID ;
concept : ID ;
outputFunctions : ID ;