astFromJson function

Ast astFromJson(
  1. Map<Object?, Object?> json
)

The node of lang-core's JSON form (Ast.jsFields as JSON).

Implementation

Ast astFromJson(Map<Object?, Object?> json) {
  Ast child(Object? v) => astFromJson(v! as Map);
  List<Ast> children(Object? v) => [for (final e in v! as List) child(e)];
  return switch (json['k']) {
    'Str' => StrNode(json['v']! as String),
    'Num' => NumNode(json['v']! as num),
    'Bool' => BoolNode(json['v']! as bool),
    'Null' => const NullNode(),
    'Arr' => ArrNode(children(json['els'])),
    'Obj' => ObjNode([
      for (final e in json['entries']! as List)
        ((e as List)[0]! as String, child(e[1])),
    ]),
    'Comp' => CompNode(
      json['name']! as String,
      children(json['args']),
      mappedProps: json['mappedProps'] == null
          ? null
          : {
              for (final MapEntry(:key, :value)
                  in (json['mappedProps']! as Map).entries)
                '$key': child(value),
            },
      isAtCall: json['fn'] == true,
    ),
    'Ref' => RefNode(json['n']! as String),
    'StateRef' => StateRefNode(json['n']! as String),
    'RuntimeRef' => RuntimeRefNode(
      json['n']! as String,
      RuntimeRefType.values.byName(json['refType']! as String),
    ),
    'BinOp' => BinOpNode(
      json['op']! as String,
      child(json['left']),
      child(json['right']),
    ),
    'UnaryOp' => UnaryOpNode(json['op']! as String, child(json['operand'])),
    'Ternary' => TernaryNode(
      child(json['cond']),
      child(json['then']),
      child(json['else']),
    ),
    'Member' => MemberNode(child(json['obj']), json['field']! as String),
    'Index' => IndexNode(child(json['obj']), child(json['index'])),
    'Assign' => AssignNode(json['target']! as String, child(json['value'])),
    'Ph' => PlaceholderNode(json['n']! as String),
    final k => throw ArgumentError.value(k, 'k', 'not an AST node kind'),
  };
}