-
Notifications
You must be signed in to change notification settings - Fork 12
/
comsyn.py
343 lines (254 loc) · 8.03 KB
/
comsyn.py
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
"""
(rename to miniRDF.py?)
An RDF Compact Syntax, suitable for transmitting an
RDF graph (optionally with variables) inside a URI.
Or using inside as URI itself, see
http://lists.w3.org/Archives/Public/uri/2003May/0007.html
x-rdfn1:foaf_mbox='sandro@w3.org';ns(foaf_,'http://xmlns.org/FOAF/0.1/')
x-rdfn1:mbox='sandro@w3.org';ns('http://xmlns.org/FOAF/0.1/')
x-rdfn1:(uri='http://xmlns.org/FOAF/0.1/mbox')='sandro@w3.org'
x-rdfn1:=x;x.foaf_mbox='sandro@w3.org';ns(foaf_,'http://xmlns.org/FOAF/0.1/')
an expr denotes an object....
all shortnames are local (bnodes) except
when a .uri for them is known
.uri itself? ick. hasURI('xxxx')
the ns(...) function is used
NS() 1 and 2 place
hasURI() to bring in long names without NS()
[ we reserve all function names;
we never use SHORTNAMES ]
version(0.1);
typed(xsd_date,'2003-02-02')
x-rdfn1:1,2,3
denotes an RDF collection with
a first of 1, etc.
x-rdfn1:1,2,3,...
does NOT close the list.
1,2,3|x;x=foo ?
x-rdfn1:foaf_mbox='sandro@w3.org';NS(foaf_,'http://xmlns.org/FOAF/0.1/')
x-rdfn1:mbox='sandro@w3.org';NS('http://xmlns.org/FOAF/0.1/')
x-rdfn1:hasURI('http://xmlns.org/FOAF/0.1/mbox')='sandro@w3.org'
x-rdfn1:=x;x.foaf_mbox='sandro@w3.org';NS(foaf_,'http://xmlns.org/FOAF/0.1/')
x-rdfn1:(=x;x.foaf_mbox='sandro@w3.org';NS(foaf_,'http://xmlns.org/FOAF/0.1/'))
dt lits: 'dsfsdf'^^type
typed(xsd_int,'34')
lists?
colors=1,2,3
colors=(1,)
parens are for precidence
, makes lists, trailing ignored
= (infix) makes sentences, trailing ignored,
sometimes involving current subject
= (prefix) relates to current subject ...?
[ or is that a null path? can you
say (self=) to say x.self=x ?
or (=) to say x=x ?
; makes compound sentences, drops directives
xxx() reserved function or directive
why apostrophe? it's allowed in URI
when to undo %? after parsing for ' ; = ...
query?
http://server.com?=x,y,z;x.y=z
does that =x,y,z really make sense.... the matched
object is a list....
http://server.com?mustbind=x,y,z&pattern=(x.y=z)
do cgi's split on a second "="? I doubt it.
can bnodes default to maybind or dontbind? Sure, they're
just existentials; a good proof of existence would bind them.
mustbind means must-bind to a URI or something.
default ?x to mustbind, _x to dontbind, ??x to maybind.
nesting / parsetype=quote?
easy with quote(expr)
urp -- ( ) shouldnt introduce a new subject;
Resource(...) should do that.
sandro.knows=Resource(nick='DanC')
HasURI('...')
<-> Resource(uri=...)
[ messes with local names ]
Statement(sandro.knows=dan).type=true
MinRDF TinyRDF OneLineRDF
'31'@xsd_int
xx'31'
+-/*^ when - isnt swallowed, etc.
age=3*42/7; ??? maybe
"""
__version__ = "$Revision$"
# $Id$
import rdfpath
import hotswap
import pluggable
#class Serializer(pluggable.Serializer):
# pass
tokens = (
'LPAREN', 'RPAREN', 'SEMI', 'EQUALS', 'PERIOD',
'SHORTNAME', 'NUMERAL', 'QUOTEDSTRING'
)
# these need to be functions to check for them first
t_EQUALS = r'\='
t_LPAREN = r'\('
t_RPAREN = r'\)'
t_SEMI = r';'
t_PERIOD = r'\.'
t_SHORTNAME = r'[-_a-zA-Z0-9:?]+'
def t_NUMERAL(t):
r'\d+'
try:
t.value = int(t.value)
except ValueError:
print "Integer value too large", t.value
t.value = 0
return t
def t_QUOTEDSTRING(t):
r"'[^']*'" # need \-handling
t.value = t.value[1:-1]
return t
# Ignored characters
# t_ignore = " \t"
def t_error(t):
print "Illegal character '%s'" % t.value[0]
t.skip(1)
# Build the lexer
import ply.lex
ply.lex.lex()
# Test it out
data = '''abc.def'''
## lex.input(data)
## # Tokenize
## while 1:
## tok = lex.token()
## if not tok: break # No more input
## print tok
# Precedence rules for the arithmetic operators
#precedence = (
# ('left', 'IMPLIES'),
# ('left', 'IFF'),
# ('left', 'OR'),
# ('left', 'AND'),
# ('right','NOT'),
# )
def p_expr_single(t):
'''expr : part'''
t[0] = [t[1]]
def p_expr(t):
'''expr : expr SEMI part'''
t[0] = t[1] + [t[3]]
def p_part(t):
'''part : left EQUALS right'''
t[0] = ("=", t[1], t[3])
def p_part_2(t):
'''part : EQUALS right'''
t[0] = ("=", t[2])
def p_left(t):
'''left : path'''
t[0] = t[1]
def p_right_string(t):
'''right : QUOTEDSTRING'''
t[0] = ('STR', t[1])
def p_right_numeral(t):
'''right : NUMERAL'''
t[0] = ('NUM', t[1])
def p_right_word(t):
'''right : path'''
t[0] = ('PATH', t[1])
def p_right_p(t):
'''right : LPAREN expr RPAREN'''
t[0] = t[2]
def p_path(t):
'''path : name'''
t[0] = (t[1],)
def p_path_more(t):
'''path : path PERIOD name'''
t[0] = t[1]+(t[3],)
def p_name(t):
'''name : SHORTNAME'''
t[0] = ('SHORTNAME', t[1])
def p_name_2(t):
'''name : LPAREN expr RPAREN'''
t[0] = t[2]
def p_error(t):
print "Syntax error at '%s'" % (t.value)
raise RuntimeError
import ply.yacc
ply.yacc.yacc(tabmodule="comsyn_ply_tab")
debug=0
print ply.yacc.parse("hello='world'", debug=debug)
print ply.yacc.parse("hello=35", debug=debug)
print ply.yacc.parse("hello=35;foo=bar;baz='Bux'", debug=debug)
print ply.yacc.parse("a=(b='c')", debug=debug)
print ply.yacc.parse("a=(x.b='c')", debug=debug)
print ply.yacc.parse("a=(x.b.x.q='c');a.b=c.d", debug=debug)
print ply.yacc.parse("=x;x.y=z", debug=debug)
# the left is always a path
# if it has only one element, the subject is implied
# if it has 2 or more, the subject is given, and the others
# are property names
# if it has ZERO, then it's equating the resulting thing
# xxprint ply.yacc.parse("a=ext(http'))", debug=debug)
# (uri='http://www.w3.org/')
# ns(foaf_, 'http://....')
import sys
sys.exit()
class Parser(pluggable.Parser):
languages=["MiniRDF"]
requiredSinkType='triples, owns it nodes' #hack
def parse(self, stream, host):
self.sink = host.pluginManager.get("store", rdfpath.Store)
x = ply.yacc.parse(stream)
print "Result:", x
class xxxParser:
def __init__(self, sink=None, flags=""):
self.kb = sink
def load(self, inputURI):
stream = urllib.urlopen(inputURI)
s = stream.read()
global kb
kb = self.kb
ply.yacc.parse(s)
def parse(self, s):
global kb
kb = self.kb
ply.yacc.parse(s)
def parseToFormula(self, s):
global kb
kb = newkb.KB()
ply.yacc.parse(s)
f = kb.asFormula()
kb = None
class xxxSerializer:
def __init__(self, stream, flags=""):
self.stream = stream
def makeComment(self, comment):
self.stream.write("% "+comment+"\n")
def serializeKB(self, kb):
pass
# $Log$
# Revision 1.2 2003-08-01 15:35:27 sandro
# added some comments
#
# Revision 1.1 2003/05/02 06:06:58 sandro
# compact syntax for rdf; notes and partial impl
#
# Revision 1.8 2003/02/14 19:40:32 sandro
# working lbase -> otter translation, with regression test
#
# Revision 1.7 2003/02/14 17:21:59 sandro
# Switched to import-as-needed for LX languages and engines
#
# Revision 1.6 2003/02/14 14:47:24 sandro
# possible fix to yacc warning
#
# Revision 1.5 2003/02/14 00:52:03 sandro
# added literals, some tweaks in URI handling
#
# Revision 1.4 2003/02/13 19:50:55 sandro
# better support for "holds", changed parser API a little
#
# Revision 1.3 2003/02/01 06:23:55 sandro
# intermediate lbase support; getting even better
#
# Revision 1.2 2003/02/01 05:58:12 sandro
# intermediate lbase support; getting there but buggy; commented out some fol chreccks
#
# Revision 1.1 2003/01/30 22:11:52 sandro
# handles syntax now, I think; needs to build LX AST
#