-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror.go
More file actions
147 lines (138 loc) · 3.55 KB
/
Copy patherror.go
File metadata and controls
147 lines (138 loc) · 3.55 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
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
package parser
import (
"github.com/ProCode-Software/klar/internal/klarerrs"
"github.com/ProCode-Software/klar/internal/lexer"
"github.com/ProCode-Software/klar/internal/ranges"
)
func (p *Parser) unexpectedTokenError() {
tok := p.AdvanceNonBoundary()
if tok.Kind == lexer.Newline {
tok = p.Advance()
}
p.Error(klarerrs.UnexpectedToken(tok))
p.skipUntilBoundary()
}
func (p *Parser) skipRestOfStatement() (end lexer.Position) {
var brackCount int
for p.HasTokens() {
tok := p.Advance()
switch tok.Kind {
case lexer.Newline:
if brackCount == 0 {
return tok.End()
}
case lexer.LeftParenthesis, lexer.LeftBracket, lexer.LeftCurlyBrace, lexer.HashLeftCurlyBrace:
brackCount++
case lexer.RightParenthesis, lexer.RightBracket, lexer.RightCurlyBrace:
brackCount--
}
}
return
}
func (p *Parser) nudError() {
switch curr := p.Curr(); curr.Kind {
case lexer.Illegal:
if p.checkIllegal(curr) {
break
}
fallthrough
default:
p.unexpectedTokenError()
return
case lexer.If:
p.ErrorLabelled(klarerrs.Token(klarerrs.ErrIfStatement, curr), "Use a 'when' block instead")
case lexer.Plus:
p.ErrorLabelled(
klarerrs.Token(klarerrs.ErrPositiveSign, curr),
"A leading '+' sign doesn't change a number",
)
case lexer.NotNot:
count := p.countConsecutiveNot()
err := klarerrs.Range(
klarerrs.ErrDoubleNot,
ranges.Offset(curr.Position, 0, uint32(count)),
)
err.SetParam("count", count)
p.Error(err)
}
p.skipUntilBoundary()
}
func (p *Parser) ledError() {
tok := p.Curr()
if tok.Kind == lexer.Not {
err := klarerrs.UnexpectedToken(tok)
err.Hint("Did you mean '!!' for an assertion?")
p.Error(err)
p.Advance()
return
}
p.unexpectedTokenError()
}
// countConsecutiveNot counts the number of consecutive `!` tokens.
func (p *Parser) countConsecutiveNot() (n int) {
for p.HasTokens() {
switch p.Curr().Kind {
case lexer.Not:
n++
case lexer.NotNot:
n += 2
default:
return
}
p.Advance()
}
return
}
func (p *Parser) skipUntilBoundary() {
var brackCount int
for p.HasTokens() {
switch p.CurrKind() {
case lexer.Comma, lexer.Newline:
if brackCount <= 0 {
return
}
case lexer.LeftParenthesis, lexer.LeftBracket, lexer.LeftCurlyBrace, lexer.HashLeftCurlyBrace:
brackCount++
case lexer.RightParenthesis, lexer.RightBracket, lexer.RightCurlyBrace:
brackCount--
if brackCount < 0 {
return
}
}
p.Advance()
}
}
// mismatchedLabelFormatError formats an error for a mismatch between type-only
// and labels-and-types parameters.
func mismatchedLabelFormatError(err *klarerrs.Error,
prevIsTypeOnly bool, prevRange ranges.Range,
) {
var msg string
if prevIsTypeOnly {
err.Label = "This parameter shouldn't have a label"
msg = "This parameter already only has a type"
} else {
err.Label = "This parameter should have a label"
msg = "This parameter already has a label"
}
err.Highlights = append(err.Highlights, klarerrs.Highlight{
Range: prevRange,
Message: msg,
})
}
// missingParamTypeAnnotError formats an error for a missing type annotation for
// a parameter or tuple item. missingParamTypeAnnotError sets err.Label.
func (p *Parser) missingParamTypeAnnotError(err *klarerrs.Error,
kind string, labelCount int, lastParamRange ranges.Range,
) {
err.SetParam("length", labelCount)
if labelCount > 1 {
err.Label = "These " + kind + "s need type annotations"
} else {
err.Label = "This " + kind + " needs a type annotation"
}
err.Highlights = append(err.Highlights, klarerrs.Highlight{
Range: lastParamRange,
Message: "This " + kind + " already has a type",
})
}