From fc0a9e266b9d663b1eeca3963495c68ca3384be2 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 10 Feb 2018 14:00:23 +0300 Subject: G: introduce names --- grammar.ron | 2 ++ src/parser/grammar/items/mod.rs | 5 +++-- src/parser/grammar/mod.rs | 12 +++++++++++- src/syntax_kinds.rs | 4 ++++ tests/data/parser/inline/0005_extern_crate.txt | 5 +++-- tests/data/parser/ok/0007_extern_crate.txt | 17 ++++++++++------- tests/data/parser/ok/0015_use_tree.txt | 15 +++++++++------ 7 files changed, 42 insertions(+), 18 deletions(-) diff --git a/grammar.ron b/grammar.ron index c38bf654d..0130b7b37 100644 --- a/grammar.ron +++ b/grammar.ron @@ -118,5 +118,7 @@ Grammar( "LIFETIME_PARAM", "TYPE_PARAM", "ABI", + "NAME", + "NAME_REF", ] ) diff --git a/src/parser/grammar/items/mod.rs b/src/parser/grammar/items/mod.rs index ffe86fa97..d671568b1 100644 --- a/src/parser/grammar/items/mod.rs +++ b/src/parser/grammar/items/mod.rs @@ -196,8 +196,9 @@ fn extern_crate_item(p: &mut Parser) { p.bump(); assert!(p.at(CRATE_KW)); p.bump(); - - p.expect(IDENT) && alias(p) && p.expect(SEMI); + name(p); + alias(p); + p.expect(SEMI); } fn extern_block(p: &mut Parser) { diff --git a/src/parser/grammar/mod.rs b/src/parser/grammar/mod.rs index b949583ff..6e82d7c69 100644 --- a/src/parser/grammar/mod.rs +++ b/src/parser/grammar/mod.rs @@ -44,12 +44,22 @@ fn alias(p: &mut Parser) -> bool { if p.at(AS_KW) { let alias = p.start(); p.bump(); - p.expect(IDENT); + name(p); alias.complete(p, ALIAS); } true //FIXME: return false if three are errors } +fn name(p: &mut Parser) { + if p.at(IDENT) { + let m = p.start(); + p.bump(); + m.complete(p, NAME); + } else { + p.error("expected a name"); + } +} + fn error_block(p: &mut Parser, message: &str) { assert!(p.at(L_CURLY)); let err = p.start(); diff --git a/src/syntax_kinds.rs b/src/syntax_kinds.rs index dc287f0f4..7450f9d6f 100644 --- a/src/syntax_kinds.rs +++ b/src/syntax_kinds.rs @@ -116,6 +116,8 @@ pub enum SyntaxKind { LIFETIME_PARAM, TYPE_PARAM, ABI, + NAME, + NAME_REF, // Technical SyntaxKinds: they appear temporally during parsing, // but never end up in the final tree @@ -239,6 +241,8 @@ impl SyntaxKind { LIFETIME_PARAM => &SyntaxInfo { name: "LIFETIME_PARAM" }, TYPE_PARAM => &SyntaxInfo { name: "TYPE_PARAM" }, ABI => &SyntaxInfo { name: "ABI" }, + NAME => &SyntaxInfo { name: "NAME" }, + NAME_REF => &SyntaxInfo { name: "NAME_REF" }, TOMBSTONE => &SyntaxInfo { name: "TOMBSTONE" }, EOF => &SyntaxInfo { name: "EOF" }, diff --git a/tests/data/parser/inline/0005_extern_crate.txt b/tests/data/parser/inline/0005_extern_crate.txt index 07cc47875..cbdf5aba1 100644 --- a/tests/data/parser/inline/0005_extern_crate.txt +++ b/tests/data/parser/inline/0005_extern_crate.txt @@ -3,7 +3,8 @@ FILE@[0; 18) EXTERN_KW@[0; 6) WHITESPACE@[6; 7) CRATE_KW@[7; 12) - WHITESPACE@[12; 13) - IDENT@[13; 16) "foo" + NAME@[12; 16) + WHITESPACE@[12; 13) + IDENT@[13; 16) "foo" SEMI@[16; 17) WHITESPACE@[17; 18) diff --git a/tests/data/parser/ok/0007_extern_crate.txt b/tests/data/parser/ok/0007_extern_crate.txt index aff70131b..33ce2ae65 100644 --- a/tests/data/parser/ok/0007_extern_crate.txt +++ b/tests/data/parser/ok/0007_extern_crate.txt @@ -3,20 +3,23 @@ FILE@[0; 43) EXTERN_KW@[0; 6) WHITESPACE@[6; 7) CRATE_KW@[7; 12) - WHITESPACE@[12; 13) - IDENT@[13; 16) "foo" + NAME@[12; 16) + WHITESPACE@[12; 13) + IDENT@[13; 16) "foo" SEMI@[16; 17) WHITESPACE@[17; 18) EXTERN_CRATE_ITEM@[18; 43) EXTERN_KW@[18; 24) WHITESPACE@[24; 25) CRATE_KW@[25; 30) - WHITESPACE@[30; 31) - IDENT@[31; 34) "foo" - ALIAS@[34; 41) + NAME@[30; 35) + WHITESPACE@[30; 31) + IDENT@[31; 34) "foo" WHITESPACE@[34; 35) + ALIAS@[35; 41) AS_KW@[35; 37) - WHITESPACE@[37; 38) - IDENT@[38; 41) "bar" + NAME@[37; 41) + WHITESPACE@[37; 38) + IDENT@[38; 41) "bar" SEMI@[41; 42) WHITESPACE@[42; 43) diff --git a/tests/data/parser/ok/0015_use_tree.txt b/tests/data/parser/ok/0015_use_tree.txt index 10bdef5fe..28f5f789e 100644 --- a/tests/data/parser/ok/0015_use_tree.txt +++ b/tests/data/parser/ok/0015_use_tree.txt @@ -9,8 +9,9 @@ FILE@[0; 55) WHITESPACE@[7; 8) ALIAS@[8; 14) AS_KW@[8; 10) - WHITESPACE@[10; 11) - IDENT@[11; 14) "bar" + NAME@[10; 14) + WHITESPACE@[10; 11) + IDENT@[11; 14) "bar" SEMI@[14; 15) WHITESPACE@[15; 16) USE_ITEM@[16; 55) @@ -29,8 +30,9 @@ FILE@[0; 55) WHITESPACE@[27; 28) ALIAS@[28; 32) AS_KW@[28; 30) - WHITESPACE@[30; 31) - IDENT@[31; 32) "b" + NAME@[30; 32) + WHITESPACE@[30; 31) + IDENT@[31; 32) "b" COMMA@[32; 33) USE_TREE@[33; 35) WHITESPACE@[33; 34) @@ -50,8 +52,9 @@ FILE@[0; 55) WHITESPACE@[47; 48) ALIAS@[48; 52) AS_KW@[48; 50) - WHITESPACE@[50; 51) - IDENT@[51; 52) "x" + NAME@[50; 52) + WHITESPACE@[50; 51) + IDENT@[51; 52) "x" R_CURLY@[52; 53) SEMI@[53; 54) WHITESPACE@[54; 55) -- cgit v1.2.3