filterlang.pegjs 4.1 KB
Newer Older
ransome1's avatar
ransome1 committed
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
{{
    import { addIntervalToDate } from "./recurrences.mjs";
}}

filterQuery
    = _ left:orExpr _  { return left; }
    / _ { return []; }

orExpr
    = left:andExpr _ OrOp _ right:orExpr { return left.concat(right, ["||"]); }
    / left:andExpr { return left; }

andExpr
    = left:notExpr _ AndOp _ right:andExpr { return left.concat(right, ["&&"]); }
    / left:notExpr { return left; }

notExpr
    = NotOp _ left:notExpr { return left.concat(["!!"]); }
    / left:boolExpr { return left; }

boolExpr
    = left:project { return left; }
    / left:context { return left; }
    / "(" _ left:orExpr _ ")" { return left; }
    / left:comparison  { return left; }
    / "complete"  { return ["complete"]; }
    / left:StringLiteral  { return ["string", left]; }
    / left:RegexLiteral  { return ["regex", left]; }

project
    = "+" left:name      { return ["++", left]; }
    / "+"               { return ["++", "*"]; }

context
    = "@" left:name      { return ["@@", left]; }
    / "@"               { return ["@@", "*"]; }

OrOp
    = "||"
    / "or"i

AndOp
    = "&&"
    / "and"i

NotOp
    = "!"
    / "not"i

comparison
    = left:priorityComparison { return left; }
    / left:dueComparison { return left; }

priorityComparison
    = priorityKeyword _ op:compareOp _ right:priorityLiteral  { return ["priority", right, op]; }
    / priorityKeyword  { return ["priority"]; }

priorityLiteral
    = [A-Z]  { return text(); }

priorityKeyword
    = "pri" ("o" ("r" ("i" ("t" "y"?)?)?)?)?

dueComparison
    = "due" _ op:compareOp _ right:dateExpr  { return ["due"].concat(right, [op]); }
    / "due"  { return ["due"]; }

dateExpr
    = left:dateLiteral _ op:dateOp _ count:number unit:[dbwmy]  {
        if (op == "-") {
            count = count * -1;
        }
        // we do our date math with the same code as we use for
        // recurrence calculations.  All dates are returned from
        // the parser as millisec since epoch (getTime()) to
        // simplify comparisons in the filter lang execution engine.
        let d = addIntervalToDate(new Date(left), count, unit);
        return d.getTime();
    }
    / left:dateLiteral  { return left; }

dateOp
    = "+"  { return text(); }
    / "-"  { return text(); }

compareOp
    = "=="  { return text(); }
    / "!="  { return text(); }
    / ">="  { return text(); }
    / "<="  { return text(); }
    / ">"   { return text(); }
    / "<"   { return text(); }

dateLiteral
    = year:number4 "-" month: number2 "-" day:number2  {
    	let d = new Date(year, month-1, day);
        return d.getTime();
    }
    / "today" {
    	let d = new Date();  // now, w current time of day
        d = new Date(d.getFullYear(), d.getMonth(), d.getDate());
        return d.getTime();
    }
    / "tomorrow" {
        let d = new Date();  // now, w current time of day
        d = new Date(d.getFullYear(), d.getMonth(), d.getDate());
        return d.getTime() + 24*60*60*1000;
    }
    / "yesterday" {
        let d = new Date();  // now, w current time of day
        d = new Date(d.getFullYear(), d.getMonth(), d.getDate());
        return d.getTime() - 24*60*60*1000;
    }

number4
    = [0-9][0-9][0-9][0-9] { return text(); }

number2
    = [0-9][0-9] { return text(); }

number
    = [0-9]+ { return text(); }

StringLiteral "string"
    = '"' chars:DoubleStringCharacter* '"'? {
        return chars.join("");
    }
    / "'" chars:SingleStringCharacter* "'"? {
        return chars.join("");
    }

DoubleStringCharacter
    = '\\' '"' { return '"'; }
    / !'"' SourceCharacter { return text(); }

SingleStringCharacter
    = '\\' "'" { return "'"; }
    / !"'" SourceCharacter { return text(); }

RegexLiteral "regex"
    = "/" chars:RegexCharacter* "/"  "i" {
        return new RegExp(chars.join(""), "i");
    }
    / "/" chars:RegexCharacter* "/"? {
        return new RegExp(chars.join(""));
    }

RegexCharacter
    = "\\" "/" { return "/"; }
    / !"/" SourceCharacter { return text(); }

SourceCharacter
    = .

name
	= '"' nonblank+ '"'    { return text(); }
	/ nonblank+  '"'       { return '"' + text(); }
	/ nonblank+            { return text(); }

nonblank
    = [^ \t\n\r"()]

_ "whitespace"
  = [ \t\n\r]*