Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error resilient codegen #6

Merged
merged 6 commits into from
Nov 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 28 additions & 15 deletions scopegraphs-macros/src/regex.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::rc::Rc;

use proc_macro::TokenStream;
use quote::quote_spanned;
use scopegraphs_regular_expressions::Regex;
use syn::parse::{Parse, ParseStream};
use syn::spanned::Spanned;
use syn::{Attribute, Ident, Meta, Token, Type};

#[cfg(feature = "dot")]
Expand All @@ -19,25 +19,39 @@ pub(crate) struct RegexInput {
_close: Token![>],
_equals: Token![=],
regex: Regex,
errors: Vec<syn::Error>,
}

impl Parse for RegexInput {
fn parse(input: ParseStream) -> syn::Result<Self> {
let attrs = input.call(Attribute::parse_outer)?;
let _type = input.parse()?;
let name = input.parse()?;
let _open = input.parse()?;
let alphabet_type = input.parse()?;
let _close = input.parse()?;
let _equals = input.parse()?;
let (regex, errors) = match input.parse() {
Ok(re) => (re, vec![]),
Err(err) => (Regex::Complement(Rc::new(Regex::EmptySet)), vec![err]),
};
Ok(Self {
attrs: input.call(Attribute::parse_outer)?,
_type: input.parse()?,
name: input.parse()?,
_open: input.parse()?,
alphabet_type: input.parse()?,
_close: input.parse()?,
_equals: input.parse()?,
regex: input.parse()?,
attrs,
_type,
name,
_open,
alphabet_type,
_close,
_equals,
regex,
errors,
})
}
}

impl RegexInput {
pub fn compile(self) -> TokenStream {
let mut errors = self.errors;
#[cfg(feature = "dot")]
let mut graph = None;

Expand All @@ -55,10 +69,7 @@ impl RegexInput {
}) if path.is_ident("graph") => {
graph = Some(s);
}
i => {
return quote_spanned!(i.span() => compile_error!("unexpected attribute");)
.into();
}
i => errors.push(syn::Error::new_spanned(i, "unexpected attribute")),
}
}

Expand All @@ -74,6 +85,8 @@ impl RegexInput {
.unwrap_or_else(|e| panic!("failed while graphing at {path}: {e}"));
}

compiled.emit(&self.name, &self.alphabet_type).into()
compiled
.emit(&self.name, &self.alphabet_type, errors)
.into()
}
}
14 changes: 10 additions & 4 deletions scopegraphs-regular-expressions/src/emit.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use crate::{Automaton, MatchState};
use proc_macro2::{Ident, TokenStream};
use quote::quote;
use quote::{quote, TokenStreamExt};
use syn::Type;

impl Automaton {
/// Convert this compiled regex into rust code that accepts this regular expression.
/// `name` is the name of the type that is emitted, and `alphabet` is the type of symbols
/// that the machine should accept.
pub fn emit(&self, name: &Ident, alphabet: &Type) -> TokenStream {
pub fn emit(&self, name: &Ident, alphabet: &Type, errors: Vec<syn::Error>) -> TokenStream {
let Self {
states, initial, ..
} = self;
Expand Down Expand Up @@ -39,8 +39,12 @@ impl Automaton {

let finals: Vec<_> = states.iter().map(|i| i.is_final).collect();
let accepting: Vec<_> = states.iter().map(MatchState::is_accepting).collect();
let compile_errors: TokenStream = errors
.iter()
.flat_map(syn::Error::to_compile_error)
.collect();

quote!(
let mut result = quote!(
struct #name {
state: usize,
}
Expand Down Expand Up @@ -77,6 +81,8 @@ impl Automaton {
}
}
}
)
);
result.append_all(compile_errors);
result
}
}
4 changes: 0 additions & 4 deletions scopegraphs-regular-expressions/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -834,10 +834,6 @@ impl<'a> Parser<'a> {

/// Entry point: parses the `input` to a [`Regex`].
pub fn parse_regex(input: ParseStream) -> syn::Result<Regex> {
if input.is_empty() {
return Ok(Regex::Complement(Rc::new(Regex::EmptySet)));
}

let mut parser = Parser::init(input)?;
let mut accept = false;
while !accept {
Expand Down
13 changes: 0 additions & 13 deletions scopegraphs/tests/test_regex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,16 +108,3 @@ fn test_negate_or() {
assert!(!Machine::new().accepts([B]));
assert!(Machine::new().accepts([C]));
}

#[test]
fn test_empty() {
use Alphabet::*;

compile_regex!(#[graph="empty.dot"] type Machine<Alphabet> = );

assert!(Machine::new().accepts([]));
assert!(Machine::new().accepts([A]));
assert!(Machine::new().accepts([B]));
assert!(Machine::new().accepts([C]));
assert!(Machine::new().accepts([A, B, C]));
}
Loading