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/src | |
parent | 229d7943d8471eb114f961ab419c318736772da4 (diff) |
deserialize grammar
Diffstat (limited to 'crates/ra_tools/src')
-rw-r--r-- | crates/ra_tools/src/codegen.rs | 36 |
1 files changed, 33 insertions, 3 deletions
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 | } | ||