macro_rules! span {
($id:expr, $l:expr, $r:expr) => {
crate::SourceSpan::new($id, $l..$r)
};
($id:expr, $i:expr) => {
crate::SourceSpan::at($id, $i)
};
}
lalrpop_util::lalrpop_mod!(
#[allow(clippy::all)]
grammar,
"/parser/grammar.rs"
);
mod error;
mod lexer;
mod scanner;
mod token;
use alloc::{boxed::Box, collections::BTreeSet, string::ToString, sync::Arc, vec::Vec};
pub use self::{
error::{BinErrorKind, HexErrorKind, LiteralErrorKind, ParsingError},
lexer::Lexer,
scanner::Scanner,
token::{BinEncodedValue, DocumentationType, HexEncodedValue, Token},
};
use crate::{
ast,
diagnostics::{Report, SourceFile, SourceSpan, Span, Spanned},
sema, LibraryPath, SourceManager,
};
type ParseError<'a> = lalrpop_util::ParseError<u32, Token<'a>, ParsingError>;
#[derive(Default)]
pub struct ModuleParser {
kind: ast::ModuleKind,
interned: BTreeSet<Arc<str>>,
warnings_as_errors: bool,
}
impl ModuleParser {
pub fn new(kind: ast::ModuleKind) -> Self {
Self {
kind,
interned: Default::default(),
warnings_as_errors: false,
}
}
pub fn set_warnings_as_errors(&mut self, yes: bool) {
self.warnings_as_errors = yes;
}
pub fn parse(
&mut self,
path: LibraryPath,
source: Arc<SourceFile>,
) -> Result<Box<ast::Module>, Report> {
let forms = parse_forms_internal(source.clone(), &mut self.interned)
.map_err(|err| Report::new(err).with_source_code(source.clone()))?;
sema::analyze(source, self.kind, path, forms, self.warnings_as_errors).map_err(Report::new)
}
#[cfg(feature = "std")]
pub fn parse_file<P>(
&mut self,
name: LibraryPath,
path: P,
source_manager: &dyn SourceManager,
) -> Result<Box<ast::Module>, Report>
where
P: AsRef<std::path::Path>,
{
use vm_core::debuginfo::SourceManagerExt;
use crate::diagnostics::{IntoDiagnostic, WrapErr};
let path = path.as_ref();
let source_file = source_manager
.load_file(path)
.into_diagnostic()
.wrap_err_with(|| format!("failed to load source file from '{}'", path.display()))?;
self.parse(name, source_file)
}
pub fn parse_str(
&mut self,
name: LibraryPath,
source: impl ToString,
source_manager: &dyn SourceManager,
) -> Result<Box<ast::Module>, Report> {
use vm_core::debuginfo::SourceContent;
let path = Arc::from(name.path().into_owned().into_boxed_str());
let content = SourceContent::new(Arc::clone(&path), source.to_string().into_boxed_str());
let source_file = source_manager.load_from_raw_parts(path, content);
self.parse(name, source_file)
}
}
#[cfg(any(test, feature = "testing"))]
pub fn parse_forms(source: Arc<SourceFile>) -> Result<Vec<ast::Form>, ParsingError> {
let mut interned = BTreeSet::default();
parse_forms_internal(source, &mut interned)
}
fn parse_forms_internal(
source: Arc<SourceFile>,
interned: &mut BTreeSet<Arc<str>>,
) -> Result<Vec<ast::Form>, ParsingError> {
let source_id = source.id();
let scanner = Scanner::new(source.as_str());
let lexer = Lexer::new(source_id, scanner);
grammar::FormsParser::new()
.parse(&source, interned, core::marker::PhantomData, lexer)
.map_err(|err| ParsingError::from_parse_error(source_id, err))
}
#[cfg(feature = "std")]
pub fn read_modules_from_dir(
namespace: crate::LibraryNamespace,
dir: &std::path::Path,
source_manager: &dyn SourceManager,
) -> Result<impl Iterator<Item = Box<ast::Module>>, Report> {
use std::collections::{btree_map::Entry, BTreeMap};
use miette::miette;
use module_walker::{ModuleEntry, WalkModules};
use crate::diagnostics::{IntoDiagnostic, WrapErr};
if !dir.is_dir() {
return Err(miette!("the provided path '{}' is not a valid directory", dir.display()));
}
if dir.join(ast::Module::ROOT_FILENAME).exists() {
return Err(miette!("{} is not allowed in the root directory", ast::Module::ROOT_FILENAME));
}
let mut modules = BTreeMap::default();
let walker = WalkModules::new(namespace.clone(), dir)
.into_diagnostic()
.wrap_err_with(|| format!("failed to load modules from '{}'", dir.display()))?;
for entry in walker {
let ModuleEntry { mut name, source_path } = entry?;
if name.last() == ast::Module::ROOT {
name.pop();
}
let mut parser = ModuleParser::new(ast::ModuleKind::Library);
let ast = parser.parse_file(name.clone(), &source_path, source_manager)?;
match modules.entry(name) {
Entry::Occupied(ref entry) => {
return Err(miette!("duplicate module '{0}'", entry.key().clone()));
},
Entry::Vacant(entry) => {
entry.insert(ast);
},
}
}
Ok(modules.into_values())
}
#[cfg(feature = "std")]
mod module_walker {
use std::{
ffi::OsStr,
fs::{self, DirEntry, FileType},
io,
path::{Path, PathBuf},
};
use miette::miette;
use crate::{
ast::Module,
diagnostics::{IntoDiagnostic, Report},
LibraryNamespace, LibraryPath,
};
pub struct ModuleEntry {
pub name: LibraryPath,
pub source_path: PathBuf,
}
pub struct WalkModules<'a> {
namespace: LibraryNamespace,
root: &'a Path,
stack: alloc::collections::VecDeque<io::Result<DirEntry>>,
}
impl<'a> WalkModules<'a> {
pub fn new(namespace: LibraryNamespace, path: &'a Path) -> io::Result<Self> {
use alloc::collections::VecDeque;
let stack = VecDeque::from_iter(fs::read_dir(path)?);
Ok(Self { namespace, root: path, stack })
}
fn next_entry(
&mut self,
entry: &DirEntry,
ty: &FileType,
) -> Result<Option<ModuleEntry>, Report> {
if ty.is_dir() {
let dir = entry.path();
self.stack.extend(fs::read_dir(dir).into_diagnostic()?);
return Ok(None);
}
let mut file_path = entry.path();
let is_module = file_path
.extension()
.map(|ext| ext == AsRef::<OsStr>::as_ref(Module::FILE_EXTENSION))
.unwrap_or(false);
if !is_module {
return Ok(None);
}
file_path.set_extension("");
if file_path.is_dir() {
return Err(miette!(
"file and directory with same name are not allowed: {}",
file_path.display()
));
}
let relative_path = file_path
.strip_prefix(self.root)
.expect("expected path to be a child of the root directory");
let mut libpath = LibraryPath::from(self.namespace.clone());
for component in relative_path.iter() {
let component = component.to_str().ok_or_else(|| {
let p = entry.path();
miette!("{} is an invalid directory entry", p.display())
})?;
libpath.push(component).into_diagnostic()?;
}
Ok(Some(ModuleEntry { name: libpath, source_path: entry.path() }))
}
}
impl Iterator for WalkModules<'_> {
type Item = Result<ModuleEntry, Report>;
fn next(&mut self) -> Option<Self::Item> {
loop {
let entry = self
.stack
.pop_front()?
.and_then(|entry| entry.file_type().map(|ft| (entry, ft)))
.into_diagnostic();
match entry {
Ok((ref entry, ref file_type)) => {
match self.next_entry(entry, file_type).transpose() {
None => continue,
result => break result,
}
},
Err(err) => break Some(Err(err)),
}
}
}
}
}
#[cfg(test)]
mod tests {
use vm_core::assert_matches;
use super::*;
use crate::SourceId;
#[test]
fn lex_exp() {
let source_id = SourceId::default();
let scanner = Scanner::new("begin exp.u9 end");
let mut lexer = Lexer::new(source_id, scanner).map(|result| result.map(|(_, t, _)| t));
assert_matches!(lexer.next(), Some(Ok(Token::Begin)));
assert_matches!(lexer.next(), Some(Ok(Token::ExpU)));
assert_matches!(lexer.next(), Some(Ok(Token::Int(n))) if n == 9);
assert_matches!(lexer.next(), Some(Ok(Token::End)));
}
#[test]
fn lex_block() {
let source_id = SourceId::default();
let scanner = Scanner::new(
"\
const.ERR1=1
begin
u32assertw
u32assertw.err=ERR1
u32assertw.err=2
end
",
);
let mut lexer = Lexer::new(source_id, scanner).map(|result| result.map(|(_, t, _)| t));
assert_matches!(lexer.next(), Some(Ok(Token::Const)));
assert_matches!(lexer.next(), Some(Ok(Token::Dot)));
assert_matches!(lexer.next(), Some(Ok(Token::ConstantIdent("ERR1"))));
assert_matches!(lexer.next(), Some(Ok(Token::Equal)));
assert_matches!(lexer.next(), Some(Ok(Token::Int(1))));
assert_matches!(lexer.next(), Some(Ok(Token::Begin)));
assert_matches!(lexer.next(), Some(Ok(Token::U32Assertw)));
assert_matches!(lexer.next(), Some(Ok(Token::U32Assertw)));
assert_matches!(lexer.next(), Some(Ok(Token::Dot)));
assert_matches!(lexer.next(), Some(Ok(Token::Err)));
assert_matches!(lexer.next(), Some(Ok(Token::Equal)));
assert_matches!(lexer.next(), Some(Ok(Token::ConstantIdent("ERR1"))));
assert_matches!(lexer.next(), Some(Ok(Token::U32Assertw)));
assert_matches!(lexer.next(), Some(Ok(Token::Dot)));
assert_matches!(lexer.next(), Some(Ok(Token::Err)));
assert_matches!(lexer.next(), Some(Ok(Token::Equal)));
assert_matches!(lexer.next(), Some(Ok(Token::Int(2))));
assert_matches!(lexer.next(), Some(Ok(Token::End)));
assert_matches!(lexer.next(), Some(Ok(Token::Eof)));
}
#[test]
fn lex_emit() {
let source_id = SourceId::default();
let scanner = Scanner::new(
"\
begin
push.1
emit.1
end
",
);
let mut lexer = Lexer::new(source_id, scanner).map(|result| result.map(|(_, t, _)| t));
assert_matches!(lexer.next(), Some(Ok(Token::Begin)));
assert_matches!(lexer.next(), Some(Ok(Token::Push)));
assert_matches!(lexer.next(), Some(Ok(Token::Dot)));
assert_matches!(lexer.next(), Some(Ok(Token::Int(1))));
assert_matches!(lexer.next(), Some(Ok(Token::Emit)));
assert_matches!(lexer.next(), Some(Ok(Token::Dot)));
assert_matches!(lexer.next(), Some(Ok(Token::Int(1))));
assert_matches!(lexer.next(), Some(Ok(Token::End)));
assert_matches!(lexer.next(), Some(Ok(Token::Eof)));
}
}