-
Notifications
You must be signed in to change notification settings - Fork 1
/
importDirective.go
169 lines (148 loc) · 3.58 KB
/
importDirective.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
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
package solparser
import (
"github.com/uji/solparser/ast"
"github.com/uji/solparser/token"
)
func (p *Parser) ParsePath() (ast.Path, error) {
lit, err := p.lexer.Peek()
if err != nil {
return ast.Path{}, err
}
if lit.Type != token.NonEmptyStringLiteral {
return ast.Path{}, token.NewPosError(lit.Position, "not found non-empty-string-literal.")
}
if _, err := p.lexer.Scan(); err != nil {
return ast.Path{}, err
}
return ast.Path(lit), nil
}
func (p *Parser) ParseImportDirectivePathElement() (*ast.ImportDirectivePathElement, error) {
path, err := p.ParsePath()
if err != nil {
return nil, err
}
as, err := p.lexer.Peek()
if err != nil {
return nil, err
}
if as.Type != token.As {
return &ast.ImportDirectivePathElement{Path: path}, nil
}
if _, err := p.lexer.Scan(); err != nil {
return nil, err
}
id, err := p.ParseIdentifier()
if err != nil {
return nil, err
}
return &ast.ImportDirectivePathElement{
Path: path,
As: &as.Position,
Identifier: &id,
}, nil
}
func (p *Parser) ParseImportDirectiveSymbolAliasesElement() (*ast.ImportDirectiveSymbolAliasesElement, error) {
aliases, err := p.ParseSymbolAliases()
if err != nil {
return nil, err
}
from, err := p.lexer.Scan()
if err != nil {
return nil, err
}
if from.Type != token.From {
return nil, token.NewPosError(from.Position, "not found from keyword.")
}
path, err := p.ParsePath()
if err != nil {
return nil, err
}
return &ast.ImportDirectiveSymbolAliasesElement{
SymbolAliases: aliases,
From: from.Position,
Path: path,
}, nil
}
func (p *Parser) ParseImportDirectiveMulElement() (*ast.ImportDirectiveMulElement, error) {
mul, err := p.lexer.Peek()
if err != nil {
return nil, err
}
if mul.Type != token.Mul {
return nil, token.NewPosError(mul.Position, "not found mul keyword.")
}
if _, err := p.lexer.Scan(); err != nil {
return nil, err
}
as, err := p.lexer.Scan()
if err != nil {
return nil, err
}
if as.Type != token.As {
return nil, token.NewPosError(as.Position, "not found as keyword.")
}
id, err := p.ParseIdentifier()
if err != nil {
return nil, err
}
from, err := p.lexer.Scan()
if err != nil {
return nil, err
}
if from.Type != token.From {
return nil, token.NewPosError(from.Position, "not found from keyword.")
}
path, err := p.ParsePath()
if err != nil {
return nil, err
}
return &ast.ImportDirectiveMulElement{
Mul: mul.Position,
As: as.Position,
Identifier: id,
From: from.Position,
Path: path,
}, nil
}
func (p *Parser) ParseImportDirective() (*ast.ImportDirective, error) {
impt, err := p.lexer.Peek()
if err != nil {
return nil, err
}
if impt.Type != token.Import {
return nil, token.NewPosError(impt.Position, "not found import keyword.")
}
if _, err := p.lexer.Scan(); err != nil {
return nil, err
}
tkn, err := p.lexer.Peek()
if err != nil {
return nil, err
}
var el ast.ImportDirectiveElement
switch tkn.Type {
case token.NonEmptyStringLiteral:
el, err = p.ParseImportDirectivePathElement()
case token.LBrace:
el, err = p.ParseImportDirectiveSymbolAliasesElement()
case token.Mul:
el, err = p.ParseImportDirectiveMulElement()
default:
return nil, token.NewPosError(tkn.Position, "not found import-directive element.")
}
if err != nil {
return nil, err
}
semi, err := p.lexer.Scan()
if err != nil {
return nil, err
}
if semi.Type != token.Semicolon {
return nil, token.NewPosError(semi.Position, "not found semicolon.")
}
return &ast.ImportDirective{
Import: impt.Position,
Element: el,
Semicolon: semi.Position,
}, nil
}