of static method
Null when text (one line, no indentation) does not start with a word.
Implementation
static LineParts? of(String text) {
final head = RegExp(r'^([A-Za-z][A-Za-z0-9]*)((?:\.[A-Za-z][A-Za-z0-9]*)*)(#[A-Za-z_][A-Za-z0-9_-]*)?(?= |$)').firstMatch(text);
if (head == null) return null;
final parts = <LinePart>[];
var i = head.end;
final n = text.length;
int closeOfString(int open) {
var depth = 0;
for (var k = open + 1; k < n; k++) {
final c = text[k];
if (c == r'\') {
k++;
} else if (c == '{') {
depth++;
} else if (c == '}' && depth > 0) {
depth--;
} else if (c == '"' && depth == 0) {
return k + 1;
}
}
return n;
}
int closeOfBalanced(int open) {
var depth = 0, quoted = false;
for (var k = open; k < n; k++) {
final c = text[k];
if (c == '"' && text[k - 1] != r'\') quoted = !quoted;
if (quoted) continue;
if (c == '[' || c == '{' || c == '(') depth++;
if ((c == ']' || c == '}' || c == ')') && --depth == 0) return k + 1;
}
return n;
}
while (i < n) {
if (text[i] == ' ') {
i++;
continue;
}
final start = i;
if (text[i] == '"') {
i = closeOfString(i);
parts.add(LinePart.string(text.substring(start, i))..at = start);
continue;
}
if (text[i] == '{') {
parts.add(LinePart.tail(text.substring(i))..at = start);
break;
}
// A word, up to a space; a value after = may be a string or bracketed.
String? key;
while (i < n && text[i] != ' ') {
final c = text[i];
// `hovered.fill={…}`, `motion.translateY={p}`: a dotted key whose value
// is braced or a list is a key=value too (a word, its value would end
// at its first space).
// `onKeyDown(key=c)={…}`: an event of a trigger with fields.
if ((key == null && i == start ? RegExp(r'on[A-Z][A-Za-z0-9]*\([^()]*\)=[{]').matchAsPrefix(text, start) : null) case final ev?) {
key = text.substring(start, ev.end - 2);
i = closeOfBalanced(ev.end - 1);
break;
}
if (c == '=' &&
key == null &&
i > start &&
(RegExp(r'^[A-Za-z_][A-Za-z0-9_-]*$').hasMatch(text.substring(start, i)) ||
(i + 1 < n && (text[i + 1] == '{' || text[i + 1] == '[') && RegExp(r'^[A-Za-z_][A-Za-z0-9_-]*(\.[A-Za-z_][A-Za-z0-9_-]*)+$').hasMatch(text.substring(start, i))))) {
key = text.substring(start, i);
i++;
// `x=@0.5`, `x=@"text"`, `x=@[0, 100]`: a signature's bound
// parameter with its default, read whole.
if (i + 1 < n && text[i] == '@' && (text[i + 1] == '"' || text[i + 1] == '{' || text[i + 1] == '[')) i++;
if (i < n && text[i] == '"') {
i = closeOfString(i);
break;
}
if (i < n && (text[i] == '{' || text[i] == '[')) {
i = closeOfBalanced(i);
break;
}
continue;
}
// `@value=[0, 100]`: a signature's value with its default, read whole.
if (c == '=' && key == null && text[start] == '@' && i + 1 < n && (text[i + 1] == '{' || text[i + 1] == '[')) {
i = closeOfBalanced(i + 1);
break;
}
// `style.fontFamily="Georgia"`: a dotted key's quoted value is the
// key's (round 11b: a paint paragraph's style, 15 lines in 2 builds,
// read as a word and a second text).
if (c == '"' && key == null && RegExp(r'^[A-Za-z_][A-Za-z0-9_]*(?:\.[A-Za-z_][A-Za-z0-9_]*)+=$').hasMatch(text.substring(start, i))) {
key = text.substring(start, i - 1);
i = closeOfString(i);
break;
}
if (c == '"' && key == null) break;
i++;
}
if (i == start) {
i++;
continue;
}
final raw = text.substring(start, i);
parts.add((key == null ? LinePart.word(raw) : LinePart.pair(key, raw.substring(key.length + 1)))..at = start);
}
return LineParts._(head[1]!, head[2]!, head[3] ?? '', parts);
}