implement-modules #43
33
parser/parser.go
Normal file
33
parser/parser.go
Normal 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
|
||||||
|
}
|
@ -3,7 +3,6 @@ package parser
|
|||||||
import "io"
|
import "io"
|
||||||
import "os"
|
import "os"
|
||||||
import "fmt"
|
import "fmt"
|
||||||
import "git.tebibyte.media/sashakoshka/fspl/lexer"
|
|
||||||
import "git.tebibyte.media/sashakoshka/fspl/entity"
|
import "git.tebibyte.media/sashakoshka/fspl/entity"
|
||||||
|
|
||||||
// Tree represents a parsed abstract syntax tree. It has no constructor and its
|
// Tree represents a parsed abstract syntax tree. It has no constructor and its
|
||||||
@ -21,12 +20,6 @@ func (this *Tree) String () string {
|
|||||||
return out
|
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.
|
// ParseFile parses the contents of the given file into the tree.
|
||||||
func (this *Tree) ParseFile (name string) error {
|
func (this *Tree) ParseFile (name string) error {
|
||||||
file, err := os.Open(name)
|
file, err := os.Open(name)
|
||||||
@ -34,3 +27,10 @@ func (this *Tree) ParseFile (name string) error {
|
|||||||
defer file.Close()
|
defer file.Close()
|
||||||
return this.Parse(name, file)
|
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)
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user