This commit is contained in:
parent
320e023d74
commit
ca91cd693c
18 changed files with 64 additions and 61 deletions
|
@ -15,12 +15,14 @@ var errs = []struct {
|
|||
ExpectedError error
|
||||
}{
|
||||
{"ExpectedFunctionDefinition.q", scanner.ExpectedFunctionDefinition},
|
||||
{"ExpectedFunctionDefinition2.q", scanner.ExpectedFunctionDefinition},
|
||||
{"ExpectedPackageName.q", scanner.ExpectedPackageName},
|
||||
{"InvalidCharacter.q", &scanner.InvalidCharacter{Character: "@"}},
|
||||
{"InvalidCharacter2.q", &scanner.InvalidCharacter{Character: "@"}},
|
||||
{"InvalidCharacter3.q", &scanner.InvalidCharacter{Character: "@"}},
|
||||
{"InvalidCharacter4.q", &scanner.InvalidCharacter{Character: "+++"}},
|
||||
{"InvalidFunctionDefinition.q", scanner.InvalidFunctionDefinition},
|
||||
{"InvalidFunctionDefinition2.q", scanner.InvalidFunctionDefinition},
|
||||
{"InvalidTopLevel.q", &scanner.InvalidTopLevel{Instruction: "123"}},
|
||||
{"InvalidTopLevel2.q", &scanner.InvalidTopLevel{Instruction: "\"Hello\""}},
|
||||
{"InvalidTopLevel3.q", &scanner.InvalidTopLevel{Instruction: "+"}},
|
||||
|
@ -28,6 +30,7 @@ var errs = []struct {
|
|||
{"MissingBlockEnd2.q", scanner.MissingBlockEnd},
|
||||
{"MissingBlockStart.q", scanner.MissingBlockStart},
|
||||
{"MissingGroupEnd.q", scanner.MissingGroupEnd},
|
||||
{"MissingGroupEnd2.q", scanner.MissingGroupEnd},
|
||||
{"MissingGroupStart.q", scanner.MissingGroupStart},
|
||||
{"MissingParameter.q", scanner.MissingParameter},
|
||||
{"MissingParameter2.q", scanner.MissingParameter},
|
||||
|
|
|
@ -56,11 +56,7 @@ func (s *scanner) scanFunction(file *fs.File, tokens token.List, i int) (int, er
|
|||
return i, errors.New(MissingBlockEnd, file, tokens[i].Position)
|
||||
}
|
||||
|
||||
if bodyStart == -1 {
|
||||
return i, errors.New(ExpectedFunctionDefinition, file, tokens[i].Position)
|
||||
}
|
||||
|
||||
return i, nil
|
||||
return i, errors.New(ExpectedFunctionDefinition, file, tokens[i].Position)
|
||||
}
|
||||
|
||||
if blockLevel > 0 {
|
||||
|
|
|
@ -12,10 +12,10 @@ func scanSignature(file *fs.File, tokens token.List, i int, delimiter token.Kind
|
|||
var (
|
||||
groupLevel = 0
|
||||
nameStart = i
|
||||
paramsStart = -1
|
||||
paramsEnd = -1
|
||||
typeStart = -1
|
||||
typeEnd = -1
|
||||
inputStart = -1
|
||||
inputEnd = -1
|
||||
outputStart = -1
|
||||
outputEnd = -1
|
||||
)
|
||||
|
||||
i++
|
||||
|
@ -27,7 +27,7 @@ func scanSignature(file *fs.File, tokens token.List, i int, delimiter token.Kind
|
|||
i++
|
||||
|
||||
if groupLevel == 1 {
|
||||
paramsStart = i
|
||||
inputStart = i
|
||||
}
|
||||
|
||||
continue
|
||||
|
@ -41,7 +41,7 @@ func scanSignature(file *fs.File, tokens token.List, i int, delimiter token.Kind
|
|||
}
|
||||
|
||||
if groupLevel == 0 {
|
||||
paramsEnd = i
|
||||
inputEnd = i
|
||||
i++
|
||||
break
|
||||
}
|
||||
|
@ -59,10 +59,6 @@ func scanSignature(file *fs.File, tokens token.List, i int, delimiter token.Kind
|
|||
return nil, i, errors.New(MissingGroupEnd, file, tokens[i].Position)
|
||||
}
|
||||
|
||||
if paramsStart == -1 {
|
||||
return nil, i, errors.New(InvalidFunctionDefinition, file, tokens[i].Position)
|
||||
}
|
||||
|
||||
return nil, i, errors.New(InvalidFunctionDefinition, file, tokens[i].Position)
|
||||
}
|
||||
|
||||
|
@ -76,18 +72,18 @@ func scanSignature(file *fs.File, tokens token.List, i int, delimiter token.Kind
|
|||
|
||||
// Return type
|
||||
if i < len(tokens) && tokens[i].Kind == token.ReturnType {
|
||||
typeStart = i + 1
|
||||
outputStart = i + 1
|
||||
|
||||
for i < len(tokens) && tokens[i].Kind != delimiter {
|
||||
i++
|
||||
}
|
||||
|
||||
typeEnd = i
|
||||
outputEnd = i
|
||||
}
|
||||
|
||||
name := tokens[nameStart].String(file.Bytes)
|
||||
function := core.NewFunction(name, file)
|
||||
parameters := tokens[paramsStart:paramsEnd]
|
||||
parameters := tokens[inputStart:inputEnd]
|
||||
|
||||
for param := range parameters.Split {
|
||||
if len(param) == 0 {
|
||||
|
@ -104,40 +100,45 @@ func scanSignature(file *fs.File, tokens token.List, i int, delimiter token.Kind
|
|||
})
|
||||
}
|
||||
|
||||
if typeStart != -1 {
|
||||
if tokens[typeStart].Kind == token.GroupStart && tokens[typeEnd-1].Kind == token.GroupEnd {
|
||||
typeStart++
|
||||
typeEnd--
|
||||
}
|
||||
if outputStart == -1 {
|
||||
return function, i, nil
|
||||
}
|
||||
|
||||
outputTokens := tokens[typeStart:typeEnd]
|
||||
|
||||
if len(outputTokens) == 0 {
|
||||
return nil, i, errors.New(MissingParameter, file, tokens[typeStart].Position)
|
||||
}
|
||||
|
||||
errorPos := token.Position(typeStart)
|
||||
|
||||
for param := range outputTokens.Split {
|
||||
if len(param) == 0 {
|
||||
return nil, i, errors.New(MissingParameter, file, errorPos)
|
||||
}
|
||||
|
||||
if len(param) == 1 {
|
||||
function.Output = append(function.Output, &core.Parameter{
|
||||
Name: "",
|
||||
TypeTokens: param,
|
||||
})
|
||||
} else {
|
||||
function.Output = append(function.Output, &core.Parameter{
|
||||
Name: param[0].String(file.Bytes),
|
||||
TypeTokens: param[1:],
|
||||
})
|
||||
}
|
||||
|
||||
errorPos = param[len(param)-1].End() + 1
|
||||
if tokens[outputStart].Kind == token.GroupStart {
|
||||
if tokens[outputEnd-1].Kind == token.GroupEnd {
|
||||
outputStart++
|
||||
outputEnd--
|
||||
} else {
|
||||
return nil, i, errors.New(MissingGroupEnd, file, tokens[outputEnd-1].Position)
|
||||
}
|
||||
}
|
||||
|
||||
outputTokens := tokens[outputStart:outputEnd]
|
||||
|
||||
if len(outputTokens) == 0 {
|
||||
return nil, i, errors.New(MissingParameter, file, tokens[outputStart].Position)
|
||||
}
|
||||
|
||||
errorPos := token.Position(outputStart)
|
||||
|
||||
for param := range outputTokens.Split {
|
||||
if len(param) == 0 {
|
||||
return nil, i, errors.New(MissingParameter, file, errorPos)
|
||||
}
|
||||
|
||||
if len(param) == 1 {
|
||||
function.Output = append(function.Output, &core.Parameter{
|
||||
Name: "",
|
||||
TypeTokens: param,
|
||||
})
|
||||
} else {
|
||||
function.Output = append(function.Output, &core.Parameter{
|
||||
Name: param[0].String(file.Bytes),
|
||||
TypeTokens: param[1:],
|
||||
})
|
||||
}
|
||||
|
||||
errorPos = param[len(param)-1].End() + 1
|
||||
}
|
||||
return function, i, nil
|
||||
}
|
|
@ -1 +1 @@
|
|||
main()
|
||||
f()
|
1
src/scanner/testdata/errors/ExpectedFunctionDefinition2.q
vendored
Normal file
1
src/scanner/testdata/errors/ExpectedFunctionDefinition2.q
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
f() 123
|
|
@ -1 +1 @@
|
|||
main@
|
||||
f@
|
|
@ -1,4 +1,4 @@
|
|||
main() {
|
||||
f() {
|
||||
x := 123 +++ 456
|
||||
syscall(60, x)
|
||||
}
|
|
@ -1,3 +1,3 @@
|
|||
main() {
|
||||
f() {
|
||||
if 42 {}
|
||||
}
|
|
@ -1,3 +1,3 @@
|
|||
main() {
|
||||
f() {
|
||||
syscall(+, -, *, /)
|
||||
}
|
|
@ -1 +1 @@
|
|||
main
|
||||
f
|
1
src/scanner/testdata/errors/InvalidFunctionDefinition2.q
vendored
Normal file
1
src/scanner/testdata/errors/InvalidFunctionDefinition2.q
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
f 123
|
|
@ -1 +1 @@
|
|||
main(){
|
||||
f(){
|
|
@ -1,3 +1,3 @@
|
|||
main() {
|
||||
f() {
|
||||
loop {
|
||||
}
|
|
@ -1 +1 @@
|
|||
main()}
|
||||
f()}
|
|
@ -1 +1 @@
|
|||
main(
|
||||
f(
|
1
src/scanner/testdata/errors/MissingGroupEnd2.q
vendored
Normal file
1
src/scanner/testdata/errors/MissingGroupEnd2.q
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
f() -> (
|
|
@ -1 +1 @@
|
|||
main)
|
||||
f)
|
|
@ -1 +1 @@
|
|||
f(,) {}
|
||||
f(a (int)->(int),) {}
|
Loading…
Add table
Add a link
Reference in a new issue