Added a function scanner

This commit is contained in:
Eduard Urbach 2023-10-31 12:17:03 +01:00
parent ac157e580c
commit b6be9357bf
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
2 changed files with 38 additions and 4 deletions

View File

@ -1,3 +1,7 @@
package build
type Function struct{}
import "git.akyoto.dev/cli/q/src/token"
type Function struct {
Tokens token.List
}

View File

@ -7,7 +7,6 @@ import (
"sync"
"git.akyoto.dev/cli/q/src/directory"
"git.akyoto.dev/cli/q/src/log"
"git.akyoto.dev/cli/q/src/token"
)
@ -60,8 +59,39 @@ func scanFile(path string, functions chan<- *Function) error {
tokens := token.Tokenize(contents)
for _, t := range tokens {
log.Info.Println(t.Kind, t.Position, strings.TrimSpace(t.String()))
var (
groupLevel = 0
blockLevel = 0
functionStart = -1
)
for i, t := range tokens {
switch t.Kind {
case token.Identifier:
if blockLevel == 0 && groupLevel == 0 {
functionStart = i
}
case token.GroupStart:
groupLevel++
case token.GroupEnd:
groupLevel--
case token.BlockStart:
blockLevel++
case token.BlockEnd:
blockLevel--
if blockLevel == 0 {
function := &Function{
Tokens: tokens[functionStart : i+1],
}
functions <- function
}
}
}
return nil