diff options
author | Aleksey Kladov <[email protected]> | 2019-08-18 19:54:51 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2019-08-18 19:54:51 +0100 |
commit | d545a5c75cb181758dd745b031eacfd7fc8a6929 (patch) | |
tree | 579057500a85b87fdded27394224246d3803d9ee /crates/ra_tools | |
parent | 229d7943d8471eb114f961ab419c318736772da4 (diff) |
deserialize grammar
Diffstat (limited to 'crates/ra_tools')
-rw-r--r-- | crates/ra_tools/Cargo.toml | 1 | ||||
-rw-r--r-- | crates/ra_tools/src/codegen.rs | 36 |
2 files changed, 34 insertions, 3 deletions
diff --git a/crates/ra_tools/Cargo.toml b/crates/ra_tools/Cargo.toml index 02bab8f52..ab9fa5d86 100644 --- a/crates/ra_tools/Cargo.toml +++ b/crates/ra_tools/Cargo.toml | |||
@@ -12,3 +12,4 @@ itertools = "0.8.0" | |||
12 | clap = "2.32.0" | 12 | clap = "2.32.0" |
13 | quote = "1.0.2" | 13 | quote = "1.0.2" |
14 | ron = "0.5.1" | 14 | ron = "0.5.1" |
15 | serde = { version = "1.0.0", features = ["derive"] } | ||
diff --git a/crates/ra_tools/src/codegen.rs b/crates/ra_tools/src/codegen.rs index 405fb623c..f0a54808a 100644 --- a/crates/ra_tools/src/codegen.rs +++ b/crates/ra_tools/src/codegen.rs | |||
@@ -1,6 +1,8 @@ | |||
1 | use std::{fs, path::Path}; | 1 | use std::{collections::BTreeMap, fs, path::Path}; |
2 | 2 | ||
3 | use quote::quote; | ||
3 | use ron; | 4 | use ron; |
5 | use serde::Deserialize; | ||
4 | 6 | ||
5 | use crate::{project_root, Mode, Result, AST, GRAMMAR}; | 7 | use crate::{project_root, Mode, Result, AST, GRAMMAR}; |
6 | 8 | ||
@@ -12,10 +14,38 @@ pub fn generate(mode: Mode) -> Result<()> { | |||
12 | } | 14 | } |
13 | 15 | ||
14 | fn generate_ast(grammar_src: &Path, dst: &Path, mode: Mode) -> Result<()> { | 16 | fn generate_ast(grammar_src: &Path, dst: &Path, mode: Mode) -> Result<()> { |
15 | let src: ron::Value = { | 17 | let src: Grammar = { |
16 | let text = fs::read_to_string(grammar_src)?; | 18 | let text = fs::read_to_string(grammar_src)?; |
17 | ron::de::from_str(&text)? | 19 | ron::de::from_str(&text)? |
18 | }; | 20 | }; |
19 | eprintln!("{:?}", src); | 21 | eprintln!("{:#?}", src); |
20 | Ok(()) | 22 | Ok(()) |
21 | } | 23 | } |
24 | |||
25 | #[derive(Deserialize, Debug)] | ||
26 | struct Grammar { | ||
27 | single_byte_tokens: Vec<(String, String)>, | ||
28 | multi_byte_tokens: Vec<(String, String)>, | ||
29 | keywords: Vec<String>, | ||
30 | contextual_keywords: Vec<String>, | ||
31 | literals: Vec<String>, | ||
32 | tokens: Vec<String>, | ||
33 | ast: BTreeMap<String, AstNode>, | ||
34 | } | ||
35 | |||
36 | #[derive(Deserialize, Debug)] | ||
37 | struct AstNode { | ||
38 | #[serde(default)] | ||
39 | traits: Vec<String>, | ||
40 | #[serde(default)] | ||
41 | collections: Vec<Attr>, | ||
42 | #[serde(default)] | ||
43 | options: Vec<Attr>, | ||
44 | } | ||
45 | |||
46 | #[derive(Deserialize, Debug)] | ||
47 | #[serde(untagged)] | ||
48 | enum Attr { | ||
49 | Type(String), | ||
50 | NameType(String, String), | ||
51 | } | ||