parse method

  1. @override
TreeSitterNode parse(
  1. String source
)
override

Implementation

@override
TreeSitterNode parse(String source) {
  final tokens = _SourceScanner(
    source,
    keywords: _keywords,
    includeKeywords: const {'export', 'from', 'import'},
    constantBuiltins: const {'Infinity', 'NaN', 'null', 'undefined'},
  ).scan();
  final classified = <TreeSitterNode>[];
  for (var index = 0; index < tokens.length; index++) {
    final token = tokens[index];
    var kind = token.kind;
    final previous = index == 0 ? null : tokens[index - 1];
    final next = index + 1 == tokens.length ? null : tokens[index + 1];
    if (kind == 'identifier') {
      if (previous != null && _declarationKeywords.contains(previous.text)) {
        kind = 'type_identifier';
      } else if (token.text.isNotEmpty &&
          token.text.codeUnitAt(0) >= 0x41 &&
          token.text.codeUnitAt(0) <= 0x5a) {
        kind = 'type_identifier';
      } else if (previous?.text == '.') {
        kind = 'property_identifier';
      } else if (next?.text == '(') {
        kind = 'function_identifier';
      }
    }
    classified.add(token.node(kind));
  }
  return TreeSitterNode(
    kind: 'program',
    startByte: 0,
    endByte: utf8.encode(source).length,
    startOffset: const TreeSitterUtf16Offset(0),
    endOffset: TreeSitterUtf16Offset(source.length),
    children: List.unmodifiable(classified),
  );
}