forked from regb/scala-smtlib
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ParserCommands.scala
343 lines (275 loc) · 9.13 KB
/
ParserCommands.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
package smtlib
package parser
import lexer.Tokens
import trees.Terms._
import trees.Commands._
import scala.collection.mutable.ListBuffer
trait ParserCommands { this: ParserCommon with ParserTerms =>
import Parser._
def parseScript: Script = {
val cmds = new ListBuffer[Command]()
var cmd = parseCommand
while(cmd != null) {
cmds.append(cmd)
cmd = parseCommand
}
Script(cmds.toList)
}
protected def parseCommandWithoutParens: Command = {
val tok = nextToken()
tok.kind match {
case Tokens.Assert =>
Assert(parseTerm)
case Tokens.CheckSat => CheckSat()
case Tokens.CheckSatAssuming =>
val props = parseMany(() => parsePropLit)
CheckSatAssuming(props)
case Tokens.DeclareConst =>
val name = parseSymbol
val sort = parseSort
DeclareConst(name, sort)
case Tokens.DeclareFun =>
val sym = parseSymbol
val params = new ListBuffer[Sort]
eat(Tokens.OParen)
while(peekToken.kind != Tokens.CParen)
params.append(parseSort)
eat(Tokens.CParen)
val sort = parseSort
DeclareFun(sym, params.toList, sort)
case Tokens.DeclareSort =>
val sym = parseSymbol
val arity = parseNumeral
DeclareSort(sym, arity.value.toInt)
case Tokens.DefineFun =>
val funDef = parseFunDef
DefineFun(funDef)
case Tokens.DefineFunRec =>
val funDef = parseFunDef
DefineFunRec(funDef)
case Tokens.DefineFunsRec =>
val (funDef, funDefs) = parseOneOrMore(() => parseWithin(Tokens.OParen, Tokens.CParen)(() => parseFunDec))
val (body, bodies) = parseOneOrMore(() => parseTerm)
assert(funDefs.size == bodies.size)
DefineFunsRec(funDef +: funDefs, body +: bodies)
case Tokens.DefineSort =>
val sym = parseSymbol
val vars = new ListBuffer[SSymbol]
eat(Tokens.OParen)
while(peekToken.kind != Tokens.CParen)
vars.append(parseSymbol)
eat(Tokens.CParen)
val sort = parseSort
DefineSort(sym, vars.toList, sort)
case Tokens.Echo =>
val value = parseString
Echo(value)
case Tokens.Exit => Exit()
case Tokens.GetAssertions => GetAssertions()
case Tokens.GetAssignment => GetAssignment()
case Tokens.GetInfo =>
val infoFlag = parseInfoFlag
GetInfo(infoFlag)
case Tokens.GetModel => GetModel()
case Tokens.GetOption =>
val keyword = parseKeyword
GetOption(keyword)
case Tokens.GetProof => GetProof()
case Tokens.GetUnsatAssumptions => GetUnsatAssumptions()
case Tokens.GetUnsatCore => GetUnsatCore()
case Tokens.GetValue =>
eat(Tokens.OParen)
val ts = new ListBuffer[Term]
while(peekToken.kind != Tokens.CParen)
ts.append(parseTerm)
eat(Tokens.CParen)
GetValue(ts.head, ts.tail.toList)
case Tokens.Pop =>
val n = parseNumeral
Pop(n.value.toInt)
case Tokens.Push =>
val n = parseNumeral
Push(n.value.toInt)
case Tokens.Reset => Reset()
case Tokens.ResetAssertions => ResetAssertions()
case Tokens.SetInfo =>
SetInfo(parseAttribute)
case Tokens.SetLogic =>
val logicSymbol: SSymbol = parseSymbol
val logic: Logic =
Logic.standardLogicFromString.lift(logicSymbol.name).getOrElse({
logicSymbol match {
case SSymbol("ALL") => ALL()
case _ => NonStandardLogic(logicSymbol)
}
})
SetLogic(logic.setPos(logicSymbol))
case Tokens.SetOption =>
SetOption(parseOption)
case Tokens.DeclareDatatypes =>
val names = parseMany(() => parseSortDecl)
val ctorss = parseMany(() => parseConstructors)
if (names.size != ctorss.size) {
throw new IllformedCommandException(tok, "Sort and datatypes declaration do not correspond")
}
DeclareDatatypes(names.zip(ctorss))
case kind =>
throw new UnknownCommandException(kind)
}
}
def parseSortDecl: SSymbol = {
eat(Tokens.OParen)
val name = parseSymbol
val arity = eat(Tokens.NumeralLitKind)
arity match {
case Tokens.NumeralLit(n) =>
if (n != 0) throw new UnsupportedException(s"Parametric sort is unsupported (position ${arity.getPos})")
case _ =>
assert(false) // Unreachable because we expect a numeral
}
eat(Tokens.CParen)
name
}
def parseCommand: Command = if(peekToken == null) null else {
val head = nextToken()
check(head, Tokens.OParen)
val cmd = parseCommandWithoutParens
eat(Tokens.CParen)
cmd.setPos(head)
}
def parsePropLit: PropLiteral = {
peekToken.kind match {
case Tokens.SymbolLitKind => {
val sym = parseSymbol
PropLiteral(sym, true).setPos(sym)
}
case Tokens.OParen => {
val start = eat(Tokens.OParen)
eat(Tokens.SymbolLit("not"))
val sym = parseSymbol
eat(Tokens.CParen)
PropLiteral(sym, false).setPos(start)
}
case _ => {
expected(peekToken, Tokens.SymbolLitKind, Tokens.OParen)
}
}
}
def parseFunDec: FunDec = {
val name = parseSymbol
val sortedVars = parseMany(() => parseSortedVar)
val sort = parseSort
FunDec(name, sortedVars, sort)
}
def parseFunDef: FunDef = {
val name = parseSymbol
val sortedVars = parseMany(() => parseSortedVar)
val sort = parseSort
val body = parseTerm
FunDef(name, sortedVars, sort, body)
}
// SMT 2.5 & TIP
def parseDatatypes: (SSymbol, Seq[Constructor]) = {
eat(Tokens.OParen)
val name = parseSymbol
val constructors = new ListBuffer[Constructor]
while(peekToken.kind != Tokens.CParen) {
constructors.append(parseConstructor)
}
eat(Tokens.CParen)
(name, constructors.toSeq)
}
// SMT 2.6+
def parseConstructors: Seq[Constructor] = {
eat(Tokens.OParen)
val constructors = new ListBuffer[Constructor]
while(peekToken.kind != Tokens.CParen) {
constructors.append(parseConstructor)
}
eat(Tokens.CParen)
constructors.toSeq
}
def parseConstructor: Constructor = {
eat(Tokens.OParen)
val name = parseSymbol
val fields = new ListBuffer[(SSymbol, Sort)]
while(peekToken.kind != Tokens.CParen) {
eat(Tokens.OParen)
val fieldName = parseSymbol
val fieldSort = parseSort
eat(Tokens.CParen)
fields.append((fieldName, fieldSort))
}
eat(Tokens.CParen)
Constructor(name, fields.toList)
}
def parseInfoFlag: InfoFlag = {
val t = nextToken()
val flag = t match {
case Tokens.Keyword("all-statistics") => AllStatisticsInfoFlag()
case Tokens.Keyword("assertion-stack-levels") => AssertionStackLevelsInfoFlag()
case Tokens.Keyword("authors") => AuthorsInfoFlag()
case Tokens.Keyword("error-behavior") => ErrorBehaviorInfoFlag()
case Tokens.Keyword("name") => NameInfoFlag()
case Tokens.Keyword("reason-unknown") => ReasonUnknownInfoFlag()
case Tokens.Keyword("version") => VersionInfoFlag()
case Tokens.Keyword(keyword) => KeywordInfoFlag(keyword)
case t => expected(t, Tokens.KeywordKind)
}
flag.setPos(t)
}
def parseOption: SMTOption = {
val peekPosition = peekToken.getPos
val opt = peekToken match {
case Tokens.Keyword("diagnostic-output-channel") =>
nextToken()
DiagnosticOutputChannel(parseString.value)
case Tokens.Keyword("global-declarations") =>
nextToken()
GlobalDeclarations(parseBool)
case Tokens.Keyword("print-success") =>
nextToken()
PrintSuccess(parseBool)
case Tokens.Keyword("produce-assertions") =>
nextToken()
ProduceAssertions(parseBool)
case Tokens.Keyword("produce-assignments") =>
nextToken()
ProduceAssignments(parseBool)
case Tokens.Keyword("produce-models") =>
nextToken()
ProduceModels(parseBool)
case Tokens.Keyword("produce-proofs") =>
nextToken()
ProduceProofs(parseBool)
case Tokens.Keyword("produce-unsat-assumptions") =>
nextToken()
ProduceUnsatAssumptions(parseBool)
case Tokens.Keyword("produce-unsat-cores") =>
nextToken()
ProduceUnsatCores(parseBool)
case Tokens.Keyword("random-seed") =>
nextToken()
RandomSeed(parseNumeral.value.toInt)
case Tokens.Keyword("regular-output-channel") =>
nextToken()
RegularOutputChannel(parseString.value)
case Tokens.Keyword("reproducible-resource-limit") =>
nextToken()
ReproducibleResourceLimit(parseNumeral.value.toInt)
case Tokens.Keyword("verbosity") =>
nextToken()
Verbosity(parseNumeral.value.toInt)
case _ =>
AttributeOption(parseAttribute)
}
opt.setPos(peekPosition)
}
def parseBool: Boolean = {
nextToken() match {
case Tokens.SymbolLit("true") => true
case Tokens.SymbolLit("false") => false
case t => expected(t) //TODO: not sure how to tell we were expecting one of two specific symbols
}
}
}