rust/src/parser/event_parser/grammar.rs

111 lines
1.8 KiB
Rust
Raw Normal View History

2018-01-01 07:05:46 -06:00
use super::parser::Parser;
use syntax_kinds::*;
2018-01-01 09:58:46 -06:00
// Items //
2018-01-01 08:21:53 -06:00
pub fn file(p: &mut Parser) {
2018-01-01 07:05:46 -06:00
p.start(FILE);
2018-01-01 09:58:46 -06:00
shebang(p);
inner_attributes(p);
mod_items(p);
p.finish();
}
type Result = ::std::result::Result<(), ()>;
const OK: Result = Ok(());
const ERR: Result = Err(());
fn shebang(_: &mut Parser) {
//TODO
}
fn inner_attributes(_: &mut Parser) {
//TODO
}
fn mod_items(p: &mut Parser) {
loop {
skip_until_item(p);
if p.is_eof() {
return;
}
if item(p).is_err() {
skip_one_token(p);
}
}
}
fn item(p: &mut Parser) -> Result {
outer_attributes(p)?;
visibility(p)?;
2018-01-01 13:13:04 -06:00
if p.current_is(STRUCT_KW) {
p.start(STRUCT_ITEM);
p.bump();
2018-01-01 14:22:01 -06:00
let _ = struct_item(p);
2018-01-01 13:13:04 -06:00
p.finish();
return OK;
}
2018-01-01 09:58:46 -06:00
ERR
}
2018-01-06 08:16:00 -06:00
fn struct_item(p: &mut Parser) -> Result {
2018-01-01 14:22:01 -06:00
p.expect(IDENT)?;
2018-01-06 08:16:00 -06:00
p.curly_block(|p| {
comma_list(p, struct_field)
})
}
fn struct_field(p: &mut Parser) -> Result {
if !p.current_is(IDENT) {
return ERR;
}
p.start(STRUCT_FIELD);
p.bump();
ignore_errors(|| {
p.expect(COLON)?;
p.expect(IDENT)?;
OK
});
p.finish();
OK
2018-01-01 14:22:01 -06:00
}
2018-01-01 09:58:46 -06:00
// Paths, types, attributes, and stuff //
fn outer_attributes(_: &mut Parser) -> Result {
OK
}
fn visibility(_: &mut Parser) -> Result {
OK
}
// Expressions //
// Error recovery and high-order utils //
fn skip_until_item(_: &mut Parser) {
//TODO
}
fn skip_one_token(p: &mut Parser) {
p.start(ERROR);
p.bump().unwrap();
2018-01-01 07:05:46 -06:00
p.finish();
2018-01-06 08:16:00 -06:00
}
fn ignore_errors<F: FnOnce() -> Result>(f: F) {
drop(f());
}
fn comma_list<F: Fn(&mut Parser) -> Result>(p: &mut Parser, element: F) {
loop {
if element(p).is_err() {
return
}
if p.expect(COMMA).is_err() {
return
}
}
2018-01-01 07:05:46 -06:00
}