Created testing framework for parser

This commit is contained in:
Sasha Koshka 2023-09-18 01:55:38 -04:00
parent ecd222ef42
commit 8972d5ad8e
6 changed files with 60 additions and 21 deletions

View File

@ -133,10 +133,7 @@ func (this *BitCast) String () string {
// be assigned to interfaces. An operation is never a valid location expression.
type Operation struct {
Pos lexer.Position
Operator string `parser:" '[' @('+' | '++' | '-' | '--' | '*' | '/' |
'%' | '!!' | '||' | '&&' | '^^' | '!' |
'|' | '&' | '^' | '<<' | '>>' | '<' |
'>' | '<=' | '>=' | '=') "`
Operator string `parser:" '[' @('+' | '++' | '-' | '--' | '*' | '/' | '%' | '!!' | '||' | '&&' | '^^' | '!' | '|' | '&' | '^' | '<<' | '>>' | '<' | '>' | '<=' | '>=' | '=') "`
Arguments []Expression `parser:" @@+ ']' "`
}
func (*Operation) expression(){}
@ -174,8 +171,8 @@ func (this *Block) String () string {
// struct being accessed is.
type MemberAccess struct {
Pos lexer.Position
Source Expression `parser:" @@ "`
Member string `parser:" '.' @@ "`
Source Expression `parser:" @@ "`
Member string `parser:" '.' @Ident "`
}
func (*MemberAccess) expression(){}
func (this *MemberAccess) String () string {
@ -187,8 +184,8 @@ func (this *MemberAccess) String () string {
// method access is never a valid location expression.
type MethodAccess struct {
Pos lexer.Position
Source Expression `parser:" @@ "`
Member string `parser:" '::' @@ "`
Source Expression `parser:" @@ "`
Member string `parser:" '::' @Ident "`
}
func (*MethodAccess) expression(){}
func (this *MethodAccess) String () string {
@ -235,7 +232,7 @@ func (this *Loop) String () string {
// to anything. It is never a valid location expression.
type Break struct {
Pos lexer.Position
Value Expression `parser:" '[' 'break' @@? " ']'`
Value Expression `parser:" '[' 'break' @@? ']' "`
}
func (*Break) expression(){}
func (this *Break) String () string {
@ -248,7 +245,7 @@ func (this *Break) String () string {
// to anything. A return statement is never a valid location expression.
type Return struct {
Pos lexer.Position
Value Expression `parser:" '[' 'return' @@? " ']'`
Value Expression `parser:" '[' 'return' @@? ']' "`
}
func (*Return) expression(){}
func (this *Return) String () string {

9
parser/parser_test.go Normal file
View File

@ -0,0 +1,9 @@
package parser
import "testing"
func TestType (test *testing.T) {
testString (test,
`Hello:Int`,
`Hello: Int`)
}

32
parser/test-common.go Normal file
View File

@ -0,0 +1,32 @@
package parser
import "io"
import "fmt"
import "testing"
import "strings"
func testString (test *testing.T, correct, input string) {
testReader(test, correct, strings.NewReader(input))
}
func testReader (test *testing.T, correct string, inputs ...io.Reader) {
tree := Tree { }
for index, stream := range inputs {
err := tree.Parse(fmt.Sprintf("stream%d.fspl", index), stream)
if err != nil {
test.Error("parser returned error: ", err)
return
}
}
got := tree.String()
if got != correct {
test.Log("strings do not match")
test.Log("tree.String():")
test.Log(got)
test.Log("correct:")
test.Log(correct)
test.Fail()
return
}
}

View File

@ -12,9 +12,9 @@ type TopLevel interface {
type Typedef struct {
// Syntax
Pos lexer.Position
Public bool `parser: " @'+'? "`
Name string `parser: " @Ident "`
Type Type `parser: " ':' @@ "`
Public bool `parser:" @'+'? "`
Name string `parser:" @Ident "`
Type Type `parser:" ':' @@ "`
// Semantics
Methods map[string] Method
@ -38,9 +38,9 @@ func (this *Typedef) String () string {
type Function struct {
// Syntax
Pos lexer.Position
Public bool `parser: " @'+'? "`
Signature `parser: " @@ "`
Body Expression `parser: " ( '=' @@ )? "`
Public bool `parser:" @'+'? "`
Signature `parser:" @@ "`
Body Expression `parser:" ( '=' @@ )? "`
}
func (*Function) topLevel(){}
func (this *Function) String () string {
@ -58,10 +58,10 @@ func (this *Function) String () string {
type Method struct {
// Syntax
Pos lexer.Position
Public bool `parser: " @'+'? "`
TypeName string `parser: " @Ident "`
Signature `parser: " '.' @@ "`
Body Expression `parser: " ( '=' @@ )? "`
Public bool `parser:" @'+'? "`
TypeName string `parser:" @Ident "`
Signature `parser:" '.' @@ "`
Body Expression `parser:" ( '=' @@ )? "`
}
func (*Method) topLevel(){}
func (this *Method) String () string {

View File

@ -11,6 +11,7 @@ var parser = participle.MustBuild[Tree] (
&Typedef { },
&Method { }),
participle.Union[Type] (
&TypeNamed { },
&TypePointer { },
&TypeInterface { },
&TypeStruct { },

View File

@ -44,7 +44,7 @@ func (this *TypeArray) String () string {
// are specified in. Structs are passed by value unless a pointer is used.
type TypeStruct struct {
Pos lexer.Position
Members []*Declaration `parser:" '(' @@+ ')' `
Members []*Declaration `parser:" '(' @@+ ')' "`
}
func (*TypeStruct) ty(){}
func (this *TypeStruct) String () string {