Skip to content
This repository was archived by the owner on Nov 19, 2021. It is now read-only.

Commit 42d35a1

Browse files
author
Tom Ashworth
committed
Generalise pairs and make then in/excludable
1 parent eeb859b commit 42d35a1

File tree

3 files changed

+57
-41
lines changed

3 files changed

+57
-41
lines changed

grammar/query.peg

Lines changed: 7 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,46 +15,22 @@ grammar Query
1515
or_groups <- (or_sep value:orable)+ <Values>
1616

1717
# Only some oprators work in an OR
18-
orable <- filter
19-
/ reply
20-
/ engagement
21-
/ lang
22-
/ including
18+
orable <- including
2319

2420
# If it's not an OR you can use any operator
25-
op <- filter
26-
/ reply
27-
/ since
28-
/ until
29-
/ list
30-
/ engagement
31-
/ lang
32-
/ exclude
33-
/ question
21+
op <- question
3422
/ excluding
3523
/ including
3624

37-
engagement <- min_rts
38-
/ min_faves
39-
/ min_replies
40-
4125
excluding <- "-" value:base <Excluding>
4226
including <- "" value:base <Including>
4327

44-
base <- exact / hashtag / mention / word
28+
base <- list / pair / exact / hashtag / mention / word
4529

4630
# Query Syntax Rules
4731

48-
filter <- k:"filter" sep v:word <Pair>
49-
reply <- k:"to" sep "@"? v:screen_name <Pair>
50-
since <- k:"since" sep v:date <Pair>
51-
until <- k:"until" sep v:date <Pair>
32+
pair <- k:slug sep v:word <Pair>
5233
list <- "list" sep list_name <List>
53-
min_rts <- k:(min "retweets") sep v:integer <Pair>
54-
min_faves <- k:(min "faves") sep v:integer <Pair>
55-
min_replies <- k:(min "replies") sep v:integer <Pair>
56-
lang <- k:"lang" sep v:word <Pair>
57-
exclude <- k:"exclude" sep v:word <Pair>
5834
question <- "?" <IsQuestion>
5935
exact <- '"' value:([^\"]*) '"' <Exactly>
6036
hashtag <- "#" tag <Text>
@@ -66,11 +42,12 @@ grammar Query
6642
or_sep <- __+ "OR" __+
6743
list_name <- screen_name "/" list_slug
6844
list_slug <- [a-z-]+
69-
screen_name <- [a-zA-z0-9_]+
45+
screen_name <- slug
7046
# There's probably a better way to do this
7147
date <- d d d d "-" d d "-" d d
7248
# This is clearly wrong — needs to account for unicode
73-
tag <- [a-zA-z0-9_]+
49+
tag <- slug
50+
slug <- [a-zA-Z0-9_]+
7451
min <- "min_"
7552
sep <- ":"
7653
integer <- d d*

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"gen": "canopy grammar/query.peg --lang js",
1313
"lint": "xo",
1414
"lint:fix": "npm run lint -- --fix",
15-
"specs": "ava",
15+
"specs": "ava src/__tests__/**/*.js",
1616
"try": "babel-node try.js",
1717
"test": "npm run gen && npm run lint && npm run specs"
1818
},

src/__tests__/parse.js

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,33 @@ const testCases = [
2121
]
2222
]
2323
],
24+
[
25+
'pairs',
26+
`filter:vine exclude:retweets min_replies:100 lang:es to:jack since:2016-01-01
27+
-filter:vine -exclude:retweets -min_replies:100 -lang:es -to:jack -since:2016-01-01`,
28+
[
29+
['Including', ['Pair', 'filter', 'vine']],
30+
['Including', ['Pair', 'exclude', 'retweets']],
31+
['Including', ['Pair', 'min_replies', '100']],
32+
['Including', ['Pair', 'lang', 'es']],
33+
['Including', ['Pair', 'to', 'jack']],
34+
['Including', ['Pair', 'since', '2016-01-01']],
35+
['Excluding', ['Pair', 'filter', 'vine']],
36+
['Excluding', ['Pair', 'exclude', 'retweets']],
37+
['Excluding', ['Pair', 'min_replies', '100']],
38+
['Excluding', ['Pair', 'lang', 'es']],
39+
['Excluding', ['Pair', 'to', 'jack']],
40+
['Excluding', ['Pair', 'since', '2016-01-01']]
41+
]
42+
],
43+
[
44+
'list',
45+
'list:beep/boop -list:beep/boop',
46+
[
47+
['Including', ['List', 'beep', 'boop']],
48+
['Excluding', ['List', 'beep', 'boop']]
49+
]
50+
],
2451
[
2552
'extreme example',
2653
`search #search @search -query filter:vine exclude:retweets exclude:nativeretweets
@@ -33,17 +60,29 @@ const testCases = [
3360
['Including', ['Text', '#search']],
3461
['Including', ['Text', '@search']],
3562
['Excluding', ['Text', 'query']],
36-
['Pair', 'filter', 'vine'],
37-
['Pair', 'exclude', 'retweets'],
38-
['Pair', 'exclude', 'nativeretweets'],
39-
['Or', [['Pair', 'min_replies', '10'], ['Pair', 'min_retweets', '100']]],
40-
['Pair', 'min_faves', '20'],
41-
['Or', [['Pair', 'lang', 'es'], ['Pair', 'to', 'jack']]],
63+
['Including', ['Pair', 'filter', 'vine']],
64+
['Including', ['Pair', 'exclude', 'retweets']],
65+
['Including', ['Pair', 'exclude', 'nativeretweets']],
66+
[
67+
'Or',
68+
[
69+
['Including', ['Pair', 'min_replies', '10']],
70+
['Including', ['Pair', 'min_retweets', '100']]
71+
]
72+
],
73+
['Including', ['Pair', 'min_faves', '20']],
74+
[
75+
'Or',
76+
[
77+
['Including', ['Pair', 'lang', 'es']],
78+
['Including', ['Pair', 'to', 'jack']]
79+
]
80+
],
4281
['IsQuestion', true],
43-
['Pair', 'since', '2016-01-01'],
44-
['Pair', 'until', '2016-02-01'],
45-
['List', 'NASA', 'astronauts-in-space-now'],
46-
['Pair', 'filter', 'verified'],
82+
['Including', ['Pair', 'since', '2016-01-01']],
83+
['Including', ['Pair', 'until', '2016-02-01']],
84+
['Including', ['List', 'NASA', 'astronauts-in-space-now']],
85+
['Including', ['Pair', 'filter', 'verified']],
4786
[
4887
'Or',
4988
[

0 commit comments

Comments
 (0)