Skip to content

Commit

Permalink
Merge pull request #435 from Alex-Fischman/static_parser
Browse files Browse the repository at this point in the history
Static parser
  • Loading branch information
Alex-Fischman authored Oct 7, 2024
2 parents 8ac9cae + f75d70d commit c34c448
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 31 deletions.
23 changes: 1 addition & 22 deletions src/ast/desugar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,13 @@ use crate::*;

pub struct Desugar {
pub(crate) fresh_gen: SymbolGen,
// Store the parser because it takes some time
// on startup for some reason
parser: ast::parse::ProgramParser,
}

impl Default for Desugar {
fn default() -> Self {
Self {
// the default reserved string in egglog is "_"
fresh_gen: SymbolGen::new("_".repeat(2)),
parser: ast::parse::ProgramParser::new(),
}
}
}
Expand Down Expand Up @@ -230,7 +226,7 @@ pub(crate) fn desugar_command(
let s = std::fs::read_to_string(&file)
.unwrap_or_else(|_| panic!("{} Failed to read file {file}", span.get_quote()));
return desugar_commands(
desugar.parse_program(Some(file), &s)?,
parse_program(Some(file), &s)?,
desugar,
get_all_proofs,
seminaive_transform,
Expand Down Expand Up @@ -387,7 +383,6 @@ impl Clone for Desugar {
fn clone(&self) -> Self {
Self {
fresh_gen: self.fresh_gen.clone(),
parser: ast::parse::ProgramParser::new(),
}
}
}
Expand All @@ -406,20 +401,4 @@ impl Desugar {
let res = desugar_commands(program, self, get_all_proofs, seminaive_transform)?;
Ok(res)
}

pub fn parse_program(
&self,
filename: Option<String>,
input: &str,
) -> Result<Vec<Command>, Error> {
let filename = filename.unwrap_or_else(|| DEFAULT_FILENAME.to_string());
let srcfile = Arc::new(SrcFile {
name: filename,
contents: Some(input.to_string()),
});
Ok(self
.parser
.parse(&srcfile, input)
.map_err(|e| e.map_token(|tok| tok.to_string()))?)
}
}
18 changes: 18 additions & 0 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,24 @@ lalrpop_mod!(
"/ast/parse.rs"
);

// For some reason the parser is slow to construct so
// we make it static and only construct it once.
lazy_static! {
static ref PARSER: parse::ProgramParser = Default::default();
}

/// Parse a file into a program.
pub fn parse_program(filename: Option<String>, input: &str) -> Result<Vec<Command>, Error> {
let filename = filename.unwrap_or_else(|| DEFAULT_FILENAME.to_string());
let srcfile = Arc::new(SrcFile {
name: filename,
contents: Some(input.to_string()),
});
Ok(PARSER
.parse(&srcfile, input)
.map_err(|e| e.map_token(|tok| tok.to_string()))?)
}

use crate::{
core::{GenericAtom, GenericAtomTerm, HeadOrEq, Query, ResolvedCall},
*,
Expand Down
10 changes: 1 addition & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1458,14 +1458,6 @@ impl EGraph {
Ok(self.flush_msgs())
}

pub fn parse_program(
&self,
filename: Option<String>,
input: &str,
) -> Result<Vec<Command>, Error> {
self.desugar.parse_program(filename, input)
}

/// Takes a source program `input`, parses it, runs it, and returns a list of messages.
///
/// `filename` is an optional argument to indicate the source of
Expand All @@ -1476,7 +1468,7 @@ impl EGraph {
filename: Option<String>,
input: &str,
) -> Result<Vec<String>, Error> {
let parsed = self.desugar.parse_program(filename, input)?;
let parsed = parse_program(filename, input)?;
self.run_program(parsed)
}

Expand Down

0 comments on commit c34c448

Please sign in to comment.