G: const item
This commit is contained in:
parent
5e7504b978
commit
e5273d33d0
8 changed files with 114 additions and 14 deletions
21
src/parser/event_parser/grammar/items/consts.rs
Normal file
21
src/parser/event_parser/grammar/items/consts.rs
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
use super::*;
|
||||
|
||||
pub(super) fn static_item(p: &mut Parser) {
|
||||
const_or_static(p, STATIC_KW)
|
||||
}
|
||||
|
||||
pub(super) fn const_item(p: &mut Parser) {
|
||||
const_or_static(p, CONST_KW)
|
||||
}
|
||||
|
||||
fn const_or_static(p: &mut Parser, kw: SyntaxKind) {
|
||||
assert!(p.at(kw));
|
||||
p.bump();
|
||||
p.eat(MUT_KW); // TODO: validator to forbid const mut
|
||||
p.expect(IDENT);
|
||||
p.expect(COLON);
|
||||
types::type_ref(p);
|
||||
p.expect(EQ);
|
||||
expressions::expr(p);
|
||||
p.expect(SEMI);
|
||||
}
|
||||
|
|
@ -2,6 +2,7 @@ use super::*;
|
|||
|
||||
mod structs;
|
||||
mod use_item;
|
||||
mod consts;
|
||||
|
||||
pub(super) fn mod_contents(p: &mut Parser, stop_on_r_curly: bool) {
|
||||
attributes::inner_attributes(p);
|
||||
|
|
@ -47,9 +48,26 @@ fn item(p: &mut Parser) {
|
|||
}
|
||||
}
|
||||
STATIC_KW => {
|
||||
static_item(p);
|
||||
consts::static_item(p);
|
||||
STATIC_ITEM
|
||||
}
|
||||
CONST_KW => match p.nth(1) {
|
||||
FN_KW => {
|
||||
p.bump();
|
||||
fn_item(p);
|
||||
FN_ITEM
|
||||
}
|
||||
UNSAFE_KW if p.nth(2) == FN_KW => {
|
||||
p.bump();
|
||||
p.bump();
|
||||
fn_item(p);
|
||||
FN_ITEM
|
||||
}
|
||||
_ => {
|
||||
consts::const_item(p);
|
||||
CONST_ITEM
|
||||
}
|
||||
},
|
||||
MOD_KW => {
|
||||
mod_item(p);
|
||||
MOD_ITEM
|
||||
|
|
@ -101,19 +119,6 @@ fn extern_block(p: &mut Parser) {
|
|||
p.bump();
|
||||
p.expect(R_CURLY);
|
||||
}
|
||||
|
||||
fn static_item(p: &mut Parser) {
|
||||
assert!(p.at(STATIC_KW));
|
||||
p.bump();
|
||||
p.eat(MUT_KW);
|
||||
p.expect(IDENT);
|
||||
p.expect(COLON);
|
||||
types::type_ref(p);
|
||||
p.expect(EQ);
|
||||
expressions::expr(p);
|
||||
p.expect(SEMI);
|
||||
}
|
||||
|
||||
fn mod_item(p: &mut Parser) {
|
||||
assert!(p.at(MOD_KW));
|
||||
p.bump();
|
||||
|
|
|
|||
|
|
@ -31,6 +31,7 @@ pub enum SyntaxKind {
|
|||
CONST_KW,
|
||||
STATIC_KW,
|
||||
MUT_KW,
|
||||
UNSAFE_KW,
|
||||
ERROR,
|
||||
IDENT,
|
||||
UNDERSCORE,
|
||||
|
|
@ -90,6 +91,7 @@ pub enum SyntaxKind {
|
|||
MOD_ITEM,
|
||||
USE_ITEM,
|
||||
STATIC_ITEM,
|
||||
CONST_ITEM,
|
||||
EXTERN_BLOCK,
|
||||
ENUM_VARIANT,
|
||||
NAMED_FIELD,
|
||||
|
|
@ -144,6 +146,7 @@ impl SyntaxKind {
|
|||
CONST_KW => &SyntaxInfo { name: "CONST_KW" },
|
||||
STATIC_KW => &SyntaxInfo { name: "STATIC_KW" },
|
||||
MUT_KW => &SyntaxInfo { name: "MUT_KW" },
|
||||
UNSAFE_KW => &SyntaxInfo { name: "UNSAFE_KW" },
|
||||
ERROR => &SyntaxInfo { name: "ERROR" },
|
||||
IDENT => &SyntaxInfo { name: "IDENT" },
|
||||
UNDERSCORE => &SyntaxInfo { name: "UNDERSCORE" },
|
||||
|
|
@ -203,6 +206,7 @@ impl SyntaxKind {
|
|||
MOD_ITEM => &SyntaxInfo { name: "MOD_ITEM" },
|
||||
USE_ITEM => &SyntaxInfo { name: "USE_ITEM" },
|
||||
STATIC_ITEM => &SyntaxInfo { name: "STATIC_ITEM" },
|
||||
CONST_ITEM => &SyntaxInfo { name: "CONST_ITEM" },
|
||||
EXTERN_BLOCK => &SyntaxInfo { name: "EXTERN_BLOCK" },
|
||||
ENUM_VARIANT => &SyntaxInfo { name: "ENUM_VARIANT" },
|
||||
NAMED_FIELD => &SyntaxInfo { name: "NAMED_FIELD" },
|
||||
|
|
@ -253,6 +257,7 @@ pub(crate) fn ident_to_keyword(ident: &str) -> Option<SyntaxKind> {
|
|||
"const" => Some(CONST_KW),
|
||||
"static" => Some(STATIC_KW),
|
||||
"mut" => Some(MUT_KW),
|
||||
"unsafe" => Some(UNSAFE_KW),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue