implement-modules #43

Closed
sashakoshka wants to merge 502 commits from implement-modules into main
2 changed files with 40 additions and 7 deletions
Showing only changes of commit 0d79482568 - Show all commits

33
parser/parser.go Normal file
View File

@ -0,0 +1,33 @@
package parser
import "io"
import "git.tebibyte.media/sashakoshka/fspl/lexer"
// Parser parses tokens from a lexer into syntax entities, which it places into
// a tree.
type Parser struct {
lexer lexer.Lexer
tree *Tree
}
func NewParser (name string, file io.Reader) (*Parser, error) {
lx, err := lexer.NewLexer(name, file)
if err != nil { return nil, err }
return &Parser {
lexer: lx,
}, nil
}
func (this *Parser) ParseInto (tree *Tree) error {
this.tree = tree
return this.parse()
}
func (this *Parser) parse () error {
for {
err := this.parseTopLevel()
if err != nil { return err }
}
return nil
}

View File

@ -3,7 +3,6 @@ package parser
import "io"
import "os"
import "fmt"
import "git.tebibyte.media/sashakoshka/fspl/lexer"
import "git.tebibyte.media/sashakoshka/fspl/entity"
// Tree represents a parsed abstract syntax tree. It has no constructor and its
@ -21,12 +20,6 @@ func (this *Tree) String () string {
return out
}
// Parse parses the contents of the given io.Reader into the tree.
func (this *Tree) Parse (name string, file io.Reader) error {
// TODO
return nil
}
// ParseFile parses the contents of the given file into the tree.
func (this *Tree) ParseFile (name string) error {
file, err := os.Open(name)
@ -34,3 +27,10 @@ func (this *Tree) ParseFile (name string) error {
defer file.Close()
return this.Parse(name, file)
}
// Parse parses the contents of the given io.Reader into the tree.
func (this *Tree) Parse (name string, file io.Reader) error {
parser, err := NewParser(name, file)
if err != nil { return err }
return parser.ParseInto(this)
}