Introduce EOF token

This commit is contained in:
Aleksey Kladov 2018-01-07 21:09:05 +03:00
parent fc4d6cc298
commit 5562931e4f
3 changed files with 133 additions and 137 deletions

View file

@ -1,5 +1,6 @@
use super::parser::Parser;
use {SyntaxKind};
use tree::EOF;
use syntax_kinds::*;
// Items //
@ -18,11 +19,7 @@ pub(crate) fn file(p: &mut Parser) {
}
fn item_first(p: &Parser) -> bool {
let current = match p.current() {
Some(c) => c,
None => return false,
};
match current {
match p.current() {
STRUCT_KW | FN_KW => true,
_ => false,
}
@ -79,7 +76,7 @@ fn visibility(_: &mut Parser) {
// Error recovery and high-order utils //
fn node_if<F: FnOnce(&mut Parser)>(p: &mut Parser, first: SyntaxKind, node_kind: SyntaxKind, rest: F) -> bool {
p.current_is(first) && { node(p, node_kind, |p| { p.bump(); rest(p); }); true }
p.current() == first && { node(p, node_kind, |p| { p.bump(); rest(p); }); true }
}
fn node<F: FnOnce(&mut Parser)>(p: &mut Parser, node_kind: SyntaxKind, rest: F) {
@ -95,7 +92,7 @@ fn many<F: Fn(&mut Parser) -> bool>(p: &mut Parser, f: F) {
fn comma_list<F: Fn(&mut Parser) -> bool>(p: &mut Parser, f: F) {
many(p, |p| {
f(p);
if p.is_eof() {
if p.current() == EOF {
false
} else {
p.expect(COMMA);
@ -119,7 +116,7 @@ where
f(p);
return true;
}
if p.is_eof() {
if p.current() == EOF {
if skipped {
p.finish();
}
@ -131,18 +128,14 @@ where
.message(message)
.emit();
}
p.bump().unwrap();
p.bump();
skipped = true;
}
}
impl<'p> Parser<'p> {
fn current_is(&self, kind: SyntaxKind) -> bool {
self.current() == Some(kind)
}
pub(crate) fn expect(&mut self, kind: SyntaxKind) -> bool {
if self.current_is(kind) {
if self.current() == kind {
self.bump();
true
} else {
@ -154,7 +147,7 @@ impl<'p> Parser<'p> {
}
fn optional(&mut self, kind: SyntaxKind) {
if self.current_is(kind) {
if self.current() == kind {
self.bump();
}
}

View file

@ -2,8 +2,7 @@ use {Token, SyntaxKind, TextUnit};
use super::{Event};
use super::super::is_insignificant;
use syntax_kinds::{L_CURLY, R_CURLY, ERROR};
pub(crate) const EOF: SyntaxKind = SyntaxKind(10000);
use tree::EOF;
pub(crate) struct Parser<'t> {
@ -46,19 +45,22 @@ impl<'t> Parser<'t> {
}
pub(crate) fn into_events(self) -> Vec<Event> {
assert!(self.is_eof());
assert!(self.curly_limit.is_none());
assert!(self.current() == EOF);
self.events
}
pub(crate) fn is_eof(&self) -> bool {
pub(crate) fn current(&self) -> SyntaxKind {
if self.pos == self.tokens.len() {
return true
return EOF;
}
if let Some(limit) = self.curly_limit {
let token = self.tokens[self.pos];
return limit == self.curly_level && token.kind == R_CURLY;
if let Some(limit) = self.curly_limit {
if limit == self.curly_level && token.kind == R_CURLY {
return EOF
}
false
}
token.kind
}
pub(crate) fn start(&mut self, kind: SyntaxKind) {
@ -73,24 +75,17 @@ impl<'t> Parser<'t> {
ErrorBuilder::new(self)
}
pub(crate) fn current(&self) -> Option<SyntaxKind> {
if self.is_eof() {
return None;
}
let token = self.tokens[self.pos];
Some(token.kind)
}
pub(crate) fn bump(&mut self) -> Option<SyntaxKind> {
let kind = self.current()?;
pub(crate) fn bump(&mut self) -> SyntaxKind {
let kind = self.current();
match kind {
L_CURLY => self.curly_level += 1,
R_CURLY => self.curly_level -= 1,
EOF => return EOF,
_ => (),
}
self.pos += 1;
self.event(Event::Token { kind, n_raw_tokens: 1 });
Some(kind)
kind
}
pub(crate) fn lookahead(&self, kinds: &[SyntaxKind]) -> bool {
@ -114,7 +109,7 @@ impl<'t> Parser<'t> {
if !self.expect(R_CURLY) {
self.start(ERROR);
while self.curly_level > old_level {
if self.bump().is_none() {
if self.bump() == EOF {
break;
}
}

View file

@ -10,8 +10,16 @@ pub use self::file_builder::{FileBuilder, Sink};
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct SyntaxKind(pub(crate) u32);
pub(crate) const EOF: SyntaxKind = SyntaxKind(10000);
pub(crate) const EOF_INFO: SyntaxInfo = SyntaxInfo {
name: "EOF"
};
impl SyntaxKind {
fn info(self) -> &'static SyntaxInfo {
if self == EOF {
return &EOF_INFO;
}
syntax_info(self)
}
}