docs

您正在閱讀 v0.1.0 的文件。Nabi 0.1.0 已停止支援。 最新版本:v0.1.3 →

本頁尚未翻譯,以下為英文原文。

#JSON parser

本頁內容

JSON values as an enum, a character-level recursive-descent parser that reports error positions with throws, and an indenting pretty printer.

#Code

#Walkthrough

  • A JSON value is the six-variant enum Json. Arrays and objects hold themselves recursively as List<Json> and Dict<str, Json>; a Dict keeps key order (Sum types, Structs, enums and traits).
  • The parser is a struct holding the position pos, and the methods that advance it take ref self. When such a method returns (or throws), the updated self is written back to the caller (4.7.5 method receivers).
  • End of input is checked with let c = self.peek() else { throw ...; };. peek() returns char | none, so let-else leaves on none, and below it c is a char (3.16 let-else).
  • Every error is a single ParseError carrying a message and a position. Errors thrown by the standard library, such as a failed number parse, are turned into positioned errors with catch _ { throw ParseError { ... }; } (Errors).
  • Output is built by the recursive function pretty, which returns a string. For arrays and objects each element is rendered first, then block adds indentation and commas; string values use {s:?} (Debug) for quotes and escapes (Strings).

Found a mistake? Edit this page on GitHub. Last updated