-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.go
More file actions
163 lines (145 loc) · 4.12 KB
/
Copy pathhelpers.go
File metadata and controls
163 lines (145 loc) · 4.12 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package parser
import (
"github.com/ProCode-Software/klar/internal/ast"
"github.com/ProCode-Software/klar/internal/klarerrs"
"github.com/ProCode-Software/klar/internal/lexer"
"github.com/ProCode-Software/klar/internal/ranges"
"github.com/ProCode-Software/klar/pkg/printer"
)
func newOperator(t lexer.Token) ast.Operator {
return ast.Operator{Kind: t.Kind, Position: t.Position}
}
// isEqual returns true if the current token is '='. If it is ':=', isEqual
// reports an error and still returns true.
func (p *Parser) isEqual(t lexer.Token) bool {
switch t.Kind {
case lexer.ColonEqual:
p.Error(klarerrs.Token(klarerrs.ErrColonEqual, t))
return true
case lexer.Equal:
return true
}
return false
}
func (p *Parser) ParseAssignable() ast.Assignable {
if p.CurrKind() == lexer.Underscore {
return rangeFromToken(&ast.Discard{}, p.Advance())
}
return p.validateAssignable(p.ParseExpression(ExpressionBindingPower))
}
func (p *Parser) lastTokEnd() lexer.Position {
last := p.Tokens[p.Index-1]
return last.End()
}
func (p *Parser) expectShorthand() (key *ast.Symbol, value ast.Expression) {
var ok, isComputed bool
sym := p.ParseExpression(ExpressionBindingPower)
// Allowed if the expression inside is an index or symbol
var old ast.Expression
var inner *ast.Expression
switch expr := sym.(type) {
case *ast.AsExpression:
old, inner = expr, &expr.Expression
sym = expr.Expression
case *ast.RestExpression:
old, inner = expr, &expr.Expression
sym = expr.Expression
}
switch sym := sym.(type) {
case *ast.Symbol:
key, value = sym, sym
ok = true
case *ast.IndexExpression:
if sym.Computed {
isComputed = true
break
}
if prop, ok2 := sym.Property.(*ast.Symbol); ok2 {
key, value = prop, sym
ok = true
}
// TODO: In the future, functions could be allowed?
// :value() for `value: value()`
// :info.name() for `name: info.name()`
}
if !ok {
err := klarerrs.Node(klarerrs.ErrInvalidLabelShorthand, sym)
err.Params = klarerrs.ErrorParams{"computed": isComputed}
p.Error(err)
return &ast.Symbol{}, &ast.BadExpression{}
}
if old != nil {
*inner = value
value = old
}
return key, value
}
// Range utils
func markEndPos[T ast.Node](p *Parser, node T) T {
node.SetPos(node.GetRange().Start, p.lastTokEnd())
return node
}
func markStartEndPos[T ast.Node](p *Parser, node T, start lexer.Position) T {
node.SetPos(start, p.lastTokEnd())
return node
}
func rangeFromToken[T ast.Node](node T, tok lexer.Token) T {
rang := ranges.FromToken(tok)
node.SetPos(rang.Start, rang.End)
return node
}
func copyPos[F, T ast.Node](from F, to T) T {
to.SetPos(from.GetRange().Start, from.GetRange().End)
return to
}
func newBaseNode(start, end lexer.Position) ast.BaseNode {
return ast.BaseNode{Range: ranges.Range{start, end}}
}
func isAssignment(kind lexer.TokenType) bool {
switch kind {
case lexer.Equal, lexer.ColonEqual, lexer.PlusEqual,
lexer.MinusEqual, lexer.AsteriskEqual, lexer.SlashEqual,
lexer.PercentEqual, lexer.CaretEqual:
return true
}
return false
}
func printTokens(tok []lexer.Token) []byte {
return printer.PrintTokens(tok, printer.PrettyPrint|printer.SingleLine)
}
func isComment(t lexer.TokenType) bool {
switch t {
case lexer.BlockComment, lexer.LineComment, lexer.Hashbang:
return true
}
return false
}
// Reports [klarerrs.ErrCurlyQuote] if tok is a Unicode curly quote (single or double)
func (p *Parser) checkCurlyQuote(tok lexer.Token) bool {
var alt string
switch tok.Source {
case "“", "”":
alt = `"`
case "‘", "’":
alt = "'"
default:
return false
}
err := klarerrs.Token(klarerrs.ErrCurlyQuote, tok)
err.SetParam("alt", alt)
err.Hint("This may have been caused by smart quoting by a mobile keyboard or word processor, which automatically types curly quotation marks instead of straight ones. In Klar, strings are delimited by straight quotes.")
p.Error(err)
return true
}
func (p *Parser) checkIllegal(tok lexer.Token) bool {
if p.checkCurlyQuote(tok) {
return true
}
if tok.Attributes != nil {
if _, ok := tok.Attributes["invalidCharacter"]; ok {
p.Error(klarerrs.Token(klarerrs.ErrInvalidCharacter, tok))
return true
}
}
return false
}