fastBuild function

Future<void> fastBuild(
  1. Context context,
  2. List<Named> parsers,
  3. String filename, {
  4. bool addErrorMessageProcessor = true,
  5. String? footer,
  6. bool format = true,
  7. String? header,
  8. String? partOf,
  9. Map<String, Named> publish = const {},
})

Implementation

Future<void> fastBuild(Context context, List<Named> parsers, String filename,
    {bool addErrorMessageProcessor = true,
    String? footer,
    bool format = true,
    String? header,
    String? partOf,
    Map<String, Named> publish = const {}}) async {
  for (var i = 0; i < 2; i++) {
    context.pass = i;
    context.globalDeclarations.clear();
    context.globalAllocator = context.globalAllocator.clone();
    for (final parser in parsers) {
      context.localAllocator = context.localAllocator.clone();
      final result = context.getResult(parser, true);
      parser.build(context, result);
    }
  }

  final declarations = context.globalDeclarations;
  for (final entry in publish.entries) {
    final code = _publish(entry.key, entry.value);
    declarations.insert(0, code);
  }

  if (addErrorMessageProcessor) {
    declarations.add(ParseRuntime.getErrorMessageProcessor());
  }

  declarations.addAll(ParseRuntime.getClasses());
  var code = declarations.join('\n\n');
  if (partOf != null) {
    code = 'part of \'$partOf\';\n\n' + code;
  }

  if (header != null) {
    code = header + code;
  }

  if (footer != null) {
    code += footer;
  }

  File(filename).writeAsStringSync(code);
  if (format) {
    final process =
        await Process.start(Platform.executable, ['format', filename]);
    process.stdout.transform(utf8.decoder).forEach(print);
    process.stderr.transform(utf8.decoder).forEach(print);
  }
}