I've finished an implementation of a solution for designing semantic models (symbols) in Rust using methods containing dynamic dispatches. The reason why I implemented it is not easy to explain to everyone, but anyway, let's focus in the problem.
I'm testing it now to see if it has bugs, and it already yields a problem at the struct
keyword in this example (related to the syn
crate):
smodel! {
type Arena = MeaningArena;
struct Meaning {
let x: f64 = 0.0;
let ref y: String = "".into();
pub fn Meaning() {
super();
println!("{}", this.m());
}
pub fn m(&self) -> String {
"".into()
}
pub fn m1(&self) {
println!("base");
}
}
struct FooMeaning: Meaning {
pub fn FooMeaning() {
super();
}
pub override fn m(&self) -> String {
"Foo".into()
}
pub override fn m1(&self) {
if true {
super.m1();
}
}
}
}
I'm getting this at the second struct
keyword:
error: expected `fn`
--> crates\smodel\src\lib.rs:55:13
|
55 | struct FooMeaning: Meaning {
| ^^^^^^
But I'm really parsing it right:
...
impl Parse for MeaningTree {
fn parse(input: ParseStream) -> Result<Self> {
let arena_type_name = parse_meaning_arena_type_name(input)?.to_token_stream();
let mut meanings = vec![];
while !input.is_empty() {
meanings.push(Rc::new(input.parse::<Meaning>()?));
}
Ok(Self {
arena_type_name,
meanings,
})
}
}
...
let braced_content;
let _ = braced!(braced_content in input);
while !braced_content.is_empty() {
if input.peek(Token![let]) {
fields.push(Rc::new(parse_meaning_field(input)?));
} else {
match parse_meaning_method(input, &name_str)? {
MeaningMethodOrConstructor::Constructor(ctor) => {
constructor = Some(ctor);
},
MeaningMethodOrConstructor::Method(m) => {
methods.push(Rc::new(m));
},
}
}
}
Here's the master source: