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. // be assigned to interfaces. An operation is never a valid location expression.
type Operation struct { type Operation struct {
Pos lexer.Position Pos lexer.Position
Operator string `parser:" '[' @('+' | '++' | '-' | '--' | '*' | '/' | Operator string `parser:" '[' @('+' | '++' | '-' | '--' | '*' | '/' | '%' | '!!' | '||' | '&&' | '^^' | '!' | '|' | '&' | '^' | '<<' | '>>' | '<' | '>' | '<=' | '>=' | '=') "`
'%' | '!!' | '||' | '&&' | '^^' | '!' |
'|' | '&' | '^' | '<<' | '>>' | '<' |
'>' | '<=' | '>=' | '=') "`
Arguments []Expression `parser:" @@+ ']' "` Arguments []Expression `parser:" @@+ ']' "`
} }
func (*Operation) expression(){} func (*Operation) expression(){}
@ -174,8 +171,8 @@ func (this *Block) String () string {
// struct being accessed is. // struct being accessed is.
type MemberAccess struct { type MemberAccess struct {
Pos lexer.Position Pos lexer.Position
Source Expression `parser:" @@ "` Source Expression `parser:" @@ "`
Member string `parser:" '.' @@ "` Member string `parser:" '.' @Ident "`
} }
func (*MemberAccess) expression(){} func (*MemberAccess) expression(){}
func (this *MemberAccess) String () string { func (this *MemberAccess) String () string {
@ -187,8 +184,8 @@ func (this *MemberAccess) String () string {
// method access is never a valid location expression. // method access is never a valid location expression.
type MethodAccess struct { type MethodAccess struct {
Pos lexer.Position Pos lexer.Position
Source Expression `parser:" @@ "` Source Expression `parser:" @@ "`
Member string `parser:" '::' @@ "` Member string `parser:" '::' @Ident "`
} }
func (*MethodAccess) expression(){} func (*MethodAccess) expression(){}
func (this *MethodAccess) String () string { func (this *MethodAccess) String () string {
@ -235,7 +232,7 @@ func (this *Loop) String () string {
// to anything. It is never a valid location expression. // to anything. It is never a valid location expression.
type Break struct { type Break struct {
Pos lexer.Position Pos lexer.Position
Value Expression `parser:" '[' 'break' @@? " ']'` Value Expression `parser:" '[' 'break' @@? ']' "`
} }
func (*Break) expression(){} func (*Break) expression(){}
func (this *Break) String () string { 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. // to anything. A return statement is never a valid location expression.
type Return struct { type Return struct {
Pos lexer.Position Pos lexer.Position
Value Expression `parser:" '[' 'return' @@? " ']'` Value Expression `parser:" '[' 'return' @@? ']' "`
} }
func (*Return) expression(){} func (*Return) expression(){}
func (this *Return) String () string { 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 { type Typedef struct {
// Syntax // Syntax
Pos lexer.Position Pos lexer.Position
Public bool `parser: " @'+'? "` Public bool `parser:" @'+'? "`
Name string `parser: " @Ident "` Name string `parser:" @Ident "`
Type Type `parser: " ':' @@ "` Type Type `parser:" ':' @@ "`
// Semantics // Semantics
Methods map[string] Method Methods map[string] Method
@ -38,9 +38,9 @@ func (this *Typedef) String () string {
type Function struct { type Function struct {
// Syntax // Syntax
Pos lexer.Position Pos lexer.Position
Public bool `parser: " @'+'? "` Public bool `parser:" @'+'? "`
Signature `parser: " @@ "` Signature `parser:" @@ "`
Body Expression `parser: " ( '=' @@ )? "` Body Expression `parser:" ( '=' @@ )? "`
} }
func (*Function) topLevel(){} func (*Function) topLevel(){}
func (this *Function) String () string { func (this *Function) String () string {
@ -58,10 +58,10 @@ func (this *Function) String () string {
type Method struct { type Method struct {
// Syntax // Syntax
Pos lexer.Position Pos lexer.Position
Public bool `parser: " @'+'? "` Public bool `parser:" @'+'? "`
TypeName string `parser: " @Ident "` TypeName string `parser:" @Ident "`
Signature `parser: " '.' @@ "` Signature `parser:" '.' @@ "`
Body Expression `parser: " ( '=' @@ )? "` Body Expression `parser:" ( '=' @@ )? "`
} }
func (*Method) topLevel(){} func (*Method) topLevel(){}
func (this *Method) String () string { func (this *Method) String () string {

View File

@ -11,6 +11,7 @@ var parser = participle.MustBuild[Tree] (
&Typedef { }, &Typedef { },
&Method { }), &Method { }),
participle.Union[Type] ( participle.Union[Type] (
&TypeNamed { },
&TypePointer { }, &TypePointer { },
&TypeInterface { }, &TypeInterface { },
&TypeStruct { }, &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. // are specified in. Structs are passed by value unless a pointer is used.
type TypeStruct struct { type TypeStruct struct {
Pos lexer.Position Pos lexer.Position
Members []*Declaration `parser:" '(' @@+ ')' ` Members []*Declaration `parser:" '(' @@+ ')' "`
} }
func (*TypeStruct) ty(){} func (*TypeStruct) ty(){}
func (this *TypeStruct) String () string { func (this *TypeStruct) String () string {