-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathuser_syntax.py
137 lines (115 loc) · 5.95 KB
/
user_syntax.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
# python3
# Parsing of user syntax
# Needs tweaking to handle recent changes
import pyparsing as pp
boolean = pp.Literal('true')('boolean') ^ pp.Literal('false')('boolean')
nums = pp.Word(pp.nums)
integer = pp.Combine(pp.Optional(pp.Literal('-')^pp.Literal('+')) + nums)
exponent = pp.Combine((pp.Literal('e')^pp.Literal('E')) + integer)('exponent')
point = pp.Literal('.')('point')
number = pp.Combine( ( integer('integer') +
pp.Optional(point + pp.Optional(nums)('fraction')) + \
pp.Optional(exponent) ) ^ \
( point + nums('fraction') + pp.Optional(exponent) ) )
prefixName = pp.Word(pp.alphas,pp.alphanums)
localName = pp.Word(pp.alphas,pp.alphanums)
IRI = pp.Combine(pp.Literal('<').suppress() +
pp.Word(pp.srange("[\x21-\xFFFF]"),excludeChars='<>"{}|^`') +
pp.Literal('>').suppress())
qname = pp.Group(prefixName + pp.Literal(':')('qname') + localName)
name = qname | IRI
language = pp.Word(pp.alphas,pp.alphanums+'-')
lexical = pp.QuotedString('"',escChar='\\',endQuoteChar='"')
literal = pp.Group ( lexical('lexical') +
( pp.Optional ( '@' + language('language') ) ^
pp.Optional ( '^^' + name('datatype') ) ) ^
number ^ boolean )
string = pp.Group(pp.QuotedString('"','\\'))
nonnegativeInteger = pp.Word(pp.nums)
regex = string
property = name
clss = name
datatype = name
value = name ^ literal
pathpart = property ^ pp.Group( property + pp.Literal('⁻¹').setResultsName('inverse') )
path = pp.Group( pp.OneOrMore( pathpart ) )
component = pp.Forward()
subshape = pp.Forward()
shape = pp.Forward()
component << pp.Or( [ name ,
( pp.Literal('∈')('token') + clss.setResultsName('class',True) +
pp.ZeroOrMore( '∪' + clss.setResultsName('class',True) ) ) ,
( pp.Literal('^^')('token') +
datatype.setResultsName('datatype',True).setResultsName('datatype',True) +
pp.ZeroOrMore( '∪' + datatype.setResultsName('datatype',True) ) ) ,
( pp.Literal('∈')('token') +
'{' + pp.ZeroOrMore(value.setResultsName('set',True)) + '}' ) ,
( pp.Literal('⋹')('token') + clss ) ,
( pp.Literal('ℓ')('token') + '≤' + nonnegativeInteger ) ,
( pp.Literal('ℓ')('token') + '≥' + nonnegativeInteger ) ,
( pp.Literal('>')('token') + literal ) ,
( pp.Literal('≥')('token') + literal ) ,
( pp.Literal('<')('token') + literal ) ,
( pp.Literal('≤')('token') + literal ) ,
pp.Literal('IRI')('token') ,
pp.Literal('Literal')('token') ,
pp.Literal('BlankNode')('token') ,
( regex + pp.Literal('★')('token') + pp.Optional( string ) ) ,
( path + pp.Literal('=')('token') + path ) ,
( path + pp.Literal('∅')('token') + path ) ,
( path + pp.Literal('<')('token') + path ) ,
( path + pp.Literal('≤')('token') + path ) ,
( pp.Literal('⟦')('token') +
pp.ZeroOrMore( pathpart.setResultsName('part',True) ) + '⟧' ) ,
( pp.Literal('∋')('token') + value ) ,
( pp.Literal('|')('token') + '≥' + nonnegativeInteger + '|' ) ,
( pp.Literal('|')('token') + '≤' + nonnegativeInteger + '|' ) ,
( pp.Literal('|')('token') + '=' + nonnegativeInteger + '|' ) ,
( pp.Literal('➀')('token') ),
( pp.Literal('¬')('token') + pp.Group(component)('component') ) ,
( path.setResultsName('path') + pp.Literal('∝')('token') +
pp.Group(component)('component') ) ,
( pp.Literal('⦇')('token') + pp.Group(shape)('component') + '⦈' ) ,
( pp.Literal('(').suppress() + subshape + pp.Literal(')').suppress() ) ] )
subshape << pp.Or( [ shape ,
( pp.Group(component).setResultsName('component',True) +
pp.OneOrMore( pp.Literal('∨')('token') +
pp.Group(component).setResultsName('component',True) ) ) ,
( pp.OneOrMore ( pp.Group(component).setResultsName('component',True) +
pp.Literal('∖')('token') ) ) ] )
def conjunctionPP(toks) :
if len(toks) == 1 : return toks[0]
else : return None
conjunction = ( pp.Group(component).setResultsName('component',True) +
pp.ZeroOrMore( pp.Literal('∧')('token') +
pp.Group(component).setResultsName('component',True))
).setParseAction(conjunctionPP)
def shapePP(toks) :
if toks.token == '→' :
toks["filter"] = toks[0]
toks["body"] = toks[2]
return toks
else :
toks["body"] = toks[0]
toks["token"] = '→'
return toks
shape << ( pp.Group(conjunction) +
pp.Optional ( pp.Literal('→')('token') +
pp.Group(conjunction) ) ).setParseAction(shapePP)
def reduce(shape) :
if shape.get('filter') is None : return shape.get('body')
else : return shape
scope = pp.Group ( value ^ \
pp.Literal('∈').setResultsName('token') + clss ^ \
pp.Literal('¹').setResultsName('token') + property ^ \
property + pp.Literal('²').setResultsName('token') ^ \
pp.Literal('¹').setResultsName('token') + '?' ^ \
'?' + pp.Literal('²').setResultsName('token') )
scoping = scope.setResultsName('scope',True) + \
pp.ZeroOrMore( '∪' + scope.setResultsName('scope',True) ) + \
pp.Literal('⊩')('token') + pp.Group(shape).setResultsName('shape')
definition = name('name') + '≡' + ( pp.Group(shape)('shape') ^ scoping )
prefix = pp.Keyword('@prefix')('token') + prefixName + ':' + IRI
shacl = pp.Group( prefix ^ definition ^ scoping ) + \
pp.ZeroOrMore( pp.Literal('.').suppress() + pp.Group( prefix ^ definition ^ scoping ) ) + \
(pp.Literal('.').suppress())