-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathParserCommon.scala
354 lines (306 loc) · 9.9 KB
/
ParserCommon.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
package smtlib
package parser
import lexer.Tokens
import Tokens.{Token, TokenKind}
import lexer.Lexer
import trees.Terms._
import scala.collection.mutable.ListBuffer
/*
* ParserCommon exports common feature of the parser. That
* is, the useful functions to apply parsing (parseMany, parseUntil, etc)
* as well as the core Lexer + lookahead mecanism. It also provides basic
* syntactic units that don't really belong to Terms or Commands, such as
* attributes and identifiers.
*/
trait ParserCommon {
val lexer: Lexer
import Parser._
private var _currentToken: Token = null
/* lookAhead token is Some(null) if we reached eof */
private var _lookAhead: Option[Token] = None
//return a next token or throw UnexpectedEOFException if the next token is null
protected def nextToken(): Token = {
_lookAhead match {
case Some(t) => {
_lookAhead = None
_currentToken = t
t
}
case None => {
_currentToken = lexer.nextToken
_lookAhead = None
_currentToken
}
}
}
/*
* return the look ahead token, or null if EOF
* Note: do not throw an exception as it is okay to lookahead into EOF
*/
protected def peekToken: Token = {
_lookAhead match {
case Some(t) => t
case None => {
_lookAhead = Some(lexer.nextToken)
_lookAhead.get
}
}
}
/*
* Same as peekToken, but expect a token to be present
* and result in a UnexpectedEOFException if the token is null
*/
protected def getPeekToken: Token = {
val tmp = peekToken
if(tmp == null)
throw new UnexpectedEOFException(Seq())
tmp
}
/*
* Make sure the next token has the expected kind and read it
*/
protected def eat(expected: TokenKind): Token = {
val token = nextToken()
check(token, expected)
token
}
/*
* Make sure the next token is exactly ``expected`` (usually a symbol lit)
*/
protected def eat(expected: Token): Token = {
val token = nextToken()
if(token != expected)
throw new UnexpectedTokenException(token, Seq(expected.kind))
token
}
protected def check(current: Token, exp: TokenKind): Unit = {
if(current == null)
throw new UnexpectedEOFException(Seq(exp))
if(current.kind != exp) {
expected(current, exp)
}
}
protected def parseBefore[A](endKind: TokenKind)(parseFun: () => A): A = {
val res = parseFun()
eat(endKind)
res
}
protected def parseWithin[A](startKind: TokenKind, endKind: TokenKind)(parseFun: () => A): A = {
eat(startKind)
parseBefore(endKind)(parseFun)
}
protected def parseUntil[A](endKind: TokenKind, eatEnd: Boolean = true)(parseFun: () => A): Seq[A] = {
val items = new ListBuffer[A]
while(peekToken != null && peekToken.kind != endKind)
items.append(parseFun())
if(eatEnd)
eat(endKind)
items.toList
}
protected def parseMany[A](parseFun: () => A): Seq[A] = {
eat(Tokens.OParen)
parseUntil(Tokens.CParen)(parseFun)
}
/* Parse a sequence of A inside () */
protected def parseOneOrMore[A](parseFun: () => A): (A, Seq[A]) = {
val items = new ListBuffer[A]
eat(Tokens.OParen)
val head = parseFun()
while(peekToken != null && peekToken.kind != Tokens.CParen)
items.append(parseFun())
eat(Tokens.CParen)
(head, items.toList)
}
//TODO: we need a token class/type, instead of precise token with content + position
protected def expected(found: Token, expected: TokenKind*): Nothing = {
if(found == null)
throw new UnexpectedEOFException(expected)
else
throw new UnexpectedTokenException(found, expected)
}
/*
* From now on, parse methods for shared syntactic units
*/
def parseAttribute: Attribute = {
val keyword = parseKeyword
val attributeValue = tryParseAttributeValue
Attribute(keyword, attributeValue).setPos(keyword)
}
def parseAttributeValue: AttributeValue = {
val attributeValuesTokenKinds: Seq[Tokens.TokenKind] = Seq(
Tokens.NumeralLitKind, Tokens.BinaryLitKind, Tokens.HexadecimalLitKind,
Tokens.DecimalLitKind, Tokens.StringLitKind, Tokens.SymbolLitKind, Tokens.OParen)
getPeekToken.kind match {
case Tokens.NumeralLitKind => parseNumeral
case Tokens.BinaryLitKind => parseBinary
case Tokens.HexadecimalLitKind => parseHexadecimal
case Tokens.DecimalLitKind => parseDecimal
case Tokens.StringLitKind => parseString
case Tokens.SymbolLitKind => parseSymbol
case Tokens.OParen => parseSList
case _ => expected(peekToken, attributeValuesTokenKinds:_*)
}
}
def tryParseAttributeValue: Option[AttributeValue] = {
if(peekToken == null) None else peekToken.kind match {
case Tokens.NumeralLitKind => Some(parseNumeral)
case Tokens.BinaryLitKind => Some(parseBinary)
case Tokens.HexadecimalLitKind => Some(parseHexadecimal)
case Tokens.DecimalLitKind => Some(parseDecimal)
case Tokens.StringLitKind => Some(parseString)
case Tokens.SymbolLitKind => Some(parseSymbol)
case Tokens.OParen => Some(parseSList)
case _ => None
}
}
def parseKeyword: SKeyword = {
nextToken() match {
case [email protected](k) => {
val keyword = SKeyword(k)
keyword.setPos(t)
}
case token => expected(token, Tokens.KeywordKind)
}
}
def parseSort: Sort = {
if(getPeekToken.kind == Tokens.OParen) {
val startPos = getPeekToken.getPos
eat(Tokens.OParen)
val res = if(getPeekToken.kind == Tokens.Underscore) {
val qid = parseUnderscoreIdentifier.setPos(startPos)
Sort(qid).setPos(startPos)
} else {
val name = parseIdentifier
val subSorts = parseUntil(Tokens.CParen, eatEnd = false)(() => parseSort)
Sort(name, subSorts.toList).setPos(startPos)
}
eat(Tokens.CParen)
res
} else {
val id = parseIdentifier
Sort(id).setPos(id)
}
}
//currently not used, but should be used with identifiers
//however we parse any s-expr as an index currently for
//convenience reason with existing tools (not correct standard-wise)
def parseIndex: Index = {
getPeekToken.kind match {
case Tokens.SymbolLitKind => parseSymbol
case Tokens.NumeralLitKind => parseNumeral
case _ => expected(peekToken, Tokens.SymbolLitKind, Tokens.NumeralLitKind)
}
}
//the caller should set the position of the id to be the OParen
def parseUnderscoreIdentifier: Identifier = {
eat(Tokens.Underscore)
val sym = parseSymbol
val head = parseSExpr
val indices = parseUntil(Tokens.CParen, eatEnd = false)(() => parseSExpr)
Identifier(sym, head +: indices)
}
def parseAsIdentifier: QualifiedIdentifier = {
eat(Tokens.As)
val id = parseIdentifier
val sort = parseSort
QualifiedIdentifier(id, Some(sort))
}
def parseIdentifier: Identifier = {
if(getPeekToken.kind == Tokens.OParen) {
val pos = getPeekToken.getPos
parseWithin(Tokens.OParen, Tokens.CParen)(() => parseUnderscoreIdentifier).setPos(pos)
} else {
val sym = parseSymbol
Identifier(sym).setPos(sym)
}
}
def parseSymbol: SSymbol = {
nextToken() match {
case [email protected](s) => {
val symbol = SSymbol(s)
symbol.setPos(t)
}
case t => expected(t, Tokens.SymbolLitKind)
}
}
/*
* parse{String,Numeral,Decimal,Hexadecimal,Binary} are considered as
* terms in the trees, but technically they
* can be used in many other situations (attribute values, command values)
* so are rather part of Common parsers than just Term parsers.
*/
def parseString: SString = {
nextToken() match {
case [email protected](s) => {
val str = SString(s)
str.setPos(t)
}
case t => expected(t, Tokens.StringLitKind)
}
}
def parseNumeral: SNumeral = {
nextToken() match {
case [email protected](n) => {
val num = SNumeral(n)
num.setPos(t)
}
case token => expected(token, Tokens.NumeralLitKind)
}
}
def parseDecimal: SDecimal = {
nextToken() match {
case [email protected](n) => {
val dec = SDecimal(n)
dec.setPos(t)
}
case token => expected(token, Tokens.DecimalLitKind)
}
}
def parseHexadecimal: SHexadecimal = {
nextToken() match {
case [email protected](h) => {
val hexa = SHexadecimal(h)
hexa.setPos(t)
}
case token => expected(token, Tokens.HexadecimalLitKind)
}
}
def parseBinary: SBinary = {
nextToken() match {
case [email protected](b) => {
val bin = SBinary(b.toList)
bin.setPos(t)
}
case token => expected(token, Tokens.BinaryLitKind)
}
}
def parseSList: SList = SList(parseMany(() => parseSExpr).toList)
/**
*
* @note This is slighly inconsistent with the fact that Command and Term inherit
* from SExpr, in the sense that this will never return a Command or Term
* but rather returns the equivalent SList representation. So no
* {{{ SetLogic(QF_LIA) }}} but {{{ SList(SSymbol("set-logic"), SSymbol("QF_LIA")) }}}
*/
def parseSExpr: SExpr = {
getPeekToken.kind match {
case Tokens.SymbolLitKind => parseSymbol
case (word: Tokens.ReservedWord) => {
nextToken()
SSymbol(Tokens.reservedToSymbol(word))
}
case Tokens.NumeralLitKind => parseNumeral
case Tokens.BinaryLitKind => parseBinary
case Tokens.HexadecimalLitKind => parseHexadecimal
case Tokens.DecimalLitKind => parseDecimal
case Tokens.StringLitKind => parseString
case Tokens.KeywordKind => parseKeyword
case Tokens.OParen => parseSList
case kind =>
expected(peekToken,
Tokens.SymbolLitKind, Tokens.NumeralLitKind, Tokens.BinaryLitKind,
Tokens.HexadecimalLitKind, Tokens.DecimalLitKind, Tokens.StringLitKind,
Tokens.KeywordKind, Tokens.OParen)
}
}
}