-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample.py
More file actions
44 lines (36 loc) · 1.38 KB
/
example.py
File metadata and controls
44 lines (36 loc) · 1.38 KB
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
from ExpressionParser import ExpressionParser
class ExampleExpressionParser(ExpressionParser):
# syntax definition for the parser
# ! order is important
syntax = [
# group, eg: "(...)"
{ 'start': '\\s*\(',
'end': '\)\\s*',
'type': 'group'
},
# propval, eg: "hasNotes:true"
# with optional NOT modifier
{ 'start': '\\s*([Nn][Oo][Tt])?\\s*([a-z][a-zA-Z]*): *("(.+?)"|[^ ()]+)\\s*',
'type': 'propval',
'create': lambda m: (m.group(2), m.group(4) or m.group(3), m.group(1))
},
# operator, eg: "AND", "OR"
{ 'start': '\\s*(?i)(AND|OR)\\s*', # case insensitive
'type': 'operator',
'create': lambda m: m.group(1)
},
# text
# with optional NOT modifier
# if nothing else matched - match all characters until special
# character found
{ 'start': '\\s*([Nn][Oo][Tt])?\\s*([ ()]?[^ ()]*)',
'type': 'text',
'create': lambda m: (m.group(2), m.group(1))
}
]
#
# Usage example
#
query = "(list:Personal OR list:Work) AND NOT due:tomorrow"
parser = ExampleExpressionParser()
print parser.parse(query)