<commit_before>// Copyright (c) 2012, the Dart project authors.  Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

class ValidatorListener implements DiagnosticListener {
  final SourceFile sourceFile;

  ValidatorListener(this.sourceFile);

  void cancel([String reason, node, token, instruction, element]) {
    assert(token !== null);
    SourceSpan.withOffsets(token, token, (beginOffset, endOffset) {
        String errorMessage =
            sourceFile.getLocationMessage(reason, beginOffset, endOffset, true,
                                          (s) => s /* no color */);
        print(errorMessage);
    });
    throw new CompilerCancelledException(reason);
  }

  void log(message) {}
}

/**
 * Checks result's of [Node] unparse.
 */
class UnparseValidator extends CompilerTask {
  final bool validateUnparse;

  String get name() => "Unparse validator";

  UnparseValidator(Compiler compiler, this.validateUnparse) : super(compiler);

  void check(Element element) {
    if (!validateUnparse) return;

    // TODO(antonm): consider supporting other kinds of elements.
    if (element is! PartialFunctionElement) return;

    PartialFunctionElement originalFunction = element;
    FunctionExpression originalNode = originalFunction.parseNode(compiler);
    String unparsed = originalNode.unparse();

    Token newTokens = new StringScanner(unparsed).tokenize();

    // Find the getOrSet token.
    // TODO(ahe): This is to frigging complicated. Simplify it.
    Parser parser = new Parser(new Listener());
    Token getOrSet =
        parser.findGetOrSet(parser.parseModifiers(newTokens));

    // TODO(ahe): This is also too frigging complicated.
    Script originalScript = element.getCompilationUnit().script;
    String name = SourceSpan.withOffsets(
        originalFunction.beginToken, originalFunction.endToken,
        (beginOffset, endOffset) =>
            'synthesized:${originalScript.name}#$beginOffset:$endOffset');
    SourceFile synthesizedSourceFile = new SourceFile(name, unparsed);
    Script synthesizedScript =
        new Script(originalScript.uri, synthesizedSourceFile);
    LibraryElement lib = new LibraryElement(synthesizedScript);
    lib.canUseNative = element.getLibrary().canUseNative;
    NodeListener listener =
        new NodeListener(new ValidatorListener(synthesizedSourceFile), lib);
    parser = new Parser(listener);
    parser.parseFunction(newTokens, getOrSet);
    FunctionExpression newNode = listener.popNode();
    // TODO(antonm): add Node comparison.
  }
}<commit_msg>Place more guards for the cases which should be covered.<commit_after>// Copyright (c) 2012, the Dart project authors.  Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

class ValidatorListener implements DiagnosticListener {
  final SourceFile sourceFile;

  ValidatorListener(this.sourceFile);

  void cancel([String reason, node, token, instruction, element]) {
    assert(token !== null);
    SourceSpan.withOffsets(token, token, (beginOffset, endOffset) {
        String errorMessage =
            sourceFile.getLocationMessage(reason, beginOffset, endOffset, true,
                                          (s) => s /* no color */);
        print(errorMessage);
    });
    throw new CompilerCancelledException(reason);
  }

  void log(message) {}
}

/**
 * Checks result's of [Node] unparse.
 */
class UnparseValidator extends CompilerTask {
  final bool validateUnparse;

  String get name() => "Unparse validator";

  UnparseValidator(Compiler compiler, this.validateUnparse) : super(compiler);

  void checkFunction(PartialFunctionElement originalFunction) {
    FunctionExpression originalNode = originalFunction.parseNode(compiler);
    String unparsed = originalNode.unparse();

    Token newTokens = new StringScanner(unparsed).tokenize();

    // Find the getOrSet token.
    // TODO(ahe): This is to frigging complicated. Simplify it.
    Parser parser = new Parser(new Listener());
    Token getOrSet =
        parser.findGetOrSet(parser.parseModifiers(newTokens));

    // TODO(ahe): This is also too frigging complicated.
    Script originalScript = originalFunction.getCompilationUnit().script;
    String name = SourceSpan.withOffsets(
        originalFunction.beginToken, originalFunction.endToken,
        (beginOffset, endOffset) =>
            'synthesized:${originalScript.name}#$beginOffset:$endOffset');
    SourceFile synthesizedSourceFile = new SourceFile(name, unparsed);
    Script synthesizedScript =
        new Script(originalScript.uri, synthesizedSourceFile);
    LibraryElement lib = new LibraryElement(synthesizedScript);
    lib.canUseNative = originalFunction.getLibrary().canUseNative;
    NodeListener listener =
        new NodeListener(new ValidatorListener(synthesizedSourceFile), lib);
    parser = new Parser(listener);
    parser.parseFunction(newTokens, getOrSet);
    FunctionExpression newNode = listener.popNode();
    // TODO(antonm): add Node comparison.
  }

  void check(Element element) {
    if (!validateUnparse) return;

    if (element is PartialFunctionElement) {
      checkFunction(element);
    } else if (element.isGenerativeConstructor()) {
      assert(element is FunctionElement);
      // Generative constructors parse to very special function expressions.
      // Handle them when classes are properly handled.
    } else if (element.isField()) {
      assert(element is VariableElement);
      // Fields are just names with possible initialization expressions.
      // Nothing to care about for now.
    } else {
      compiler.cancel('Cannot handle $element', element: element);
    }
  }
}
{'fraction_non_alphanumeric': 0.09415194649288287, 'fraction_numerical': 0.0013719773623735209, 'mean_word_length': 3.647011952191235, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5778962', 'n_tokens_mistral': 1546, 'n_tokens_neox': 1482, 'n_words': 445}