mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-03 18:47:47 +00:00
71 lines
1.6 KiB
Go
71 lines
1.6 KiB
Go
package compiler
|
|
|
|
import (
|
|
"fmt"
|
|
"strconv"
|
|
"strings"
|
|
"unicode"
|
|
"unicode/utf8"
|
|
)
|
|
|
|
// parseGoEmbed parses the text following "//go:embed" to extract the glob patterns.
|
|
// It accepts unquoted space-separated patterns as well as double-quoted and back-quoted Go strings.
|
|
// Copied from https://github.com/golang/go/blob/219fe9d547d09d3de1b024c6c8b8314dd0bf12e4/src/cmd/compile/internal/noder/noder.go#L1717-L1776
|
|
func parseGoEmbed(args string) ([]string, error) {
|
|
var list []string
|
|
for args = strings.TrimSpace(args); args != ""; args = strings.TrimSpace(args) {
|
|
var path string
|
|
Switch:
|
|
switch args[0] {
|
|
default:
|
|
i := len(args)
|
|
for j, c := range args {
|
|
if unicode.IsSpace(c) {
|
|
i = j
|
|
break
|
|
}
|
|
}
|
|
path = args[:i]
|
|
args = args[i:]
|
|
|
|
case '`':
|
|
i := strings.Index(args[1:], "`")
|
|
if i < 0 {
|
|
return nil, fmt.Errorf("invalid quoted string in //go:embed: %s", args)
|
|
}
|
|
path = args[1 : 1+i]
|
|
args = args[1+i+1:]
|
|
|
|
case '"':
|
|
i := 1
|
|
for ; i < len(args); i++ {
|
|
if args[i] == '\\' {
|
|
i++
|
|
continue
|
|
}
|
|
if args[i] == '"' {
|
|
q, err := strconv.Unquote(args[:i+1])
|
|
if err != nil {
|
|
return nil, fmt.Errorf("invalid quoted string in //go:embed: %s", args[:i+1])
|
|
}
|
|
path = q
|
|
args = args[i+1:]
|
|
break Switch
|
|
}
|
|
}
|
|
if i >= len(args) {
|
|
return nil, fmt.Errorf("invalid quoted string in //go:embed: %s", args)
|
|
}
|
|
}
|
|
|
|
if args != "" {
|
|
r, _ := utf8.DecodeRuneInString(args)
|
|
if !unicode.IsSpace(r) {
|
|
return nil, fmt.Errorf("invalid quoted string in //go:embed: %s", args)
|
|
}
|
|
}
|
|
list = append(list, path)
|
|
}
|
|
return list, nil
|
|
}
|