From cb7a238c800751bfa3cd51251eb62dc356e47c30 Mon Sep 17 00:00:00 2001 From: conzer Date: Thu, 5 Dec 2024 18:54:03 -0500 Subject: [PATCH] Do a thing --- parser/parser.go | 74 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 72 insertions(+), 2 deletions(-) diff --git a/parser/parser.go b/parser/parser.go index f85bc00..d08f258 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -2,12 +2,19 @@ package parser import ( "errors" + "fmt" "io/ioutil" "strings" ) // THE HYLIA PARSER +type Element struct { + Name string + Content string + FilePath string +} + func ParseFile(filename string) ([]string error) { // Hey, does the file we're trying to parse actually exist? data, err := ioutil.ReadFile(filename) @@ -21,10 +28,73 @@ func ParseFile(filename string) ([]string error) { return nil, errors.New("The structure of this file is invalid. Are you sure this is a Hylia (.hy) file?") } - // Extract the custom elements, which are wrapped in tags + // Extract the actual Hylia content + start := strings.Index(content, "") + end := strings.Index(content, "") + if start == -1 || end == -1 { + return nil, errors.New("Missing tags. Are you sure this is a Hylia (.hy) file?") + } + + content = content[start+len("") : end] + + // Extract the custom elements, which are wrapped in tags and handle imports elements := []string{} - // ... TODO + 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(" 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, " 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 +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:], "") + if end == -1 { + return "", errors.New("missing end tag of element") + } + return content[start+len(tag) : start+end], nil } \ No newline at end of file