-
Notifications
You must be signed in to change notification settings - Fork 3
/
interface.go
103 lines (92 loc) · 2.69 KB
/
interface.go
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
package parser
import (
"errors"
"fmt"
"path/filepath"
"runtime/debug"
"github.com/DDP-Projekt/Kompilierer/src/ast"
"github.com/DDP-Projekt/Kompilierer/src/ddperror"
"github.com/DDP-Projekt/Kompilierer/src/scanner"
"github.com/DDP-Projekt/Kompilierer/src/token"
)
// Options on where to get the source-tokens from
type Options struct {
// Optional Filename to name the source
// this file is read if Source and Tokens are nil
FileName string
// Optional ddp-source-code
// if nil, FileName is read
Source []byte
// Optional scanned token slice
// if nil, Source is used
Tokens []token.Token
// maps modules that have already been passed by their filenames
// the the parser may add new imported modules to this map
// so that after passing it contains *at least* all modules
// that the resulting module depends on
Modules map[string]*ast.Module
// ErrorHandler used during scanning and parsing
// May be nil
ErrorHandler ddperror.Handler
}
func (options *Options) ToScannerOptions(scannerMode scanner.Mode) scanner.Options {
return scanner.Options{
FileName: options.FileName,
Source: options.Source,
ScannerMode: scannerMode,
ErrorHandler: options.ErrorHandler,
}
}
func validateOptions(options *Options) error {
if options.Source == nil && options.Tokens == nil && options.FileName == "" {
return errors.New("Kein Quellcode gegeben")
}
if options.Modules == nil {
options.Modules = make(map[string]*ast.Module)
}
if options.ErrorHandler == nil {
options.ErrorHandler = ddperror.EmptyHandler
}
return nil
}
// parse the provided ddp-source-code from the given Options
// if an error occured the resulting Ast is nil
func Parse(options Options) (*ast.Module, error) {
defer panic_wrapper()
// validate the options
err := validateOptions(&options)
if err != nil {
return nil, fmt.Errorf("Ungültige Parser Optionen: %w", err)
}
if options.Tokens == nil {
options.Tokens, err = scanner.Scan(options.ToScannerOptions(scanner.ModeStrictCapitalization))
if err != nil {
return nil, fmt.Errorf("Fehler beim Scannen: %w", err)
}
}
module := newParser(options.FileName, options.Tokens, options.Modules, options.ErrorHandler).parse()
if options.FileName != "" {
path, err := filepath.Abs(options.FileName)
if err != nil {
return nil, err
}
module.FileName = path
}
return module, nil
}
// wraps a panic with more information and re-panics
func panic_wrapper() {
if err := recover(); err != nil {
if err, ok := err.(*ParserError); ok {
panic(err)
}
stack_trace := debug.Stack()
err, _ := err.(error)
panic(&ParserError{
Err: err,
Msg: "unknown parser panic",
ModulePath: "not found",
StackTrace: stack_trace,
})
}
}