hylia/parser/parser.go

100 lines
No EOL
2.7 KiB
Go

package parser
import (
"errors"
"fmt"
"io/ioutil"
"strings"
)
// THE HYLIA PARSER
type Element struct {
Name string
Content string
FilePath string
}
func ParseFile(filename string) ([]Element, error) {
// Hey, does the file we're trying to parse actually exist?
data, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
// Parse the file, and ask "Is this a Hylia file?"
content := string(data)
if !strings.Contains(content, "<hylia>")|| !strings.Contains(content, "</hylia>") {
return nil, errors.New("The structure of this file is invalid. Are you sure this is a Hylia (.hy) file?")
}
// Extract the actual Hylia content
start := strings.Index(content, "<hylia>")
end := strings.Index(content, "</hylia>")
if start == -1 || end == -1 {
return nil, errors.New("Missing <hylia> tags. Are you sure this is a Hylia (.hy) file?")
}
content = content[start+len("<hylia>") : end]
// Extract the custom elements, which are wrapped in <element> tags and handle imports
elements := []Element{}
lines := strings.Split(content, "\n")
for _, line := range lines {
line = strings.TrimSpace(line)
if strings.HasPrefix(line, "<element") {
// Extract the element name
name := extractAttributeValue(line, "name")
if name == "" {
return nil, errors.New("<element> tag is missing a name. ('name' attribute)")
}
content, err := extractElementContent(line, content)
if err != nil {
return nil, err
}
elements = append(elements, Element{Name: name, Content: content, FilePath: filename})
} else if strings.HasPrefix(line, "<import") {
filePath := extractAttributeValue(line, "file")
if filePath == "" {
return nil, errors.New("<import> tag is missing a file ('file' attribute)")
}
// Recursively parse
importedElements, err := ParseFile(filePath)
if err != nil {
return nil, fmt.Errorf("ERROR: Could not import file %s: %w", filePath, err)
}
elements = append(elements, importedElements...)
}
}
return elements, nil
}
// Extracts an attribute
func extractAttributeValue(tag, attribute string) string {
prefix := fmt.Sprintf(`%s="`, attribute)
start := strings.Index(tag, prefix)
if start == -1 {
return ""
}
start += len(prefix)
end := strings.Index(tag[start:], `"`)
if end == -1 {
return ""
}
return tag[start : start+end]
}
// Extracts an <element>
func extractElementContent(tag, content string) (string, error) {
start := strings.Index(content, tag)
if start == -1 {
return "", errors.New("element tag not found in content")
}
end := strings.Index(content[start:], "</element>")
if end == -1 {
return "", errors.New("missing end tag of element")
}
return content[start+len(tag) : start+end], nil
}