-
Notifications
You must be signed in to change notification settings - Fork 214
/
RqlParserBase.scala
193 lines (154 loc) · 5.53 KB
/
RqlParserBase.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
* 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.internal
import org.parboiled2._
import org.parboiled2.support.hlist._
import scala.{:: => _}
/**
* RQL Parser base containing commonly used types for both Predicate and Options parsing in EBNF:
* <pre>
* Literal = FloatLiteral | IntegerLiteral | StringLiteral | "true" | "false" | "null"
* DoubleLiteral = [ '+' | '-' ], "0.", Digit, { Digit } | [ '+' | '-' ], DigitWithoutZero, { Digit }, '.', Digit, { Digit }
* LongLiteral = '0' | [ '+' | '-' ], DigitWithoutZero, { Digit }
* DigitWithoutZero = '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'
* Digit = '0' | DigitWithoutZero
* StringLiteral = '"', ? printable characters ?, '"'
* PropertyLiteral = ? printable characters ?
* </pre>
*/
class RqlParserBase(val input: ParserInput) extends Parser {
protected val sb = new java.lang.StringBuilder
private def Digit = rule {
anyOf("0123456789")
}
private def Digit19 = rule {
anyOf("123456789")
}
private def HexDigit = rule {
anyOf("0123456789ABCDEFabcdef")
}
private def WhiteSpaceChar = rule {
anyOf(" \n\r\t\f")
}
private def CommaClosingParenthesisQuote = rule {
anyOf(",)\"\\")
}
private def QuoteBackslash = rule {
anyOf("\"\\")
}
private def SingleQuoteBackslash = rule {
anyOf("'\\")
}
private def QuoteBackslashSlash = rule {
QuoteBackslash | "/"
}
private def SingleQuoteBackslashSlash = rule {
SingleQuoteBackslash | "/"
}
protected def Literal: Rule1[java.lang.Object] = rule {
(DoubleLiteral | LongLiteral | StringLiteral | StringSingleQuoteLiteral | PlaceholderLiteral |
"true" ~ WhiteSpace ~ push(java.lang.Boolean.TRUE) |
"false" ~ WhiteSpace ~ push(java.lang.Boolean.FALSE) |
"null" ~ WhiteSpace ~ push(None)
) ~ WhiteSpace
}
protected def DoubleLiteral: Rule1[java.lang.Double] = rule {
capture(Integer ~ Frac) ~> (numb => java.lang.Double.valueOf(numb)) ~ WhiteSpace
}
protected def LongLiteral: Rule1[java.lang.Long] = rule {
!"-0" ~ capture(Integer) ~> (numb => java.lang.Long.valueOf(numb)) ~ WhiteSpace
}
protected def Integer: Rule[HNil, HNil] = rule {
optional(anyOf("+-")) ~ (Digit19 ~ Digits | Digit)
}
protected def Digits: Rule[HNil, HNil] = rule {
oneOrMore(Digit)
}
protected def Frac: Rule[HNil, HNil] = rule {
"." ~ Digits
}
protected def StringLiteral: Rule1[java.lang.String] = rule {
'"' ~ clearSB() ~ CharactersInQuotes ~ ws('"') ~ push(sb.toString)
}
protected def StringSingleQuoteLiteral: Rule1[java.lang.String] = rule {
'\'' ~ clearSB() ~ CharactersInSingleQuotes ~ ws('\'') ~ push(sb.toString)
}
protected def PlaceholderLiteral: Rule1[org.eclipse.ditto.rql.model.ParsedPlaceholder] = rule {
capture(PlaceholderPrefix ~ PlaceholderName) ~> (p => org.eclipse.ditto.rql.model.ParsedPlaceholder.of(p)) ~ WhiteSpace
}
protected def PlaceholderPrefix: Rule[HNil, HNil] = rule {
"time:"
// "time:"|"header:"
}
protected def PlaceholderName: Rule[HNil, HNil] = rule {
Characters
}
protected def PropertyLiteral: Rule1[java.lang.String] = rule {
clearSB() ~ Characters ~ push(sb.toString)
}
protected def CharactersInQuotes: Rule[HNil, HNil] = rule {
zeroOrMore(NormalCharInQuotes | '\\' ~ EscapedChar)
}
protected def CharactersInSingleQuotes: Rule[HNil, HNil] = rule {
zeroOrMore(NormalCharInSingleQuotes | '\\' ~ SingleQuoteEscapedChar)
}
protected def Characters: Rule[HNil, HNil] = rule {
zeroOrMore(NormalChar | '\\' ~ EscapedChar)
}
protected def NormalCharInQuotes: Rule[HNil, HNil] = rule {
!QuoteBackslash ~ ANY ~ appendSB()
}
protected def NormalCharInSingleQuotes: Rule[HNil, HNil] = rule {
!SingleQuoteBackslash ~ ANY ~ appendSB()
}
protected def NormalChar: Rule[HNil, HNil] = rule {
!CommaClosingParenthesisQuote ~ ANY ~ appendSB()
}
protected def EscapedChar: Rule[HNil, HNil] = rule(
QuoteBackslashSlash ~ appendSB()
| 'b' ~ appendSB('\b')
| 'f' ~ appendSB('\f')
| 'n' ~ appendSB('\n')
| 'r' ~ appendSB('\r')
| 't' ~ appendSB('\t')
| Unicode ~> { code: Int => sb.append(code.asInstanceOf[Char]); () }
)
protected def SingleQuoteEscapedChar: Rule[HNil, HNil] = rule(
SingleQuoteBackslashSlash ~ appendSB()
| 'b' ~ appendSB('\b')
| 'f' ~ appendSB('\f')
| 'n' ~ appendSB('\n')
| 'r' ~ appendSB('\r')
| 't' ~ appendSB('\t')
| Unicode ~> { code: Int => sb.append(code.asInstanceOf[Char]); () }
)
protected def Unicode: Rule[HNil, Int :: HNil] = rule {
'u' ~ capture(HexDigit ~ HexDigit ~ HexDigit ~ HexDigit) ~>
((code: String) => java.lang.Integer.parseInt(code, 16))
}
protected def WhiteSpace: Rule[HNil, HNil] = rule {
zeroOrMore(WhiteSpaceChar)
}
protected def ws(c: Char): Rule[HNil, HNil] = rule {
c ~ WhiteSpace
}
protected def clearSB(): Rule0 = rule {
run(sb.setLength(0))
}
protected def appendSB(): Rule0 = rule {
run(sb.append(lastChar))
}
protected def appendSB(c: Char): Rule0 = rule {
run(sb.append(c))
}
}