Intern static tokens

This commit is contained in:
Aleksey Kladov 2018-07-30 18:11:33 +03:00
parent 60e8a845ca
commit beaddb4780
3 changed files with 101 additions and 6 deletions

View file

@ -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)
}
}

View file

@ -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,
};

View file

@ -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<str> },
Token { kind: SyntaxKind, text: Option<Arc<str>> },
}
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,
},
}
}