Skip to content

Commit

Permalink
Merge pull request #304 from KisaragiEffective/test/enable-position-t…
Browse files Browse the repository at this point in the history
…est-of-underscore-discard
  • Loading branch information
KisaragiEffective authored Oct 22, 2023
2 parents 50da653 + cc336a8 commit 922262a
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 27 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

63 changes: 46 additions & 17 deletions package/origlang-compiler/src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,19 @@ impl<T> AssociateWithPos for T {
}
}

#[derive(Debug)]
pub struct LcManager {
/// don't use directly.
line: Cell<NonZeroUsize>,
/// don't use directly.
column: Cell<NonZeroUsize>,
}

#[derive(Debug)]
pub struct Lexer<'src> {
source_bytes_nth: Cell<Utf8CharBoundaryStartByte>,
source: &'src str,
line: Cell<NonZeroUsize>,
column: Cell<NonZeroUsize>,
lc_manager: LcManager,
}

impl<'src> Lexer<'src> {
Expand All @@ -50,10 +57,12 @@ impl<'src> Lexer<'src> {
Self {
source_bytes_nth: Cell::new(Utf8CharBoundaryStartByte::new(0)),
source,
// SAFETY: 1 != 0
line: Cell::new(unsafe { NonZeroUsize::new_unchecked(1) }),
// SAFETY: 1 != 0
column: Cell::new(unsafe { NonZeroUsize::new_unchecked(1) }),
lc_manager: LcManager {
// SAFETY: 1 != 0
line: Cell::new(unsafe { NonZeroUsize::new_unchecked(1) }),
// SAFETY: 1 != 0
column: Cell::new(unsafe { NonZeroUsize::new_unchecked(1) }),
}
}
}
}
Expand Down Expand Up @@ -254,6 +263,7 @@ impl Lexer<'_> {
}

pub fn next(&self) -> WithPosition<Token> {
debug!("-------------------------------------------------");
self.drain_space();

if self.reached_end() {
Expand All @@ -274,8 +284,8 @@ impl Lexer<'_> {

fn current_pos(&self) -> SourcePos {
SourcePos {
line: self.line.get(),
column: self.column.get(),
line: self.line(),
column: self.column(),
}
}

Expand Down Expand Up @@ -352,34 +362,36 @@ impl Lexer<'_> {
fn set_current_index(&self, future_index: Utf8CharBoundaryStartByte) -> Result<(), OutOfRangeError> {
let old = self.source_bytes_nth.get().as_usize();
let new = future_index.as_usize();
debug!("index: {old} -> {future_index:?}");

if old == new {
return Ok(())
}

let current_line = self.line.get().get();
let current_line = self.line().get();

let src = &self.source;
if old < new {
// forward
let new_line = current_line + src[old..new].bytes().filter(|x| *x == b'\n').count();
let new_col = src[old..new].rfind('\n').map_or_else(|| {
let mut c = self.column.get().get();
let mut c = self.column().get();
c += new - old;

c
}, |old_relative| {
new - (old + old_relative)
});

self.line.set(NonZeroUsize::new(new_line).expect("overflow"));
self.column.set(NonZeroUsize::new(new_col).expect("overflow"));
self.set_line(NonZeroUsize::new(new_line).expect("overflow"));
self.set_column(NonZeroUsize::new(new_col).expect("overflow"));
} else {
// THIS BRANCH IS IMPORTANT!!! OTHERWISE, RESET OPERATION WILL NOT WORK!!!
// back
let new_line = current_line - src[new..old].bytes().filter(|x| *x == b'\n').count();
let new_col = src[new..old].find('\n').map_or_else(|| {
let mut c = self.column.get().get();
c += old - new;
let mut c = self.column().get();
c -= old - new;

c
}, |new_relative| {
Expand All @@ -398,11 +410,10 @@ impl Lexer<'_> {
})
});

self.line.set(NonZeroUsize::new(new_line).expect("overflow"));
self.column.set(NonZeroUsize::new(new_col).expect("overflow"));
self.set_line(NonZeroUsize::new(new_line).expect("overflow"));
self.set_column(NonZeroUsize::new(new_col).expect("overflow"));
}

debug!("index: requested = {future_index:?}");
self.source_bytes_nth.set(future_index);

Ok(())
Expand Down Expand Up @@ -548,4 +559,22 @@ impl Lexer<'_> {
max: self.source.len(),
})
}

fn line(&self) -> NonZeroUsize {
self.lc_manager.line.get()
}

fn set_line(&self, line: NonZeroUsize) {
debug!("line: {old} -> {new}", old = self.line(), new = line);
self.lc_manager.line.set(line)
}

fn column(&self) -> NonZeroUsize {
self.lc_manager.column.get()
}

fn set_column(&self, column: NonZeroUsize) {
debug!("column: {old} -> {new}", old = self.column(), new = column);
self.lc_manager.column.set(column)
}
}
3 changes: 2 additions & 1 deletion package/origlang-compiler/src/lexer/token.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::num::NonZeroUsize;
use origlang_ast::{Comment, Identifier};
use crate::chars::boundary::Utf8CharBoundaryStartByte;
use crate::lexer::Lexer;
Expand All @@ -16,7 +17,7 @@ impl TemporalLexerUnwindToken {
}

pub fn reset(self, lexer: &Lexer) {
lexer.source_bytes_nth.set(self.unwind_index);
lexer.set_current_index(self.unwind_index).expect("Error during reset");
}
}

Expand Down
1 change: 1 addition & 0 deletions package/origlang-testsuite/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ origlang-compiler = { version = "0.1.0", path = "../origlang-compiler" }
origlang-ir = { version = "0.1.0", path = "../origlang-ir" }
origlang-ir-optimizer = { version = "0.1.0", path = "../origlang-ir-optimizer" }
origlang-runtime = { version = "0.1.0", path = "../origlang-runtime" }
origlang-source-span = { version = "*", path = "../origlang-source-span" }
thiserror = "1.0.50"
22 changes: 13 additions & 9 deletions package/origlang-testsuite/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ use origlang_compiler::type_check::TypeChecker;
use origlang_ir::IntoVerbatimSequencedIR;
use origlang_ir_optimizer::lower::{EachStep, LowerStep, TheTranspiler};
use origlang_ir_optimizer::preset::{NoOptimization, SimpleOptimization};
use origlang_source_span::SourcePosition;

type Err = TestFailureCause;

#[derive(Error, Debug)]
#[derive(Error, Debug, Eq, PartialEq)]
pub enum TestFailureCause {
#[error("parser failure: {0}")]
Parser(#[from] SimpleErrorWithPos),
Expand Down Expand Up @@ -461,17 +462,20 @@ print 1
assert_eq!(Self::evaluated_expressions("var _ = 1\n")?, []);
assert_eq!(Self::evaluated_expressions("var a = block\n print 1\n()\nend\n").expect("FATAL: shouldn't fail"), type_boxes![ 1 => NonCoercedInteger ]);
assert_eq!(Self::evaluated_expressions("var _ = block\n print 1\n()\nend\n")?, type_boxes![ 1 => NonCoercedInteger ]);
assert!(
matches!(Self::evaluated_expressions("var _ = _\n"),
Err(
TestFailureCause::Parser(
SimpleErrorWithPos {
kind: ParserError::UnderscoreCanNotBeRightHandExpression,
..
assert_eq!(
Self::evaluated_expressions("var _ = _\n"),
Err(
TestFailureCause::Parser(
SimpleErrorWithPos {
kind: ParserError::UnderscoreCanNotBeRightHandExpression,
position: SourcePosition {
line: 1.try_into().unwrap(),
column: 9.try_into().unwrap(),
}
)
}
)
)

);

Ok(())
Expand Down

0 comments on commit 922262a

Please sign in to comment.