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

Find keywords using perfect hashing #1590

Open
wants to merge 1 commit into
base: main
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
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ visitor = ["sqlparser_derive"]
[dependencies]
bigdecimal = { version = "0.4.1", features = ["serde"], optional = true }
log = "0.4"
phf = { version = "0.11", default-features = false }
serde = { version = "1.0", features = ["derive"], optional = true }
# serde_json is only used in examples/cli, but we have to put it outside
# of dev-dependencies because of
Expand All @@ -58,6 +59,10 @@ simple_logger = "5.0"
matches = "0.1"
pretty_assertions = "1"

[build-dependencies]
phf = { version = "0.11", default-features = false }
phf_codegen = "0.11"

[package.metadata.docs.rs]
# Document these features on docs.rs
features = ["serde", "visitor"]
101 changes: 101 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
use std::env;
use std::fs::File;
use std::io::{BufWriter, Write};
use std::path::Path;

fn read_keywords() -> Vec<(String, Option<String>)> {
let path = Path::new("src").join("keywords.txt");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is quite clever

if !path.is_file() {
panic!("Missing src/keywords.txt");
}

let data = std::fs::read_to_string(path).expect("Error reading src/keywords.txt");

data.lines()
.filter_map(|line| {
let line = line.trim();
if line.is_empty() || line.starts_with('#') {
return None;
}

let parts = line.split_ascii_whitespace().collect::<Vec<_>>();
if parts.len() == 1 {
Some((parts[0].to_string(), None))
} else if parts.len() == 2 {
Some((parts[0].to_string(), Some(parts[1].to_string())))
} else {
panic!("Invalid keyword: {}", line);
}
})
.collect::<Vec<_>>()
}

fn write_keyword_enum<W>(file: &mut BufWriter<W>, keywords: &[(String, Option<String>)])
where
W: ?Sized + Write,
{
let header = &[
"#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash)]\n",
"#[cfg_attr(feature = \"serde\", derive(Serialize, Deserialize))]\n",
"#[cfg_attr(feature = \"visitor\", derive(Visit, VisitMut))]\n",
"#[allow(non_camel_case_types)]\n",
"pub enum Keyword {\n",
" NoKeyword,\n",
];
let header = header.join("");
write!(file, "{}", header).unwrap();

keywords.iter().for_each(|kw| {
writeln!(file, " {},", kw.0).unwrap();
});

writeln!(file, "}}\n").unwrap();
}

fn write_all_keywords<W>(file: &mut BufWriter<W>, keywords: &[(String, Option<String>)])
where
W: ?Sized + Write,
{
writeln!(file, "pub const ALL_KEYWORDS: &[&str] = &[").unwrap();
keywords.iter().for_each(|kw| {
if kw.1.is_some() {
writeln!(file, " \"{}\",", kw.1.as_ref().unwrap()).unwrap();
} else {
writeln!(file, " \"{}\",", kw.0).unwrap();
}
});
writeln!(file, "];\n").unwrap();
}

fn write_phf_map<W>(file: &mut BufWriter<W>, keywords: &[(String, Option<String>)])
where
W: ?Sized + Write,
{
let map = phf_codegen::Map::new();
let map = keywords.iter().fold(map, |mut map, kw| {
if kw.1.is_some() {
map.entry(kw.1.as_ref().unwrap(), &format!("Keyword::{}", kw.0));
} else {
map.entry(&kw.0, &format!("Keyword::{}", kw.0));
}
map
});

write!(
file,
"static KEYWORD_MAP: phf::Map<&'static str, Keyword> = {}",
map.build()
)
.unwrap();
writeln!(file, ";").unwrap();
}

fn main() {
let keywords = read_keywords();
let path = Path::new(&env::var("OUT_DIR").unwrap()).join("keyword_gen.rs");
let mut file = BufWriter::new(File::create(&path).unwrap());

write_keyword_enum(&mut file, &keywords);
write_all_keywords(&mut file, &keywords);
write_phf_map(&mut file, &keywords);
}
Loading
Loading