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.
Implementation
NotationAppState readNotationAppState(String text, {Catalog? catalog, bool partial = false}) {
final parser = _Parser(catalog ?? Catalog.current, const NotationOptions());
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>{};
for (var round = 0; round < formulas.length; round++) {
var changed = false;
for (final a in formulas.keys.toList()) {
for (final b in formulas.entries.toList()) {
if (ringed.contains(a)) break;
final now = formulas[a]!;
final next = parser._rename(' $now', b.key, '(${b.value})').substring(1);
if (next == now) continue;
if (a == b.key || next.length > 20000) {
ringed.add(a);
break;
}
formulas[a] = next;
changed = true;
}
}
if (!changed) break;
}
// (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);
}