Skip to content

Commit

Permalink
Rename Point constructors, add field accessors
Browse files Browse the repository at this point in the history
  • Loading branch information
TheSignPainter98 committed Aug 28, 2023
1 parent 57bd821 commit 16d8dd6
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 82 deletions.
19 changes: 11 additions & 8 deletions crates/emblem_core/src/ast/parsed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ mod test {
#[test]
fn args() {
let ctx = Context::new();
let p1 = Point::new(
let p1 = Point::at_start_of(
ctx.alloc_file_name("fname.em"),
ctx.alloc_file_content("helloworld"),
);
Expand All @@ -388,8 +388,8 @@ mod test {
let tests = vec![
vec![],
vec![
Attr::unnamed(p2.src.clone(), Location::new(&p1, &p2)),
Attr::unnamed(p3.src.clone(), Location::new(&p2, &p3)),
Attr::unnamed(p2.src().clone(), Location::new(&p1, &p2)),
Attr::unnamed(p3.src().clone(), Location::new(&p2, &p3)),
],
];

Expand All @@ -410,8 +410,9 @@ mod test {
fn unnamed() {
let ctx = Context::new();
let raw = " \tfoo\t ";
let p1 = Point::new(ctx.alloc_file_name("fname.em"), ctx.alloc_file_content(raw));
let attr = Attr::unnamed(p1.src.clone(), Location::new(&p1, &p1.clone().shift(raw)));
let p1 =
Point::at_start_of(ctx.alloc_file_name("fname.em"), ctx.alloc_file_content(raw));
let attr = Attr::unnamed(p1.src().clone(), Location::new(&p1, &p1.clone().shift(raw)));

assert_eq!(attr.name(), None);
assert_eq!(attr.repr(), "foo");
Expand All @@ -423,8 +424,9 @@ mod test {
fn named() {
let ctx = Context::new();
let raw = " \tfoo\t =\t bar \t";
let p1 = Point::new(ctx.alloc_file_name("fname.em"), ctx.alloc_file_content(raw));
let attr = Attr::named(p1.src.clone(), Location::new(&p1, &p1.clone().shift(raw)));
let p1 =
Point::at_start_of(ctx.alloc_file_name("fname.em"), ctx.alloc_file_content(raw));
let attr = Attr::named(p1.src().clone(), Location::new(&p1, &p1.clone().shift(raw)));

assert_eq!(attr.name().unwrap(), "foo");
assert_eq!(attr.repr(), "foo");
Expand All @@ -441,7 +443,8 @@ mod test {
fn call_name() {
let ctx = Context::new();
let text = "hello, world!";
let p1 = Point::new(ctx.alloc_file_name("main.em"), ctx.alloc_file_content(text));
let p1 =
Point::at_start_of(ctx.alloc_file_name("main.em"), ctx.alloc_file_content(text));
let p2 = p1.clone().shift(text);
let loc = Location::new(&p1, &p2);

Expand Down
4 changes: 2 additions & 2 deletions crates/emblem_core/src/log/messages/unexpected_eof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ pub struct UnexpectedEOF {
impl UnexpectedEOF {
pub fn new(mut point: Point, expected: Vec<String>) -> Self {
assert!(
point.index > 0,
point.index() > 0,
"internal error: empty files are supposed to be valid"
);

point.index -= 1;
*point.index_mut() -= 1;

Self { point, expected }
}
Expand Down
4 changes: 2 additions & 2 deletions crates/emblem_core/src/log/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -524,8 +524,8 @@ mod test {
let ctx = Context::new();
let content = ctx.alloc_file_content("hello, world");
let srcs = [
Point::new(ctx.alloc_file_name("main.em"), content.clone()),
Point::new(ctx.alloc_file_name("something-else.em"), content),
Point::at_start_of(ctx.alloc_file_name("main.em"), content.clone()),
Point::at_start_of(ctx.alloc_file_name("something-else.em"), content),
]
.into_iter()
.map(|p| {
Expand Down
2 changes: 1 addition & 1 deletion crates/emblem_core/src/log/note.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ mod test {

fn placeholder_loc() -> Location {
let ctx = Context::new();
let p = Point::new(
let p = Point::at_start_of(
ctx.alloc_file_name("main.em"),
ctx.alloc_file_content("hello, world!"),
);
Expand Down
4 changes: 2 additions & 2 deletions crates/emblem_core/src/log/src.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ mod test {
#[test]
fn loc() {
let ctx = Context::new();
let p = Point::new(
let p = Point::at_start_of(
ctx.alloc_file_name("main.em"),
ctx.alloc_file_content("1111111111111"),
);
Expand All @@ -77,7 +77,7 @@ mod test {
#[test]
fn annotations() {
let ctx = Context::new();
let start = Point::new(
let start = Point::at_start_of(
ctx.alloc_file_name("main.em"),
ctx.alloc_file_content("111111222222"),
);
Expand Down
6 changes: 3 additions & 3 deletions crates/emblem_core/src/parser/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ impl Lexer {
failed: false,
start_of_line: true,
current_indent: 0,
curr_point: Point::new(file.clone(), input.clone()),
prev_point: Point::new(file, input),
curr_point: Point::at_start_of(file.clone(), input.clone()),
prev_point: Point::at_start_of(file, input),
open_braces: Vec::new(),
next_toks: VecDeque::new(),
multi_line_comment_starts: Vec::new(),
Expand Down Expand Up @@ -354,7 +354,7 @@ impl Iterator for Lexer {
}

if self.start_of_line {
if self.curr_point.index == 0 {
if self.curr_point.index() == 0 {
if let Some(shebang) = &self.try_consume(&SHEBANG) {
return Some(Ok(self.span(Tok::Shebang(shebang.slice(2..)))));
}
Expand Down
60 changes: 30 additions & 30 deletions crates/emblem_core/src/parser/location.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ pub struct Location {
impl Location {
pub fn new(start: &Point, end: &Point) -> Self {
Self {
file_name: start.file_name.clone(),
src: start.src.clone(),
lines: (start.line, end.line),
indices: (start.index, end.index),
cols: (start.col, cmp::max(1, end.col - 1)),
file_name: start.file_name().clone(),
src: start.src().clone(),
lines: (start.line(), end.line()),
indices: (start.index(), end.index()),
cols: (start.col(), cmp::max(1, end.col() - 1)),
}
}

Expand All @@ -45,23 +45,23 @@ impl Location {
}

pub fn start(&self) -> Point {
Point {
file_name: self.file_name.clone(),
src: self.src.clone(),
line: self.lines.0,
col: self.cols.0,
index: self.indices.0,
}
Point::new(
self.file_name.clone(),
self.src.clone(),
self.lines.0,
self.cols.0,
self.indices.0,
)
}

pub fn end(&self) -> Point {
Point {
file_name: self.file_name.clone(),
src: self.src.clone(),
line: self.lines.1,
col: self.cols.1,
index: self.indices.1,
}
Point::new(
self.file_name.clone(),
self.src.clone(),
self.lines.1,
self.cols.1,
self.indices.1,
)
}

pub fn span_to(&self, other: &Self) -> Self {
Expand Down Expand Up @@ -136,40 +136,40 @@ mod test {
fn mid_line() {
let ctx = Context::new();
let text = "my name\nis methos";
let start = Point::new(
let start = Point::at_start_of(
ctx.alloc_file_name("fname.em"),
ctx.alloc_file_content(text),
);
let end = start.clone().shift(text);
let loc = Location::new(&start, &end);
assert_eq!("fname.em", loc.file_name());
assert_eq!(text, loc.src().raw());
assert_eq!((start.line, end.line), loc.lines());
assert_eq!((start.col, end.col - 1), loc.cols());
assert_eq!((start.line(), end.line()), loc.lines());
assert_eq!((start.col(), end.col() - 1), loc.cols());
}

#[test]
fn end_of_line() {
let ctx = Context::new();
let text = "my name is methos\n";
let start = Point::new(
let start = Point::at_start_of(
ctx.alloc_file_name("fname.em"),
ctx.alloc_file_content(text),
);
let end = start.clone().shift(text);
let loc = Location::new(&start, &end);
assert_eq!("fname.em", loc.file_name());
assert_eq!(text, loc.src().raw());
assert_eq!((start.line, end.line), loc.lines());
assert_eq!((start.col, 1), loc.cols());
assert_eq!((start.line(), end.line()), loc.lines());
assert_eq!((start.col(), 1), loc.cols());
}
}

#[test]
fn start() {
let ctx = Context::new();
let text = "my name is methos\n";
let start = Point::new(
let start = Point::at_start_of(
ctx.alloc_file_name("fname.em"),
ctx.alloc_file_content(text),
);
Expand All @@ -182,7 +182,7 @@ mod test {
fn end() {
let ctx = Context::new();
let text = "my name is methos\n";
let start = Point::new(
let start = Point::at_start_of(
ctx.alloc_file_name("fname.em"),
ctx.alloc_file_content(text),
);
Expand All @@ -195,7 +195,7 @@ mod test {
fn span_to() {
let ctx = Context::new();
let text = "my name is methos\n";
let p1 = Point::new(
let p1 = Point::at_start_of(
ctx.alloc_file_name("fname.em"),
ctx.alloc_file_content(text),
);
Expand Down Expand Up @@ -235,7 +235,7 @@ mod test {
fn single_line() {
let ctx = Context::new();
let text = "oh! santiana gained a day";
let text_start = Point::new(
let text_start = Point::at_start_of(
ctx.alloc_file_name("fname.em"),
ctx.alloc_file_content(text),
);
Expand Down Expand Up @@ -264,7 +264,7 @@ mod test {
];
for newline in ["\n", "\r", "\r\n"] {
let text = lines.join(newline);
let text_start = Point::new(
let text_start = Point::at_start_of(
ctx.alloc_file_name("fname.em"),
ctx.alloc_file_content(&text),
);
Expand Down
Loading

0 comments on commit 16d8dd6

Please sign in to comment.