Files
models/obj/text.sakura
T
2026-11-12 19:27:17 -06:00

147 lines
4.0 KiB
Plaintext

#! Contains reading OBJ's and MTL's text: its statements, a line each, blank
#! lines and comments passed over and a line ending with `\` joined to the
#! next; and the words and numbers of each.
module malunal::obj;
# The statements of a file's text, a line each.
struct statements {
var text: strview;
var at: u64;
#! The statement given last, its spaces about it trimmed.
var current: strview;
#! The number of the line it began on, from 1.
var line: u64;
var counted: u64;
var joined: string;
}
# The statements of text.
def statements_of(text: strview): statements {
return statements{ text = text, joined = string::new(nothing{}) };
}
# Moves to the next statement, passing over blank lines and those beginning
# with `#`.
# @returns Whether there was one; it's `current`.
def statements::next(&mut this): bool {
while this.at < this.text.length {
this.joined.clear();
var joining := false;
this.line = this.counted + 1;
while true {
val begun := this.at;
while this.at < this.text.length && this.text[this.at] != '\n' {
this.at++;
}
var end := this.at;
if this.at < this.text.length {
this.at++;
}
this.counted++;
while end > begun && is_space(this.text[end - 1]) {
end--;
}
# A line ending with `\` goes on in the next.
if end > begun && this.text[end - 1] == '\\' && this.at < this.text.length {
this.joined.append(this.text.substr(begun, end - 1 - begun));
this.joined.push(' ');
joining = true;
continue;
}
if joining {
this.joined.append(this.text.substr(begun, end - begun));
this.current = trimmed(this.joined.span);
} else {
this.current = trimmed(this.text.substr(begun, end - begun));
}
break;
}
continue if this.current.length == 0 || this.current[0] == '#';
return true;
}
return false;
}
# Whether a byte is a space between words.
def is_space(c: u8): bool {
return c == ' ' || c == '\t' || c == '\r' || c == 11 || c == 12;
}
# Text with the spaces about it taken away.
def trimmed(text: strview): strview {
var first: u64 = 0;
var end := text.length;
while first < end && is_space(text[first]) {
first++;
}
while end > first && is_space(text[end - 1]) {
end--;
}
return text.substr(first, end - first);
}
# The words of a statement, one after another.
struct words {
var text: strview;
var at: u64;
}
def words_of(text: strview): words {
return words{ text = text };
}
# The next word, or empty at the statement's end, or at a word beginning with
# `#`, a comment after it.
def words::next(&mut this): strview {
while this.at < this.text.length && is_space(this.text[this.at]) {
this.at++;
}
if this.at < this.text.length && this.text[this.at] == '#' {
this.at = this.text.length;
}
val begun := this.at;
while this.at < this.text.length && !is_space(this.text[this.at]) {
this.at++;
}
return this.text.substr(begun, this.at - begun);
}
# What's left of the statement, the spaces about it trimmed: a name, which may
# hold spaces.
def words::rest(&mut this): strview {
val left := trimmed(this.text.substr(this.at, this.text.length - this.at));
this.at = this.text.length;
return left;
}
# Whether nothing's left of the statement but spaces, or a comment.
def words::ended(&this): bool {
var at := this.at;
while at < this.text.length && is_space(this.text[at]) {
at++;
}
return at >= this.text.length || this.text[at] == '#';
}
# A word as a number.
def number_of(word: strview): maybe<f64> {
if real::parse::<f64>(word) is value(n) {
return n;
}
return nothing{};
}
# A word as a whole number.
def integer_of(word: strview): maybe<i64> {
if integer::parse::<i64>(word) is value(n) {
return n;
}
return nothing{};
}
# An error of malformed text, at a line.
def at_line(line: u64, why: strview): obj_error {
return failed(failure::malformed, $"line {line}: {why}".span);
}