From 6983091d6d255bcfd17c4f8c14015d8abc77928d Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 30 Jul 2018 14:06:22 +0300 Subject: Cleanup tools --- src/bin/cli/parse.rs | 20 +++++++++++ src/syntax_kinds/generated.rs | 69 +++++++++++++++++++------------------- src/syntax_kinds/generated.rs.tera | 59 ++++++++++++++++++++++++++++++++ 3 files changed, 113 insertions(+), 35 deletions(-) create mode 100644 src/bin/cli/parse.rs create mode 100644 src/syntax_kinds/generated.rs.tera (limited to 'src') diff --git a/src/bin/cli/parse.rs b/src/bin/cli/parse.rs new file mode 100644 index 000000000..563ea92f6 --- /dev/null +++ b/src/bin/cli/parse.rs @@ -0,0 +1,20 @@ +extern crate libsyntax2; + +use std::io::Read; + +use libsyntax2::{ + parse, utils::dump_tree_green +}; + +fn main() { + let text = read_input(); + let file = parse(text); + let tree = dump_tree_green(&file); + println!("{}", tree); +} + +fn read_input() -> String { + let mut buff = String::new(); + ::std::io::stdin().read_to_string(&mut buff).unwrap(); + buff +} diff --git a/src/syntax_kinds/generated.rs b/src/syntax_kinds/generated.rs index d332fd02e..029972bb3 100644 --- a/src/syntax_kinds/generated.rs +++ b/src/syntax_kinds/generated.rs @@ -1,6 +1,5 @@ #![allow(bad_style, missing_docs, unreachable_pub)] #![cfg_attr(rustfmt, rustfmt_skip)] -//! Generated from grammar.ron use super::SyntaxInfo; /// The kind of syntax node, e.g. `IDENT`, `USE_KW`, or `STRUCT_DEF`. @@ -138,7 +137,6 @@ pub enum SyntaxKind { VALUE_PARAMETER, BLOCK, LET_STMT, - // Technical SyntaxKinds: they appear temporally during parsing, // but never end up in the final tree #[doc(hidden)] @@ -146,7 +144,7 @@ pub enum SyntaxKind { #[doc(hidden)] EOF, } -pub(crate) use self::SyntaxKind::*; +use self::SyntaxKind::*; impl SyntaxKind { pub(crate) fn info(self) -> &'static SyntaxInfo { @@ -289,38 +287,39 @@ impl SyntaxKind { } } pub(crate) fn from_keyword(ident: &str) -> Option { - match ident { - "use" => Some(USE_KW), - "fn" => Some(FN_KW), - "struct" => Some(STRUCT_KW), - "enum" => Some(ENUM_KW), - "trait" => Some(TRAIT_KW), - "impl" => Some(IMPL_KW), - "true" => Some(TRUE_KW), - "false" => Some(FALSE_KW), - "as" => Some(AS_KW), - "extern" => Some(EXTERN_KW), - "crate" => Some(CRATE_KW), - "mod" => Some(MOD_KW), - "pub" => Some(PUB_KW), - "self" => Some(SELF_KW), - "super" => Some(SUPER_KW), - "in" => Some(IN_KW), - "where" => Some(WHERE_KW), - "for" => Some(FOR_KW), - "loop" => Some(LOOP_KW), - "while" => Some(WHILE_KW), - "if" => Some(IF_KW), - "match" => Some(MATCH_KW), - "const" => Some(CONST_KW), - "static" => Some(STATIC_KW), - "mut" => Some(MUT_KW), - "unsafe" => Some(UNSAFE_KW), - "type" => Some(TYPE_KW), - "ref" => Some(REF_KW), - "let" => Some(LET_KW), - _ => None, - } + let kw = match ident { + "use" => USE_KW, + "fn" => FN_KW, + "struct" => STRUCT_KW, + "enum" => ENUM_KW, + "trait" => TRAIT_KW, + "impl" => IMPL_KW, + "true" => TRUE_KW, + "false" => FALSE_KW, + "as" => AS_KW, + "extern" => EXTERN_KW, + "crate" => CRATE_KW, + "mod" => MOD_KW, + "pub" => PUB_KW, + "self" => SELF_KW, + "super" => SUPER_KW, + "in" => IN_KW, + "where" => WHERE_KW, + "for" => FOR_KW, + "loop" => LOOP_KW, + "while" => WHILE_KW, + "if" => IF_KW, + "match" => MATCH_KW, + "const" => CONST_KW, + "static" => STATIC_KW, + "mut" => MUT_KW, + "unsafe" => UNSAFE_KW, + "type" => TYPE_KW, + "ref" => REF_KW, + "let" => LET_KW, + _ => return None, + }; + Some(kw) } } diff --git a/src/syntax_kinds/generated.rs.tera b/src/syntax_kinds/generated.rs.tera new file mode 100644 index 000000000..aa672d89a --- /dev/null +++ b/src/syntax_kinds/generated.rs.tera @@ -0,0 +1,59 @@ +#![allow(bad_style, missing_docs, unreachable_pub)] +#![cfg_attr(rustfmt, rustfmt_skip)] +use super::SyntaxInfo; + +/// The kind of syntax node, e.g. `IDENT`, `USE_KW`, or `STRUCT_DEF`. +#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub enum SyntaxKind { +{%- for t in tokens %} + {{t}}, +{%- endfor -%} +{% for kw in keywords %} + {{kw | upper}}_KW, +{%- endfor -%} +{% for kw in contextual_keywords %} + {{kw | upper}}_KW, +{%- endfor -%} +{% for node in nodes %} + {{node}}, +{%- endfor %} + // Technical SyntaxKinds: they appear temporally during parsing, + // but never end up in the final tree + #[doc(hidden)] + TOMBSTONE, + #[doc(hidden)] + EOF, +} +use self::SyntaxKind::*; + +impl SyntaxKind { + pub(crate) fn info(self) -> &'static SyntaxInfo { + match self { +{%- for t in tokens %} + {{t}} => &SyntaxInfo { name: "{{t}}" }, +{%- endfor -%} +{% for kw in keywords %} + {{kw | upper}}_KW => &SyntaxInfo { name: "{{kw | upper}}_KW" }, +{%- endfor -%} +{% for kw in contextual_keywords %} + {{kw | upper}}_KW => &SyntaxInfo { name: "{{kw | upper}}_KW" }, +{%- endfor -%} +{% for node in nodes %} + {{node}} => &SyntaxInfo { name: "{{node}}" }, +{%- endfor %} + + TOMBSTONE => &SyntaxInfo { name: "TOMBSTONE" }, + EOF => &SyntaxInfo { name: "EOF" }, + } + } + pub(crate) fn from_keyword(ident: &str) -> Option { + let kw = match ident { +{%- for kw in keywords %} + "{{kw}}" => {{kw | upper}}_KW, +{%- endfor %} + _ => return None, + }; + Some(kw) + } +} + -- cgit v1.2.3