2019-01-08 21:03:51 -08:00
|
|
|
use super::util;
|
2021-06-09 12:32:22 -04:00
|
|
|
use anyhow::{anyhow, Context, Result};
|
2019-01-07 17:57:36 -08:00
|
|
|
use std::io::{self, Write};
|
2019-01-08 21:03:51 -08:00
|
|
|
use std::path::Path;
|
2020-10-22 15:55:51 -07:00
|
|
|
use std::sync::atomic::AtomicUsize;
|
2023-09-28 13:05:10 +03:00
|
|
|
use std::time::{Duration, Instant};
|
2020-10-22 15:55:51 -07:00
|
|
|
use std::{fmt, fs, usize};
|
2023-07-03 20:59:01 -04:00
|
|
|
use tree_sitter::{ffi, InputEdit, Language, LogType, Parser, Point, Tree};
|
2019-04-08 09:21:03 -07:00
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
pub struct Edit {
|
|
|
|
|
pub position: usize,
|
|
|
|
|
pub deleted_length: usize,
|
|
|
|
|
pub inserted_text: Vec<u8>,
|
|
|
|
|
}
|
2019-01-07 17:57:36 -08:00
|
|
|
|
2020-09-29 12:34:25 -04:00
|
|
|
#[derive(Debug, Default)]
|
|
|
|
|
pub struct Stats {
|
2020-09-30 09:28:58 -04:00
|
|
|
pub successful_parses: usize,
|
|
|
|
|
pub total_parses: usize,
|
2023-09-28 13:05:10 +03:00
|
|
|
pub total_bytes: usize,
|
|
|
|
|
pub total_duration: Duration,
|
2020-09-29 12:34:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl fmt::Display for Stats {
|
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2023-09-28 13:05:10 +03:00
|
|
|
let duration_us = self.total_duration.as_micros();
|
|
|
|
|
writeln!(
|
|
|
|
|
f,
|
|
|
|
|
"Total parses: {}; successful parses: {}; failed parses: {}; success percentage: {:.2}%; average speed: {} bytes/ms",
|
|
|
|
|
self.total_parses,
|
|
|
|
|
self.successful_parses,
|
|
|
|
|
self.total_parses - self.successful_parses,
|
|
|
|
|
((self.successful_parses as f64) / (self.total_parses as f64)) * 100.0,
|
|
|
|
|
if duration_us != 0 {
|
|
|
|
|
((self.total_bytes as u128) * 1_000) / duration_us
|
|
|
|
|
} else {
|
|
|
|
|
0
|
|
|
|
|
}
|
|
|
|
|
)
|
2020-09-29 12:34:25 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-13 12:33:34 -08:00
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
|
pub enum ParseOutput {
|
|
|
|
|
Normal,
|
|
|
|
|
Quiet,
|
|
|
|
|
Xml,
|
|
|
|
|
Dot,
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-03 20:59:01 -04:00
|
|
|
pub struct ParseFileOptions<'a> {
|
|
|
|
|
pub language: Language,
|
|
|
|
|
pub path: &'a Path,
|
|
|
|
|
pub edits: &'a [&'a str],
|
|
|
|
|
pub max_path_length: usize,
|
|
|
|
|
pub output: ParseOutput,
|
|
|
|
|
pub print_time: bool,
|
|
|
|
|
pub timeout: u64,
|
|
|
|
|
pub debug: bool,
|
|
|
|
|
pub debug_graph: bool,
|
|
|
|
|
pub cancellation_flag: Option<&'a AtomicUsize>,
|
|
|
|
|
pub encoding: Option<u32>,
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-28 13:05:10 +03:00
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
|
pub struct ParseResult {
|
|
|
|
|
pub successful: bool,
|
|
|
|
|
pub bytes: usize,
|
|
|
|
|
pub duration: Option<Duration>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn parse_file_at_path(parser: &mut Parser, opts: &ParseFileOptions) -> Result<ParseResult> {
|
2019-01-17 17:15:10 -08:00
|
|
|
let mut _log_session = None;
|
2023-11-27 15:50:08 -08:00
|
|
|
parser.set_language(&opts.language)?;
|
2023-07-03 20:59:01 -04:00
|
|
|
let mut source_code = fs::read(opts.path)
|
|
|
|
|
.with_context(|| format!("Error reading source file {:?}", opts.path))?;
|
2019-01-08 21:03:51 -08:00
|
|
|
|
2019-03-21 10:56:32 -07:00
|
|
|
// If the `--cancel` flag was passed, then cancel the parse
|
|
|
|
|
// when the user types a newline.
|
2023-07-03 20:59:01 -04:00
|
|
|
unsafe { parser.set_cancellation_flag(opts.cancellation_flag) };
|
2019-03-21 10:56:32 -07:00
|
|
|
|
|
|
|
|
// Set a timeout based on the `--time` flag.
|
2023-07-03 20:59:01 -04:00
|
|
|
parser.set_timeout_micros(opts.timeout);
|
2019-03-21 10:56:32 -07:00
|
|
|
|
|
|
|
|
// Render an HTML graph if `--debug-graph` was passed
|
2023-07-03 20:59:01 -04:00
|
|
|
if opts.debug_graph {
|
2022-09-06 22:41:52 -07:00
|
|
|
_log_session = Some(util::log_graphs(parser, "log.html")?);
|
2019-03-21 10:56:32 -07:00
|
|
|
}
|
|
|
|
|
// Log to stderr if `--debug` was passed
|
2023-07-03 20:59:01 -04:00
|
|
|
else if opts.debug {
|
2019-01-08 21:03:51 -08:00
|
|
|
parser.set_logger(Some(Box::new(|log_type, message| {
|
|
|
|
|
if log_type == LogType::Lex {
|
2024-02-04 01:30:33 -05:00
|
|
|
io::stderr().write_all(b" ").unwrap();
|
2019-01-08 21:03:51 -08:00
|
|
|
}
|
2024-02-04 01:30:33 -05:00
|
|
|
writeln!(&mut io::stderr(), "{message}").unwrap();
|
2019-01-08 21:03:51 -08:00
|
|
|
})));
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-17 17:15:10 -08:00
|
|
|
let time = Instant::now();
|
2023-07-03 20:59:01 -04:00
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
|
fn is_utf16_bom(bom_bytes: &[u8]) -> bool {
|
|
|
|
|
bom_bytes == [0xFF, 0xFE] || bom_bytes == [0xFE, 0xFF]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let tree = match opts.encoding {
|
2023-09-03 06:47:27 +03:00
|
|
|
Some(encoding) if encoding == ffi::TSInputEncodingUTF16 => {
|
2023-07-03 20:59:01 -04:00
|
|
|
let source_code_utf16 = source_code
|
|
|
|
|
.chunks_exact(2)
|
|
|
|
|
.map(|chunk| u16::from_le_bytes([chunk[0], chunk[1]]))
|
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
parser.parse_utf16(&source_code_utf16, None)
|
|
|
|
|
}
|
2023-07-27 03:32:55 -04:00
|
|
|
None if source_code.len() >= 2 && is_utf16_bom(&source_code[0..2]) => {
|
2023-07-03 20:59:01 -04:00
|
|
|
let source_code_utf16 = source_code
|
|
|
|
|
.chunks_exact(2)
|
|
|
|
|
.map(|chunk| u16::from_le_bytes([chunk[0], chunk[1]]))
|
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
parser.parse_utf16(&source_code_utf16, None)
|
|
|
|
|
}
|
|
|
|
|
_ => parser.parse(&source_code, None),
|
|
|
|
|
};
|
2019-01-07 17:57:36 -08:00
|
|
|
|
2023-10-31 15:59:37 -07:00
|
|
|
parser.stop_printing_dot_graphs();
|
|
|
|
|
|
2019-01-07 17:57:36 -08:00
|
|
|
let stdout = io::stdout();
|
|
|
|
|
let mut stdout = stdout.lock();
|
2019-01-17 17:15:10 -08:00
|
|
|
|
2019-04-08 09:21:03 -07:00
|
|
|
if let Some(mut tree) = tree {
|
2023-07-03 20:59:01 -04:00
|
|
|
if opts.debug_graph && !opts.edits.is_empty() {
|
2020-06-04 13:40:04 -07:00
|
|
|
println!("BEFORE:\n{}", String::from_utf8_lossy(&source_code));
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-03 20:59:01 -04:00
|
|
|
for (i, edit) in opts.edits.iter().enumerate() {
|
2019-04-08 09:21:03 -07:00
|
|
|
let edit = parse_edit_flag(&source_code, edit)?;
|
2023-10-04 11:21:48 +03:00
|
|
|
perform_edit(&mut tree, &mut source_code, &edit)?;
|
2019-04-08 09:21:03 -07:00
|
|
|
tree = parser.parse(&source_code, Some(&tree)).unwrap();
|
2020-06-04 13:40:04 -07:00
|
|
|
|
2023-07-03 20:59:01 -04:00
|
|
|
if opts.debug_graph {
|
2024-02-04 01:30:33 -05:00
|
|
|
println!("AFTER {i}:\n{}", String::from_utf8_lossy(&source_code));
|
2020-06-04 13:40:04 -07:00
|
|
|
}
|
2019-04-08 09:21:03 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let duration = time.elapsed();
|
2023-09-28 13:05:10 +03:00
|
|
|
let duration_ms = duration.as_micros() as f64 / 1e3;
|
2019-03-14 11:52:50 -07:00
|
|
|
let mut cursor = tree.walk();
|
|
|
|
|
|
2023-07-03 20:59:01 -04:00
|
|
|
if matches!(opts.output, ParseOutput::Normal) {
|
2019-03-14 11:52:50 -07:00
|
|
|
let mut needs_newline = false;
|
|
|
|
|
let mut indent_level = 0;
|
|
|
|
|
let mut did_visit_children = false;
|
|
|
|
|
loop {
|
|
|
|
|
let node = cursor.node();
|
|
|
|
|
let is_named = node.is_named();
|
|
|
|
|
if did_visit_children {
|
|
|
|
|
if is_named {
|
2024-02-04 01:30:33 -05:00
|
|
|
stdout.write_all(b")")?;
|
2019-03-14 11:52:50 -07:00
|
|
|
needs_newline = true;
|
2019-01-17 17:15:10 -08:00
|
|
|
}
|
2019-03-14 11:52:50 -07:00
|
|
|
if cursor.goto_next_sibling() {
|
|
|
|
|
did_visit_children = false;
|
|
|
|
|
} else if cursor.goto_parent() {
|
|
|
|
|
did_visit_children = true;
|
|
|
|
|
indent_level -= 1;
|
|
|
|
|
} else {
|
|
|
|
|
break;
|
2019-01-17 17:15:10 -08:00
|
|
|
}
|
|
|
|
|
} else {
|
2019-03-14 11:52:50 -07:00
|
|
|
if is_named {
|
|
|
|
|
if needs_newline {
|
2024-02-04 01:30:33 -05:00
|
|
|
stdout.write_all(b"\n")?;
|
2019-03-14 11:52:50 -07:00
|
|
|
}
|
|
|
|
|
for _ in 0..indent_level {
|
2024-02-04 01:30:33 -05:00
|
|
|
stdout.write_all(b" ")?;
|
2019-03-14 11:52:50 -07:00
|
|
|
}
|
|
|
|
|
let start = node.start_position();
|
|
|
|
|
let end = node.end_position();
|
2019-03-26 11:58:21 -07:00
|
|
|
if let Some(field_name) = cursor.field_name() {
|
2024-02-04 01:30:33 -05:00
|
|
|
write!(&mut stdout, "{field_name}: ")?;
|
2019-03-26 11:58:21 -07:00
|
|
|
}
|
2019-03-14 11:52:50 -07:00
|
|
|
write!(
|
|
|
|
|
&mut stdout,
|
|
|
|
|
"({} [{}, {}] - [{}, {}]",
|
|
|
|
|
node.kind(),
|
|
|
|
|
start.row,
|
|
|
|
|
start.column,
|
|
|
|
|
end.row,
|
|
|
|
|
end.column
|
|
|
|
|
)?;
|
|
|
|
|
needs_newline = true;
|
2019-01-17 17:15:10 -08:00
|
|
|
}
|
2019-03-14 11:52:50 -07:00
|
|
|
if cursor.goto_first_child() {
|
|
|
|
|
did_visit_children = false;
|
|
|
|
|
indent_level += 1;
|
|
|
|
|
} else {
|
|
|
|
|
did_visit_children = true;
|
2019-02-07 12:29:20 -08:00
|
|
|
}
|
2019-01-07 17:57:36 -08:00
|
|
|
}
|
2019-03-14 11:52:50 -07:00
|
|
|
}
|
|
|
|
|
cursor.reset(tree.root_node());
|
2024-02-04 01:30:33 -05:00
|
|
|
println!();
|
2021-01-04 22:07:38 +00:00
|
|
|
}
|
|
|
|
|
|
2023-07-03 20:59:01 -04:00
|
|
|
if matches!(opts.output, ParseOutput::Xml) {
|
2021-01-04 22:07:38 +00:00
|
|
|
let mut needs_newline = false;
|
|
|
|
|
let mut indent_level = 0;
|
|
|
|
|
let mut did_visit_children = false;
|
|
|
|
|
let mut tags: Vec<&str> = Vec::new();
|
|
|
|
|
loop {
|
|
|
|
|
let node = cursor.node();
|
|
|
|
|
let is_named = node.is_named();
|
|
|
|
|
if did_visit_children {
|
|
|
|
|
if is_named {
|
|
|
|
|
let tag = tags.pop();
|
2024-02-04 01:30:33 -05:00
|
|
|
writeln!(&mut stdout, "</{}>", tag.expect("there is a tag"))?;
|
2021-01-04 22:07:38 +00:00
|
|
|
needs_newline = true;
|
|
|
|
|
}
|
|
|
|
|
if cursor.goto_next_sibling() {
|
|
|
|
|
did_visit_children = false;
|
|
|
|
|
} else if cursor.goto_parent() {
|
|
|
|
|
did_visit_children = true;
|
|
|
|
|
indent_level -= 1;
|
|
|
|
|
} else {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if is_named {
|
|
|
|
|
if needs_newline {
|
2024-02-04 01:30:33 -05:00
|
|
|
stdout.write_all(b"\n")?;
|
2021-01-04 22:07:38 +00:00
|
|
|
}
|
|
|
|
|
for _ in 0..indent_level {
|
2024-02-04 01:30:33 -05:00
|
|
|
stdout.write_all(b" ")?;
|
2021-01-04 22:07:38 +00:00
|
|
|
}
|
|
|
|
|
write!(&mut stdout, "<{}", node.kind())?;
|
|
|
|
|
if let Some(field_name) = cursor.field_name() {
|
2024-02-04 01:30:33 -05:00
|
|
|
write!(&mut stdout, " type=\"{field_name}\"")?;
|
2021-01-04 22:07:38 +00:00
|
|
|
}
|
|
|
|
|
write!(&mut stdout, ">")?;
|
|
|
|
|
tags.push(node.kind());
|
|
|
|
|
needs_newline = true;
|
|
|
|
|
}
|
|
|
|
|
if cursor.goto_first_child() {
|
|
|
|
|
did_visit_children = false;
|
|
|
|
|
indent_level += 1;
|
|
|
|
|
} else {
|
|
|
|
|
did_visit_children = true;
|
|
|
|
|
let start = node.start_byte();
|
|
|
|
|
let end = node.end_byte();
|
2021-05-16 17:55:58 +03:00
|
|
|
let value =
|
|
|
|
|
std::str::from_utf8(&source_code[start..end]).expect("has a string");
|
2021-01-04 22:07:38 +00:00
|
|
|
write!(&mut stdout, "{}", html_escape::encode_text(value))?;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
cursor.reset(tree.root_node());
|
2024-02-04 01:30:33 -05:00
|
|
|
println!();
|
2019-03-14 11:52:50 -07:00
|
|
|
}
|
|
|
|
|
|
2023-07-03 20:59:01 -04:00
|
|
|
if matches!(opts.output, ParseOutput::Dot) {
|
2023-02-13 12:33:34 -08:00
|
|
|
util::print_tree_graph(&tree, "log.html").unwrap();
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-14 11:52:50 -07:00
|
|
|
let mut first_error = None;
|
|
|
|
|
loop {
|
|
|
|
|
let node = cursor.node();
|
|
|
|
|
if node.has_error() {
|
|
|
|
|
if node.is_error() || node.is_missing() {
|
|
|
|
|
first_error = Some(node);
|
|
|
|
|
break;
|
2024-02-04 01:30:33 -05:00
|
|
|
} else if !cursor.goto_first_child() {
|
|
|
|
|
break;
|
2019-03-14 11:52:50 -07:00
|
|
|
}
|
|
|
|
|
} else if !cursor.goto_next_sibling() {
|
2019-04-08 09:21:03 -07:00
|
|
|
break;
|
2019-01-07 17:57:36 -08:00
|
|
|
}
|
2019-01-17 17:15:10 -08:00
|
|
|
}
|
|
|
|
|
|
2023-07-03 20:59:01 -04:00
|
|
|
if first_error.is_some() || opts.print_time {
|
2019-03-14 11:52:50 -07:00
|
|
|
write!(
|
|
|
|
|
&mut stdout,
|
2023-09-28 13:05:10 +03:00
|
|
|
"{:width$}\t{duration_ms:>7.2} ms\t{:>6} bytes/ms",
|
2023-07-03 20:59:01 -04:00
|
|
|
opts.path.to_str().unwrap(),
|
2023-09-28 13:05:10 +03:00
|
|
|
(source_code.len() as u128 * 1_000_000) / duration.as_nanos(),
|
2023-07-03 20:59:01 -04:00
|
|
|
width = opts.max_path_length
|
2019-03-14 11:52:50 -07:00
|
|
|
)?;
|
|
|
|
|
if let Some(node) = first_error {
|
|
|
|
|
let start = node.start_position();
|
|
|
|
|
let end = node.end_position();
|
2019-05-15 16:25:53 -07:00
|
|
|
write!(&mut stdout, "\t(")?;
|
|
|
|
|
if node.is_missing() {
|
|
|
|
|
if node.is_named() {
|
|
|
|
|
write!(&mut stdout, "MISSING {}", node.kind())?;
|
|
|
|
|
} else {
|
|
|
|
|
write!(
|
|
|
|
|
&mut stdout,
|
|
|
|
|
"MISSING \"{}\"",
|
2024-02-04 01:30:33 -05:00
|
|
|
node.kind().replace('\n', "\\n")
|
2019-05-15 16:25:53 -07:00
|
|
|
)?;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
write!(&mut stdout, "{}", node.kind())?;
|
|
|
|
|
}
|
2019-03-14 11:52:50 -07:00
|
|
|
write!(
|
|
|
|
|
&mut stdout,
|
2019-05-15 16:25:53 -07:00
|
|
|
" [{}, {}] - [{}, {}])",
|
|
|
|
|
start.row, start.column, end.row, end.column
|
2019-03-14 11:52:50 -07:00
|
|
|
)?;
|
2019-01-17 17:15:10 -08:00
|
|
|
}
|
2024-02-04 01:30:33 -05:00
|
|
|
writeln!(&mut stdout)?;
|
2019-01-17 17:15:10 -08:00
|
|
|
}
|
|
|
|
|
|
2023-09-28 13:05:10 +03:00
|
|
|
return Ok(ParseResult {
|
|
|
|
|
successful: first_error.is_none(),
|
|
|
|
|
bytes: source_code.len(),
|
|
|
|
|
duration: Some(duration),
|
|
|
|
|
});
|
2023-07-03 20:59:01 -04:00
|
|
|
} else if opts.print_time {
|
2019-04-08 09:21:03 -07:00
|
|
|
let duration = time.elapsed();
|
2023-09-28 13:05:10 +03:00
|
|
|
let duration_ms = duration.as_micros() as f64 / 1e3;
|
2019-03-14 11:52:50 -07:00
|
|
|
writeln!(
|
2019-01-17 17:15:10 -08:00
|
|
|
&mut stdout,
|
2023-09-28 13:05:10 +03:00
|
|
|
"{:width$}\t{duration_ms:>7.2} ms\t(timed out)",
|
2023-07-03 20:59:01 -04:00
|
|
|
opts.path.to_str().unwrap(),
|
|
|
|
|
width = opts.max_path_length
|
2019-01-17 17:15:10 -08:00
|
|
|
)?;
|
2019-01-07 17:57:36 -08:00
|
|
|
}
|
|
|
|
|
|
2023-09-28 13:05:10 +03:00
|
|
|
Ok(ParseResult {
|
|
|
|
|
successful: false,
|
|
|
|
|
bytes: source_code.len(),
|
|
|
|
|
duration: None,
|
|
|
|
|
})
|
2019-01-07 17:57:36 -08:00
|
|
|
}
|
2019-04-08 09:21:03 -07:00
|
|
|
|
2023-10-04 11:21:48 +03:00
|
|
|
pub fn perform_edit(tree: &mut Tree, input: &mut Vec<u8>, edit: &Edit) -> Result<InputEdit> {
|
2019-04-08 09:21:03 -07:00
|
|
|
let start_byte = edit.position;
|
|
|
|
|
let old_end_byte = edit.position + edit.deleted_length;
|
|
|
|
|
let new_end_byte = edit.position + edit.inserted_text.len();
|
2023-10-04 11:21:48 +03:00
|
|
|
let start_position = position_for_offset(input, start_byte)?;
|
|
|
|
|
let old_end_position = position_for_offset(input, old_end_byte)?;
|
2024-02-04 01:30:33 -05:00
|
|
|
input.splice(start_byte..old_end_byte, edit.inserted_text.iter().copied());
|
2023-10-04 11:21:48 +03:00
|
|
|
let new_end_position = position_for_offset(input, new_end_byte)?;
|
2019-04-08 09:21:03 -07:00
|
|
|
let edit = InputEdit {
|
|
|
|
|
start_byte,
|
|
|
|
|
old_end_byte,
|
|
|
|
|
new_end_byte,
|
|
|
|
|
start_position,
|
|
|
|
|
old_end_position,
|
|
|
|
|
new_end_position,
|
|
|
|
|
};
|
|
|
|
|
tree.edit(&edit);
|
2023-10-04 11:21:48 +03:00
|
|
|
Ok(edit)
|
2019-04-08 09:21:03 -07:00
|
|
|
}
|
|
|
|
|
|
2024-02-04 01:30:33 -05:00
|
|
|
fn parse_edit_flag(source_code: &[u8], flag: &str) -> Result<Edit> {
|
2019-04-08 09:21:03 -07:00
|
|
|
let error = || {
|
2021-06-09 12:32:22 -04:00
|
|
|
anyhow!(concat!(
|
2019-04-08 09:21:03 -07:00
|
|
|
"Invalid edit string '{}'. ",
|
|
|
|
|
"Edit strings must match the pattern '<START_BYTE_OR_POSITION> <REMOVED_LENGTH> <NEW_TEXT>'"
|
2021-06-09 12:32:22 -04:00
|
|
|
), flag)
|
2019-04-08 09:21:03 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Three whitespace-separated parts:
|
|
|
|
|
// * edit position
|
|
|
|
|
// * deleted length
|
|
|
|
|
// * inserted text
|
2024-02-04 01:30:33 -05:00
|
|
|
let mut parts = flag.split(' ');
|
2019-04-08 09:21:03 -07:00
|
|
|
let position = parts.next().ok_or_else(error)?;
|
|
|
|
|
let deleted_length = parts.next().ok_or_else(error)?;
|
|
|
|
|
let inserted_text = parts.collect::<Vec<_>>().join(" ").into_bytes();
|
|
|
|
|
|
|
|
|
|
// Position can either be a byte_offset or row,column pair, separated by a comma
|
2021-06-23 10:04:29 -04:00
|
|
|
let position = if position == "$" {
|
|
|
|
|
source_code.len()
|
2024-02-04 01:30:33 -05:00
|
|
|
} else if position.contains(',') {
|
|
|
|
|
let mut parts = position.split(',');
|
2019-04-08 09:21:03 -07:00
|
|
|
let row = parts.next().ok_or_else(error)?;
|
2024-02-04 01:30:33 -05:00
|
|
|
let row = row.parse::<usize>().map_err(|_| error())?;
|
2019-04-08 09:21:03 -07:00
|
|
|
let column = parts.next().ok_or_else(error)?;
|
2024-02-04 01:30:33 -05:00
|
|
|
let column = column.parse::<usize>().map_err(|_| error())?;
|
2023-10-04 11:21:48 +03:00
|
|
|
offset_for_position(source_code, Point { row, column })?
|
2019-04-08 09:21:03 -07:00
|
|
|
} else {
|
2024-02-04 01:30:33 -05:00
|
|
|
position.parse::<usize>().map_err(|_| error())?
|
2019-04-08 09:21:03 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Deleted length must be a byte count.
|
2024-02-04 01:30:33 -05:00
|
|
|
let deleted_length = deleted_length.parse::<usize>().map_err(|_| error())?;
|
2019-04-08 09:21:03 -07:00
|
|
|
|
|
|
|
|
Ok(Edit {
|
|
|
|
|
position,
|
|
|
|
|
deleted_length,
|
|
|
|
|
inserted_text,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-04 11:21:48 +03:00
|
|
|
pub fn offset_for_position(input: &[u8], position: Point) -> Result<usize> {
|
2023-10-04 11:20:18 +03:00
|
|
|
let mut row = 0;
|
|
|
|
|
let mut offset = 0;
|
|
|
|
|
let mut iter = memchr::memchr_iter(b'\n', input);
|
|
|
|
|
loop {
|
|
|
|
|
if let Some(pos) = iter.next() {
|
|
|
|
|
if row < position.row {
|
|
|
|
|
row += 1;
|
|
|
|
|
offset = pos;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2019-04-08 09:21:03 -07:00
|
|
|
}
|
2023-10-04 11:20:18 +03:00
|
|
|
offset += 1;
|
|
|
|
|
break;
|
2019-04-08 09:21:03 -07:00
|
|
|
}
|
2023-10-04 11:21:48 +03:00
|
|
|
if position.row - row > 0 {
|
|
|
|
|
return Err(anyhow!("Failed to address a row: {}", position.row));
|
2019-04-08 09:21:03 -07:00
|
|
|
}
|
2023-10-04 11:21:48 +03:00
|
|
|
if let Some(pos) = iter.next() {
|
|
|
|
|
if (pos - offset < position.column) || (input[offset] == b'\n' && position.column > 0) {
|
|
|
|
|
return Err(anyhow!("Failed to address a column: {}", position.column));
|
|
|
|
|
};
|
|
|
|
|
} else if input.len() - offset < position.column {
|
|
|
|
|
return Err(anyhow!("Failed to address a column over the end"));
|
|
|
|
|
}
|
|
|
|
|
Ok(offset + position.column)
|
2019-04-08 09:21:03 -07:00
|
|
|
}
|
|
|
|
|
|
2023-10-04 11:21:48 +03:00
|
|
|
pub fn position_for_offset(input: &[u8], offset: usize) -> Result<Point> {
|
|
|
|
|
if offset > input.len() {
|
|
|
|
|
return Err(anyhow!("Failed to address an offset: {offset}"));
|
|
|
|
|
}
|
2019-04-08 09:21:03 -07:00
|
|
|
let mut result = Point { row: 0, column: 0 };
|
2023-10-04 11:20:18 +03:00
|
|
|
let mut last = 0;
|
|
|
|
|
for pos in memchr::memchr_iter(b'\n', &input[..offset]) {
|
|
|
|
|
result.row += 1;
|
|
|
|
|
last = pos;
|
2019-04-08 09:21:03 -07:00
|
|
|
}
|
2023-10-04 11:20:18 +03:00
|
|
|
result.column = if result.row > 0 {
|
|
|
|
|
offset - last - 1
|
|
|
|
|
} else {
|
|
|
|
|
offset
|
|
|
|
|
};
|
2023-10-04 11:21:48 +03:00
|
|
|
Ok(result)
|
2019-04-08 09:21:03 -07:00
|
|
|
}
|