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

feat: mut variable syntax support for TaggedExpr and RuleParam #372

Closed
A4-Tacks opened this issue Apr 15, 2024 · 2 comments
Closed

feat: mut variable syntax support for TaggedExpr and RuleParam #372

A4-Tacks opened this issue Apr 15, 2024 · 2 comments

Comments

@A4-Tacks
Copy link
Contributor

A4-Tacks commented Apr 15, 2024

use std::{collections::HashMap, cell::RefCell};

peg::parser!(grammar parser() for str {
    pub rule keyvals() -> HashMap<&'input str, &'input str>
        = map:({ RefCell::new(HashMap::new()) })
        (k:$(['a'..='z']+) ":" v:$(['a'..='z']+) {
            map.borrow_mut().insert(k, v)
        })**","
        { map.into_inner() }
});

// Expect
//peg::parser!(grammar parser() for str {
//    pub rule keyvals() -> HashMap<&'input str, &'input str>
//        = mut map:({ HashMap::new() })
//        (k:$(['a'..='z']+) ":" v:$(['a'..='z']+) {
//            map.insert(k, v)
//        })**","
//        { map }
//});
@A4-Tacks
Copy link
Contributor Author

Changing the state through repeated attempts at deduction may not be a good choice

@A4-Tacks A4-Tacks closed this as not planned Won't fix, can't repro, duplicate, stale Apr 15, 2024
@kevinmehall
Copy link
Owner

Yes, mutability could be dangerous in the presence of backtracking.

For building a HashMap without allocating an intermediate Vec, it seems like there could be a trait that exposes new(), append(&mut self, elem: T) and maybe finish(self) that the generated code could use in place of Vec to collect the results of each iteration of a repeat operator. I am not sure what the syntax would look like to specify this type on a repeat operator.

Related to #294 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants