-
-
Notifications
You must be signed in to change notification settings - Fork 146
/
headline_filter_parser.grammar.pegjs
174 lines (145 loc) · 5.48 KB
/
headline_filter_parser.grammar.pegjs
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
// Grammar for the parser for the headline filter.
// It will automatically be compiled to the parser JavaScript file using the
// parser generator pegjs.
// You can use https://pegjs.org/online to debug it.
// Note: As suggested by alphapapa, the parser can be extended to support
// additional types of filter terms ("predicates"), e.g. ts:on=today
// ts-active:from=2019-12-31 priority:A,B
// - https://github.com/alphapapa/org-ql#non-sexp-query-syntax
// Note: The parser will fail when the syntax does not match the grammar.
// For example, it fails for filter strings like ":" or "this|" because the
// grammar dictates a property or tag after ":" and an alternative word after
// "|". That is intended. Handling this edge cases would add complexity.
// Just wrap parser.parse() in a try-catch block and handle the case of an
// incomplete/incorrect filter string.
{
const checkDate = (year, month, day) => {
const date = new Date(year + "-" + month + "-" + day);
if (isNaN(date.valueOf())) {
throw { message: 'invalid date'};
} else {
return {
type: 'timestamp',
year: parseInt(year),
month: parseInt(month),
day: parseInt(day)
};
}
};
const checkMonth = (year, month) => {
if (month >= 1 && month <= 12) {
return { type: 'timestamp', year: parseInt(year), month: parseInt(month), day: null };
} else {
throw {message: 'invalid month'};
}
};
}
Expression "filter expression"
= _* head:LocationAnnotedTerm tail:(_+ LocationAnnotedTerm)* _* {
return tail.reduce((result, element) => {
result.push(element[1]);
return result;
}, [head]);
}
/ _* { return [] }
// Used for computation of completions
LocationAnnotedTerm
= a:Term {
a.offset = location().start.offset;
a.endOffset = location().end.offset;
return a;
};
// The order of lines is important here.
Term "filter term"
= "-" a:PlainTerm {
if ( a.type === 'field' && a.field.type !== 'desc') {
throw { message: `can't negate timerange searches yet` };
} else {
a.exclude = true;
return a;
} }
/ a:PlainTerm { a.exclude = false; return a; }
PlainTerm
= TermField
/ TermText
/ TermProp
/ TermTag
TermText "text filter term"
= a:StringAlternatives {
let type = 'ignore-case';
// It's hard to check for upper-case chars in JS.
// Best approach: https://stackoverflow.com/a/31415820/999007
// Simplest approach for now:
if (text().match(/[A-Z]/))
type = 'case-sensitive';
return {type: type, words: a}
}
TermTag "tag filter term"
= ":" a:TagAlternatives { return {type: 'tag', words: a} }
TermProp "property filter term"
= ":" a:PropertyName ":" b:StringAlternatives? {
return {
type: 'property',
property: a,
words: b === null ? [''] : b
}
};
TermField "search outside of header"
= "date:" a:TimeRange { return { type: 'field', field: { type: 'date', timerange: a } }; }
/ "clock:" a:TimeRange { return { type: 'field', field: { type: 'clock', timerange: a } }; }
/ ("sched:" / "scheduled:") a:TimeRange { return { type: 'field', field: { type: 'scheduled', timerange: a } }; }
/ ("dead:" / "deadline:") a:TimeRange { return { type: 'field', field: { type: 'deadline', timerange: a } }; }
/ ("desc:" / "description:") a:TermText { return { type: 'field', field: { type: 'description', text: a } }; }
TimeRange "moments and timeranges"
= a:Moment ".." b:Moment { return { type: 'range', from: a, to: b }; }
/ a:Moment ".." { return { type: 'range', from: a, to: null }; }
/ a:Moment { return { type: 'point', point: a }; }
/ ".." a:Moment { return { type: 'range', from: null, to: a }; }
/ ".." { return { type: 'all' }; }
Moment "moment"
= TimeOffset
/ Timestamp
/ TimeUnit
/ a:$("today" / "now") { return { type: 'special', value: a }; }
Timestamp
= year:Year [-./]? month:Month [-./]? day:Day { return checkDate(year, month, day); }
/ year:Year [-./]? month:Month { return checkMonth(year, month); }
/ year:Year { return { type: 'timestamp', year: parseInt(year), month: null, day: null }; }
TimeUnit
= a:[hdwmy] { return { type: 'unit', unit: a }; }
Year "year"
= a:$([0-9][0-9][0-9][0-9]) { return a; }
Month "month"
= a:$([0-9][0-9]) { return a; }
Day "day"
= a:$([0-9][0-9]) { return a; }
TimeOffset "offset"
= a:$([0-9]+) b:([hdwmy]) { return { type: 'offset', value: parseInt(a), unit: b, }; }
StringAlternatives "alternatives"
= head:String tail:("|" String)* {
return tail.reduce((result, element) => {
result.push(element[1]);
return result;
}, [head])
}
TagAlternatives "tag alternatives"
= head:TagName tail:("|" TagName)* {
return tail.reduce((result, element) => {
result.push(element[1]);
return result;
}, [head])
}
String "string"
= [^: \t|'"]+ { return text() }
/ "'" a:([^']+) "'" { return a.join('') }
/ '"' a:([^"]+) '"' { return a.join('') }
// https://orgmode.org/manual/Property-Syntax.html
// - Property names (keys) are case-insensitive
// - Property names must not contain space
PropertyName "property name"
= [^: \t]+ { return text() }
// https://orgmode.org/manual/Tags.html
TagName "tag name"
= [a-zA-Z0-9_@]+ { return text() }
_ "whitespace"
= [ \t]