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

fix listener codegen when labeled alts are involved #65

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
10 changes: 8 additions & 2 deletions src/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -436,11 +436,17 @@ where
T: ParseTreeListener<'input, Node> + 'a + ?Sized,
Node::Type: Listenable<T>,
{
pub fn walk_mut<Listener, Ctx>(mut listener: &mut Listener, t: &Ctx)
where
Listener: CoerceTo<T>,
Ctx: CoerceTo<Node::Type>,
{
Self::walk_inner(listener.coerce_mut_to(), t.coerce_ref_to());
}

/// Walks recursively over tree `t` with `listener`
pub fn walk<Listener, Ctx>(mut listener: Box<Listener>, t: &Ctx) -> Box<Listener>
where
// for<'x> &'x mut Listener: CoerceUnsized<&'x mut T>,
// for<'x> &'x Ctx: CoerceUnsized<&'x Node::Type>,
Listener: CoerceTo<T>,
Ctx: CoerceTo<Node::Type>,
{
Expand Down
6 changes: 5 additions & 1 deletion templates/Rust.stg
Original file line number Diff line number Diff line change
Expand Up @@ -1095,7 +1095,11 @@ impl\<'input> <parser.name>Context\<'input> for <struct.name>\<'input>{}
<if(parser.file.genListener)>
impl\<'input,'a> Listenable\<dyn <parser.grammarName>Listener\<'input> + 'a> for <struct.name>\<'input>{
<! a bit scuffed but prevents rewriting other targets !>
<trunc(dispatchMethods); separator="\n">
<if(parser.file.genVisitor)>
<trunc(dispatchMethods); separator="\n">
<else>
<dispatchMethods>
<endif>
}
<endif>

Expand Down
28 changes: 28 additions & 0 deletions tests/gen/labelsparser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,10 @@ impl<'input, 'a> Listenable<dyn LabelsListener<'input> + 'a> for AddContext<'inp
listener.enter_every_rule(self);
listener.enter_add(self);
}
fn exit(&self, listener: &mut (dyn LabelsListener<'input> + 'a)) {
listener.exit_add(self);
listener.exit_every_rule(self);
}
}

impl<'input> CustomRuleContext<'input> for AddContextExt<'input> {
Expand Down Expand Up @@ -570,6 +574,10 @@ impl<'input, 'a> Listenable<dyn LabelsListener<'input> + 'a> for ParensContext<'
listener.enter_every_rule(self);
listener.enter_parens(self);
}
fn exit(&self, listener: &mut (dyn LabelsListener<'input> + 'a)) {
listener.exit_parens(self);
listener.exit_every_rule(self);
}
}

impl<'input> CustomRuleContext<'input> for ParensContextExt<'input> {
Expand Down Expand Up @@ -645,6 +653,10 @@ impl<'input, 'a> Listenable<dyn LabelsListener<'input> + 'a> for MultContext<'in
listener.enter_every_rule(self);
listener.enter_mult(self);
}
fn exit(&self, listener: &mut (dyn LabelsListener<'input> + 'a)) {
listener.exit_mult(self);
listener.exit_every_rule(self);
}
}

impl<'input> CustomRuleContext<'input> for MultContextExt<'input> {
Expand Down Expand Up @@ -712,6 +724,10 @@ impl<'input, 'a> Listenable<dyn LabelsListener<'input> + 'a> for DecContext<'inp
listener.enter_every_rule(self);
listener.enter_dec(self);
}
fn exit(&self, listener: &mut (dyn LabelsListener<'input> + 'a)) {
listener.exit_dec(self);
listener.exit_every_rule(self);
}
}

impl<'input> CustomRuleContext<'input> for DecContextExt<'input> {
Expand Down Expand Up @@ -779,6 +795,10 @@ impl<'input, 'a> Listenable<dyn LabelsListener<'input> + 'a> for AnIDContext<'in
listener.enter_every_rule(self);
listener.enter_anID(self);
}
fn exit(&self, listener: &mut (dyn LabelsListener<'input> + 'a)) {
listener.exit_anID(self);
listener.exit_every_rule(self);
}
}

impl<'input> CustomRuleContext<'input> for AnIDContextExt<'input> {
Expand Down Expand Up @@ -846,6 +866,10 @@ impl<'input, 'a> Listenable<dyn LabelsListener<'input> + 'a> for AnIntContext<'i
listener.enter_every_rule(self);
listener.enter_anInt(self);
}
fn exit(&self, listener: &mut (dyn LabelsListener<'input> + 'a)) {
listener.exit_anInt(self);
listener.exit_every_rule(self);
}
}

impl<'input> CustomRuleContext<'input> for AnIntContextExt<'input> {
Expand Down Expand Up @@ -911,6 +935,10 @@ impl<'input, 'a> Listenable<dyn LabelsListener<'input> + 'a> for IncContext<'inp
listener.enter_every_rule(self);
listener.enter_inc(self);
}
fn exit(&self, listener: &mut (dyn LabelsListener<'input> + 'a)) {
listener.exit_inc(self);
listener.exit_every_rule(self);
}
}

impl<'input> CustomRuleContext<'input> for IncContextExt<'input> {
Expand Down
29 changes: 25 additions & 4 deletions tests/general_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ mod gen {
};
use crate::gen::csvvisitor::CSVVisitor;
use crate::gen::labelslexer::LabelsLexer;
use crate::gen::labelsparser::{EContextAll, LabelsParser};
use crate::gen::referencetoatnparser::{
ReferenceToATNParserContext, ReferenceToATNParserContextType,
};
Expand Down Expand Up @@ -337,19 +336,41 @@ if (x < x && a > 0) then duh

#[test]
fn test_complex_convert() {
use labelsparser::*;

struct Listener(String);

impl ParseTreeListener<'_, LabelsParserContextType> for Listener {}
impl<'input> labelslistener::LabelsListener<'input> for Listener {
fn enter_add(&mut self, _ctx: &AddContext<'input>) {
self.0.push('(');
}

fn exit_add(&mut self, ctx: &AddContext<'input>) {
self.0 += ctx.get_v();
self.0.push(')');
}
}

let codepoints = "(a+4)*2".chars().map(|x| x as u32).collect::<Vec<_>>();
// let codepoints = "(a+4)*2";
let input = InputStream::new(&*codepoints);
let lexer = LabelsLexer::new(input);
let token_source = CommonTokenStream::new(lexer);
let mut parser = LabelsParser::new(token_source);
let result = parser.s().expect("parser error");
let string = result.q.as_ref().unwrap().get_v();

let root = parser.s().expect("parser error");
let string = root.q.as_ref().unwrap().get_v();
assert_eq!("* + a 4 2", string);
let x = result.q.as_deref().unwrap();
let x = root.q.as_deref().unwrap();

match x {
EContextAll::MultContext(x) => assert_eq!("(a+4)", x.a.as_ref().unwrap().get_text()),
_ => panic!("oops"),
}

let mut listener = Listener(String::new());
LabelsTreeWalker::walk_mut(&mut listener, &*root);
assert_eq!(listener.0, "(+ a 4)");
}
}