Files
rust/crates/ra_tools/src/codegen.rs

52 lines
1.2 KiB
Rust
Raw Normal View History

2019-08-18 21:54:51 +03:00
use std::{collections::BTreeMap, fs, path::Path};
2019-08-18 21:33:31 +03:00
2019-08-18 21:54:51 +03:00
use quote::quote;
2019-08-18 21:44:24 +03:00
use ron;
2019-08-18 21:54:51 +03:00
use serde::Deserialize;
2019-08-18 21:34:55 +03:00
2019-08-18 21:44:24 +03:00
use crate::{project_root, Mode, Result, AST, GRAMMAR};
pub fn generate(mode: Mode) -> Result<()> {
let grammar = project_root().join(GRAMMAR);
// let syntax_kinds = project_root().join(SYNTAX_KINDS);
let ast = project_root().join(AST);
generate_ast(&grammar, &ast, mode)
}
fn generate_ast(grammar_src: &Path, dst: &Path, mode: Mode) -> Result<()> {
2019-08-18 21:54:51 +03:00
let src: Grammar = {
2019-08-18 21:44:24 +03:00
let text = fs::read_to_string(grammar_src)?;
ron::de::from_str(&text)?
};
2019-08-18 21:54:51 +03:00
eprintln!("{:#?}", src);
2019-08-18 21:34:55 +03:00
Ok(())
}
2019-08-18 21:54:51 +03:00
#[derive(Deserialize, Debug)]
struct Grammar {
single_byte_tokens: Vec<(String, String)>,
multi_byte_tokens: Vec<(String, String)>,
keywords: Vec<String>,
contextual_keywords: Vec<String>,
literals: Vec<String>,
tokens: Vec<String>,
ast: BTreeMap<String, AstNode>,
}
#[derive(Deserialize, Debug)]
struct AstNode {
#[serde(default)]
traits: Vec<String>,
#[serde(default)]
collections: Vec<Attr>,
#[serde(default)]
options: Vec<Attr>,
}
#[derive(Deserialize, Debug)]
#[serde(untagged)]
enum Attr {
Type(String),
NameType(String, String),
}