generate: 在构建字符集时进行二次扫描

作者: jkbz64创建于 2026年9月30日更新于 2026年9月30日
标签parser-generationperf
  •    // Regex character classes provide sorted, disjoint ranges. Appending
  •    // them should not rescan all of the ranges already in the set.
  •    if self.ranges.last().is_none_or(|range| range.end < start) {
  •        let index = self.ranges.len();
  •        self.ranges.push(start..end);
  •        return index;
  •    }
       while i < self.ranges.len() {
           let range = &mut self.ranges[i];
           if range.start > end {
               i += 1;
  •            // If the range is not in the set, add it and return its index.
  •            if range.end < start {
  •                set.add_range(
  •                    char::from_u32(range.start).unwrap(),
  •                    char::from_u32(range.end - 1).unwrap(),
  •                );
  •                return set.ranges.len();
  •            }
  •            // If the range is in the set, check if it is contained in the
  •            // current range. If so, update the current range to include the
  •            // new range.
  •            if range.start <= start && range.end >= end {
  •                set.ranges[i] = range;
  •                return i;
  •            }
  •            // If the range is not contained in the current range, add it to
  •            // the set and return its index.
  •            set.add_range(
  •                char::from_u32(range.start).unwrap(),
  •                char::from_u32(range.end - 1).unwrap(),
  •            );
  •            return set.ranges.len();
  •        }
  •    }
  •    // If the range is not in the set, add it and return its index.
  •    if range.end < start {
  •        set.add_range(
  •            char::from_u32(range.start).unwrap(),
  •            char::from_u32(range.end - 1).unwrap(),
  •        );
  •        return set.ranges.len();
  •    }
  •    // If the range is in the set, check if it is contained in the current
  •    // range. If so, update the current range to include the new range.
  •    if range.start <= start && range.end >= end {
  •        set.ranges[i] = range;
  •        return i;
  •    }
  •    // If the range is not contained in the current range, add it to the set
  •    // and return its index.
  •    set.add_range(
  •        char::from_u32(range.start).unwrap(),
  •        char::from_u32(range.end - 1).unwrap(),
  •    );
  •    return set.ranges.len();
  • }

+} +

内容来源: tree-sitter/tree-sitter