Skip to content

Commit

Permalink
Merge pull request #329 from KisaragiEffective/chore/source-pos-const…
Browse files Browse the repository at this point in the history
…ructor
  • Loading branch information
KisaragiEffective authored Dec 20, 2023
2 parents 95b4d13 + 185bba2 commit daa964e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 74 deletions.
75 changes: 15 additions & 60 deletions package/origlang-compiler/src/lexer/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,84 +105,57 @@ fn token_location() {

assert_eq!(lexer.next(), Pointed {
data: Token::VarKeyword,
position: SourcePosition {
line: NonZeroUsize::new(1).unwrap(),
column: NonZeroUsize::new(1).unwrap()
}
position: SourcePosition::try_new((1, 1)).unwrap(),
});

assert_eq!(lexer.next(), Pointed {
data: Token::Identifier {
inner: Identifier::new("x".to_string())
},
position: SourcePosition {
line: NonZeroUsize::new(1).unwrap(),
column: NonZeroUsize::new(5).unwrap()
}
position: SourcePosition::try_new((1, 5)).unwrap()
});

assert_eq!(lexer.next(), Pointed {
data: Token::SymEq,
position: SourcePosition {
line: NonZeroUsize::new(1).unwrap(),
column: NonZeroUsize::new(7).unwrap()
}
position: SourcePosition::try_new((1, 7)).unwrap()
});

assert_eq!(lexer.next(), Pointed {
data: Token::Digits {
sequence: "1".to_string(),
suffix: None,
},
position: SourcePosition {
line: NonZeroUsize::new(1).unwrap(),
column: NonZeroUsize::new(9).unwrap()
}
position: SourcePosition::try_new((1, 9)).unwrap()
});

assert_eq!(lexer.next(), Pointed {
data: Token::NewLine,
position: SourcePosition {
line: NonZeroUsize::new(1).unwrap(),
column: NonZeroUsize::new(10).unwrap()
}
position: SourcePosition::try_new((1, 10)).unwrap()
});

assert_eq!(lexer.next(), Pointed {
data: Token::VarKeyword,
position: SourcePosition {
line: NonZeroUsize::new(2).unwrap(),
column: NonZeroUsize::new(1).unwrap()
}
position: SourcePosition::try_new((2, 1)).unwrap()
});

assert_eq!(lexer.next(), Pointed {
data: Token::Identifier {
inner: Identifier::new("y".to_string())
},
position: SourcePosition {
line: NonZeroUsize::new(2).unwrap(),
column: NonZeroUsize::new(5).unwrap()
}
position: SourcePosition::try_new((2, 5)).unwrap()
});

assert_eq!(lexer.next(), Pointed {
data: Token::SymEq,
position: SourcePosition {
line: NonZeroUsize::new(2).unwrap(),
column: NonZeroUsize::new(7).unwrap()
}
position: SourcePosition::try_new((2, 7)).unwrap()
});

assert_eq!(lexer.next(), Pointed {
data: Token::Digits {
sequence: "2".to_string(),
suffix: None,
},
position: SourcePosition {
line: NonZeroUsize::new(2).unwrap(),
column: NonZeroUsize::new(9).unwrap()
}
position: SourcePosition::try_new((2, 9)).unwrap()
});
}

Expand Down Expand Up @@ -227,10 +200,7 @@ fn off_by_one_range_regression() {
sequence: "9".to_string(),
suffix: None,
},
position: SourcePosition {
line: NonZeroUsize::new(1).unwrap(),
column: NonZeroUsize::new(1).unwrap(),
}
position: SourcePosition::try_new((1, 1)).unwrap()
});
}

Expand All @@ -239,40 +209,25 @@ fn skip_whitespace_only_lines() {
let lexer = Lexer::create(" \n \n \nprint 1");
assert_eq!(lexer.next(), Pointed {
data: Token::NewLine,
position: SourcePosition {
line: NonZeroUsize::new(1).unwrap(),
column: NonZeroUsize::new(5).unwrap(),
}
position: SourcePosition::try_new((1, 5)).unwrap()
});
assert_eq!(lexer.next(), Pointed {
data: Token::NewLine,
position: SourcePosition {
line: NonZeroUsize::new(2).unwrap(),
column: NonZeroUsize::new(5).unwrap(),
}
position: SourcePosition::try_new((2, 5)).unwrap()
});
assert_eq!(lexer.next(), Pointed {
data: Token::NewLine,
position: SourcePosition {
line: NonZeroUsize::new(3).unwrap(),
column: NonZeroUsize::new(5).unwrap(),
}
position: SourcePosition::try_new((3, 5)).unwrap()
});
assert_eq!(lexer.next(), Pointed {
data: Token::KeywordPrint,
position: SourcePosition {
line: NonZeroUsize::new(4).unwrap(),
column: NonZeroUsize::new(1).unwrap(),
}
position: SourcePosition::try_new((4, 1)).unwrap()
});
assert_eq!(lexer.next(), Pointed {
data: Token::Digits {
sequence: "1".to_string(),
suffix: None,
},
position: SourcePosition {
line: NonZeroUsize::new(4).unwrap(),
column: NonZeroUsize::new(7).unwrap(),
},
position: SourcePosition::try_new((4, 7)).unwrap(),
})
}
43 changes: 29 additions & 14 deletions package/origlang-source-span/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,33 @@ pub struct SourcePosition {
pub column: NonZeroUsize,
}

impl SourcePosition {
pub const fn new(line: NonZeroUsize, column: NonZeroUsize) -> Self {
Self {
line, column
}
}

pub fn try_new<E>(lc: impl TryIntoSourcePosition<Err = E>) -> Result<Self, E> {
lc.try_into()
}
}

pub trait TryIntoSourcePosition {
type Err;

fn try_into(self) -> Result<SourcePosition, Self::Err>;
}

impl<R: TryInto<NonZeroUsize> + Ord + Eq> TryIntoSourcePosition for (R, R) {
type Err = R::Error;

fn try_into(self) -> Result<SourcePosition, Self::Err> {
let (l, c) = self;
Ok(SourcePosition::new(l.try_into()?, c.try_into()?))
}
}

impl Display for SourcePosition {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.write_str(&self.line.to_string())?;
Expand Down Expand Up @@ -49,19 +76,7 @@ mod tests {
#[test]
fn source_pos_order() {
// 辞書式順序の理解があっているかどうか
assert!(SourcePosition {
line: 1.try_into().unwrap(),
column: 1.try_into().unwrap(),
} < SourcePosition {
line: 1.try_into().unwrap(),
column: 2.try_into().unwrap(),
});
assert!(SourcePosition {
line: 1.try_into().unwrap(),
column: usize::MAX.try_into().unwrap(),
} < SourcePosition {
line: 2.try_into().unwrap(),
column: 1.try_into().unwrap(),
});
assert!(SourcePosition::try_new((1, 1)).unwrap() < SourcePosition::try_new((1, 2)).unwrap());
assert!(SourcePosition::try_new((1, usize::MAX)).unwrap() < SourcePosition::try_new((2, 1)).unwrap());
}
}

0 comments on commit daa964e

Please sign in to comment.