Modified const and extern to always use blocks

This commit is contained in:
2025-04-05 19:34:50 +02:00
parent ba34637dc1
commit 543558a02b
16 changed files with 164 additions and 90 deletions

View File

@ -9,14 +9,15 @@ import (
// scanConst scans a block of constants.
func (s *Scanner) scanConst(file *fs.File, tokens token.List, i int) (int, error) {
groupName := tokens[i].Text(file.Bytes)
i += 2
i++
if tokens[i].Kind != token.BlockStart {
return i, errors.New(errors.MissingBlockStart, file, tokens[i].Position)
}
i++
prefix := file.Package + "."
var stack []string
for i < len(tokens) {
switch tokens[i].Kind {
@ -24,13 +25,30 @@ func (s *Scanner) scanConst(file *fs.File, tokens token.List, i int) (int, error
name := tokens[i].Text(file.Bytes)
i++
s.constants <- &core.Constant{
Name: file.Package + "." + groupName + "." + name,
Token: tokens[i],
File: file,
switch tokens[i].Kind {
case token.Number:
s.constants <- &core.Constant{
Name: prefix + name,
Token: tokens[i],
File: file,
}
case token.BlockStart:
stack = append(stack, prefix)
prefix += name + "."
default:
return i, errors.New(errors.NotImplemented, file, tokens[i].Position)
}
case token.BlockEnd:
if len(stack) > 0 {
prefix = stack[len(stack)-1]
stack = stack[:len(stack)-1]
i++
continue
}
return i, nil
}

View File

@ -6,10 +6,38 @@ import (
"git.urbach.dev/cli/q/src/token"
)
// scanExtern scans a block of external function declarations.
// scanExtern scans a block of external libraries.
func (s *Scanner) scanExtern(file *fs.File, tokens token.List, i int) (int, error) {
i++
if tokens[i].Kind != token.BlockStart {
return i, errors.New(errors.MissingBlockStart, file, tokens[i].Position)
}
for i < len(tokens) {
switch tokens[i].Kind {
case token.Identifier:
var err error
i, err = s.scanExternLibrary(file, tokens, i)
if err != nil {
return i, err
}
case token.BlockEnd:
return i, nil
}
i++
}
return i, errors.New(errors.MissingBlockEnd, file, tokens[i].Position)
}
// scanExternLibrary scans a block of external function declarations.
func (s *Scanner) scanExternLibrary(file *fs.File, tokens token.List, i int) (int, error) {
dllName := tokens[i].Text(file.Bytes)
i += 2
i++
if tokens[i].Kind != token.BlockStart {
return i, errors.New(errors.MissingBlockStart, file, tokens[i].Position)

View File

@ -43,10 +43,6 @@ func (s *Scanner) scanFile(path string, pkg string) error {
i, err = s.scanFunction(file, tokens, i)
case token.BlockStart:
i, err = s.scanStruct(file, tokens, i)
case token.Extern:
i, err = s.scanExtern(file, tokens, i)
case token.Const:
i, err = s.scanConst(file, tokens, i)
case token.GroupEnd:
return errors.New(errors.MissingGroupStart, file, next.Position)
case token.BlockEnd:
@ -56,6 +52,10 @@ func (s *Scanner) scanFile(path string, pkg string) error {
default:
return errors.New(errors.InvalidDefinition, file, next.Position)
}
case token.Const:
i, err = s.scanConst(file, tokens, i)
case token.Extern:
i, err = s.scanExtern(file, tokens, i)
case token.Import:
i, err = s.scanImport(file, tokens, i)
case token.EOF: