From 55602727c8adfa12c026a8c7881a8bc57fba9db8 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 1 Jan 2018 23:22:01 +0300 Subject: [PATCH] Parser: first struct :-) --- src/parser/event_parser/grammar.rs | 7 ++++++- src/parser/event_parser/parser.rs | 9 +++++++++ tests/data/parser/0001_struct_item.txt | 5 +---- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/parser/event_parser/grammar.rs b/src/parser/event_parser/grammar.rs index 0896506fb1..be61483ecd 100644 --- a/src/parser/event_parser/grammar.rs +++ b/src/parser/event_parser/grammar.rs @@ -43,13 +43,18 @@ fn item(p: &mut Parser) -> Result { if p.current_is(STRUCT_KW) { p.start(STRUCT_ITEM); p.bump(); + let _ = struct_item(p); p.finish(); return OK; } ERR } - +fn struct_item(p: &mut Parser) -> Result{ + p.expect(IDENT)?; + p.expect(L_CURLY)?; + p.expect(R_CURLY) +} // Paths, types, attributes, and stuff // diff --git a/src/parser/event_parser/parser.rs b/src/parser/event_parser/parser.rs index 2d5418a29a..36452ef671 100644 --- a/src/parser/event_parser/parser.rs +++ b/src/parser/event_parser/parser.rs @@ -69,6 +69,15 @@ impl<'t> Parser<'t> { Some(kind) } + pub(crate) fn expect(&mut self, kind: SyntaxKind) -> Result<(), ()> { + if kind == self.current().ok_or(())? { + self.bump(); + Ok(()) + } else { + Err(()) + } + } + fn event(&mut self, event: Event) { self.events.push(event) } diff --git a/tests/data/parser/0001_struct_item.txt b/tests/data/parser/0001_struct_item.txt index 5ef5442823..f599e9d2c5 100644 --- a/tests/data/parser/0001_struct_item.txt +++ b/tests/data/parser/0001_struct_item.txt @@ -1,12 +1,9 @@ FILE@[0; 13) - STRUCT_ITEM@[0; 7) + STRUCT_ITEM@[0; 13) STRUCT_KW@[0; 6) WHITESPACE@[6; 7) - ERROR@[7; 9) IDENT@[7; 8) WHITESPACE@[8; 9) - ERROR@[9; 12) L_CURLY@[9; 10) WHITESPACE@[10; 12) - ERROR@[12; 13) R_CURLY@[12; 13)