build method

  1. @override
String build(
  1. Context context,
  2. ParserResult? result
)
override

Implementation

@override
String build(Context context, ParserResult? result) {
  if (table.isEmpty) {
    throw ArgumentError.value(
        table, 'table', 'The map of tags must not be empty: $this');
  }

  context.refersToStateSource = true;
  final fast = result == null;
  final values = <String, String>{};
  final map = <int, List<String>>{};
  for (final tag in table.keys) {
    if (tag.isEmpty) {
      throw ArgumentError.value(
          table, 'table', 'The map of tags must not contain empty tags');
    }

    final value = table[tag];
    if (value == null) {
      throw ArgumentError.value(
          table, 'table', 'The map of tags must not contain null values');
    }

    final c = tag.codeUnitAt(0);
    var list = map[c];
    if (list == null) {
      list = [];
      map[c] = list;
    }

    list.add(tag);
  }

  final branches = <String, String>{};
  for (final c in map.keys) {
    final tags = map[c]!;
    tags.sort((x, y) => y.length.compareTo(x.length));
    final tests = <String, String>{};
    for (final tag in tags) {
      final length = tag.length;
      final escaped = helper.escapeString(tag);
      final id = table[tag];
      final values = {
        'id': helper.getAsCode(id),
        'length': '$length',
        'tag': escaped,
      };

      final isLong = tag.length > 1;
      final template = isLong ? _templateTestLong : _templateTestShort;
      final branch = render(template, values);
      final condition = isLong ? 'source.startsWith($escaped, pos)' : '';
      tests[condition] = branch;
    }

    final branch = helper.buildConditional(tests);
    branches['c == $c'] = branch;
  }

  values.addAll({
    'O1': helper.isNullableType<O1>() ? '$O1' : '$O1?',
    'tests': helper.buildConditional(branches),
    'tokenize':
        fast ? '' : tokenize.build(context, 'tokenize', ['v1', 'v2', 'v3']),
    'type': getResultType(),
  });
  return render2(fast, _templateFast, _template, values, [result]);
}