Skip to content

Commit

Permalink
Remove legacy parsing from wit-parser (#821)
Browse files Browse the repository at this point in the history
There's no longer any need to parse a single `Interface`, so support for
parsing that from a file can now be removed.
  • Loading branch information
alexcrichton committed Nov 16, 2022
1 parent 001e5fc commit 40ef021
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 68 deletions.
9 changes: 0 additions & 9 deletions crates/wit-parser/src/ast.rs
Expand Up @@ -201,15 +201,6 @@ impl<'a> Interface<'a> {
}
Ok(items)
}

pub(super) fn parse_legacy_items(tokens: &mut Tokenizer<'a>) -> Result<Vec<InterfaceItem<'a>>> {
let mut items = Vec::new();
while tokens.clone().next()?.is_some() {
let docs = parse_docs(tokens)?;
items.push(InterfaceItem::parse(tokens, docs)?);
}
Ok(items)
}
}

pub enum InterfaceItem<'a> {
Expand Down
59 changes: 0 additions & 59 deletions crates/wit-parser/src/lib.rs
Expand Up @@ -407,65 +407,6 @@ fn unwrap_md(contents: &str) -> String {
}

impl Interface {
pub fn parse(name: &str, input: &str) -> Result<Interface> {
Interface::parse_with(name, input)
}

pub fn parse_file(path: impl AsRef<Path>) -> Result<Interface> {
let path = path.as_ref();
let contents = std::fs::read_to_string(&path)
.with_context(|| format!("failed to read: {}", path.display()))?;
Interface::parse_with(path, &contents)
}

pub fn parse_with(filename: impl AsRef<Path>, contents: &str) -> Result<Interface> {
Interface::_parse_with(filename.as_ref(), contents)
}

fn _parse_with(filename: &Path, contents: &str) -> Result<Interface> {
let name = filename
.file_name()
.context("wit path must end in a file name")?
.to_str()
.context("wit filename must be valid unicode")?
// TODO: replace with `file_prefix` if/when that gets stabilized.
.split(".")
.next()
.unwrap();
let mut contents = contents;

// If we have a ".md" file, it's a wit file wrapped in a markdown file;
// parse the markdown to extract the `wit` code blocks.
let md_contents;
if filename.extension().and_then(|s| s.to_str()) == Some("md") {
md_contents = unwrap_md(contents);
contents = &md_contents[..];
}

let mut lexer = Tokenizer::new(&contents)?;

// Parse the `contents `into an AST
let items = match ast::Interface::parse_legacy_items(&mut lexer) {
Ok(ast) => ast,
Err(mut e) => {
let file = filename.display().to_string();
ast::rewrite_error(&mut e, &file, contents);
return Err(e);
}
};

// and finally resolve everything into our final instance
let mut resolver = ast::Resolver::default();
match resolver.resolve(name, &items, &Default::default()) {
Ok(i) => Ok(i),
Err(mut e) => {
let file = filename.display().to_string();
ast::rewrite_error(&mut e, &file, contents);
Err(e)
}
}
}

pub fn topological_types(&self) -> Vec<TypeId> {
let mut ret = Vec::new();
let mut visited = HashSet::new();
Expand Down

0 comments on commit 40ef021

Please sign in to comment.