generate method

String generate()

Generates parser source code and returns that code.

Implementation

String generate() {
  final grammarGenerator = GrammarGenerator(
    inputType: options.inputType,
    source: source,
  );
  final grammar = grammarGenerator.generate();
  final libraryCode = StringBuffer();
  final globals = grammar.globals;
  final members = grammar.members;
  if (globals != null) {
    _writeText(libraryCode, globals);
    libraryCode.writeln();
  }

  final classCode = StringBuffer();
  classCode.writeln('// dart format off');
  classCode.writeln('class ${options.name} {');
  if (members != null) {
    final lines = CodeBuilder.unindentText(members);
    _writeText(classCode, lines.join('\n'), '  ');
    classCode.writeln();
  }

  final productions = grammar.productions;
  final methods = <String>[];
  for (final production in productions) {
    final productionGenerator = ProductionGenerator(
      options: options,
      production: production,
    );
    final method = productionGenerator.generate();
    methods.add(method);
  }

  final methodsCode = CodeBuilder.indentText(
    methods.join('\n\n'),
    removeEmptyLines: false,
  );

  classCode.writeln(methodsCode);
  classCode.writeln('}');
  classCode.writeln('// dart format on');
  libraryCode.writeln(classCode);

  final runtimeGenerator = RuntimeGenerator(inputType: options.inputType);
  final runtime = runtimeGenerator.generate();
  libraryCode.writeln(runtime);
  return '$libraryCode';
}