Added a function scanner
This commit is contained in:
parent
21cf4b5c6d
commit
92b7c22c61
2 changed files with 38 additions and 4 deletions
|
@ -1,3 +1,7 @@
|
|||
package build
|
||||
|
||||
type Function struct{}
|
||||
import "git.akyoto.dev/cli/q/src/token"
|
||||
|
||||
type Function struct {
|
||||
Tokens token.List
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue