mirror of
https://github.com/rust-lang/rust.git
synced 2025-01-20 19:52:48 +00:00
Merge #1257
1257: Implemented tkn! macro for syntax kinds r=matklad a=pasa Implementation of #1248 Co-authored-by: Sergey Parilin <sergey.parilin@fxdd.com>
This commit is contained in:
commit
033a32f349
@ -4,6 +4,7 @@ use ra_syntax::{
|
||||
algo::{find_covering_element, find_token_at_offset, TokenAtOffset},
|
||||
SyntaxKind::*, SyntaxToken,
|
||||
ast::{self, AstNode, AstToken},
|
||||
T
|
||||
};
|
||||
|
||||
use crate::{FileRange, db::RootDatabase};
|
||||
@ -135,7 +136,7 @@ fn pick_best<'a>(l: SyntaxToken<'a>, r: SyntaxToken<'a>) -> SyntaxToken<'a> {
|
||||
fn priority(n: SyntaxToken) -> usize {
|
||||
match n.kind() {
|
||||
WHITESPACE => 0,
|
||||
IDENT | SELF_KW | SUPER_KW | CRATE_KW | LIFETIME => 2,
|
||||
IDENT | T![self] | T![super] | T![crate] | LIFETIME => 2,
|
||||
_ => 1,
|
||||
}
|
||||
}
|
||||
|
@ -14,6 +14,7 @@
|
||||
|
||||
#[macro_use]
|
||||
mod token_set;
|
||||
#[macro_use]
|
||||
mod syntax_kind;
|
||||
mod event;
|
||||
mod parser;
|
||||
|
@ -1,3 +1,4 @@
|
||||
#[macro_use]
|
||||
mod generated;
|
||||
|
||||
use std::fmt;
|
||||
|
@ -241,6 +241,101 @@ pub enum SyntaxKind {
|
||||
}
|
||||
use self::SyntaxKind::*;
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! T {
|
||||
(;) => { $crate::SyntaxKind::SEMI };
|
||||
(,) => { $crate::SyntaxKind::COMMA };
|
||||
(() => { $crate::SyntaxKind::L_PAREN };
|
||||
()) => { $crate::SyntaxKind::R_PAREN };
|
||||
('{') => { $crate::SyntaxKind::L_CURLY };
|
||||
('}') => { $crate::SyntaxKind::R_CURLY };
|
||||
('[') => { $crate::SyntaxKind::L_BRACK };
|
||||
(']') => { $crate::SyntaxKind::R_BRACK };
|
||||
(<) => { $crate::SyntaxKind::L_ANGLE };
|
||||
(>) => { $crate::SyntaxKind::R_ANGLE };
|
||||
(@) => { $crate::SyntaxKind::AT };
|
||||
(#) => { $crate::SyntaxKind::POUND };
|
||||
(~) => { $crate::SyntaxKind::TILDE };
|
||||
(?) => { $crate::SyntaxKind::QUESTION };
|
||||
($) => { $crate::SyntaxKind::DOLLAR };
|
||||
(&) => { $crate::SyntaxKind::AMP };
|
||||
(|) => { $crate::SyntaxKind::PIPE };
|
||||
(+) => { $crate::SyntaxKind::PLUS };
|
||||
(*) => { $crate::SyntaxKind::STAR };
|
||||
(/) => { $crate::SyntaxKind::SLASH };
|
||||
(^) => { $crate::SyntaxKind::CARET };
|
||||
(%) => { $crate::SyntaxKind::PERCENT };
|
||||
(_) => { $crate::SyntaxKind::UNDERSCORE };
|
||||
(.) => { $crate::SyntaxKind::DOT };
|
||||
(..) => { $crate::SyntaxKind::DOTDOT };
|
||||
(...) => { $crate::SyntaxKind::DOTDOTDOT };
|
||||
(..=) => { $crate::SyntaxKind::DOTDOTEQ };
|
||||
(:) => { $crate::SyntaxKind::COLON };
|
||||
(::) => { $crate::SyntaxKind::COLONCOLON };
|
||||
(=) => { $crate::SyntaxKind::EQ };
|
||||
(==) => { $crate::SyntaxKind::EQEQ };
|
||||
(=>) => { $crate::SyntaxKind::FAT_ARROW };
|
||||
(!) => { $crate::SyntaxKind::EXCL };
|
||||
(!=) => { $crate::SyntaxKind::NEQ };
|
||||
(-) => { $crate::SyntaxKind::MINUS };
|
||||
(->) => { $crate::SyntaxKind::THIN_ARROW };
|
||||
(<=) => { $crate::SyntaxKind::LTEQ };
|
||||
(>=) => { $crate::SyntaxKind::GTEQ };
|
||||
(+=) => { $crate::SyntaxKind::PLUSEQ };
|
||||
(-=) => { $crate::SyntaxKind::MINUSEQ };
|
||||
(|=) => { $crate::SyntaxKind::PIPEEQ };
|
||||
(&=) => { $crate::SyntaxKind::AMPEQ };
|
||||
(^=) => { $crate::SyntaxKind::CARETEQ };
|
||||
(/=) => { $crate::SyntaxKind::SLASHEQ };
|
||||
(*=) => { $crate::SyntaxKind::STAREQ };
|
||||
(%=) => { $crate::SyntaxKind::PERCENTEQ };
|
||||
(&&) => { $crate::SyntaxKind::AMPAMP };
|
||||
(||) => { $crate::SyntaxKind::PIPEPIPE };
|
||||
(<<) => { $crate::SyntaxKind::SHL };
|
||||
(>>) => { $crate::SyntaxKind::SHR };
|
||||
(<<=) => { $crate::SyntaxKind::SHLEQ };
|
||||
(>>=) => { $crate::SyntaxKind::SHREQ };
|
||||
(async) => { $crate::SyntaxKind::ASYNC_KW };
|
||||
(use) => { $crate::SyntaxKind::USE_KW };
|
||||
(fn) => { $crate::SyntaxKind::FN_KW };
|
||||
(struct) => { $crate::SyntaxKind::STRUCT_KW };
|
||||
(enum) => { $crate::SyntaxKind::ENUM_KW };
|
||||
(trait) => { $crate::SyntaxKind::TRAIT_KW };
|
||||
(impl) => { $crate::SyntaxKind::IMPL_KW };
|
||||
(dyn) => { $crate::SyntaxKind::DYN_KW };
|
||||
(true) => { $crate::SyntaxKind::TRUE_KW };
|
||||
(false) => { $crate::SyntaxKind::FALSE_KW };
|
||||
(as) => { $crate::SyntaxKind::AS_KW };
|
||||
(extern) => { $crate::SyntaxKind::EXTERN_KW };
|
||||
(crate) => { $crate::SyntaxKind::CRATE_KW };
|
||||
(mod) => { $crate::SyntaxKind::MOD_KW };
|
||||
(pub) => { $crate::SyntaxKind::PUB_KW };
|
||||
(self) => { $crate::SyntaxKind::SELF_KW };
|
||||
(super) => { $crate::SyntaxKind::SUPER_KW };
|
||||
(in) => { $crate::SyntaxKind::IN_KW };
|
||||
(where) => { $crate::SyntaxKind::WHERE_KW };
|
||||
(for) => { $crate::SyntaxKind::FOR_KW };
|
||||
(loop) => { $crate::SyntaxKind::LOOP_KW };
|
||||
(while) => { $crate::SyntaxKind::WHILE_KW };
|
||||
(continue) => { $crate::SyntaxKind::CONTINUE_KW };
|
||||
(break) => { $crate::SyntaxKind::BREAK_KW };
|
||||
(if) => { $crate::SyntaxKind::IF_KW };
|
||||
(else) => { $crate::SyntaxKind::ELSE_KW };
|
||||
(match) => { $crate::SyntaxKind::MATCH_KW };
|
||||
(const) => { $crate::SyntaxKind::CONST_KW };
|
||||
(static) => { $crate::SyntaxKind::STATIC_KW };
|
||||
(mut) => { $crate::SyntaxKind::MUT_KW };
|
||||
(unsafe) => { $crate::SyntaxKind::UNSAFE_KW };
|
||||
(type) => { $crate::SyntaxKind::TYPE_KW };
|
||||
(ref) => { $crate::SyntaxKind::REF_KW };
|
||||
(let) => { $crate::SyntaxKind::LET_KW };
|
||||
(move) => { $crate::SyntaxKind::MOVE_KW };
|
||||
(return) => { $crate::SyntaxKind::RETURN_KW };
|
||||
(auto) => { $crate::SyntaxKind::AUTO_KW };
|
||||
(default) => { $crate::SyntaxKind::DEFAULT_KW };
|
||||
(union) => { $crate::SyntaxKind::UNION_KW };
|
||||
}
|
||||
|
||||
impl From<u16> for SyntaxKind {
|
||||
fn from(d: u16) -> SyntaxKind {
|
||||
assert!(d <= (__LAST as u16));
|
||||
|
@ -33,6 +33,20 @@ pub enum SyntaxKind {
|
||||
}
|
||||
use self::SyntaxKind::*;
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! T {
|
||||
{%- for t in concat(a=single_byte_tokens, b=multi_byte_tokens) %}
|
||||
{%- if t.0 == '{' or t.0 == '}' or t.0 == '[' or t.0 == ']' %}
|
||||
('{{t.0}}') => { $crate::SyntaxKind::{{t.1}} };
|
||||
{%- else %}
|
||||
({{t.0}}) => { $crate::SyntaxKind::{{t.1}} };
|
||||
{%- endif %}
|
||||
{%- endfor -%}
|
||||
{% for kw in concat(a=keywords, b=contextual_keywords) %}
|
||||
({{kw}}) => { $crate::SyntaxKind::{{kw | upper}}_KW };
|
||||
{%- endfor %}
|
||||
}
|
||||
|
||||
impl From<u16> for SyntaxKind {
|
||||
fn from(d: u16) -> SyntaxKind {
|
||||
assert!(d <= (__LAST as u16));
|
||||
|
@ -33,6 +33,7 @@ pub mod fuzz;
|
||||
|
||||
pub use rowan::{SmolStr, TextRange, TextUnit};
|
||||
pub use ra_parser::SyntaxKind;
|
||||
pub use ra_parser::T;
|
||||
pub use crate::{
|
||||
ast::AstNode,
|
||||
syntax_error::{SyntaxError, SyntaxErrorKind, Location},
|
||||
|
Loading…
Reference in New Issue
Block a user