Skip to content

fix(craft): accept CR LF, nest list elements, locate expansion errors - #38

Open
innkuika wants to merge 1 commit into
craft/engine-limitsfrom
craft/fix-parser
Open

innkuika wants to merge 1 commit into
craft/engine-limitsfrom
craft/fix-parser

Conversation

@innkuika

Copy link
Copy Markdown
Contributor

Description

Fixes from dogfooding the Craft engine: we built real apps on top of the stack as a power user would. Stacked on #33.

  • Line endings: CR LF and lone CR are accepted as line breaks, with the same line and column numbers as the LF version of the source. This is a documented difference from RFW 2.0.0. A raw CR inside a string is rejected, and a CR ends a // comment.
  • Nested list elements: ...for x in l: ...if c: e (and any nesting of ...for / ...if) parses and filters a list. The binary format doesn't change.
  • Error reports: an expansion error now names the template (library and declaration) whose body holds the failing node, plus its line and column when the library was parsed with a source identifier. Several misleading parser messages are clearer.
  • :: must be written as two adjacent colons; a comment between them is now rejected.

Each fix starts from the dogfooding repro, turned into a conformance case or unit test that fails before the change and passes after it. The blueprints are updated (craft_text_format, craft_language_extensions, craft_runtime, craft_libraries_and_names, craft_a2ui_integration). An independent review checked the blueprints, tests and code, including about 306,000 line-ending variants.

Pre-launch Checklist

One time:

For this PR:

  • I updated/added relevant documentation.
  • My code changes (if any) have tests.
  • License checks pass (python3 ./scripts/fix_licenses.py --check).
  • Format checks pass (./scripts/fix_format.sh --check).

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request adds support for nested list elements (such as nested ...for and ...if loops) and CR/CRLF line breaks in the Craft text format. It also enhances runtime error reporting by tracking and outputting the template name and line number of the failing node, with corresponding updates to the conformance schemas, test suites, and catalog generation. Feedback on the changes suggests optimizing the _lineStarts helper in text.dart to avoid allocating an intermediate list of all runes in the file, which improves memory efficiency for large files.

Comment on lines +697 to +707
List<int> _lineStarts(String file) {
final List<int> characters = file.runes.toList();
final List<int> starts = <int>[0];
for (int i = 0; i < characters.length; i += 1) {
final int c = characters[i];
if (c == 0x0A || (c == 0x0D && characters.elementAtOrNull(i + 1) != 0x0A)) {
starts.add(i + 1);
}
}
return starts;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The current implementation of _lineStarts calls file.runes.toList(), which allocates a new list containing all runes in the file. For large files, this can lead to unnecessary memory allocation and overhead.

We can optimize this by iterating over file.runes directly and tracking the previous character to detect line breaks (including CR LF, CR, and LF). This avoids allocating the intermediate list entirely.

List<int> _lineStarts(String file) {
  final List<int> starts = <int>[0];
  int i = 0;
  int? previous;
  for (final int c in file.runes) {
    if (previous == 0x0A || (previous == 0x0D && c != 0x0A)) {
      starts.add(i);
    }
    previous = c;
    i += 1;
  }
  if (previous == 0x0A || previous == 0x0D) {
    starts.add(i);
  }
  return starts;
}

@ditman

ditman commented Dec 2, 2026

Copy link
Copy Markdown

(Do let me know when #38 and #39 are ready to review; my agent already has thoughts but I'm assuming you're still working on these?)

@innkuika
innkuika marked this pull request as ready for review December 2, 2026 20:24
@innkuika
innkuika requested a review from ditman December 2, 2026 20:24
Fixes dogfood bugs B04, B15 and B08.

- B04: CR LF and CR are line breaks (craft_text_format requirement 6, a
  deliberate difference from RFW 2.0.0). A CR LF counts as one line in
  every reported position, including the source size limit; a CR inside a
  string literal is rejected and a CR ends an end-of-line comment.
- B15: the element of a `...for` or `...if` may itself be a `...for` or
  `...if` (craft_language_extensions requirement 7), so
  `...for x in l: ...if c: e` filters a list. It parses to a loop whose
  output is a loop; the runtime flattens it (craft_runtime requirement 6),
  catalog schema derivation places the inner loop as an element, and the
  limits count it one level deeper.
- B08: expansion error reports name the template that holds the failing
  node and, for libraries parsed with a source identifier, its line and
  column (craft_runtime requirement 12; conformance cases check `template`
  and `line`). Source locations carry line and column. Clearer messages
  for a self-call, a widget, builder or handler where a value is required
  (naming the function and argument, event, switch or setter), `let` after
  the bindings or inside a value, and a whole builder argument.
- Token start offsets after a comment are now right, so the position of a
  node after a comment is correct. As a consequence, a comment between the
  two colons of `::` (`ui:/*c*/:Card()`) is rejected: craft_libraries_and_names
  requirement 1 now says that nothing, not even a comment, may separate
  them (RFW-derived code accepted it before).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants