45 lines
1.2 KiB
Go
45 lines
1.2 KiB
Go
package fsplParser
|
|
|
|
import "fmt"
|
|
import "git.tebibyte.media/fspl/fspl/lexer"
|
|
import "git.tebibyte.media/fspl/fspl/entity"
|
|
|
|
// Tree represents a parsed abstract syntax tree. It has no constructor and its
|
|
// zero value can be used safely.
|
|
type Tree struct {
|
|
Declarations []entity.TopLevel
|
|
}
|
|
|
|
// String returns a string representation of the tree. The result of this will
|
|
// be syntactically valid.
|
|
func (this *Tree) String () string {
|
|
out := ""
|
|
for index, declaration := range this.Declarations {
|
|
if index > 0 { out += "\n" }
|
|
out += fmt.Sprint(declaration)
|
|
}
|
|
return out
|
|
}
|
|
|
|
// Parse parses the output of the given lexer into the tree.
|
|
func (this *Tree) Parse (lx lexer.Lexer) error {
|
|
parser, err := newParser(lx)
|
|
parser.skim = false
|
|
if err != nil { return err }
|
|
return parser.parseInto(this)
|
|
}
|
|
|
|
// Skim is like Parse, but does not keep code within functions and methods,
|
|
// instead marking them as external.
|
|
func (this *Tree) Skim (lx lexer.Lexer) error {
|
|
parser, err := newParser(lx)
|
|
parser.skim = true
|
|
if err != nil { return err }
|
|
return parser.parseInto(this)
|
|
}
|
|
|
|
// AddDeclaration adds a top-level entity to the tree.
|
|
func (this *Tree) AddDeclaration (topLevel ...entity.TopLevel) {
|
|
this.Declarations = append(this.Declarations, topLevel...)
|
|
}
|