-
Notifications
You must be signed in to change notification settings - Fork 214
/
RqlOptionParser.scala
144 lines (125 loc) · 4.83 KB
/
RqlOptionParser.scala
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
/*
* Copyright (c) 2017 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.rql.parser.thingsearch.internal
import org.parboiled2._
import org.eclipse.ditto.rql.model.ParserException
import org.eclipse.ditto.rql.parser.internal.RqlParserBase
import org.eclipse.ditto.rql.parser.thingsearch.OptionParser
import org.eclipse.ditto.thingsearch.model
import org.eclipse.ditto.thingsearch.model.{LimitOption, SearchModelFactory, SortOption, SortOptionEntry}
import java.util
import scala.collection.JavaConverters
import scala.util.{Failure, Success}
/**
* RQL Parser. Parses options in the RQL "standard" according to https://github.com/persvr/rql with the following EBNF:
* <pre>
* Options = Option, { ',', Option }
* Option = Sort | Limit
* Sort = "sort", '(', SortProperty, { ',', SortProperty }, ')'
* SortProperty = SortOrder, PropertyLiteral
* SortOrder = '+' | '-'
* Limit = "limit", '(', IntegerLiteral, ',', IntegerLiteral, ')'
* </pre>
*/
private class RqlOptionParser(override val input: ParserInput) extends RqlParserBase(input) {
/**
* @return the root for parsing RQL Options.
*/
def OptionsRoot: Rule1[Seq[model.Option]] = rule {
WhiteSpace ~ Options ~ EOI
}
/**
* Options = Option, { ',', Option }
*/
private def Options: Rule1[Seq[model.Option]] = rule {
oneOrMore(Option).separatedBy(',')
}
/**
* Option = Sort | Limit | Cursor | Size
*/
private def Option: Rule1[model.Option] = rule {
Sort | Limit | Cursor | Size
}
/**
* Sort = "sort", '(', SortProperty, { ',', SortProperty }, ')'
*/
private def Sort: Rule1[SortOption] = rule {
"sort" ~ '(' ~ oneOrMore(SortProperty).separatedBy(',') ~ ')' ~>
((options: Seq[SortOptionEntry]) => SearchModelFactory.newSortOption(JavaConverters.seqAsJavaList(options)))
}
/**
* SortProperty = SortOrder, PropertyLiteral
*/
private def SortProperty: Rule1[SortOptionEntry] = rule {
SortOrder ~ PropertyLiteral ~>
((order: SortOptionEntry.SortOrder, property: CharSequence) =>
SearchModelFactory.newSortOptionEntry(property, order))
}
/**
* SortOrder = '+' | '-'
*/
private def SortOrder: Rule1[SortOptionEntry.SortOrder] = rule {
Asc | Desc
}
private def Asc: Rule1[SortOptionEntry.SortOrder] = rule {
'+' ~ push(SortOptionEntry.SortOrder.ASC)
}
private def Desc: Rule1[SortOptionEntry.SortOrder] = rule {
'-' ~ push(SortOptionEntry.SortOrder.DESC)
}
/**
* Limit = "limit", '(', IntegerLiteral, ',', IntegerLiteral, ')'
*/
private def Limit: Rule1[LimitOption] = rule {
"limit" ~ '(' ~ LongLiteral ~ ',' ~ LongLiteral ~ ')' ~> ((offset: java.lang.Long, count: java.lang.Long) =>
SearchModelFactory.newLimitOption(offset.toInt, count.toInt))
}
/**
* Cursor = "cursor", '(', StringLiteral, ')'
*/
private def Cursor[CursorOption] = rule {
"cursor" ~ '(' ~ CursorString ~ ')' ~>
((cursor: String) => SearchModelFactory.newCursorOption(cursor))
}
/**
* Size = "size", '(', IntegerLiteral, ')'
*/
private def Size[SizeOption] = rule {
"size" ~ '(' ~ capture(Digits) ~ ')' ~>
((size: String) => SearchModelFactory.newSizeOption(java.lang.Integer.valueOf(size)))
}
private def CursorString: Rule1[String] = PropertyLiteral
}
/**
* Companion singleton Object.
*/
object RqlOptionParser extends OptionParser {
/**
* Parse the specified input.
*
* @param input the input that should be parsed.
* @return the AST RootNode representing the root of the AST.
* @throws NullPointerException if input is null.
* @throws ParserException if input could not be parsed.
*/
override def parse(input: String): util.List[model.Option] = parseOptions(input)
private def parseOptions(string: String): util.List[model.Option] = {
val parser = rqlOptionsParser(string)
parser.OptionsRoot.run() match {
case Success(o) => JavaConverters.seqAsJavaList(o)
case Failure(f: ParseError) => throw new ParserException(parser.formatError(f), f)
case Failure(f) => throw new ParserException("Unknown error during parsing options: " + f.getMessage, f)
}
}
private def rqlOptionsParser(string: String): RqlOptionParser = new RqlOptionParser(ParserInput.apply(string))
}