aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-08-18 19:54:51 +0100
committerAleksey Kladov <[email protected]>2019-08-18 19:54:51 +0100
commitd545a5c75cb181758dd745b031eacfd7fc8a6929 (patch)
tree579057500a85b87fdded27394224246d3803d9ee /crates
parent229d7943d8471eb114f961ab419c318736772da4 (diff)
deserialize grammar
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_syntax/src/grammar.ron106
-rw-r--r--crates/ra_tools/Cargo.toml1
-rw-r--r--crates/ra_tools/src/codegen.rs36
3 files changed, 87 insertions, 56 deletions
diff --git a/crates/ra_syntax/src/grammar.ron b/crates/ra_syntax/src/grammar.ron
index f2c20573e..a18810253 100644
--- a/crates/ra_syntax/src/grammar.ron
+++ b/crates/ra_syntax/src/grammar.ron
@@ -2,61 +2,61 @@
2// See `cargo gen-syntax` (defined in crates/tools/src/main.rs) 2// See `cargo gen-syntax` (defined in crates/tools/src/main.rs)
3Grammar( 3Grammar(
4 single_byte_tokens: [ 4 single_byte_tokens: [
5 [";", "SEMI"], 5 (";", "SEMI"),
6 [",", "COMMA"], 6 (",", "COMMA"),
7 ["(", "L_PAREN"], 7 ("(", "L_PAREN"),
8 [")", "R_PAREN"], 8 (")", "R_PAREN"),
9 ["{", "L_CURLY"], 9 ("{", "L_CURLY"),
10 ["}", "R_CURLY"], 10 ("}", "R_CURLY"),
11 ["[", "L_BRACK"], 11 ("[", "L_BRACK"),
12 ["]", "R_BRACK"], 12 ("]", "R_BRACK"),
13 ["<", "L_ANGLE"], 13 ("<", "L_ANGLE"),
14 [">", "R_ANGLE"], 14 (">", "R_ANGLE"),
15 ["@", "AT"], 15 ("@", "AT"),
16 ["#", "POUND"], 16 ("#", "POUND"),
17 ["~", "TILDE"], 17 ("~", "TILDE"),
18 ["?", "QUESTION"], 18 ("?", "QUESTION"),
19 ["$", "DOLLAR"], 19 ("$", "DOLLAR"),
20 ["&", "AMP"], 20 ("&", "AMP"),
21 ["|", "PIPE"], 21 ("|", "PIPE"),
22 ["+", "PLUS"], 22 ("+", "PLUS"),
23 ["*", "STAR"], 23 ("*", "STAR"),
24 ["/", "SLASH"], 24 ("/", "SLASH"),
25 ["^", "CARET"], 25 ("^", "CARET"),
26 ["%", "PERCENT"], 26 ("%", "PERCENT"),
27 ["_", "UNDERSCORE"], 27 ("_", "UNDERSCORE"),
28 ], 28 ],
29 // Tokens for which the longest match must be chosen (e.g. `..` is a DOTDOT, but `.` is a DOT) 29 // Tokens for which the longest match must be chosen (e.g. `..` is a DOTDOT, but `.` is a DOT)
30 multi_byte_tokens: [ 30 multi_byte_tokens: [
31 [".", "DOT"], 31 (".", "DOT"),
32 ["..", "DOTDOT"], 32 ("..", "DOTDOT"),
33 ["...", "DOTDOTDOT"], 33 ("...", "DOTDOTDOT"),
34 ["..=", "DOTDOTEQ"], 34 ("..=", "DOTDOTEQ"),
35 [":", "COLON"], 35 (":", "COLON"),
36 ["::", "COLONCOLON"], 36 ("::", "COLONCOLON"),
37 ["=", "EQ"], 37 ("=", "EQ"),
38 ["==", "EQEQ"], 38 ("==", "EQEQ"),
39 ["=>", "FAT_ARROW"], 39 ("=>", "FAT_ARROW"),
40 ["!", "EXCL"], 40 ("!", "EXCL"),
41 ["!=", "NEQ"], 41 ("!=", "NEQ"),
42 ["-", "MINUS"], 42 ("-", "MINUS"),
43 ["->", "THIN_ARROW"], 43 ("->", "THIN_ARROW"),
44 ["<=", "LTEQ"], 44 ("<=", "LTEQ"),
45 [">=", "GTEQ"], 45 (">=", "GTEQ"),
46 ["+=", "PLUSEQ"], 46 ("+=", "PLUSEQ"),
47 ["-=", "MINUSEQ"], 47 ("-=", "MINUSEQ"),
48 ["|=", "PIPEEQ"], 48 ("|=", "PIPEEQ"),
49 ["&=", "AMPEQ"], 49 ("&=", "AMPEQ"),
50 ["^=", "CARETEQ"], 50 ("^=", "CARETEQ"),
51 ["/=", "SLASHEQ"], 51 ("/=", "SLASHEQ"),
52 ["*=", "STAREQ"], 52 ("*=", "STAREQ"),
53 ["%=", "PERCENTEQ"], 53 ("%=", "PERCENTEQ"),
54 ["&&", "AMPAMP"], 54 ("&&", "AMPAMP"),
55 ["||", "PIPEPIPE"], 55 ("||", "PIPEPIPE"),
56 ["<<", "SHL"], 56 ("<<", "SHL"),
57 [">>", "SHR"], 57 (">>", "SHR"),
58 ["<<=", "SHLEQ"], 58 ("<<=", "SHLEQ"),
59 [">>=", "SHREQ"], 59 (">>=", "SHREQ"),
60 ], 60 ],
61 keywords: [ 61 keywords: [
62 "async", 62 "async",
@@ -692,7 +692,7 @@ Grammar(
692 "LifetimeArg": (), 692 "LifetimeArg": (),
693 693
694 "MacroItems": ( 694 "MacroItems": (
695 traits: [ "ModuleItemOwner", "FnDefOwner" ], 695 traits: [ "ModuleItemOwner", "FnDefOwner" ],
696 ), 696 ),
697 697
698 "MacroStmts" : ( 698 "MacroStmts" : (
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"
12clap = "2.32.0" 12clap = "2.32.0"
13quote = "1.0.2" 13quote = "1.0.2"
14ron = "0.5.1" 14ron = "0.5.1"
15serde = { 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 @@
1use std::{fs, path::Path}; 1use std::{collections::BTreeMap, fs, path::Path};
2 2
3use quote::quote;
3use ron; 4use ron;
5use serde::Deserialize;
4 6
5use crate::{project_root, Mode, Result, AST, GRAMMAR}; 7use crate::{project_root, Mode, Result, AST, GRAMMAR};
6 8
@@ -12,10 +14,38 @@ pub fn generate(mode: Mode) -> Result<()> {
12} 14}
13 15
14fn generate_ast(grammar_src: &Path, dst: &Path, mode: Mode) -> Result<()> { 16fn 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)]
26struct 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)]
37struct 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)]
48enum Attr {
49 Type(String),
50 NameType(String, String),
51}