2024-12-05 23:29:52 +00:00
|
|
|
package parser
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2024-12-05 23:54:03 +00:00
|
|
|
"fmt"
|
2024-12-05 23:29:52 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// THE HYLIA PARSER
|
|
|
|
|
2024-12-05 23:54:03 +00:00
|
|
|
type Element struct {
|
|
|
|
Name string
|
|
|
|
Content string
|
|
|
|
FilePath string
|
|
|
|
}
|
|
|
|
|
2024-12-06 01:57:48 +00:00
|
|
|
func ParseFile(filename string) ([]Element, error) {
|
2024-12-05 23:29:52 +00:00
|
|
|
// 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?")
|
|
|
|
}
|
|
|
|
|
2024-12-05 23:54:03 +00:00
|
|
|
// 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
|
2024-12-06 01:57:48 +00:00
|
|
|
elements := []Element{}
|
2024-12-05 23:54:03 +00:00
|
|
|
lines := strings.Split(content, "\n")
|
|
|
|
for _, line := range lines {
|
|
|
|
line = strings.TrimSpace(line)
|
2024-12-06 01:57:48 +00:00
|
|
|
if strings.HasPrefix(line, "<element") {
|
2024-12-05 23:54:03 +00:00
|
|
|
// 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") {
|
2024-12-06 01:57:48 +00:00
|
|
|
filePath := extractAttributeValue(line, "file")
|
2024-12-05 23:54:03 +00:00
|
|
|
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...)
|
|
|
|
}
|
|
|
|
}
|
2024-12-05 23:29:52 +00:00
|
|
|
|
|
|
|
return elements, nil
|
|
|
|
|
2024-12-05 23:54:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2024-12-05 23:29:52 +00:00
|
|
|
}
|