-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlisteners.go
50 lines (38 loc) · 1.13 KB
/
listeners.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
package solgo
import (
"fmt"
"github.com/antlr4-go/antlr/v4"
)
// ListenerName represents the name of a listener.
type ListenerName string
// Predefined listener names.
const (
ListenerAbi ListenerName = "abi"
ListenerContractInfo ListenerName = "contract_info"
ListenerAst ListenerName = "ast"
ListenerSyntaxErrors ListenerName = "syntax_errors"
)
func (l ListenerName) String() string {
return string(l)
}
type listeners map[ListenerName]antlr.ParseTreeListener
func (s *Parser) RegisterListener(name ListenerName, listener antlr.ParseTreeListener) error {
if s.IsListenerRegistered(name) {
return fmt.Errorf("listener %s already registered", name)
}
s.listeners[name] = listener
return nil
}
func (s *Parser) GetAllListeners() map[ListenerName]antlr.ParseTreeListener {
return s.listeners
}
func (s *Parser) GetListener(name ListenerName) (antlr.ParseTreeListener, error) {
if !s.IsListenerRegistered(name) {
return nil, fmt.Errorf("listener %s not registered", name)
}
return s.listeners[name], nil
}
func (s *Parser) IsListenerRegistered(name ListenerName) bool {
_, ok := s.listeners[name]
return ok
}