Specify a read parameter for ISLE's Lexer. (#6123)

* fix col

* fix test failure

* specify read method.

* modify isle lexer

* remove a fn
This commit is contained in:
yuyang
2023-04-25 00:11:34 +08:00
committed by GitHub
parent 4b703f9dce
commit c9a9c2c191

View File

@@ -2,7 +2,7 @@
use crate::error::{Error, Errors, Span}; use crate::error::{Error, Errors, Span};
use std::borrow::Cow; use std::borrow::Cow;
use std::path::Path; use std::path::{Path, PathBuf};
use std::sync::Arc; use std::sync::Arc;
type Result<T> = std::result::Result<T, Errors>; type Result<T> = std::result::Result<T, Errors>;
@@ -91,21 +91,26 @@ impl<'a> Lexer<'a> {
where where
P: AsRef<Path>, P: AsRef<Path>,
{ {
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<Lexer<'a>> {
let mut filenames = Vec::<Arc<str>>::new(); let mut filenames = Vec::<Arc<str>>::new();
let mut file_texts = Vec::<Arc<str>>::new(); let mut file_texts = Vec::<Arc<str>>::new();
for (f, content) in files.iter() {
for f in file_paths {
let f = f.as_ref();
filenames.push(f.display().to_string().into()); filenames.push(f.display().to_string().into());
let s = std::fs::read_to_string(f) file_texts.push(content.as_str().into());
.map_err(|e| Errors::from_io(e, format!("failed to read file: {}", f.display())))?;
file_texts.push(s.into());
} }
assert!(!filenames.is_empty()); assert!(!filenames.is_empty());
let mut file_starts = vec![]; let mut file_starts = vec![];
let mut buf = String::new(); let mut buf = String::new();
for text in &file_texts { for text in &file_texts {
@@ -113,7 +118,6 @@ impl<'a> Lexer<'a> {
buf += text; buf += text;
buf += "\n"; buf += "\n";
} }
let mut l = Lexer { let mut l = Lexer {
filenames, filenames,
file_texts, file_texts,