readNotationAppState function
Reads text, an app's state file (app.txt): the state every page and
component of the app shares, as state lines in the notation's own
grammar (a value, a type, a formula, a function, saved), with comments.
Never throws.
A line that is not state (a node, a define, an every, a key or an
on line) is an issue that says where it belongs, and is not read.
A formula with no type may read another whichever is written first; one
that reads itself, through others or not, is an issue.
With partial (more of the file is coming: a stream), a list left open
over the last lines (state rows=[ and an item a line) is read as far
as it is written.
theme: the site's theme, which a formula reads as a page's does
(theme.accent, a color of the theme's, is theme.colors.accent).
Implementation
NotationAppState readNotationAppState(String text, {Catalog? catalog, bool partial = false, Json? theme}) {
final parser = _Parser(catalog ?? Catalog.current, theme == null ? const NotationOptions() : NotationOptions.fromTheme(theme, named: true, roles: true));
final rows = const LineSplitter().convert(text);
String shown(String line) => line.length > 50 ? '${line.substring(0, 47)}...' : line;
for (var i = 0; i < rows.length; i++) {
final raw = rows[i];
final line = raw.trim();
// A list left open on the line before goes on over this one.
if (parser._continues || line.isEmpty || line.startsWith('//')) {
parser._readLine(raw);
continue;
}
final String? wrong;
if (raw.startsWith(' ') || raw.startsWith('\t')) {
wrong = '`${shown(line)}` is indented under nothing, so it is not read: app.txt holds the app\'s shared state, one `state name=value` line each at the margin.';
} else if (_appStateLine.hasMatch(line)) {
wrong = null;
} else if (RegExp(r'^(define|primitive|component)\s').hasMatch(line)) {
wrong = '`${shown(line)}` defines a word, so it is not read here: app.txt holds only the app\'s shared state (`state name=value`). A word is a file of its own, components/<Word>.txt.';
} else if (RegExp(r'^(every|key|on)\s').hasMatch(line)) {
wrong = '`${shown(line)}` runs on a page, so it is not read here: app.txt holds only the app\'s shared state (`state name=value`). '
'Write the line on the page that should run it (pages/<screen>.txt), or in a component every page shows.';
} else {
wrong = '`${shown(line)}` is not state, so it is not read: app.txt holds only the app\'s shared state (`state name=value`). A screen\'s lines go in pages/<screen>.txt.';
}
if (wrong == null) {
parser._readLine(raw);
} else {
// (Read as a blank line: the lines after it keep their numbers.)
parser._readLine('');
parser.issues.add(NotationIssue(i + 1, 1, wrong));
}
}
// Half a list of a stream: its whole items so far.
var halfList = false;
if (partial && parser._open != null) {
final open = parser._open!.trimRight();
final closed = _closedSoFar(open.endsWith(',') ? open.substring(0, open.length - 1) : open);
parser._open = null;
halfList = true;
if (closed != null) parser.line(closed);
}
try {
parser.finish();
} on Object {
// What was read stays.
}
// (Its brackets are not closed yet: that is not a mistake of half a file.)
if (halfList) parser.issues.removeWhere((i) => i.message.contains('is never closed'));
final lines = <String, int>{
for (final e in parser._stateLines.entries) e.key.substring(e.key.indexOf(' ') + 1): e.value,
};
// A formula reads the formulas written after it too.
final formulas = parser._derived;
final ringed = <String>{};
// Each after the ones it reads (whichever is written first), so that one
// is written out in another once: one that has others written out in it
// and is read again (twice by one formula, or by a second) is kept by
// its name, a variable with its formula, as on a page. No formula grows
// past what was written; one that reads itself is a ring.
final visiting = <String>{}, resolved = <String>{};
void resolve(String a) {
if (resolved.contains(a) || !formulas.containsKey(a) || !visiting.add(a)) return;
for (final b in formulas.keys.toList()) {
final now = formulas[a];
if (now == null) break;
if (!formulas.containsKey(b) || !now.contains(b)) continue;
final marked = parser._rename(' $now', b, '\u0001').substring(1);
final times = '\u0001'.allMatches(marked).length;
if (times == 0) continue;
if (b == a || visiting.contains(b)) {
ringed
..add(a)
..add(b);
continue;
}
resolve(b);
if (ringed.contains(b)) {
ringed.add(a);
continue;
}
if (!formulas.containsKey(b)) continue;
if (parser._derivedGrown.contains(b) && (times > 1 || parser._derivedRead.contains(b))) {
parser._derivedKept(b);
continue;
}
formulas[a] = marked.replaceAll('\u0001', '(${formulas[b]})');
parser._derivedRead.add(b);
parser._derivedGrown.add(a);
}
visiting.remove(a);
resolved.add(a);
}
for (final a in formulas.keys.toList()) {
resolve(a);
}
// (One that still reads a formula's name after every round is in a ring.)
for (final a in formulas.keys) {
if (ringed.contains(a)) continue;
for (final b in formulas.keys) {
if (parser._rename(' ${formulas[a]}', b, '\u0000') != ' ${formulas[a]}') ringed.add(a);
}
}
for (final name in ringed) {
parser.issues.add(NotationIssue(lines[name] ?? 1, 1, 'state $name reads itself (through the formulas it names, or directly), so it gives nothing: a formula reads other states, never itself.'));
formulas.remove(name);
}
for (final v in parser.variables.values) {
if (v['formula'] case final String f) {
var next = f;
final own = readList(v['parameters']) ?? const [];
for (final d in formulas.entries) {
if (!own.contains(d.key)) next = parser._rename(' $next', d.key, '(${d.value})').substring(1);
}
v['formula'] = next;
}
}
final variables = <String, Json>{
for (final v in parser.variables.values)
if (!parser._internal.contains(v['name']))
v['name']! as String: {
for (final e in v.entries)
if (e.key != 'name') e.key: e.value,
// (A variable with no start says so: its record is then one.)
if (!v.containsKey('formula') && !v.containsKey('value')) 'value': null,
},
for (final d in formulas.entries) d.key: {notationInlineFormulaKey: d.value},
};
final issues = [...parser.issues]..sort((a, b) => a.line != b.line ? a.line - b.line : a.column - b.column);
return (variables: variables, lines: lines, issues: issues);
}