GeneratedTreeSitterNode constructor

GeneratedTreeSitterNode({
  1. required int symbol,
  2. int? structuralSymbol,
  3. required String type,
  4. required int startByte,
  5. required int endByte,
  6. required TreeSitterTablePoint startPoint,
  7. required TreeSitterTablePoint endPoint,
  8. required bool named,
  9. required bool visible,
  10. required bool extra,
  11. int productionId = 0,
  12. int dynamicPrecedence = 0,
  13. bool missing = false,
  14. bool missingNameIsNamed = false,
  15. bool recoveryRepeat = false,
  16. int? unexpectedCharacter,
  17. List<GeneratedTreeSitterNode> children = const [],
  18. List<GeneratedTreeSitterField> fields = const [],
  19. List<GeneratedTreeSitterProductionField> productionFields = const [],
  20. int parseState = 0,
  21. int? firstLeafSymbol,
  22. int firstLeafParseState = 0,
  23. int paddingBytes = 0,
  24. TreeSitterTablePoint paddingExtent = const TreeSitterTablePoint(0, 0),
  25. int? sizeBytes,
  26. TreeSitterTablePoint? sizeExtent,
  27. int lookaheadBytes = 0,
  28. bool hasChanges = false,
  29. bool fragileLeft = false,
  30. bool fragileRight = false,
  31. bool dependsOnColumn = false,
  32. bool isKeyword = false,
  33. bool hasExternalTokens = false,
  34. List<int> externalScannerStateBefore = const <int>[],
  35. List<int> externalScannerStateAfter = const <int>[],
})

Implementation

GeneratedTreeSitterNode({
  required this.symbol,
  int? structuralSymbol,
  required this.type,
  required this.startByte,
  required this.endByte,
  required this.startPoint,
  required this.endPoint,
  required this.named,
  required this.visible,
  required this.extra,
  this.productionId = 0,
  this.dynamicPrecedence = 0,
  this.missing = false,
  this.missingNameIsNamed = false,
  bool recoveryRepeat = false,
  this.unexpectedCharacter,
  List<GeneratedTreeSitterNode> children = const [],
  List<GeneratedTreeSitterField> fields = const [],
  List<GeneratedTreeSitterProductionField> productionFields = const [],
  this.parseState = 0,
  this.firstLeafSymbol,
  this.firstLeafParseState = 0,
  this.paddingBytes = 0,
  this.paddingExtent = const TreeSitterTablePoint(0, 0),
  int? sizeBytes,
  TreeSitterTablePoint? sizeExtent,
  this.lookaheadBytes = 0,
  this.hasChanges = false,
  this.fragileLeft = false,
  this.fragileRight = false,
  this.dependsOnColumn = false,
  this.isKeyword = false,
  this.hasExternalTokens = false,
  List<int> externalScannerStateBefore = const <int>[],
  List<int> externalScannerStateAfter = const <int>[],
}) : structuralSymbol = structuralSymbol ?? symbol,
     _recoveryRepeat = recoveryRepeat,
     children = children.isEmpty
         ? const <GeneratedTreeSitterNode>[]
         : List<GeneratedTreeSitterNode>.unmodifiable(children),
     fields = fields.isEmpty
         ? const <GeneratedTreeSitterField>[]
         : List<GeneratedTreeSitterField>.unmodifiable(fields),
     productionFields = productionFields.isEmpty
         ? const <GeneratedTreeSitterProductionField>[]
         : List<GeneratedTreeSitterProductionField>.unmodifiable(
             productionFields,
           ),
     externalScannerStateBefore = _freezeScannerState(
       externalScannerStateBefore,
     ),
     externalScannerStateAfter = _freezeScannerState(
       externalScannerStateAfter,
     ),
     sizeBytes = sizeBytes ?? endByte - startByte,
     _sizeExtent = sizeExtent {
  var first = _mixHash(symbol, productionId);
  first = _mixHash(first, dynamicPrecedence);
  first = _mixHash(first, startByte);
  first = _mixHash(first, endByte);
  first = _mixHash(first, missing ? 1 : 0);
  first = _mixHash(first, recoveryRepeat ? 1 : 0);
  first = _mixHash(first, unexpectedCharacter ?? -2);
  var second = _mixHash2(symbol, productionId);
  second = _mixHash2(second, dynamicPrecedence);
  second = _mixHash2(second, startByte);
  second = _mixHash2(second, endByte);
  second = _mixHash2(second, missing ? 1 : 0);
  second = _mixHash2(second, recoveryRepeat ? 1 : 0);
  second = _mixHash2(second, unexpectedCharacter ?? -2);
  var subtreeNodeCount = visible ? 1 : 0;
  // Native `ts_node_has_error` is derived from subtree error cost. Missing
  // named and anonymous nodes therefore mark every ancestor as erroneous
  // even when there is no explicit ERROR node in the CST.
  var containsRecoveryError =
      missing || unexpectedCharacter != null || type == 'ERROR';
  var hasRecoveryCost =
      recoveryRepeat || missing || (type == 'ERROR' && children.isNotEmpty);
  var visibleDescendantCount = 0;
  var nodeErrorCost = 0;
  var acceptedNodeErrorCost = 0;
  for (final child in children) {
    first = _mixHash(first, child._shapeHash);
    second = _mixHash2(second, child._shapeHash2);
    subtreeNodeCount += child._subtreeNodeCount;
    containsRecoveryError = containsRecoveryError || child._containsError;
    hasRecoveryCost = hasRecoveryCost || child._hasErrorCost;
    visibleDescendantCount += child.visible
        ? 1
        : child._visibleDescendantCount;
    nodeErrorCost += child._nodeErrorCost;
    acceptedNodeErrorCost += child._acceptedNodeErrorCost;
  }
  for (final field in fields) {
    first = _mixHash(first, field.name.hashCode);
    first = _mixHash(first, field.inherited ? 1 : 0);
    second = _mixHash2(second, field.name.hashCode);
    second = _mixHash2(second, field.inherited ? 1 : 0);
  }
  for (final field in productionFields) {
    first = _mixHash(first, field.name.hashCode);
    first = _mixHash(first, field.childIndex);
    first = _mixHash(first, field.inherited ? 1 : 0);
    second = _mixHash2(second, field.name.hashCode);
    second = _mixHash2(second, field.childIndex);
    second = _mixHash2(second, field.inherited ? 1 : 0);
  }
  _shapeHash = first;
  _shapeHash2 = second;
  _subtreeNodeCount = subtreeNodeCount;
  _containsError = containsRecoveryError;
  _hasErrorCost = hasRecoveryCost;
  _visibleDescendantCount = visibleDescendantCount;
  if (recoveryRepeat) {
    nodeErrorCost = 0;
    acceptedNodeErrorCost = 0;
  } else if (missing) {
    nodeErrorCost = 610;
    acceptedNodeErrorCost = 610;
  } else if (type == 'ERROR' && children.isNotEmpty) {
    for (final child in children) {
      if (child._recoveryRepeat) {
        acceptedNodeErrorCost += 100 * child._visibleDescendantCount;
      } else if (!child.extra &&
          !(child.type == 'ERROR' && child.children.isEmpty)) {
        acceptedNodeErrorCost += child.visible
            ? 100
            : 100 * child._visibleDescendantCount;
      }
      if (!child.extra &&
          !(child.type == 'ERROR' && child.children.isEmpty)) {
        nodeErrorCost += child.visible
            ? 100
            : 100 * child._visibleDescendantCount;
      }
    }
    final recoveryCost =
        500 + endByte - startByte + (endPoint.row - startPoint.row) * 30;
    nodeErrorCost += recoveryCost;
    acceptedNodeErrorCost += recoveryCost;
  }
  _nodeErrorCost = nodeErrorCost;
  _acceptedNodeErrorCost = acceptedNodeErrorCost;
}