From 966e9db2b83802dfb55d55bd3a26e69dced1bbd7 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 1 Aug 2018 10:40:07 +0300 Subject: Extract libeditor --- src/algo/walk.rs | 1 - src/ast.rs | 2 +- src/lib.rs | 2 +- src/parser_impl/event.rs | 2 +- src/parser_impl/mod.rs | 8 ++++---- src/syntax_kinds/generated.rs | 41 ++++++++++++++++++++++++++++++++++++++ src/syntax_kinds/generated.rs.tera | 10 ++++++++++ src/yellow/builder.rs | 10 ++++------ 8 files changed, 62 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/algo/walk.rs b/src/algo/walk.rs index a50ec2a09..ad0f2d8fb 100644 --- a/src/algo/walk.rs +++ b/src/algo/walk.rs @@ -6,7 +6,6 @@ pub fn preorder<'a>(root: SyntaxNodeRef<'a>) -> impl Iterator None, }) } - #[derive(Debug, Copy, Clone)] pub enum WalkEvent<'a> { Enter(SyntaxNodeRef<'a>), diff --git a/src/ast.rs b/src/ast.rs index 48e1d23ac..caf5fb7ef 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -9,7 +9,7 @@ pub struct File> { impl File> { pub fn parse(text: &str) -> Self { File { - syntax: ::parse(text.to_owned()), + syntax: ::parse(text), } } } diff --git a/src/lib.rs b/src/lib.rs index 8f25de9a4..611b87492 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -45,7 +45,7 @@ pub use { yellow::{SyntaxNode, SyntaxNodeRef, SyntaxRoot, TreeRoot}, }; -pub fn parse(text: String) -> SyntaxNode { +pub fn parse(text: &str) -> SyntaxNode { let tokens = tokenize(&text); parser_impl::parse::(text, &tokens) } diff --git a/src/parser_impl/event.rs b/src/parser_impl/event.rs index eb5d0a4be..66a0b6fc0 100644 --- a/src/parser_impl/event.rs +++ b/src/parser_impl/event.rs @@ -76,7 +76,7 @@ pub(crate) enum Event { }, } -pub(super) fn process(builder: &mut impl Sink, tokens: &[Token], events: Vec) { +pub(super) fn process<'a>(builder: &mut impl Sink<'a>, tokens: &[Token], events: Vec) { let mut idx = 0; let mut holes = Vec::new(); diff --git a/src/parser_impl/mod.rs b/src/parser_impl/mod.rs index b58094be3..2791c8da5 100644 --- a/src/parser_impl/mod.rs +++ b/src/parser_impl/mod.rs @@ -14,10 +14,10 @@ use { use SyntaxKind::{self, EOF, TOMBSTONE}; -pub(crate) trait Sink { +pub(crate) trait Sink<'a> { type Tree; - fn new(text: String) -> Self; + fn new(text: &'a str) -> Self; fn leaf(&mut self, kind: SyntaxKind, len: TextUnit); fn start_internal(&mut self, kind: SyntaxKind); @@ -27,9 +27,9 @@ pub(crate) trait Sink { } /// Parse a sequence of tokens into the representative node tree -pub(crate) fn parse(text: String, tokens: &[Token]) -> S::Tree { +pub(crate) fn parse<'a, S: Sink<'a>>(text: &'a str, tokens: &[Token]) -> S::Tree { let events = { - let input = input::ParserInput::new(&text, tokens); + let input = input::ParserInput::new(text, tokens); let parser_impl = ParserImpl::new(&input); let mut parser_api = Parser(parser_impl); grammar::file(&mut parser_api); diff --git a/src/syntax_kinds/generated.rs b/src/syntax_kinds/generated.rs index 435be781c..cbcd0c4e1 100644 --- a/src/syntax_kinds/generated.rs +++ b/src/syntax_kinds/generated.rs @@ -169,6 +169,47 @@ pub enum SyntaxKind { use self::SyntaxKind::*; impl SyntaxKind { + pub fn is_keyword(self) -> bool { + match self { + | USE_KW + | FN_KW + | STRUCT_KW + | ENUM_KW + | TRAIT_KW + | IMPL_KW + | TRUE_KW + | FALSE_KW + | AS_KW + | EXTERN_KW + | CRATE_KW + | MOD_KW + | PUB_KW + | SELF_KW + | SUPER_KW + | IN_KW + | WHERE_KW + | FOR_KW + | LOOP_KW + | WHILE_KW + | IF_KW + | ELSE_KW + | MATCH_KW + | CONST_KW + | STATIC_KW + | MUT_KW + | UNSAFE_KW + | TYPE_KW + | REF_KW + | LET_KW + | MOVE_KW + | AUTO_KW + | DEFAULT_KW + | UNION_KW + => true, + _ => false + } + } + pub(crate) fn info(self) -> &'static SyntaxInfo { match self { SEMI => &SyntaxInfo { name: "SEMI" }, diff --git a/src/syntax_kinds/generated.rs.tera b/src/syntax_kinds/generated.rs.tera index d719c8312..4e2ee56c3 100644 --- a/src/syntax_kinds/generated.rs.tera +++ b/src/syntax_kinds/generated.rs.tera @@ -24,6 +24,16 @@ pub enum SyntaxKind { use self::SyntaxKind::*; impl SyntaxKind { + pub fn is_keyword(self) -> bool { + match self { +{%- for kw in concat(a=keywords, b=contextual_keywords) %} + | {{kw | upper}}_KW +{%- endfor %} + => true, + _ => false + } + } + pub(crate) fn info(self) -> &'static SyntaxInfo { match self { {%- for t in concat(a=single_byte_tokens, b=multi_byte_tokens) %} diff --git a/src/yellow/builder.rs b/src/yellow/builder.rs index b68ba789e..dbe646cd7 100644 --- a/src/yellow/builder.rs +++ b/src/yellow/builder.rs @@ -4,20 +4,18 @@ use { SyntaxKind, TextRange, TextUnit, }; -pub(crate) struct GreenBuilder { - text: String, +pub(crate) struct GreenBuilder<'a> { + text: &'a str, stack: Vec, pos: TextUnit, root: Option, errors: Vec, } -impl GreenBuilder {} - -impl Sink for GreenBuilder { +impl<'a> Sink<'a> for GreenBuilder<'a> { type Tree = SyntaxNode; - fn new(text: String) -> Self { + fn new(text: &'a str) -> Self { GreenBuilder { text, stack: Vec::new(), -- cgit v1.2.3