This commit is contained in:
silva guimaraes 2025-08-23 20:02:18 -03:00
parent 0cd6040a1f
commit c1f75bb980
6 changed files with 140 additions and 67 deletions

View file

@ -42,12 +42,12 @@ func (in *Input) Seek(i int) {
in.index = i
}
func (in *Input) Take(amount int) *Input {
func (in *Input) Take(offset int) *Input {
var out = &Input{
index: 0,
input: in.input[in.index : in.index+amount],
input: in.input[in.index : in.index+offset],
}
in.index += amount
in.index += offset
return out
}
@ -70,3 +70,27 @@ func (in *Input) Find(open, close *Token) (int, bool) {
return 0, false
}
}
func (in *Input) TakeParens(open, close *Token) (*Input, error) {
if in.input[in.index].Equals(open) {
return nil, noMatch
}
start := in.index
level := 1
idx := slices.IndexFunc(in.input[start+1:], func(a Lexeme) bool {
switch {
case a.Equals(open):
level++
case a.Equals(close):
level--
return level == 0
}
return false
})
if idx > -1 {
_ = in.Take(idx + 1)
return &Input{index: 0, input: in.input[start+1 : idx]}, nil
} else {
return nil, fmt.Errorf("unmatched parenthesis starting at %s", in.Peek(start).Position())
}
}