Files
sfz/lex.go
T

164 lines
5.0 KiB
Go
Raw Normal View History

package sfz
import (
"regexp"
"strings"
"unicode/utf8"
)
var (
// headerRE reads a header at the start of the text.
headerRE = regexp.MustCompile(`^<([A-Za-z_]+)>`)
// opcodeRE reads an opcode's name and = at the start of the text.
opcodeRE = regexp.MustCompile(`^([A-Za-z0-9_$]+)=`)
// defineRE reads a #define's variable and value, up to the end of its line.
defineRE = regexp.MustCompile(`^#define[ \t]+(\$[A-Za-z0-9_]{1,128})[ \t]+([^\r\n]*)`)
// includeRE reads an #include's quoted path.
includeRE = regexp.MustCompile(`^#include[ \t]+"([^"\r\n]*)"`)
// breakRE finds where an opcode's value ends: at a header, the next
2026-12-01 18:18:40 +00:00
// opcode after whitespace, a comment, a directive after whitespace, or the
// end of the line, so that a value can hold spaces and = signs, as a
// sample's path can.
breakRE = regexp.MustCompile(`<[A-Za-z_]+>|\s[A-Za-z0-9_$]+=|//|/\*|\s#(?:define|include)\b|\n`)
)
// byteOrderMark is the UTF-8 mark some editors put at the start of a file. It
// is read as space.
const byteOrderMark = "\ufeff"
// atLineStart reports whether a directive may start at offset i: at the start
// of the text, after the byte order mark, or after whitespace.
func atLineStart(src string, i int) bool {
return i == 0 || isSpace(src[i-1]) || i == len(byteOrderMark) && strings.HasPrefix(src, byteOrderMark)
}
func isSpace(c byte) bool { return c == ' ' || c == '\t' || c == '\r' || c == '\n' }
func isNameChar(c byte) bool {
return c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9' || c == '_' || c == '$'
}
// lexer cuts text into nodes and keeps count of the position.
type lexer struct {
nodes []*Node
line int
// lineStart is the offset where the current line starts.
lineStart int
// pos is the offset the next node starts at.
pos int
}
// add appends a node for the text from the current position to end.
func (l *lexer) add(src string, end int, n *Node) {
n.Raw = src[l.pos:end]
n.line, n.col = l.line, l.pos-l.lineStart+1
if n.Kind == KindText && len(l.nodes) > 0 {
if last := l.nodes[len(l.nodes)-1]; last.Kind == KindText {
// Nodes tile the text, so the two are one run of it.
last.Raw = src[l.pos-len(last.Raw) : end]
l.advance(src, end)
return
}
}
l.nodes = append(l.nodes, n)
l.advance(src, end)
}
// advance moves the position to end, counting the lines it passes.
func (l *lexer) advance(src string, end int) {
for i := l.pos; i < end; i++ {
if src[i] == '\n' {
l.line++
l.lineStart = i + 1
}
}
l.pos = end
}
// lex cuts text into nodes whose raw text, joined, is the text.
func lex(src string) []*Node {
l := &lexer{line: 1}
for l.pos < len(src) {
i := l.pos
rest := src[i:]
switch {
case isSpace(src[i]) || i == 0 && strings.HasPrefix(src, byteOrderMark):
j := i
if i == 0 && strings.HasPrefix(src, byteOrderMark) {
j = len(byteOrderMark)
}
for j < len(src) && isSpace(src[j]) {
j++
}
l.add(src, j, &Node{Kind: KindSpace})
case strings.HasPrefix(rest, "//"):
j := len(src)
if e := strings.IndexByte(rest, '\n'); e >= 0 {
j = i + e
}
l.add(src, j, &Node{Kind: KindComment})
case strings.HasPrefix(rest, "/*"):
j := len(src)
if e := strings.Index(rest[2:], "*/"); e >= 0 {
j = i + 2 + e + 2
}
l.add(src, j, &Node{Kind: KindComment})
case src[i] == '<' && headerRE.MatchString(rest):
m := headerRE.FindStringSubmatch(rest)
l.add(src, i+len(m[0]), &Node{Kind: KindHeader, Name: m[1]})
case src[i] == '#' && atLineStart(src, i) && l.directive(src):
case opcodeRE.MatchString(rest):
m := opcodeRE.FindStringSubmatch(rest)
valueStart := i + len(m[0])
end := len(src)
if loc := breakRE.FindStringIndex(src[valueStart:]); loc != nil {
end = valueStart + loc[0]
}
for end > valueStart && isSpace(src[end-1]) {
end--
}
l.add(src, end, &Node{Kind: KindOpcode, Name: m[1], Value: strings.TrimSpace(src[valueStart:end])})
case isNameChar(src[i]):
j := i
for j < len(src) && isNameChar(src[j]) {
j++
}
l.add(src, j, &Node{Kind: KindText})
default:
_, size := utf8.DecodeRuneInString(rest)
l.add(src, i+size, &Node{Kind: KindText})
}
}
for i, n := range l.nodes {
n.idx = i
}
return l.nodes
}
// directive adds a #define or #include at the current position, and reports
// whether it was one that can be read.
func (l *lexer) directive(src string) bool {
rest := src[l.pos:]
if m := defineRE.FindStringSubmatch(rest); m != nil {
length := len(m[0])
// A comment ends the value.
valueStart := length - len(m[2])
if loc := commentRE.FindStringIndex(m[2]); loc != nil {
length = valueStart + loc[0]
}
for length > valueStart && isSpace(rest[length-1]) {
length--
}
l.add(src, l.pos+length, &Node{Kind: KindDefine, Name: m[1], Value: strings.TrimSpace(rest[valueStart:length])})
return true
}
if m := includeRE.FindStringSubmatch(rest); m != nil {
l.add(src, l.pos+len(m[0]), &Node{Kind: KindInclude, Value: m[1]})
return true
}
return false
}
// commentRE finds the start of a comment.
var commentRE = regexp.MustCompile(`//|/\*`)