blob: 621b0cec82b7a640aac05da486f77e42321a20f7 (
plain) (
blame)
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
|
grammar ConjunctiveQuery;
options {
language = Java;
output = AST;
}
tokens {
VARIABLE;
CONSTANT;
SCONSTANT;
ATOM;
HEADATOM;
PREDICATE;
ATOM_LIST;
TERM_LIST;
RULE;
EXPRESSION;
PREFIX_LIST;
ID;
PREFIX;
PREDICATE;
}
@header {
package org.semanticweb.karma2.model.cqparser;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Set;
import org.semanticweb.karma2.model.ConjunctiveQuery;
import org.semanticweb.karma2.model.cqparser.ConjunctiveQueryWalker;
import org.semanticweb.karma2.exception.IllegalInputQueryException;
}
@members{
public ConjunctiveQueryParser(String string)
throws FileNotFoundException, IOException {
this(new CommonTokenStream(new ConjunctiveQueryLexer(new ANTLRStringStream(string))));
}
public ConjunctiveQueryParser(InputStream istream) throws FileNotFoundException, IOException {
this(new CommonTokenStream(new ConjunctiveQueryLexer(new ANTLRInputStream(istream))));
}
public ConjunctiveQueryParser(File file) throws FileNotFoundException, IOException {
this(new CommonTokenStream(new ConjunctiveQueryLexer(new ANTLRInputStream(new FileInputStream(file)))));
}
public ConjunctiveQuery parse() throws IllegalInputQueryException {
cq_return r = null;
try {
r = cq();
} catch (RecognitionException e) {
e.printStackTrace();
}
CommonTree t = (CommonTree) r.getTree();
//System.out.println(t.toStringTree());
CommonTreeNodeStream nodes = new CommonTreeNodeStream(t);
// AST nodes have payloads that point into token stream
nodes.setTokenStream(input);
ConjunctiveQueryWalker walker = new ConjunctiveQueryWalker();
ConjunctiveQuery cq = walker.walkExpressionNode(t);
return cq;
}
public ConjunctiveQuery parseCQ() throws IllegalInputQueryException {
return parse();
}
}
@lexer::header{
package org.semanticweb.karma2.model.cqparser;
}
cq :
prefixlist rulebody -> ^(EXPRESSION prefixlist rulebody );
prefixlist:
prefix (',' prefix)* -> ^(PREFIX_LIST prefix*);
prefix:
'prefix' id ':' '<' url '>' -> ^(PREFIX id url);
rulebody:
headatom ('<-'|':') body '.'? -> ^(RULE headatom body);
body:
atom (',' atom)* -> ^(ATOM_LIST atom*);
headatom:
id '(' term (',' term)* ')' -> ^(HEADATOM term*);
atom:
compositeid '(' term (',' term)* ')' -> ^(ATOM compositeid term*);
compositeid:
(id) ':' (id) -> ^(ID id id);
term:
variable -> ^(VARIABLE variable)
| simpleid -> ^(SCONSTANT simpleid)
| compositeid -> ^(CONSTANT compositeid);
id : (STRING);
simpleid : '<' URLSTRING '>' | '<' STRING '>';
// TODO: FIXIT X1 can be parsed as variable
variable:
('?') id -> ^(id);
url : (URLSTRING);
URLSTRING : ('http://'|'file:/') ('a'..'z'|'A'..'Z'|'0'..'9'|'/'|'#'|'.'|'-'|'~'|'_')+;
STRING : ('a'..'z'|'A'..'Z'|'0'..'9'|'/'|'#'|'.'|'-'|'_')+;
WS : (' '|'\n'|'\r')+ {$channel=HIDDEN;} ;
|