z_mirror/luapls

git clone https://git.raiguard.me/z_mirror/luapls

Caleb HeuerImplement Punctuated abstraction over listse927fe5

main
1.1 KiB61 linesraw
1package ast
2
3import "github.com/raiguard/luapls/lua/token"
4
5type Punctuated[T Node] struct {
6	Pairs    []Pair[T]
7	StartPos token.Pos // If there are zero pairs then we need an alternative way to get the position.
8}
9
10func SimplePunctuated[T Node](node T) Punctuated[T] {
11	return Punctuated[T]{
12		Pairs:    []Pair[T]{{node, nil}},
13		StartPos: node.Pos(),
14	}
15}
16
17func (p *Punctuated[T]) Pos() token.Pos {
18	if len(p.Pairs) == 0 {
19		return p.StartPos
20	}
21	return p.Pairs[0].Pos()
22}
23
24func (p *Punctuated[T]) End() token.Pos {
25	if len(p.Pairs) == 0 {
26		return p.StartPos
27	}
28	return p.Pairs[len(p.Pairs)-1].End()
29}
30
31func (p *Punctuated[T]) String() string {
32	out := ""
33	for _, pair := range p.Pairs {
34		out += pair.String()
35	}
36	return out
37}
38
39type Pair[T Node] struct {
40	Node      T
41	Delimeter *Unit // Optional
42}
43
44func (p *Pair[T]) Pos() token.Pos {
45	return p.Node.Pos()
46}
47
48func (p *Pair[T]) End() token.Pos {
49	if p.Delimeter != nil {
50		return p.Delimeter.End()
51	}
52	return p.Node.End()
53}
54
55func (p *Pair[T]) String() string {
56	out := p.Node.String()
57	if p.Delimeter != nil {
58		out += p.Delimeter.String()
59	}
60	return out
61}