@malloydata/malloy-filter 0.0.245 → 0.0.246-dev250320220920
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/boolean_filter_expression.d.ts +3 -3
- package/dist/clause_utils.d.ts +23 -23
- package/dist/clause_utils.js +8 -8
- package/dist/clause_utils.js.map +1 -1
- package/dist/filter_interface.d.ts +120 -0
- package/dist/{filter_clause.js → filter_interface.js} +22 -18
- package/dist/filter_interface.js.map +1 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.js +4 -4
- package/dist/index.js.map +1 -1
- package/dist/lib/ftemporal_parser.js +3 -1
- package/dist/lib/ftemporal_parser.js.map +1 -1
- package/dist/nearley_parse.d.ts +2 -2
- package/dist/nearley_parse.js +19 -12
- package/dist/nearley_parse.js.map +1 -1
- package/dist/number_filter_expression.d.ts +3 -3
- package/dist/number_filter_expression.js +2 -2
- package/dist/number_filter_expression.js.map +1 -1
- package/dist/string_filter_expression.d.ts +3 -3
- package/dist/string_filter_expression.js +2 -2
- package/dist/string_filter_expression.js.map +1 -1
- package/dist/temporal_filter_expression.d.ts +3 -3
- package/dist/temporal_filter_expression.js +2 -2
- package/dist/temporal_filter_expression.js.map +1 -1
- package/package.json +2 -1
- package/src/boolean_filter_expression.ts +4 -4
- package/src/clause_utils.ts +21 -21
- package/src/{filter_clause.ts → filter_interface.ts} +78 -72
- package/src/index.ts +2 -2
- package/src/lib/ftemporal_parser.ts +4 -1
- package/src/nearley_parse.ts +21 -14
- package/src/number_filter_expression.ts +5 -5
- package/src/string_filter_expression.ts +5 -5
- package/src/temporal_filter_expression.ts +7 -7
- package/dist/filter_clause.d.ts +0 -133
- package/dist/filter_clause.js.map +0 -1
package/src/nearley_parse.ts
CHANGED
|
@@ -6,38 +6,45 @@
|
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
8
|
import type {Parser} from 'nearley';
|
|
9
|
-
import type {
|
|
10
|
-
import {
|
|
9
|
+
import type {FilterExpression, FilterLog} from './filter_interface';
|
|
10
|
+
import {isFilterExpression} from './filter_interface';
|
|
11
11
|
|
|
12
12
|
export function run_parser(
|
|
13
13
|
src: string,
|
|
14
14
|
parser: Parser
|
|
15
|
-
): {parsed:
|
|
15
|
+
): {parsed: FilterExpression | null; log: FilterLog[]} {
|
|
16
16
|
try {
|
|
17
17
|
parser.feed(src);
|
|
18
18
|
const results = parser.finish();
|
|
19
19
|
const expr = results[0];
|
|
20
|
-
if (
|
|
20
|
+
if (isFilterExpression(expr)) {
|
|
21
21
|
return {parsed: expr, log: []};
|
|
22
22
|
}
|
|
23
23
|
return {parsed: null, log: []};
|
|
24
24
|
} catch (e) {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
25
|
+
let newMessage = e.message;
|
|
26
|
+
let col = 1;
|
|
27
|
+
let len = src.length;
|
|
28
|
+
if (e.token) {
|
|
29
|
+
const token = e.token;
|
|
30
|
+
col = token.col;
|
|
31
|
+
len = token.text.length;
|
|
32
|
+
const message = e.message;
|
|
33
|
+
const expected = message
|
|
34
|
+
.match(/(?<=A ).*(?= based on:)/g)
|
|
35
|
+
.map(s => s.replace(/\s+token/i, ''));
|
|
36
|
+
newMessage = `Unexpected ${token.type} token "${token.value}"`;
|
|
37
|
+
if (expected && expected.length) {
|
|
38
|
+
newMessage += ` Tokens expected: ${[...new Set(expected)]}`;
|
|
39
|
+
}
|
|
33
40
|
}
|
|
34
41
|
return {
|
|
35
42
|
parsed: null,
|
|
36
43
|
log: [
|
|
37
44
|
{
|
|
38
45
|
message: newMessage,
|
|
39
|
-
startIndex:
|
|
40
|
-
endIndex:
|
|
46
|
+
startIndex: col - 1,
|
|
47
|
+
endIndex: col - 1 + len - 1,
|
|
41
48
|
severity: 'error',
|
|
42
49
|
},
|
|
43
50
|
],
|
|
@@ -5,24 +5,24 @@
|
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
import type {FilterParserResponse,
|
|
9
|
-
import {
|
|
8
|
+
import type {FilterParserResponse, NumberFilter} from './filter_interface';
|
|
9
|
+
import {isNumberFilter} from './filter_interface';
|
|
10
10
|
import * as nearley from 'nearley';
|
|
11
11
|
import fnumber_grammar from './lib/fexpr_number_parser';
|
|
12
12
|
import {run_parser} from './nearley_parse';
|
|
13
13
|
|
|
14
14
|
export const NumberFilterExpression = {
|
|
15
|
-
parse(src: string): FilterParserResponse<
|
|
15
|
+
parse(src: string): FilterParserResponse<NumberFilter> {
|
|
16
16
|
const fnumber_parser = new nearley.Parser(
|
|
17
17
|
nearley.Grammar.fromCompiled(fnumber_grammar)
|
|
18
18
|
);
|
|
19
19
|
const parse_result = run_parser(src, fnumber_parser);
|
|
20
|
-
if (parse_result.parsed &&
|
|
20
|
+
if (parse_result.parsed && isNumberFilter(parse_result.parsed)) {
|
|
21
21
|
return {parsed: parse_result.parsed, log: []};
|
|
22
22
|
}
|
|
23
23
|
return {parsed: null, log: parse_result.log};
|
|
24
24
|
},
|
|
25
|
-
unparse(nc:
|
|
25
|
+
unparse(nc: NumberFilter | null): string {
|
|
26
26
|
if (nc === null) {
|
|
27
27
|
return '';
|
|
28
28
|
}
|
|
@@ -5,8 +5,8 @@
|
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
import type {FilterParserResponse,
|
|
9
|
-
import {
|
|
8
|
+
import type {FilterParserResponse, StringFilter} from './filter_interface';
|
|
9
|
+
import {isStringFilter} from './filter_interface';
|
|
10
10
|
import * as nearley from 'nearley';
|
|
11
11
|
import fstring_grammar from './lib/fexpr_string_parser';
|
|
12
12
|
import {escape} from './clause_utils';
|
|
@@ -18,17 +18,17 @@ import {run_parser} from './nearley_parse';
|
|
|
18
18
|
// is not hand coded. Full apologies to the original author of the hand
|
|
19
19
|
// coded parsers.
|
|
20
20
|
export const StringFilterExpression = {
|
|
21
|
-
parse(src: string): FilterParserResponse<
|
|
21
|
+
parse(src: string): FilterParserResponse<StringFilter> {
|
|
22
22
|
const fstring_parser = new nearley.Parser(
|
|
23
23
|
nearley.Grammar.fromCompiled(fstring_grammar)
|
|
24
24
|
);
|
|
25
25
|
const parse_result = run_parser(src, fstring_parser);
|
|
26
|
-
if (parse_result.parsed &&
|
|
26
|
+
if (parse_result.parsed && isStringFilter(parse_result.parsed)) {
|
|
27
27
|
return {parsed: parse_result.parsed, log: []};
|
|
28
28
|
}
|
|
29
29
|
return {parsed: null, log: parse_result.log};
|
|
30
30
|
},
|
|
31
|
-
unparse(sc:
|
|
31
|
+
unparse(sc: StringFilter | null): string {
|
|
32
32
|
if (sc === null) {
|
|
33
33
|
return '';
|
|
34
34
|
}
|
|
@@ -8,26 +8,26 @@
|
|
|
8
8
|
import type {
|
|
9
9
|
FilterParserResponse,
|
|
10
10
|
Moment,
|
|
11
|
-
|
|
11
|
+
TemporalFilter,
|
|
12
12
|
Duration,
|
|
13
|
-
} from './
|
|
14
|
-
import {
|
|
13
|
+
} from './filter_interface';
|
|
14
|
+
import {isTemporalFilter} from './filter_interface';
|
|
15
15
|
import ftemporal_grammar from './lib/ftemporal_parser';
|
|
16
16
|
import * as nearley from 'nearley';
|
|
17
17
|
import {run_parser} from './nearley_parse';
|
|
18
18
|
|
|
19
19
|
export const TemporalFilterExpression = {
|
|
20
|
-
parse(src: string): FilterParserResponse<
|
|
20
|
+
parse(src: string): FilterParserResponse<TemporalFilter> {
|
|
21
21
|
const ftemporal_parser = new nearley.Parser(
|
|
22
22
|
nearley.Grammar.fromCompiled(ftemporal_grammar)
|
|
23
23
|
);
|
|
24
24
|
const parse_result = run_parser(src, ftemporal_parser);
|
|
25
|
-
if (parse_result.parsed &&
|
|
25
|
+
if (parse_result.parsed && isTemporalFilter(parse_result.parsed)) {
|
|
26
26
|
return {parsed: parse_result.parsed, log: []};
|
|
27
27
|
}
|
|
28
28
|
return {parsed: null, log: parse_result.log};
|
|
29
29
|
},
|
|
30
|
-
unparse(tc:
|
|
30
|
+
unparse(tc: TemporalFilter | null): string {
|
|
31
31
|
if (tc === null) {
|
|
32
32
|
return '';
|
|
33
33
|
}
|
|
@@ -67,7 +67,7 @@ export const TemporalFilterExpression = {
|
|
|
67
67
|
},
|
|
68
68
|
};
|
|
69
69
|
|
|
70
|
-
function notStr(tc:
|
|
70
|
+
function notStr(tc: TemporalFilter, s: string): string {
|
|
71
71
|
if ('not' in tc && tc.not) {
|
|
72
72
|
return 'not ' + s;
|
|
73
73
|
}
|
package/dist/filter_clause.d.ts
DELETED
|
@@ -1,133 +0,0 @@
|
|
|
1
|
-
export interface ClauseBase {
|
|
2
|
-
operator: string;
|
|
3
|
-
}
|
|
4
|
-
export declare function isClauseBase(o: Object): o is ClauseBase;
|
|
5
|
-
interface Negateable {
|
|
6
|
-
not?: boolean;
|
|
7
|
-
}
|
|
8
|
-
export interface Null extends ClauseBase, Negateable {
|
|
9
|
-
operator: 'null';
|
|
10
|
-
}
|
|
11
|
-
export type ChainOp = 'and' | 'or' | ',';
|
|
12
|
-
export declare function isChainOp(s: string): s is ChainOp;
|
|
13
|
-
interface ClauseChain<T> extends ClauseBase {
|
|
14
|
-
operator: ChainOp;
|
|
15
|
-
members: T[];
|
|
16
|
-
}
|
|
17
|
-
type BooleanChainOp = 'and' | 'or';
|
|
18
|
-
export interface BooleanChain<T> extends ClauseBase {
|
|
19
|
-
operator: BooleanChainOp;
|
|
20
|
-
members: T[];
|
|
21
|
-
}
|
|
22
|
-
interface ClauseGroup<T> extends ClauseBase, Negateable {
|
|
23
|
-
operator: '()';
|
|
24
|
-
expr: T;
|
|
25
|
-
}
|
|
26
|
-
export type StringConditionOperator = 'starts' | 'ends' | 'contains' | '=';
|
|
27
|
-
export declare function isStringCondition(sc: StringClause): sc is StringCondition;
|
|
28
|
-
export interface StringCondition extends ClauseBase, Negateable {
|
|
29
|
-
operator: StringConditionOperator;
|
|
30
|
-
values: string[];
|
|
31
|
-
}
|
|
32
|
-
export interface StringMatch extends ClauseBase, Negateable {
|
|
33
|
-
operator: '~';
|
|
34
|
-
escaped_values: string[];
|
|
35
|
-
}
|
|
36
|
-
export interface StringEmpty extends ClauseBase, Negateable {
|
|
37
|
-
operator: 'empty';
|
|
38
|
-
}
|
|
39
|
-
export type StringClause = StringCondition | StringMatch | Null | StringEmpty | ClauseChain<StringClause> | ClauseGroup<StringClause>;
|
|
40
|
-
export type BooleanOperator = 'true' | 'false' | 'false_or_null';
|
|
41
|
-
export interface BooleanCondition extends Negateable {
|
|
42
|
-
operator: BooleanOperator;
|
|
43
|
-
}
|
|
44
|
-
export type BooleanClause = BooleanCondition | Null;
|
|
45
|
-
export declare function isBooleanClause(bc: Object): bc is BooleanClause;
|
|
46
|
-
export declare function isStringClause(sc: Object): sc is StringClause;
|
|
47
|
-
export type NumberOperator = '<=' | '>=' | '!=' | '=' | '>' | '<';
|
|
48
|
-
export interface NumberCondition extends ClauseBase, Negateable {
|
|
49
|
-
operator: NumberOperator;
|
|
50
|
-
values: string[];
|
|
51
|
-
}
|
|
52
|
-
export type NumberRangeOperator = '<=' | '>=' | '>' | '<';
|
|
53
|
-
export interface NumberRange extends ClauseBase, Negateable {
|
|
54
|
-
operator: 'range';
|
|
55
|
-
startOperator: NumberRangeOperator;
|
|
56
|
-
startValue: string;
|
|
57
|
-
endOperator: NumberRangeOperator;
|
|
58
|
-
endValue: string;
|
|
59
|
-
}
|
|
60
|
-
export type NumberClause = NumberCondition | NumberRange | Null | ClauseGroup<NumberClause> | BooleanChain<NumberClause>;
|
|
61
|
-
export declare function isNumberClause(sc: Object): sc is NumberClause;
|
|
62
|
-
export type TemporalUnit = 'second' | 'minute' | 'hour' | 'day' | 'week' | 'month' | 'quarter' | 'year';
|
|
63
|
-
export interface TemporalLiteral {
|
|
64
|
-
moment: 'literal';
|
|
65
|
-
units?: TemporalUnit;
|
|
66
|
-
literal: string;
|
|
67
|
-
}
|
|
68
|
-
export interface Duration {
|
|
69
|
-
units: TemporalUnit;
|
|
70
|
-
n: string;
|
|
71
|
-
}
|
|
72
|
-
export interface WeekdayMoment {
|
|
73
|
-
moment: 'monday' | 'tuesday' | 'wednesday' | 'thursday' | 'friday' | 'saturday' | 'sunday';
|
|
74
|
-
which: 'last' | 'next';
|
|
75
|
-
}
|
|
76
|
-
export interface WhichdayMoment {
|
|
77
|
-
moment: 'yesterday' | 'today' | 'tomorrow';
|
|
78
|
-
}
|
|
79
|
-
export interface FromNowMoment extends Duration {
|
|
80
|
-
moment: 'from_now';
|
|
81
|
-
}
|
|
82
|
-
export interface AgoMoment extends Duration {
|
|
83
|
-
moment: 'ago';
|
|
84
|
-
}
|
|
85
|
-
export interface NowMoment {
|
|
86
|
-
moment: 'now';
|
|
87
|
-
}
|
|
88
|
-
export interface UnitMoment {
|
|
89
|
-
moment: 'this' | 'last' | 'next';
|
|
90
|
-
units: TemporalUnit;
|
|
91
|
-
}
|
|
92
|
-
export type Moment = UnitMoment | NowMoment | AgoMoment | FromNowMoment | TemporalLiteral | WhichdayMoment | WeekdayMoment;
|
|
93
|
-
export interface Before extends Negateable {
|
|
94
|
-
operator: 'before';
|
|
95
|
-
before: Moment;
|
|
96
|
-
}
|
|
97
|
-
export interface After extends Negateable {
|
|
98
|
-
operator: 'after';
|
|
99
|
-
after: Moment;
|
|
100
|
-
}
|
|
101
|
-
export interface To extends Negateable {
|
|
102
|
-
operator: 'to';
|
|
103
|
-
fromMoment: Moment;
|
|
104
|
-
toMoment: Moment;
|
|
105
|
-
}
|
|
106
|
-
export interface For extends Negateable, Duration {
|
|
107
|
-
operator: 'for';
|
|
108
|
-
begin: Moment;
|
|
109
|
-
}
|
|
110
|
-
export interface in_last extends Negateable, Duration {
|
|
111
|
-
operator: 'in_last';
|
|
112
|
-
}
|
|
113
|
-
export interface JustUnits extends Negateable, Duration {
|
|
114
|
-
operator: 'last' | 'next';
|
|
115
|
-
}
|
|
116
|
-
export interface InMoment extends Negateable {
|
|
117
|
-
operator: 'in';
|
|
118
|
-
in: Moment;
|
|
119
|
-
}
|
|
120
|
-
export type TemporalClause = Null | Before | After | To | For | JustUnits | in_last | InMoment | BooleanChain<TemporalClause> | ClauseGroup<TemporalClause>;
|
|
121
|
-
export declare function isTemporalClause(sc: Object): sc is TemporalClause;
|
|
122
|
-
export type FilterLogSeverity = 'error' | 'warn';
|
|
123
|
-
export interface FilterLog {
|
|
124
|
-
message: string;
|
|
125
|
-
startIndex: number;
|
|
126
|
-
endIndex: number;
|
|
127
|
-
severity: FilterLogSeverity;
|
|
128
|
-
}
|
|
129
|
-
export interface FilterParserResponse<T extends ClauseBase> {
|
|
130
|
-
parsed: T | null;
|
|
131
|
-
log: FilterLog[];
|
|
132
|
-
}
|
|
133
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"filter_clause.js","sourceRoot":"","sources":["../src/filter_clause.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;AAMH,SAAgB,YAAY,CAAC,CAAS;IACpC,OAAO,UAAU,IAAI,CAAC,CAAC;AACzB,CAAC;AAFD,oCAEC;AAWD,SAAgB,SAAS,CAAC,CAAS;IACjC,OAAO,CAAC,KAAK,EAAE,IAAI,EAAE,GAAG,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC;AACxC,CAAC;AAFD,8BAEC;AAmBD,SAAgB,iBAAiB,CAAC,EAAgB;IAChD,OAAO,CAAC,QAAQ,EAAE,MAAM,EAAE,UAAU,EAAE,GAAG,CAAC,CAAC,QAAQ,CAAC,EAAE,CAAC,QAAQ,CAAC,CAAC;AACnE,CAAC;AAFD,8CAEC;AAgCD,SAAgB,eAAe,CAAC,EAAU;IACxC,OAAO,CACL,UAAU,IAAI,EAAE;QAChB,OAAO,EAAE,CAAC,QAAQ,KAAK,QAAQ;QAC/B,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,eAAe,CAAC,CAAC,QAAQ,CAAC,EAAE,CAAC,QAAQ,CAAC,CACjE,CAAC;AACJ,CAAC;AAND,0CAMC;AAED,SAAgB,cAAc,CAAC,EAAU;IACvC,OAAO,CACL,UAAU,IAAI,EAAE;QAChB,OAAO,EAAE,CAAC,QAAQ,KAAK,QAAQ;QAC/B;YACE,QAAQ;YACR,MAAM;YACN,UAAU;YACV,GAAG;YACH,GAAG;YACH,MAAM;YACN,OAAO;YACP,KAAK;YACL,IAAI;YACJ,GAAG;YACH,IAAI;SACL,CAAC,QAAQ,CAAC,EAAE,CAAC,QAAQ,CAAC,CACxB,CAAC;AACJ,CAAC;AAlBD,wCAkBC;AA0BD,SAAgB,cAAc,CAAC,EAAU;IACvC,OAAO,CACL,UAAU,IAAI,EAAE;QAChB,OAAO,EAAE,CAAC,QAAQ,KAAK,QAAQ;QAC/B;YACE,OAAO;YACP,IAAI;YACJ,IAAI;YACJ,IAAI;YACJ,GAAG;YACH,GAAG;YACH,GAAG;YACH,KAAK;YACL,IAAI;YACJ,IAAI;YACJ,MAAM;SACP,CAAC,QAAQ,CAAC,EAAE,CAAC,QAAQ,CAAC,CACxB,CAAC;AACJ,CAAC;AAlBD,wCAkBC;AAiHD,SAAgB,gBAAgB,CAAC,EAAU;IACzC,OAAO,CACL,UAAU,IAAI,EAAE;QAChB,OAAO,EAAE,CAAC,QAAQ,KAAK,QAAQ;QAC/B;YACE,SAAS;YACT,QAAQ;YACR,OAAO;YACP,IAAI;YACJ,KAAK;YACL,IAAI;YACJ,KAAK;YACL,IAAI;YACJ,SAAS;YACT,MAAM;YACN,MAAM;YACN,MAAM;YACN,IAAI;YACJ,MAAM;SACP,CAAC,QAAQ,CAAC,EAAE,CAAC,QAAQ,CAAC,CACxB,CAAC;AACJ,CAAC;AArBD,4CAqBC"}
|