Parser now understands the separation between files

This needs to be done because each file has a metadata section at the top.
This commit is contained in:
Sasha Koshka 2022-08-12 12:02:20 -05:00
parent 2019c67bbb
commit 18bd681082

View File

@ -19,16 +19,6 @@ type ParsingOperation struct {
// converts them into an abstract syntax tree.
func Parse (modulePath string) (tree *SyntaxTree, err error) {
parser := ParsingOperation { modulePath: modulePath }
err = parser.parse()
tree = parser.tree
return
}
// parse runs the parsing operation.
func (parser *ParsingOperation) parse () (err error) {
if parser.tree == nil {
parser.tree = &SyntaxTree { }
}
if parser.modulePath[len(parser.modulePath) - 1] != '/' {
parser.modulePath += "/"
@ -47,10 +37,28 @@ func (parser *ParsingOperation) parse () (err error) {
sourceFile, err = file.Open(parser.modulePath + entry.Name())
if err != nil { return }
var tokens []lexer.Token
tokens, err = lexer.Tokenize(sourceFile)
if err != nil { return }
parser.tokens = append(parser.tokens, tokens...)
// parse the tokens into the module
err = parser.parse(sourceFile)
}
tree = parser.tree
return
}
// parse parses a file and adds it to the syntax tree.
func (parser *ParsingOperation) parse (sourceFile *file.File) (err error) {
var tokens []lexer.Token
tokens, err = lexer.Tokenize(sourceFile)
if err != nil { return }
if parser.tree == nil {
parser.tree = &SyntaxTree { }
}
if len(tokens) == 0 { return }
parser.tokens = tokens
parser.token = tokens[0]
parser.tokenIndex = 0
return
}