From 2019c67bbbae5f1a7ca99f5be39939d5ffd7e3e9 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Fri, 12 Aug 2022 11:55:17 -0500 Subject: [PATCH] Created basic test for parser --- parser/parser.go | 11 ++++++++-- parser/parser_test.go | 33 ++++++++++++++++++++++++++++++ parser/tree.go | 5 ++++- tests/parser/full/main.arf | 41 ++++++++++++++++++++++++++++++++++++++ tests/parser/meta/meta.arf | 6 ++++++ 5 files changed, 93 insertions(+), 3 deletions(-) create mode 100644 parser/parser_test.go create mode 100644 tests/parser/full/main.arf create mode 100644 tests/parser/meta/meta.arf diff --git a/parser/parser.go b/parser/parser.go index e20254d..0a6b565 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -11,18 +11,25 @@ type ParsingOperation struct { token lexer.Token tokens []lexer.Token tokenIndex int + + tree *SyntaxTree } // Parse reads the files located in the module specified by modulePath, and // converts them into an abstract syntax tree. func Parse (modulePath string) (tree *SyntaxTree, err error) { parser := ParsingOperation { modulePath: modulePath } - tree, err = parser.parse() + err = parser.parse() + tree = parser.tree return } // parse runs the parsing operation. -func (parser *ParsingOperation) parse () (tree *SyntaxTree, err error) { +func (parser *ParsingOperation) parse () (err error) { + if parser.tree == nil { + parser.tree = &SyntaxTree { } + } + if parser.modulePath[len(parser.modulePath) - 1] != '/' { parser.modulePath += "/" } diff --git a/parser/parser_test.go b/parser/parser_test.go new file mode 100644 index 0000000..f4a7181 --- /dev/null +++ b/parser/parser_test.go @@ -0,0 +1,33 @@ +package parser + +import "reflect" +import "testing" + +func checkTree (modulePath string, correct *SyntaxTree, test *testing.T) { + tree, err := Parse(modulePath) + + if err != nil { + test.Log("returned error:") + test.Log(err.Error()) + test.Fail() + return + } + + if !reflect.DeepEqual(tree, correct) { + test.Log("trees not equal") + test.Fail() + return + } +} + +func TestMeta (test *testing.T) { + checkTree("../tests/parser/meta",&SyntaxTree { + license: "GPLv3", + author: "Sasha Koshka", + + requires: []string { + "someModule", + "otherModule", + }, + }, test) +} diff --git a/parser/tree.go b/parser/tree.go index 1616abb..4b95d9d 100644 --- a/parser/tree.go +++ b/parser/tree.go @@ -4,5 +4,8 @@ package parser // can be expected to be syntactically correct, but it might not be semantically // correct (because it has not been analyzed yet.) type SyntaxTree struct { - + license string + author string + + requires []string } diff --git a/tests/parser/full/main.arf b/tests/parser/full/main.arf new file mode 100644 index 0000000..e64decb --- /dev/null +++ b/tests/parser/full/main.arf @@ -0,0 +1,41 @@ +:arf +author "Sasha Koshka" +license "GPLv3" +require "io" +--- + +# this is a global variable +data wn helloText:String "Hello, world!" + +# this is a struct definition +type rr Greeter:Obj + # "Hi." is a string constant. all Greeters will be initialized with a + # pointer to it. I don't know really it depends on what I decide that + # a String type even is. + wr text:String "Hi." + "sdfdsf" "ahh" + "asdf" + +# this is a function +func rr main + > argc:Int + > argv:{String} + < status:Int 0 + --- + let greeter:Greeter:mut + greeter.setText helloText + greeter.greet + +# this is a member function +func rr greet + @ greeter:{Greeter} + --- + io.println greeter.text + +# this is mutator member function +func rr setText + @ greeter:{Greeter} + > text:String + --- + greeter.text.set text + diff --git a/tests/parser/meta/meta.arf b/tests/parser/meta/meta.arf new file mode 100644 index 0000000..b59a53a --- /dev/null +++ b/tests/parser/meta/meta.arf @@ -0,0 +1,6 @@ +:arf +author "Sasha Koshka" +license "GPLv3" +require "someModule" +require "otherModule" +---