-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcontextual_parser.go
More file actions
32 lines (28 loc) · 1.21 KB
/
contextual_parser.go
File metadata and controls
32 lines (28 loc) · 1.21 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
package syntaxerrors
import (
"github.com/antlr4-go/antlr/v4"
"github.com/unpackdev/solgo/parser"
)
// ContextualParser is a wrapper around the SolidityParser that maintains a stack of contexts.
// It provides methods for parsing function and contract definitions, and adds the current context to the SyntaxErrorListener.
type ContextualParser struct {
*parser.SolidityParser
SyntaxErrorListener *SyntaxErrorListener
}
// NewContextualParser creates a new ContextualSolidityParser.
// It takes a token stream and a SyntaxErrorListener, and returns a pointer to a ContextualSolidityParser.
func NewContextualParser(tokens antlr.TokenStream, listener *SyntaxErrorListener) *ContextualParser {
parser := parser.NewSolidityParser(tokens)
parser.RemoveErrorListeners()
parser.AddErrorListener(listener)
return &ContextualParser{
SolidityParser: parser,
SyntaxErrorListener: listener,
}
}
// SourceUnit parses a function definition and adds the "SourceUnit" context to the SyntaxErrorListener.
func (p *ContextualParser) SourceUnit() parser.ISourceUnitContext {
p.SyntaxErrorListener.PushContext("SourceUnit")
defer p.SyntaxErrorListener.PopContext()
return p.SolidityParser.SourceUnit() // Call the original method
}