aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2018-02-03 09:05:25 +0000
committerAleksey Kladov <[email protected]>2018-02-03 09:05:57 +0000
commite5273d33d05196ca215db4b1c4e207328fa5df08 (patch)
treec7dc8cc7c4d8bfd21e64c5a946a23b2d00ca697e
parent5e7504b978c9b1365e0381691ef453e97603defa (diff)
G: const item
-rw-r--r--grammar.ron2
-rw-r--r--src/parser/event_parser/grammar/items/consts.rs21
-rw-r--r--src/parser/event_parser/grammar/items/mod.rs33
-rw-r--r--src/syntax_kinds.rs5
-rw-r--r--tests/data/parser/ok/0024_const_fn.rs5
-rw-r--r--tests/data/parser/ok/0024_const_fn.txt29
-rw-r--r--tests/data/parser/ok/0025_const_item.rs2
-rw-r--r--tests/data/parser/ok/0025_const_item.txt31
8 files changed, 114 insertions, 14 deletions
diff --git a/grammar.ron b/grammar.ron
index 53248519f..0717b7a76 100644
--- a/grammar.ron
+++ b/grammar.ron
@@ -25,6 +25,7 @@ Grammar(
25 "const", 25 "const",
26 "static", 26 "static",
27 "mut", 27 "mut",
28 "unsafe",
28 ], 29 ],
29 tokens: [ 30 tokens: [
30 "ERROR", 31 "ERROR",
@@ -89,6 +90,7 @@ Grammar(
89 "MOD_ITEM", 90 "MOD_ITEM",
90 "USE_ITEM", 91 "USE_ITEM",
91 "STATIC_ITEM", 92 "STATIC_ITEM",
93 "CONST_ITEM",
92 94
93 "EXTERN_BLOCK", 95 "EXTERN_BLOCK",
94 "ENUM_VARIANT", 96 "ENUM_VARIANT",
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 @@
1use super::*;
2
3pub(super) fn static_item(p: &mut Parser) {
4 const_or_static(p, STATIC_KW)
5}
6
7pub(super) fn const_item(p: &mut Parser) {
8 const_or_static(p, CONST_KW)
9}
10
11fn const_or_static(p: &mut Parser, kw: SyntaxKind) {
12 assert!(p.at(kw));
13 p.bump();
14 p.eat(MUT_KW); // TODO: validator to forbid const mut
15 p.expect(IDENT);
16 p.expect(COLON);
17 types::type_ref(p);
18 p.expect(EQ);
19 expressions::expr(p);
20 p.expect(SEMI);
21}
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::*;
2 2
3mod structs; 3mod structs;
4mod use_item; 4mod use_item;
5mod consts;
5 6
6pub(super) fn mod_contents(p: &mut Parser, stop_on_r_curly: bool) { 7pub(super) fn mod_contents(p: &mut Parser, stop_on_r_curly: bool) {
7 attributes::inner_attributes(p); 8 attributes::inner_attributes(p);
@@ -47,9 +48,26 @@ fn item(p: &mut Parser) {
47 } 48 }
48 } 49 }
49 STATIC_KW => { 50 STATIC_KW => {
50 static_item(p); 51 consts::static_item(p);
51 STATIC_ITEM 52 STATIC_ITEM
52 } 53 }
54 CONST_KW => match p.nth(1) {
55 FN_KW => {
56 p.bump();
57 fn_item(p);
58 FN_ITEM
59 }
60 UNSAFE_KW if p.nth(2) == FN_KW => {
61 p.bump();
62 p.bump();
63 fn_item(p);
64 FN_ITEM
65 }
66 _ => {
67 consts::const_item(p);
68 CONST_ITEM
69 }
70 },
53 MOD_KW => { 71 MOD_KW => {
54 mod_item(p); 72 mod_item(p);
55 MOD_ITEM 73 MOD_ITEM
@@ -101,19 +119,6 @@ fn extern_block(p: &mut Parser) {
101 p.bump(); 119 p.bump();
102 p.expect(R_CURLY); 120 p.expect(R_CURLY);
103} 121}
104
105fn static_item(p: &mut Parser) {
106 assert!(p.at(STATIC_KW));
107 p.bump();
108 p.eat(MUT_KW);
109 p.expect(IDENT);
110 p.expect(COLON);
111 types::type_ref(p);
112 p.expect(EQ);
113 expressions::expr(p);
114 p.expect(SEMI);
115}
116
117fn mod_item(p: &mut Parser) { 122fn mod_item(p: &mut Parser) {
118 assert!(p.at(MOD_KW)); 123 assert!(p.at(MOD_KW));
119 p.bump(); 124 p.bump();
diff --git a/src/syntax_kinds.rs b/src/syntax_kinds.rs
index 8d21d3fb7..c182aea78 100644
--- a/src/syntax_kinds.rs
+++ b/src/syntax_kinds.rs
@@ -31,6 +31,7 @@ pub enum SyntaxKind {
31 CONST_KW, 31 CONST_KW,
32 STATIC_KW, 32 STATIC_KW,
33 MUT_KW, 33 MUT_KW,
34 UNSAFE_KW,
34 ERROR, 35 ERROR,
35 IDENT, 36 IDENT,
36 UNDERSCORE, 37 UNDERSCORE,
@@ -90,6 +91,7 @@ pub enum SyntaxKind {
90 MOD_ITEM, 91 MOD_ITEM,
91 USE_ITEM, 92 USE_ITEM,
92 STATIC_ITEM, 93 STATIC_ITEM,
94 CONST_ITEM,
93 EXTERN_BLOCK, 95 EXTERN_BLOCK,
94 ENUM_VARIANT, 96 ENUM_VARIANT,
95 NAMED_FIELD, 97 NAMED_FIELD,
@@ -144,6 +146,7 @@ impl SyntaxKind {
144 CONST_KW => &SyntaxInfo { name: "CONST_KW" }, 146 CONST_KW => &SyntaxInfo { name: "CONST_KW" },
145 STATIC_KW => &SyntaxInfo { name: "STATIC_KW" }, 147 STATIC_KW => &SyntaxInfo { name: "STATIC_KW" },
146 MUT_KW => &SyntaxInfo { name: "MUT_KW" }, 148 MUT_KW => &SyntaxInfo { name: "MUT_KW" },
149 UNSAFE_KW => &SyntaxInfo { name: "UNSAFE_KW" },
147 ERROR => &SyntaxInfo { name: "ERROR" }, 150 ERROR => &SyntaxInfo { name: "ERROR" },
148 IDENT => &SyntaxInfo { name: "IDENT" }, 151 IDENT => &SyntaxInfo { name: "IDENT" },
149 UNDERSCORE => &SyntaxInfo { name: "UNDERSCORE" }, 152 UNDERSCORE => &SyntaxInfo { name: "UNDERSCORE" },
@@ -203,6 +206,7 @@ impl SyntaxKind {
203 MOD_ITEM => &SyntaxInfo { name: "MOD_ITEM" }, 206 MOD_ITEM => &SyntaxInfo { name: "MOD_ITEM" },
204 USE_ITEM => &SyntaxInfo { name: "USE_ITEM" }, 207 USE_ITEM => &SyntaxInfo { name: "USE_ITEM" },
205 STATIC_ITEM => &SyntaxInfo { name: "STATIC_ITEM" }, 208 STATIC_ITEM => &SyntaxInfo { name: "STATIC_ITEM" },
209 CONST_ITEM => &SyntaxInfo { name: "CONST_ITEM" },
206 EXTERN_BLOCK => &SyntaxInfo { name: "EXTERN_BLOCK" }, 210 EXTERN_BLOCK => &SyntaxInfo { name: "EXTERN_BLOCK" },
207 ENUM_VARIANT => &SyntaxInfo { name: "ENUM_VARIANT" }, 211 ENUM_VARIANT => &SyntaxInfo { name: "ENUM_VARIANT" },
208 NAMED_FIELD => &SyntaxInfo { name: "NAMED_FIELD" }, 212 NAMED_FIELD => &SyntaxInfo { name: "NAMED_FIELD" },
@@ -253,6 +257,7 @@ pub(crate) fn ident_to_keyword(ident: &str) -> Option<SyntaxKind> {
253 "const" => Some(CONST_KW), 257 "const" => Some(CONST_KW),
254 "static" => Some(STATIC_KW), 258 "static" => Some(STATIC_KW),
255 "mut" => Some(MUT_KW), 259 "mut" => Some(MUT_KW),
260 "unsafe" => Some(UNSAFE_KW),
256 _ => None, 261 _ => None,
257 } 262 }
258} 263}
diff --git a/tests/data/parser/ok/0024_const_fn.rs b/tests/data/parser/ok/0024_const_fn.rs
new file mode 100644
index 000000000..eba9322a1
--- /dev/null
+++ b/tests/data/parser/ok/0024_const_fn.rs
@@ -0,0 +1,5 @@
1const fn foo() {
2}
3
4const unsafe fn foo() {
5}
diff --git a/tests/data/parser/ok/0024_const_fn.txt b/tests/data/parser/ok/0024_const_fn.txt
new file mode 100644
index 000000000..0fd485997
--- /dev/null
+++ b/tests/data/parser/ok/0024_const_fn.txt
@@ -0,0 +1,29 @@
1FILE@[0; 46)
2 FN_ITEM@[0; 20)
3 CONST_KW@[0; 5)
4 WHITESPACE@[5; 6)
5 FN_KW@[6; 8)
6 WHITESPACE@[8; 9)
7 IDENT@[9; 12) "foo"
8 L_PAREN@[12; 13)
9 R_PAREN@[13; 14)
10 WHITESPACE@[14; 15)
11 L_CURLY@[15; 16)
12 WHITESPACE@[16; 17)
13 R_CURLY@[17; 18)
14 WHITESPACE@[18; 20)
15 FN_ITEM@[20; 46)
16 CONST_KW@[20; 25)
17 WHITESPACE@[25; 26)
18 UNSAFE_KW@[26; 32)
19 WHITESPACE@[32; 33)
20 FN_KW@[33; 35)
21 WHITESPACE@[35; 36)
22 IDENT@[36; 39) "foo"
23 L_PAREN@[39; 40)
24 R_PAREN@[40; 41)
25 WHITESPACE@[41; 42)
26 L_CURLY@[42; 43)
27 WHITESPACE@[43; 44)
28 R_CURLY@[44; 45)
29 WHITESPACE@[45; 46)
diff --git a/tests/data/parser/ok/0025_const_item.rs b/tests/data/parser/ok/0025_const_item.rs
new file mode 100644
index 000000000..7446859b5
--- /dev/null
+++ b/tests/data/parser/ok/0025_const_item.rs
@@ -0,0 +1,2 @@
1const FOO: u32 = 92;
2const mut BAR: u32 = 62;
diff --git a/tests/data/parser/ok/0025_const_item.txt b/tests/data/parser/ok/0025_const_item.txt
new file mode 100644
index 000000000..588e001f5
--- /dev/null
+++ b/tests/data/parser/ok/0025_const_item.txt
@@ -0,0 +1,31 @@
1FILE@[0; 46)
2 CONST_ITEM@[0; 21)
3 CONST_KW@[0; 5)
4 WHITESPACE@[5; 6)
5 IDENT@[6; 9) "FOO"
6 COLON@[9; 10)
7 WHITESPACE@[10; 11)
8 IDENT@[11; 14) "u32"
9 WHITESPACE@[14; 15)
10 EQ@[15; 16)
11 LITERAL@[16; 19)
12 WHITESPACE@[16; 17)
13 INT_NUMBER@[17; 19)
14 SEMI@[19; 20)
15 WHITESPACE@[20; 21)
16 CONST_ITEM@[21; 46)
17 CONST_KW@[21; 26)
18 WHITESPACE@[26; 27)
19 MUT_KW@[27; 30)
20 WHITESPACE@[30; 31)
21 IDENT@[31; 34) "BAR"
22 COLON@[34; 35)
23 WHITESPACE@[35; 36)
24 IDENT@[36; 39) "u32"
25 WHITESPACE@[39; 40)
26 EQ@[40; 41)
27 LITERAL@[41; 44)
28 WHITESPACE@[41; 42)
29 INT_NUMBER@[42; 44)
30 SEMI@[44; 45)
31 WHITESPACE@[45; 46)