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

Only build the parser once #252

Merged
merged 1 commit into from
Sep 29, 2023
Merged
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
15 changes: 8 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ fn compile_machine_policies_internal(
fn get_policies(input_files: Vec<&str>) -> Result<Vec<PolicyFile>, CascadeErrors> {
let mut errors = CascadeErrors::new();
let mut policies: Vec<PolicyFile> = Vec::new();
let parser = parser::PolicyParser::new();
for f in input_files {
let policy_str = match std::fs::read_to_string(f) {
Ok(s) => s,
Expand All @@ -317,7 +318,7 @@ fn get_policies(input_files: Vec<&str>) -> Result<Vec<PolicyFile>, CascadeErrors
continue;
}
};
let p = match parse_policy(&policy_str) {
let p = match parse_policy(&parser, &policy_str) {
Ok(p) => p,
Err(evec) => {
for e in evec {
Expand All @@ -332,13 +333,13 @@ fn get_policies(input_files: Vec<&str>) -> Result<Vec<PolicyFile>, CascadeErrors
errors.into_result(policies)
}

fn parse_policy(
policy: &str,
) -> Result<Box<Policy>, Vec<LalrpopParseError<usize, lalrpop_util::lexer::Token, ParseErrorMsg>>> {
fn parse_policy<'a>(
parser: &parser::PolicyParser,
policy: &'a str,
) -> Result<Box<Policy>, Vec<LalrpopParseError<usize, lalrpop_util::lexer::Token<'a>, ParseErrorMsg>>>
{
let mut errors = Vec::new();
// TODO: Probably should only construct once
// Why though?
let parse_res = parser::PolicyParser::new().parse(&mut errors, policy);
let parse_res = parser.parse(&mut errors, policy);
// errors is a vec of ErrorRecovery. ErrorRecovery is a struct wrapping a ParseError
// and a sequence of discarded characters. We don't need those characters, so we just
// remove the wrapping.
Expand Down