This repository has been archived by the owner on Nov 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathquery.peg
62 lines (51 loc) · 2.41 KB
/
query.peg
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
# https://dev.twitter.com/rest/public/search
grammar Query
# Query Shape Rules
# One or more operators makes up a query
root <- __* value:query __* <Value>
query <- (value:operator __*)+ <And>
# There are realy only two kinds of operator: include and exclude
operator <- excluding
/ including
# To exclude, you use a "-" before the query. OR cannot be negated unless it's in a group.
excluding <- "-" value:base <Excluding>
including <- "" value:(or / base) <Including>
# Operators can be grouped with an OR keyword in groups of two or more.
# I'm not sure if this is the ideal way to do this with PEG but it works for now.
or <- value:orable or_groups <Or>
or_groups <- (or_sep value:orable)+ <Values>
orable <- "" value:base <Including>
# The base operators are the things that make up the query itself.
# Filters, hashtags, text etc.
base <- group
/ list
/ pair
/ exact
/ hashtag
/ mention
/ word
# A group is a complete subquery. Recursion!
group <- "(" root ")" <Group>
# Query Syntax Rules
# These outline the specifics of the syntax
pair <- k:slug sep v:word <Pair>
list <- "list" sep list_name <List>
exact <- '"' value:([^\"]*) '"' <Exactly>
hashtag <- "#" tag <Text>
cashtag <- "$" tag <Text>
mention <- "" screen_name <Text>
word <- "+"? [^\s\)\(]+ <Text>
# Utility Rules
or_sep <- __+ "OR" __+
list_name <- screen_name "/" list_slug
list_slug <- [a-z-]+
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 <- slug
slug <- [a-zA-Z0-9_]+
sep <- ":"
integer <- d d*
d <- [0-9]
__ <- [\s]