-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror.go
More file actions
546 lines (457 loc) · 12.1 KB
/
error.go
File metadata and controls
546 lines (457 loc) · 12.1 KB
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
package gmnlisp
import (
"context"
"errors"
"fmt"
)
type ErrorNode struct {
Value error
}
var seriousConditionClass = registerClass(&BuiltInClass{
name: NewSymbol("<serious-condition>"),
instanceP: func(value Node) bool {
_, ok := value.(error)
return ok
},
create: func() Node {
return nil
},
super: []Class{ObjectClass},
})
var errorClass = registerClass(&BuiltInClass{
name: NewSymbol("<error>"),
instanceP: func(value Node) bool {
_, ok := value.(error)
return ok
},
create: func() Node {
return nil
},
super: []Class{ObjectClass, seriousConditionClass},
})
func (ErrorNode) ClassOf() Class {
return errorClass
}
func (e ErrorNode) String() string {
return e.Value.Error()
}
func (e ErrorNode) Equals(n Node, m EqlMode) bool {
f, ok := n.(ErrorNode)
if !ok {
return false
}
return errors.Is(e.Value, f.Value) || errors.Is(f.Value, e.Value)
}
func (e ErrorNode) Error() string {
return e.Value.Error()
}
func raiseError(ctx context.Context, w *World, e error) (Node, error) {
if _, ok := e.(interface{ ClassOf() Class }); ok {
return nil, e
}
condition := ErrorNode{Value: e}
return callHandler[Node](ctx, w, false, condition)
}
type ProgramError struct {
err error
}
type ControlError struct {
err error
}
type DomainError struct {
Object Node
ExpectedClass Class
Reason string
}
type ParseError struct {
str String
ExpectedClass Class
}
type StorageExhausted struct{}
type StreamError struct {
Stream Node
}
type EndOfStream struct {
Stream Node
}
var (
programErrorClass = registerNewAbstractClass[ProgramError]("<program-error>", errorClass)
domainErrorClass = registerNewAbstractClass[*DomainError]("<domain-error>", programErrorClass, errorClass)
controlErrorClass = registerNewAbstractClass[ControlError]("<control-error>", errorClass)
parseErrorClass = registerNewAbstractClass[*ParseError]("<parse-error>", errorClass)
storageExhaustedClass = registerNewAbstractClass[StorageExhausted]("<storage-exhausted>", errorClass)
streamErrorClass = registerNewAbstractClass[StreamError]("<stream-error>", errorClass)
endOfStreamClass = registerNewAbstractClass[EndOfStream]("<end-of-stream>", streamErrorClass, errorClass)
)
func (e ProgramError) ClassOf() Class {
return programErrorClass
}
func (e ProgramError) Equals(n Node, _ EqlMode) bool {
_n, ok := n.(ProgramError)
if !ok {
return false
}
return errors.Is(e.err, _n.err) || errors.Is(_n.err, e.err)
}
func (e ProgramError) String() string {
return e.err.Error()
}
func (e ProgramError) Error() string {
return e.err.Error()
}
func (e ProgramError) Unwrap() error {
return e.err
}
func (e ControlError) ClassOf() Class {
return controlErrorClass
}
func (e ControlError) Equals(n Node, _ EqlMode) bool {
_n, ok := n.(ControlError)
if !ok {
return false
}
return errors.Is(e.err, _n.err) || errors.Is(_n.err, e.err)
}
func (e ControlError) String() string {
return e.err.Error()
}
func (e ControlError) Error() string {
return e.err.Error()
}
func (e ControlError) Unwrap() error {
return e.err
}
func (e *DomainError) ClassOf() Class {
return domainErrorClass
}
func (e *DomainError) Equals(_other Node, mode EqlMode) bool {
other, ok := _other.(*DomainError)
if !ok {
return false
}
return e.Object.Equals(other.Object, mode) &&
e.ExpectedClass.Equals(other.ExpectedClass, mode)
}
func (e *DomainError) String() string {
if e.Reason != "" {
return fmt.Sprintf("Domain error: %#v: %s", e.Object, e.Reason)
}
return fmt.Sprintf("Domain error: %#v: not a %s", e.Object, e.ExpectedClass.Name().String())
}
func (e *DomainError) Error() string {
return e.String()
}
func funDomainErrorObject(ctx context.Context, w *World, arg Node) (Node, error) {
e, err := ExpectClass[*DomainError](ctx, w, arg)
if err != nil {
return nil, err
}
return e.Object, nil
}
func funDomainErrorExpectedClass(ctx context.Context, w *World, arg Node) (Node, error) {
e, err := ExpectClass[*DomainError](ctx, w, arg)
if err != nil {
return nil, err
}
return e.ExpectedClass, nil
}
func funRaiseDomainError(ctx context.Context, w *World, obj, class Node) (Node, error) {
if s, ok := class.(String); ok {
return callHandler[Node](ctx, w, false, &DomainError{
Object: obj,
Reason: s.String(),
})
}
c, err := ExpectInterface[Class](ctx, w, class, classClass)
if err != nil {
return nil, err
}
return callHandler[Node](ctx, w, false, &DomainError{
Object: obj,
ExpectedClass: c,
})
}
type Condition interface {
error
Node
}
type callHandlerLimitter struct{}
var errHandlerReturnNormally = errors.New("handler return normally")
func callHandler[T Node](ctx context.Context, w *World, cont bool, condition Condition) (T, error) {
var continuable Continuable
if cont && errors.As(condition, &continuable) {
continuable.SetContinuableString("continuable")
}
var zero T
if len(w.handler) > 0 {
if v := ctx.Value(callHandlerLimitter{}); v != nil {
return zero, condition
}
ctx = context.WithValue(ctx, callHandlerLimitter{}, 1)
for h := w.handler; len(h) > 0; h = h[:len(h)-1] {
_, e := h[len(h)-1].Call(ctx, w, quotes(condition))
var ce *_ErrContinueCondition
if cont && errors.As(e, &ce) {
if v, ok := ce.Value.(T); ok {
return v, nil
}
}
if isNonLocalExit(e) {
return zero, e
}
if e != nil {
return zero, condition
}
condition = ControlError{err: errHandlerReturnNormally}
}
return zero, ControlError{err: errHandlerReturnNormally}
}
return zero, condition
}
func ExpectInterface[T Node](ctx context.Context, w *World, v Node, class Class) (T, error) {
if _v, ok := v.(quoted); ok {
v = _v.Node
}
value, ok := v.(T)
if ok {
return value, nil
}
condition := &DomainError{
Object: v,
ExpectedClass: class,
}
return callHandler[T](ctx, w, true, condition)
}
func ExpectClass[T Node](ctx context.Context, w *World, v Node) (T, error) {
var zero T
class := zero.ClassOf() // .ClassOf must be called even when value is nil
return ExpectInterface[T](ctx, w, v, class)
}
func ExpectSymbol(ctx context.Context, w *World, v Node) (Symbol, error) {
return ExpectInterface[Symbol](ctx, w, v, symbolClass)
}
func ExpectList(ctx context.Context, w *World, arg Node) (Node, error) {
v := arg
for {
if IsNone(v) {
return arg, nil
}
cons, ok := v.(*Cons)
if !ok {
return callHandler[*Cons](ctx, w, true, &DomainError{
Object: v,
ExpectedClass: consClass,
})
}
v = cons.Cdr
}
}
func ExpectNonReservedSymbol(ctx context.Context, w *World, v Node) (_Symbol, error) {
symbol, ok := v.(_Symbol)
if ok {
return symbol, nil
}
if _, err := ExpectSymbol(ctx, w, v); err != nil {
return 0, err
}
_, err := raiseProgramError(ctx, w, errors.New("expect symbol"))
return 0, err
}
type _UndefinedEntity struct {
name Symbol
space Symbol
}
func (u *_UndefinedEntity) Error() string {
return fmt.Sprintf("undefined %s: %#v", u.space.String(), u.name.String())
}
func (u *_UndefinedEntity) String() string {
return u.Error()
}
func (u *_UndefinedEntity) Equals(other Node, mode EqlMode) bool {
o, ok := other.(*_UndefinedEntity)
if !ok {
return false
}
return u.space.Equals(o.space, mode) && u.name.Equals(o.name, mode)
}
var (
undefinedEntityClass = registerNewAbstractClass[*_UndefinedEntity]("<undefined-entity>", programErrorClass)
unboundVariableClass = registerNewAbstractClass[*_UndefinedEntity]("<unbound-variable>", undefinedEntityClass)
undefinedFunctionClass = registerNewAbstractClass[*_UndefinedEntity]("<undefined-function>", undefinedEntityClass)
)
func (u *_UndefinedEntity) ClassOf() Class {
if u == nil {
return undefinedEntityClass
}
if u.space.Equals(symVariable, STRICT) {
return unboundVariableClass
}
if u.space.Equals(symDynamicVariable, STRICT) {
return unboundVariableClass
}
return undefinedFunctionClass
}
func funUndefinedEntityName(ctx context.Context, w *World, arg Node) (Node, error) {
entity, err := ExpectClass[*_UndefinedEntity](ctx, w, arg)
if err != nil {
return nil, err
}
return entity.name, nil
}
func funUndefinedEntityNamespace(ctx context.Context, w *World, arg Node) (Node, error) {
entity, err := ExpectClass[*_UndefinedEntity](ctx, w, arg)
if err != nil {
return nil, err
}
return entity.space, nil
}
func funParseErrorString(ctx context.Context, w *World, arg Node) (Node, error) {
entity, err := ExpectClass[*ParseError](ctx, w, arg)
if err != nil {
return nil, err
}
return String(entity.str), nil
}
func funParseErrorExpectedClass(ctx context.Context, w *World, arg Node) (Node, error) {
entity, err := ExpectClass[*ParseError](ctx, w, arg)
if err != nil {
return nil, err
}
return entity.ExpectedClass, nil
}
func (p *ParseError) ClassOf() Class {
return parseErrorClass
}
func (p *ParseError) String() string {
return fmt.Sprintf("parse error: %s: %s", String(p.str), p.ExpectedClass.String())
}
func (p *ParseError) Equals(other Node, m EqlMode) bool {
o, ok := other.(*ParseError)
if !ok {
return false
}
return p.str == o.str && p.ExpectedClass.Equals(o.ExpectedClass, m)
}
func (p *ParseError) Error() string {
return p.String()
}
func (s StorageExhausted) ClassOf() Class {
return storageExhaustedClass
}
func (s StorageExhausted) Equals(n Node, _ EqlMode) bool {
_, ok := n.(StorageExhausted)
return ok
}
func (s StorageExhausted) String() string {
return "storage exhausted"
}
func (s StorageExhausted) Error() string {
return "storage exhausted"
}
func (s StreamError) ClassOf() Class {
return streamErrorClass
}
func (s StreamError) Equals(other Node, m EqlMode) bool {
o, ok := other.(StreamError)
if !ok {
return false
}
return s.Stream.Equals(o.Stream, m)
}
func (s StreamError) String() string {
return "<stream-error>"
}
func (s StreamError) Error() string {
return "<stream-error>"
}
func funStreamErrorStream(ctx context.Context, w *World, node Node) (Node, error) {
if endOfStream, ok := node.(EndOfStream); ok {
return endOfStream.Stream, nil
}
streamError, err := ExpectClass[StreamError](ctx, w, node)
if err != nil {
return nil, err
}
return streamError.Stream, nil
}
func (e EndOfStream) ClassOf() Class {
return endOfStreamClass
}
func (e EndOfStream) Equals(n Node, _ EqlMode) bool {
_, ok := n.(EndOfStream)
return ok
}
func (e EndOfStream) String() string {
return "<end-of-stream>"
}
func (e EndOfStream) Error() string {
return "<end-of-stream>"
}
type continuableImpl struct {
continuableString *String
}
func (s *continuableImpl) SetContinuableString(cs String) {
s.continuableString = &cs
}
func (s *continuableImpl) ContinuableString() Node {
if s.continuableString == nil {
return Null
}
return *s.continuableString
}
type SimpleError struct {
FormatString Node
FormatArguments Node
continuableImpl
}
var simpleErrorClass = registerNewAbstractClass[*SimpleError]("<simple-error>", errorClass)
func (s *SimpleError) ClassOf() Class {
return simpleErrorClass
}
func (s *SimpleError) String() string {
return fmt.Sprintf("simple error: Format=%#v, Arguments=%#v",
s.FormatString,
s.FormatArguments)
}
func (s *SimpleError) Equals(other Node, m EqlMode) bool {
o, ok := other.(*SimpleError)
if !ok {
return false
}
return s.FormatString.Equals(o.FormatString, m) &&
s.FormatArguments.Equals(o.FormatArguments, m)
}
func (s *SimpleError) Error() string {
return s.String()
}
func funSimpleErrorFormatString(ctx context.Context, w *World, s Node) (Node, error) {
e, err := ExpectClass[*SimpleError](ctx, w, s)
if err != nil {
return nil, err
}
return e.FormatString, nil
}
func funSimpleErrorFormatArguments(ctx context.Context, w *World, s Node) (Node, error) {
e, err := ExpectClass[*SimpleError](ctx, w, s)
if err != nil {
return nil, err
}
return e.FormatArguments, nil
}
func funMakeSimpleError(ctx context.Context, w *World, f, args Node) (Node, error) {
return &SimpleError{
FormatString: f,
FormatArguments: args,
}, nil
}
type invalidType struct{}
func (invalidType) Equals(other Node, m EqlMode) bool {
return false
}
func (invalidType) String() string {
return "(invalid)"
}
func (invalidType) ClassOf() Class {
return invalidClass
}
var invalidClass = registerNewAbstractClass[invalidType]("<invalid>")