parseNode static method

Message? parseNode(
  1. Node node,
  2. List<String> arguments, [
  3. String? name,
  4. bool addId = false,
])

Implementation

static Message? parseNode(
  Node node,
  List<String> arguments, [
  String? name,
  bool addId = false,
]) {
  final id = addId ? name : null;
  final submessages = <Message>[];
  final placeholders = <({int argIndex, int afterStringMessage})>[];
  for (var child in node.children) {
    switch (child.type) {
      case ST.string:
        submessages.add(StringMessage(child.value!, id: id));
        break;
      case ST.pluralExpr:
        submessages.add(PluralParser().parse(child, arguments, addId, name));
        break;
      case ST.placeholderExpr:
        final identifier = child.children
            .firstWhere((element) => element.type == ST.identifier)
            .value!;
        if (!arguments.contains(identifier)) {
          arguments.add(identifier);
        }
        placeholders.add((
          argIndex: arguments.indexOf(identifier),
          afterStringMessage: submessages.length,
        ));
        break;
      case ST.selectExpr:
        submessages.add(SelectParser().parse(child, arguments, addId, name));
        break;
      default:
        break;
    }
  }
  if (submessages.isEmpty && placeholders.isEmpty) {
    return null;
  } else if (submessages.length == 1 && placeholders.isEmpty) {
    return submessages.first;
  } else if (submessages.every((message) => message is StringMessage)) {
    return combineStringsAndPlaceholders(
      submessages.whereType<StringMessage>().toList(),
      id,
      placeholders,
    );
  } else {
    return CombinedMessage(id, submessages);
  }
}