Skip to content

Commit

Permalink
refactor(parser): avoid implicit clone on advance
Browse files Browse the repository at this point in the history
  • Loading branch information
KisaragiEffective committed Jun 20, 2024
1 parent 68113e4 commit 8f58e48
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
31 changes: 21 additions & 10 deletions package/origlang-parser/src/parser.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use origlang_ast::{AtomicPattern, RootAst, Statement, TypeSignature};
use origlang_source_span::{Pointed as WithPosition, SourcePosition as SourcePos};
use origlang_source_span::{Pointed as WithPosition, Pointed, SourcePosition as SourcePos};
use origlang_lexer::Lexer;
use origlang_lexer::token::Token;
use origlang_lexer::token::internal::DisplayToken;
Expand Down Expand Up @@ -78,7 +78,8 @@ impl Parser {
}

{
let t = self.lexer.next();
let t = self.lexer.peek_cloned();
self.lexer.next();
match t.data {
Token::EndOfFile | Token::NewLine => Ok(RootAst {
statement: statements,
Expand Down Expand Up @@ -132,7 +133,8 @@ impl Parser {
}
Token::KeywordType => {
self.lexer.next();
let aliased = self.lexer.next();
let aliased = self.lexer.peek_cloned();
self.lexer.next();

let Token::Identifier { inner: aliased } = aliased.data else {
return Err(ParserError::new(ParserErrorInner::UnexpectedToken {
Expand Down Expand Up @@ -163,7 +165,9 @@ impl Parser {
};

// 文は絶対に改行かEOFで終わる必要がある
let next = self.lexer.next();
let next = self.lexer.peek_cloned();
self.lexer.next();

if next.data != Token::NewLine && next.data != Token::EndOfFile {
return Err(ParserError::new(ParserErrorInner::PartiallyParsed {
hint: vec![
Expand Down Expand Up @@ -203,7 +207,9 @@ impl Parser {
})
}
Token::SymLeftPar => {
assert_eq!(self.lexer.next().data, Token::SymLeftPar);

assert_eq!(self.lexer.peek().map(|x| &x.data), Some(&Token::SymLeftPar));
self.lexer.next();
// FIXME: (1 == 2)を受け付けない
if self.lexer.peek().ok_or_else(|| ParserError::new(ParserErrorInner::EndOfFileError, self.lexer.last_position))?.data == Token::SymRightPar {
self.lexer.next();
Expand Down Expand Up @@ -511,7 +517,8 @@ impl Parser {
/// 違反した場合はErrを返す。
fn parse_int_literal(&self) -> Result<(i64, Option<Box<str>>), ParserError> {
debug!("expr:lit:int");
let n = self.lexer.next();
let n = self.lexer.peek_cloned();
self.lexer.next();
let Token::Digits { sequence, suffix } = n.data else {
return Err(ParserError::new(ParserErrorInner::UnexpectedToken {
pat: IntLiteral,
Expand Down Expand Up @@ -551,7 +558,8 @@ impl Parser {
}

fn parse_type(&self) -> Result<TypeSignature, ParserError> {
let WithPosition { position, data: maybe_tp } = self.lexer.next();
let WithPosition { position, data: maybe_tp } = self.lexer.peek_cloned();
self.lexer.next();

match maybe_tp {
Token::Identifier { inner } => Ok(inner.into()),
Expand Down Expand Up @@ -606,10 +614,12 @@ impl Parser {

// optionally, allow type annotation
let type_annotation = self.lexer.parse_fallible(|| {
match self.lexer.next().data {
Token::SymColon => {},
match self.lexer.peek() {
Some(Pointed { data: Token::SymColon, .. }) => {},
_ => return Err(())
}

self.lexer.next();

// FIXME: discarding error
let x = self.parse_type().map_err(|e| {
Expand All @@ -634,7 +644,8 @@ impl Parser {

fn parse_variable_assignment(&self) -> Result<Statement, ParserError> {
debug!("assign:var");
let ident_token = self.lexer.next();
let ident_token = self.lexer.peek_cloned();
self.lexer.next();
let Token::Identifier { inner: name } = ident_token.data else {
return Err(ParserError::new(ParserErrorInner::UnexpectedToken {
pat: TokenKind::Identifier,
Expand Down
7 changes: 2 additions & 5 deletions package/origlang-token-stream/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,13 @@ impl TokenStream {
self.peek().unwrap_or(&Pointed { data: Token::EndOfFile, position: self.last_position }).clone()
}

/// returns cloned token on current position, and advance position by one.
/// advance position by one.
#[track_caller]
pub fn next(&self) -> Pointed<Token> {
pub fn next(&self) {
let o = Location::caller();
debug!("next: {o}");
let ret = self.peek_cloned();
debug!("now[{}] = {:?}", self.current_index.get(), self.peek());
self.current_index.set(self.current_index.get() + 1);

ret
}

/// パースに失敗するかも知れないものをパースしようと試みる。
Expand Down

0 comments on commit 8f58e48

Please sign in to comment.