From 049c9b60fb0c59625a37b0349aa27acaa3cf884a Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 2 Feb 2018 23:30:17 +0300 Subject: [PATCH 1/2] L: const & static keywords --- grammar.ron | 4 +++- src/syntax_kinds.rs | 6 ++++++ tests/data/lexer/0011_keywords.rs | 3 ++- tests/data/lexer/0011_keywords.txt | 6 +++++- 4 files changed, 16 insertions(+), 3 deletions(-) diff --git a/grammar.ron b/grammar.ron index 44826c779c..e7acd3cb03 100644 --- a/grammar.ron +++ b/grammar.ron @@ -21,7 +21,9 @@ Grammar( "loop", "while", "if", - "match" + "match", + "const", + "static", ], tokens: [ "ERROR", diff --git a/src/syntax_kinds.rs b/src/syntax_kinds.rs index 7577fa0371..f5d01c6f3e 100644 --- a/src/syntax_kinds.rs +++ b/src/syntax_kinds.rs @@ -28,6 +28,8 @@ pub enum SyntaxKind { WHILE_KW, IF_KW, MATCH_KW, + CONST_KW, + STATIC_KW, ERROR, IDENT, UNDERSCORE, @@ -137,6 +139,8 @@ impl SyntaxKind { WHILE_KW => &SyntaxInfo { name: "WHILE_KW" }, IF_KW => &SyntaxInfo { name: "IF_KW" }, MATCH_KW => &SyntaxInfo { name: "MATCH_KW" }, + CONST_KW => &SyntaxInfo { name: "CONST_KW" }, + STATIC_KW => &SyntaxInfo { name: "STATIC_KW" }, ERROR => &SyntaxInfo { name: "ERROR" }, IDENT => &SyntaxInfo { name: "IDENT" }, UNDERSCORE => &SyntaxInfo { name: "UNDERSCORE" }, @@ -242,6 +246,8 @@ pub(crate) fn ident_to_keyword(ident: &str) -> Option { "while" => Some(WHILE_KW), "if" => Some(IF_KW), "match" => Some(MATCH_KW), + "const" => Some(CONST_KW), + "static" => Some(STATIC_KW), _ => None, } } diff --git a/tests/data/lexer/0011_keywords.rs b/tests/data/lexer/0011_keywords.rs index 02ca190891..7a1000bb7f 100644 --- a/tests/data/lexer/0011_keywords.rs +++ b/tests/data/lexer/0011_keywords.rs @@ -1 +1,2 @@ -fn use struct trait enum impl true false as extern crate mod pub self super in where for loop while if match +fn use struct trait enum impl true false as extern crate +mod pub self super in where for loop while if match const static diff --git a/tests/data/lexer/0011_keywords.txt b/tests/data/lexer/0011_keywords.txt index 964e3475a3..b38b4bd644 100644 --- a/tests/data/lexer/0011_keywords.txt +++ b/tests/data/lexer/0011_keywords.txt @@ -19,7 +19,7 @@ WHITESPACE 1 " " EXTERN_KW 6 "extern" WHITESPACE 1 " " CRATE_KW 5 "crate" -WHITESPACE 1 " " +WHITESPACE 1 "\n" MOD_KW 3 "mod" WHITESPACE 1 " " PUB_KW 3 "pub" @@ -41,4 +41,8 @@ WHITESPACE 1 " " IF_KW 2 "if" WHITESPACE 1 " " MATCH_KW 5 "match" +WHITESPACE 1 " " +CONST_KW 5 "const" +WHITESPACE 1 " " +STATIC_KW 6 "static" WHITESPACE 1 "\n" From 0000f4f4f2f32c6ff67db6cbc27e3a49c85f4b99 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 2 Feb 2018 23:45:15 +0300 Subject: [PATCH 2/2] G: static items --- grammar.ron | 2 ++ src/parser/event_parser/grammar/items/mod.rs | 28 ++++++++++++++---- src/syntax_kinds.rs | 5 ++++ tests/data/lexer/0011_keywords.rs | 3 +- tests/data/lexer/0011_keywords.txt | 6 ++-- tests/data/parser/ok/0023_static_items.rs | 2 ++ tests/data/parser/ok/0023_static_items.txt | 31 ++++++++++++++++++++ 7 files changed, 68 insertions(+), 9 deletions(-) create mode 100644 tests/data/parser/ok/0023_static_items.rs create mode 100644 tests/data/parser/ok/0023_static_items.txt diff --git a/grammar.ron b/grammar.ron index e7acd3cb03..53248519f6 100644 --- a/grammar.ron +++ b/grammar.ron @@ -24,6 +24,7 @@ Grammar( "match", "const", "static", + "mut", ], tokens: [ "ERROR", @@ -87,6 +88,7 @@ Grammar( "EXTERN_CRATE_ITEM", "MOD_ITEM", "USE_ITEM", + "STATIC_ITEM", "EXTERN_BLOCK", "ENUM_VARIANT", diff --git a/src/parser/event_parser/grammar/items/mod.rs b/src/parser/event_parser/grammar/items/mod.rs index 35825e7c4d..9930de3478 100644 --- a/src/parser/event_parser/grammar/items/mod.rs +++ b/src/parser/event_parser/grammar/items/mod.rs @@ -46,6 +46,10 @@ fn item(p: &mut Parser) { } } } + STATIC_KW => { + static_item(p); + STATIC_ITEM + } MOD_KW => { mod_item(p); MOD_ITEM @@ -92,6 +96,24 @@ fn extern_crate_item(p: &mut Parser) { p.expect(IDENT) && alias(p) && p.expect(SEMI); } +fn extern_block(p: &mut Parser) { + assert!(p.at(L_CURLY)); + 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(); @@ -104,12 +126,6 @@ fn mod_item(p: &mut Parser) { } } -fn extern_block(p: &mut Parser) { - assert!(p.at(L_CURLY)); - p.bump(); - p.expect(R_CURLY); -} - fn abi(p: &mut Parser) { assert!(p.at(EXTERN_KW)); let abi = p.start(); diff --git a/src/syntax_kinds.rs b/src/syntax_kinds.rs index f5d01c6f3e..8d21d3fb7b 100644 --- a/src/syntax_kinds.rs +++ b/src/syntax_kinds.rs @@ -30,6 +30,7 @@ pub enum SyntaxKind { MATCH_KW, CONST_KW, STATIC_KW, + MUT_KW, ERROR, IDENT, UNDERSCORE, @@ -88,6 +89,7 @@ pub enum SyntaxKind { EXTERN_CRATE_ITEM, MOD_ITEM, USE_ITEM, + STATIC_ITEM, EXTERN_BLOCK, ENUM_VARIANT, NAMED_FIELD, @@ -141,6 +143,7 @@ impl SyntaxKind { MATCH_KW => &SyntaxInfo { name: "MATCH_KW" }, CONST_KW => &SyntaxInfo { name: "CONST_KW" }, STATIC_KW => &SyntaxInfo { name: "STATIC_KW" }, + MUT_KW => &SyntaxInfo { name: "MUT_KW" }, ERROR => &SyntaxInfo { name: "ERROR" }, IDENT => &SyntaxInfo { name: "IDENT" }, UNDERSCORE => &SyntaxInfo { name: "UNDERSCORE" }, @@ -199,6 +202,7 @@ impl SyntaxKind { EXTERN_CRATE_ITEM => &SyntaxInfo { name: "EXTERN_CRATE_ITEM" }, MOD_ITEM => &SyntaxInfo { name: "MOD_ITEM" }, USE_ITEM => &SyntaxInfo { name: "USE_ITEM" }, + STATIC_ITEM => &SyntaxInfo { name: "STATIC_ITEM" }, EXTERN_BLOCK => &SyntaxInfo { name: "EXTERN_BLOCK" }, ENUM_VARIANT => &SyntaxInfo { name: "ENUM_VARIANT" }, NAMED_FIELD => &SyntaxInfo { name: "NAMED_FIELD" }, @@ -248,6 +252,7 @@ pub(crate) fn ident_to_keyword(ident: &str) -> Option { "match" => Some(MATCH_KW), "const" => Some(CONST_KW), "static" => Some(STATIC_KW), + "mut" => Some(MUT_KW), _ => None, } } diff --git a/tests/data/lexer/0011_keywords.rs b/tests/data/lexer/0011_keywords.rs index 7a1000bb7f..b74fc606ec 100644 --- a/tests/data/lexer/0011_keywords.rs +++ b/tests/data/lexer/0011_keywords.rs @@ -1,2 +1,3 @@ fn use struct trait enum impl true false as extern crate -mod pub self super in where for loop while if match const static +mod pub self super in where for loop while if match const +static mut diff --git a/tests/data/lexer/0011_keywords.txt b/tests/data/lexer/0011_keywords.txt index b38b4bd644..05b1f09954 100644 --- a/tests/data/lexer/0011_keywords.txt +++ b/tests/data/lexer/0011_keywords.txt @@ -43,6 +43,8 @@ WHITESPACE 1 " " MATCH_KW 5 "match" WHITESPACE 1 " " CONST_KW 5 "const" -WHITESPACE 1 " " -STATIC_KW 6 "static" +WHITESPACE 1 "\n" +STATIC_KW 6 "static" +WHITESPACE 1 " " +MUT_KW 3 "mut" WHITESPACE 1 "\n" diff --git a/tests/data/parser/ok/0023_static_items.rs b/tests/data/parser/ok/0023_static_items.rs new file mode 100644 index 0000000000..5fb92ce33f --- /dev/null +++ b/tests/data/parser/ok/0023_static_items.rs @@ -0,0 +1,2 @@ +static FOO: u32 = 1; +static mut BAR: i32 = 92; diff --git a/tests/data/parser/ok/0023_static_items.txt b/tests/data/parser/ok/0023_static_items.txt new file mode 100644 index 0000000000..7cd2228fab --- /dev/null +++ b/tests/data/parser/ok/0023_static_items.txt @@ -0,0 +1,31 @@ +FILE@[0; 47) + STATIC_ITEM@[0; 21) + STATIC_KW@[0; 6) + WHITESPACE@[6; 7) + IDENT@[7; 10) "FOO" + COLON@[10; 11) + WHITESPACE@[11; 12) + IDENT@[12; 15) "u32" + WHITESPACE@[15; 16) + EQ@[16; 17) + LITERAL@[17; 19) + WHITESPACE@[17; 18) + INT_NUMBER@[18; 19) + SEMI@[19; 20) + WHITESPACE@[20; 21) + STATIC_ITEM@[21; 47) + STATIC_KW@[21; 27) + WHITESPACE@[27; 28) + MUT_KW@[28; 31) + WHITESPACE@[31; 32) + IDENT@[32; 35) "BAR" + COLON@[35; 36) + WHITESPACE@[36; 37) + IDENT@[37; 40) "i32" + WHITESPACE@[40; 41) + EQ@[41; 42) + LITERAL@[42; 45) + WHITESPACE@[42; 43) + INT_NUMBER@[43; 45) + SEMI@[45; 46) + WHITESPACE@[46; 47)