From c9a9c2c1919858057ef51b91dc92216cf13ea477 Mon Sep 17 00:00:00 2001 From: yuyang <96557710+yuyang-ok@users.noreply.github.com> Date: Tue, 25 Apr 2023 00:11:34 +0800 Subject: [PATCH] Specify a `read` parameter for `ISLE`'s `Lexer`. (#6123) * fix col * fix test failure * specify read method. * modify isle lexer * remove a fn --- cranelift/isle/isle/src/lexer.rs | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/cranelift/isle/isle/src/lexer.rs b/cranelift/isle/isle/src/lexer.rs index 2c8ee2540a..6a41ee21e1 100644 --- a/cranelift/isle/isle/src/lexer.rs +++ b/cranelift/isle/isle/src/lexer.rs @@ -2,7 +2,7 @@ use crate::error::{Error, Errors, Span}; use std::borrow::Cow; -use std::path::Path; +use std::path::{Path, PathBuf}; use std::sync::Arc; type Result = std::result::Result; @@ -91,21 +91,26 @@ impl<'a> Lexer<'a> { where P: AsRef, { + let mut files = vec![]; + for f in file_paths.into_iter() { + let f = f.as_ref().to_path_buf(); + let s = std::fs::read_to_string(f.as_path()) + .map_err(|e| Errors::from_io(e, format!("failed to read file: {}", f.display())))?; + files.push((f, s)); + } + Self::from_file_contents(files) + } + + /// Create a new lexer from the given files and contents. + pub fn from_file_contents(files: Vec<(PathBuf, String)>) -> Result> { let mut filenames = Vec::>::new(); let mut file_texts = Vec::>::new(); - - for f in file_paths { - let f = f.as_ref(); - + for (f, content) in files.iter() { filenames.push(f.display().to_string().into()); - let s = std::fs::read_to_string(f) - .map_err(|e| Errors::from_io(e, format!("failed to read file: {}", f.display())))?; - file_texts.push(s.into()); + file_texts.push(content.as_str().into()); } - assert!(!filenames.is_empty()); - let mut file_starts = vec![]; let mut buf = String::new(); for text in &file_texts { @@ -113,7 +118,6 @@ impl<'a> Lexer<'a> { buf += text; buf += "\n"; } - let mut l = Lexer { filenames, file_texts,