From e5273d33d05196ca215db4b1c4e207328fa5df08 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 3 Feb 2018 12:05:25 +0300 Subject: G: const item --- src/parser/event_parser/grammar/items/consts.rs | 21 ++++++++++++++++ src/parser/event_parser/grammar/items/mod.rs | 33 ++++++++++++++----------- 2 files changed, 40 insertions(+), 14 deletions(-) create mode 100644 src/parser/event_parser/grammar/items/consts.rs (limited to 'src/parser/event_parser/grammar/items') diff --git a/src/parser/event_parser/grammar/items/consts.rs b/src/parser/event_parser/grammar/items/consts.rs new file mode 100644 index 000000000..c9881d681 --- /dev/null +++ b/src/parser/event_parser/grammar/items/consts.rs @@ -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); +} diff --git a/src/parser/event_parser/grammar/items/mod.rs b/src/parser/event_parser/grammar/items/mod.rs index 9930de347..8ccf8f90f 100644 --- a/src/parser/event_parser/grammar/items/mod.rs +++ b/src/parser/event_parser/grammar/items/mod.rs @@ -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(); -- cgit v1.2.3