Permalink
Browse files

Generalise pairs and make then in/excludable

  • Loading branch information...
Tom Ashworth
Tom Ashworth committed Jun 23, 2016
1 parent eeb859b commit 42d35a118324339bae590d234297752ac451f10e
Showing with 57 additions and 41 deletions.
  1. +7 −30 grammar/query.peg
  2. +1 −1 package.json
  3. +49 −10 src/__tests__/parse.js
@@ -15,46 +15,22 @@ grammar Query
or_groups <- (or_sep value:orable)+ <Values>

# Only some oprators work in an OR
orable <- filter
/ reply
/ engagement
/ lang
/ including
orable <- including

# If it's not an OR you can use any operator
op <- filter
/ reply
/ since
/ until
/ list
/ engagement
/ lang
/ exclude
/ question
op <- question
/ excluding
/ including

engagement <- min_rts
/ min_faves
/ min_replies

excluding <- "-" value:base <Excluding>
including <- "" value:base <Including>

base <- exact / hashtag / mention / word
base <- list / pair / exact / hashtag / mention / word

# Query Syntax Rules

filter <- k:"filter" sep v:word <Pair>
reply <- k:"to" sep "@"? v:screen_name <Pair>
since <- k:"since" sep v:date <Pair>
until <- k:"until" sep v:date <Pair>
pair <- k:slug sep v:word <Pair>
list <- "list" sep list_name <List>
min_rts <- k:(min "retweets") sep v:integer <Pair>
min_faves <- k:(min "faves") sep v:integer <Pair>
min_replies <- k:(min "replies") sep v:integer <Pair>
lang <- k:"lang" sep v:word <Pair>
exclude <- k:"exclude" sep v:word <Pair>
question <- "?" <IsQuestion>
exact <- '"' value:([^\"]*) '"' <Exactly>
hashtag <- "#" tag <Text>
@@ -66,11 +42,12 @@ grammar Query
or_sep <- __+ "OR" __+
list_name <- screen_name "/" list_slug
list_slug <- [a-z-]+
screen_name <- [a-zA-z0-9_]+
screen_name <- slug
# There's probably a better way to do this
date <- d d d d "-" d d "-" d d
# This is clearly wrong — needs to account for unicode
tag <- [a-zA-z0-9_]+
tag <- slug
slug <- [a-zA-Z0-9_]+
min <- "min_"
sep <- ":"
integer <- d d*
@@ -12,7 +12,7 @@
"gen": "canopy grammar/query.peg --lang js",
"lint": "xo",
"lint:fix": "npm run lint -- --fix",
"specs": "ava",
"specs": "ava src/__tests__/**/*.js",
"try": "babel-node try.js",
"test": "npm run gen && npm run lint && npm run specs"
},
@@ -21,6 +21,33 @@ const testCases = [
]
]
],
[
'pairs',
`filter:vine exclude:retweets min_replies:100 lang:es to:jack since:2016-01-01
-filter:vine -exclude:retweets -min_replies:100 -lang:es -to:jack -since:2016-01-01`,
[
['Including', ['Pair', 'filter', 'vine']],
['Including', ['Pair', 'exclude', 'retweets']],
['Including', ['Pair', 'min_replies', '100']],
['Including', ['Pair', 'lang', 'es']],
['Including', ['Pair', 'to', 'jack']],
['Including', ['Pair', 'since', '2016-01-01']],
['Excluding', ['Pair', 'filter', 'vine']],
['Excluding', ['Pair', 'exclude', 'retweets']],
['Excluding', ['Pair', 'min_replies', '100']],
['Excluding', ['Pair', 'lang', 'es']],
['Excluding', ['Pair', 'to', 'jack']],
['Excluding', ['Pair', 'since', '2016-01-01']]
]
],
[
'list',
'list:beep/boop -list:beep/boop',
[
['Including', ['List', 'beep', 'boop']],
['Excluding', ['List', 'beep', 'boop']]
]
],
[
'extreme example',
`search #search @search -query filter:vine exclude:retweets exclude:nativeretweets
@@ -33,17 +60,29 @@ const testCases = [
['Including', ['Text', '#search']],
['Including', ['Text', '@search']],
['Excluding', ['Text', 'query']],
['Pair', 'filter', 'vine'],
['Pair', 'exclude', 'retweets'],
['Pair', 'exclude', 'nativeretweets'],
['Or', [['Pair', 'min_replies', '10'], ['Pair', 'min_retweets', '100']]],
['Pair', 'min_faves', '20'],
['Or', [['Pair', 'lang', 'es'], ['Pair', 'to', 'jack']]],
['Including', ['Pair', 'filter', 'vine']],
['Including', ['Pair', 'exclude', 'retweets']],
['Including', ['Pair', 'exclude', 'nativeretweets']],
[
'Or',
[
['Including', ['Pair', 'min_replies', '10']],
['Including', ['Pair', 'min_retweets', '100']]
]
],
['Including', ['Pair', 'min_faves', '20']],
[
'Or',
[
['Including', ['Pair', 'lang', 'es']],
['Including', ['Pair', 'to', 'jack']]
]
],
['IsQuestion', true],
['Pair', 'since', '2016-01-01'],
['Pair', 'until', '2016-02-01'],
['List', 'NASA', 'astronauts-in-space-now'],
['Pair', 'filter', 'verified'],
['Including', ['Pair', 'since', '2016-01-01']],
['Including', ['Pair', 'until', '2016-02-01']],
['Including', ['List', 'NASA', 'astronauts-in-space-now']],
['Including', ['Pair', 'filter', 'verified']],
[
'Or',
[

0 comments on commit 42d35a1

Please sign in to comment.