diff --git a/parser/parser.go b/parser/parser.go index 0a6b565..8c16723 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -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 }