Make include directives work in .mnu files
This commit is contained in:
@@ -4,6 +4,7 @@ package asciiscan
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"strconv"
|
||||
@@ -15,6 +16,9 @@ var hashComment = []byte("#")
|
||||
type Scanner struct {
|
||||
bufio *bufio.Scanner
|
||||
closer io.Closer
|
||||
|
||||
// If we've peeked, there will be items here
|
||||
buffered []string
|
||||
}
|
||||
|
||||
func New(filename string) (*Scanner, error) {
|
||||
@@ -38,6 +42,13 @@ func (s *Scanner) Close() error {
|
||||
}
|
||||
|
||||
func (s *Scanner) ConsumeString() (string, error) {
|
||||
if len(s.buffered) > 0 {
|
||||
out, buffered := s.buffered[0], s.buffered[1:]
|
||||
s.buffered = buffered
|
||||
|
||||
return out, nil
|
||||
}
|
||||
|
||||
for s.bufio.Scan() {
|
||||
line := s.bufio.Bytes()
|
||||
|
||||
@@ -68,15 +79,41 @@ func ConsumeProperty(s string) (string, string) {
|
||||
}
|
||||
|
||||
parts := strings.SplitN(s, ":", 2)
|
||||
|
||||
return strings.TrimSpace(parts[0]), strings.TrimSpace(parts[1])
|
||||
}
|
||||
|
||||
// Peek ahead in the input stream to see if the next line might be a property
|
||||
// (contain a colon character).
|
||||
// Check to see if the line looks like a property (contains a colon character).
|
||||
func IsProperty(s string) bool {
|
||||
return strings.Contains(s, ":")
|
||||
}
|
||||
|
||||
// Checks if the next line might be a property, without reading it
|
||||
func (s *Scanner) PeekProperty() (bool, error) {
|
||||
str, err := s.ConsumeString()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
s.buffered = append(s.buffered, str)
|
||||
|
||||
return IsProperty(str), nil
|
||||
}
|
||||
|
||||
func (s *Scanner) ConsumeProperty() (string, string, error) {
|
||||
str, err := s.ConsumeString()
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
|
||||
if !IsProperty(str) {
|
||||
return "", "", fmt.Errorf("Not a property: %q", str)
|
||||
}
|
||||
|
||||
k, v := ConsumeProperty(str)
|
||||
return k, v, nil
|
||||
}
|
||||
|
||||
func (s *Scanner) ConsumeInt() (int, error) {
|
||||
str, err := s.ConsumeString()
|
||||
if err != nil {
|
||||
@@ -86,6 +123,39 @@ func (s *Scanner) ConsumeInt() (int, error) {
|
||||
return strconv.Atoi(str)
|
||||
}
|
||||
|
||||
// Reads a list of non-property lines, skipping any that match the given strings
|
||||
func (s *Scanner) ConsumeStringList(skip ...string) ([]string, error) {
|
||||
skipper := make(map[string]bool, len(skip))
|
||||
for _, str := range skip {
|
||||
skipper[str] = true
|
||||
}
|
||||
|
||||
var out []string
|
||||
|
||||
for {
|
||||
isProp, err := s.PeekProperty()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// The object list is terminated by the first property
|
||||
if isProp {
|
||||
break
|
||||
}
|
||||
|
||||
str, err := s.ConsumeString()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if !skipper[str] {
|
||||
out = append(out, str)
|
||||
}
|
||||
}
|
||||
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (s *Scanner) ConsumeIntPtr(to *int) error {
|
||||
val, err := s.ConsumeInt()
|
||||
if err != nil {
|
||||
|
Reference in New Issue
Block a user