This commit is contained in:
parent
9b51680af5
commit
c2b8db238e
24 changed files with 624 additions and 232 deletions
52
src/scanner/scanFile.go
Normal file
52
src/scanner/scanFile.go
Normal file
|
@ -0,0 +1,52 @@
|
|||
package scanner
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"git.urbach.dev/cli/q/src/errors"
|
||||
"git.urbach.dev/cli/q/src/fs"
|
||||
"git.urbach.dev/cli/q/src/token"
|
||||
)
|
||||
|
||||
// scanFile scans a single file.
|
||||
func (s *scanner) scanFile(path string, pkg string) error {
|
||||
contents, err := os.ReadFile(path)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
tokens := token.Tokenize(contents)
|
||||
|
||||
file := &fs.File{
|
||||
Path: path,
|
||||
Package: pkg,
|
||||
Bytes: contents,
|
||||
Tokens: tokens,
|
||||
}
|
||||
|
||||
s.files <- file
|
||||
|
||||
for i := 0; i < len(tokens); i++ {
|
||||
switch tokens[i].Kind {
|
||||
case token.NewLine:
|
||||
case token.Comment:
|
||||
case token.Identifier:
|
||||
i, err = s.scanFunction(file, tokens, i)
|
||||
case token.Import:
|
||||
i, err = s.scanImport(file, tokens, i)
|
||||
case token.EOF:
|
||||
return nil
|
||||
case token.Invalid:
|
||||
return errors.New(&errors.InvalidCharacter{Character: tokens[i].String(file.Bytes)}, file, tokens[i].Position)
|
||||
default:
|
||||
return errors.New(&errors.InvalidTopLevel{Instruction: tokens[i].String(file.Bytes)}, file, tokens[i].Position)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue