-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsolparser.go
86 lines (77 loc) · 1.85 KB
/
solparser.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
package solparser
import (
"io"
"github.com/uji/solparser/ast"
"github.com/uji/solparser/lexer"
"github.com/uji/solparser/token"
)
// Parser parses "Solidity" code and outputs ASTs.
type Parser struct {
input io.Reader
lexer *lexer.Lexer
}
func New(input io.Reader) *Parser {
return &Parser{
input: input,
lexer: lexer.New(input),
}
}
func (p *Parser) Parse() (*ast.SourceUnit, error) {
var pragmaDirective *ast.PragmaDirective
var importDirective *ast.ImportDirective
var contractDefinition *ast.ContractDefinition
var functionDefinition *ast.FunctionDefinition
for {
tkn, err := p.lexer.Peek()
if err != nil {
return nil, err
}
switch tkn.Type {
case token.Pragma:
prgm, err := p.ParsePragmaDirective()
if err != nil {
return nil, err
}
pragmaDirective = prgm
case token.Import:
imp, err := p.ParseImportDirective()
if err != nil {
return nil, err
}
importDirective = imp
case token.Abstract, token.Contract:
cntrct, err := p.ParseContractDefinition()
if err != nil {
return nil, err
}
contractDefinition = cntrct
case token.Function:
fnc, err := p.ParseFunctionDefinition()
if err != nil {
return nil, err
}
functionDefinition = fnc
case token.EOS:
return &ast.SourceUnit{
PragmaDirective: pragmaDirective,
ImportDirective: importDirective,
ContractDefinition: contractDefinition,
FunctionDefinition: functionDefinition,
}, nil
default:
return nil, token.NewPosError(tkn.Position, "invalid")
}
}
}
func (p *Parser) ParseBooleanLiteral() (*ast.BooleanLiteral, error) {
tkn, err := p.lexer.Scan()
if err != nil {
return nil, err
}
if tkn.Type != token.TrueLiteral && tkn.Type != token.FalseLiteral {
return nil, token.NewPosError(tkn.Position, "not found keyword true or false")
}
return &ast.BooleanLiteral{
Token: tkn,
}, nil
}