From beaddb478097223c87e507bf9367d85d86df5d06 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 30 Jul 2018 18:11:33 +0300 Subject: Intern static tokens --- src/syntax_kinds/generated.rs | 75 ++++++++++++++++++++++++++++++++++++++ src/syntax_kinds/generated.rs.tera | 13 +++++++ src/yellow/green.rs | 19 +++++++--- 3 files changed, 101 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/syntax_kinds/generated.rs b/src/syntax_kinds/generated.rs index 9dcf8992f..571b64af4 100644 --- a/src/syntax_kinds/generated.rs +++ b/src/syntax_kinds/generated.rs @@ -353,5 +353,80 @@ impl SyntaxKind { }; Some(tok) } + + pub(crate) fn static_text(self) -> Option<&'static str> { + let tok = match self { + SEMI => ";", + COMMA => ",", + L_PAREN => "(", + R_PAREN => ")", + L_CURLY => "{", + R_CURLY => "}", + L_BRACK => "[", + R_BRACK => "]", + L_ANGLE => "<", + R_ANGLE => ">", + AT => "@", + POUND => "#", + TILDE => "~", + QUESTION => "?", + DOLLAR => "$", + AMPERSAND => "&", + PIPE => "|", + PLUS => "+", + STAR => "*", + SLASH => "/", + CARET => "^", + PERCENT => "%", + DOT => ".", + DOTDOT => "..", + DOTDOTDOT => "...", + DOTDOTEQ => "..=", + COLON => ":", + COLONCOLON => "::", + EQ => "=", + EQEQ => "==", + FAT_ARROW => "=>", + EXCL => "!", + NEQ => "!=", + MINUS => "-", + THIN_ARROW => "->", + + USE_KW => "use", + FN_KW => "fn", + STRUCT_KW => "struct", + ENUM_KW => "enum", + TRAIT_KW => "trait", + IMPL_KW => "impl", + TRUE_KW => "true", + FALSE_KW => "false", + AS_KW => "as", + EXTERN_KW => "extern", + CRATE_KW => "crate", + MOD_KW => "mod", + PUB_KW => "pub", + SELF_KW => "self", + SUPER_KW => "super", + IN_KW => "in", + WHERE_KW => "where", + FOR_KW => "for", + LOOP_KW => "loop", + WHILE_KW => "while", + IF_KW => "if", + MATCH_KW => "match", + CONST_KW => "const", + STATIC_KW => "static", + MUT_KW => "mut", + UNSAFE_KW => "unsafe", + TYPE_KW => "type", + REF_KW => "ref", + LET_KW => "let", + AUTO_KW => "auto", + DEFAULT_KW => "default", + UNION_KW => "union", + _ => return None, + }; + Some(tok) + } } diff --git a/src/syntax_kinds/generated.rs.tera b/src/syntax_kinds/generated.rs.tera index 2a47c6632..d719c8312 100644 --- a/src/syntax_kinds/generated.rs.tera +++ b/src/syntax_kinds/generated.rs.tera @@ -53,6 +53,19 @@ impl SyntaxKind { let tok = match c { {%- for t in single_byte_tokens %} '{{t.0}}' => {{t.1}}, +{%- endfor %} + _ => return None, + }; + Some(tok) + } + + pub(crate) fn static_text(self) -> Option<&'static str> { + let tok = match self { +{%- for t in concat(a=single_byte_tokens, b=multi_byte_tokens) %} + {{t.1}} => "{{t.0}}", +{%- endfor %} +{% for kw in concat(a=keywords, b=contextual_keywords) %} + {{kw | upper}}_KW => "{{kw}}", {%- endfor %} _ => return None, }; diff --git a/src/yellow/green.rs b/src/yellow/green.rs index 507e4d57e..cda4e2167 100644 --- a/src/yellow/green.rs +++ b/src/yellow/green.rs @@ -81,7 +81,7 @@ fn assert_send_sync() { #[derive(Clone, Debug)] pub(crate) enum GreenLeaf { Whitespace { newlines: u8, spaces: u8 }, - Token { kind: SyntaxKind, text: Arc }, + Token { kind: SyntaxKind, text: Option> }, } impl GreenLeaf { @@ -96,10 +96,14 @@ impl GreenLeaf { }; } } - GreenLeaf::Token { - kind, - text: text.to_owned().into_boxed_str().into(), - } + let text = match SyntaxKind::static_text(kind) { + Some(t) => { + debug_assert_eq!(t, text); + None + } + None => Some(text.to_owned().into_boxed_str().into()), + }; + GreenLeaf::Token { kind, text } } pub(crate) fn kind(&self) -> SyntaxKind { @@ -117,7 +121,10 @@ impl GreenLeaf { assert!(newlines <= N_NEWLINES && spaces <= N_SPACES); &WS[N_NEWLINES - newlines..N_NEWLINES + spaces] } - GreenLeaf::Token { text, .. } => text, + GreenLeaf::Token { kind, text, } => match text { + None => kind.static_text().unwrap(), + Some(t) => t, + }, } } -- cgit v1.2.3