Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(parser): accept leading dot in literals #151

Merged
merged 1 commit into from
Dec 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions crates/ast/src/ast/semver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub use semver::Op as SemverOp;
// we however dedicate a separate node for this: [`SemverReqComponentKind::Range`]

/// A SemVer version number.
#[derive(Clone, Copy, Debug)]
#[derive(Clone, Copy)]
pub enum SemverVersionNumber {
/// A number.
Number(u32),
Expand Down Expand Up @@ -54,6 +54,13 @@ impl fmt::Display for SemverVersionNumber {
}
}

impl fmt::Debug for SemverVersionNumber {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(self, f)
}
}

impl PartialEq for SemverVersionNumber {
#[inline]
fn eq(&self, other: &Self) -> bool {
Expand Down Expand Up @@ -84,7 +91,7 @@ impl Ord for SemverVersionNumber {
}

/// A SemVer version.
#[derive(Clone, Debug)]
#[derive(Clone)]
pub struct SemverVersion {
pub span: Span,
/// Major version.
Expand Down Expand Up @@ -147,6 +154,15 @@ impl fmt::Display for SemverVersion {
}
}

impl fmt::Debug for SemverVersion {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("SemverVersion")
.field("span", &self.span)
.field("version", &format_args!("{self}"))
.finish()
}
}

impl From<semver::Version> for SemverVersion {
#[inline]
fn from(version: semver::Version) -> Self {
Expand Down
30 changes: 23 additions & 7 deletions crates/ast/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ pub enum TokenKind {

impl fmt::Display for TokenKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.as_str())
f.write_str(&self.description())
}
}

Expand All @@ -275,7 +275,7 @@ impl TokenKind {
}

/// Returns the string representation of the token kind.
pub fn as_str(&self) -> Cow<'static, str> {
pub fn as_str(&self) -> &str {
match self {
Self::Eq => "=",
Self::Lt => "<",
Expand Down Expand Up @@ -310,13 +310,24 @@ impl TokenKind {
Self::OpenDelim(Delimiter::Bracket) => "[",
Self::CloseDelim(Delimiter::Bracket) => "]",

Self::Literal(.., symbol) | Self::Ident(.., symbol) | Self::Comment(.., symbol) => {
symbol.as_str()
}

Self::Eof => "<eof>",
}
}

/// Returns the description of the token kind.
pub fn description(&self) -> Cow<'_, str> {
match self {
Self::Literal(kind, _) => return format!("<{}>", kind.description()).into(),
Self::Ident(symbol) => return symbol.to_string().into(),
Self::Comment(false, CommentKind::Block, _symbol) => "<block comment>",
Self::Comment(true, CommentKind::Block, _symbol) => "<block doc-comment>",
Self::Comment(false, CommentKind::Line, _symbol) => "<line comment>",
Self::Comment(true, CommentKind::Line, _symbol) => "<line doc-comment>",
Self::Eof => "<eof>",
Self::Comment(false, CommentKind::Block, _) => "<block comment>",
Self::Comment(true, CommentKind::Block, _) => "<block doc-comment>",
Self::Comment(false, CommentKind::Line, _) => "<line comment>",
Self::Comment(true, CommentKind::Line, _) => "<line doc-comment>",
_ => self.as_str(),
}
.into()
}
Expand Down Expand Up @@ -676,6 +687,11 @@ impl Token {
}
}

/// Returns the string representation of the token.
pub fn as_str(&self) -> &str {
self.kind.as_str()
}

/// Returns this token's description, if any.
#[inline]
pub fn description(&self) -> Option<TokenDescription> {
Expand Down
4 changes: 2 additions & 2 deletions crates/interface/src/diagnostics/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ impl DiagCtxt {
}

/// Creates a new `DiagCtxt` with a test emitter.
pub fn with_test_emitter() -> Self {
Self::new(Box::new(HumanEmitter::test()))
pub fn with_test_emitter(source_map: Option<Arc<SourceMap>>) -> Self {
Self::new(Box::new(HumanEmitter::test().source_map(source_map)))
}

/// Creates a new `DiagCtxt` with a stderr emitter.
Expand Down
5 changes: 3 additions & 2 deletions crates/interface/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ impl From<derive_builder::UninitializedFieldError> for SessionBuilderError {
impl SessionBuilder {
/// Sets the diagnostic context to a test emitter.
#[inline]
pub fn with_test_emitter(self) -> Self {
self.dcx(DiagCtxt::with_test_emitter())
pub fn with_test_emitter(mut self) -> Self {
let sm = self.get_source_map();
self.dcx(DiagCtxt::with_test_emitter(Some(sm)))
}

/// Sets the diagnostic context to a stderr emitter.
Expand Down
26 changes: 17 additions & 9 deletions crates/parse/src/lexer/cursor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ impl<'a> Cursor<'a> {
let kind = self.number(c);
RawTokenKind::Literal { kind }
}
'.' if self.first().is_ascii_digit() => {
let kind = self.rational_number_after_dot(Base::Decimal);
RawTokenKind::Literal { kind }
}

// One-symbol tokens.
';' => RawTokenKind::Semi,
Expand Down Expand Up @@ -265,15 +269,7 @@ impl<'a> Cursor<'a> {
// by field/method access (`12.foo()`)
'.' if !is_id_start(self.second()) => {
self.bump();
self.eat_decimal_digits();
let empty_exponent = match self.first() {
'e' | 'E' => {
self.bump();
!self.eat_exponent()
}
_ => false,
};
RawLiteralKind::Rational { base, empty_exponent }
self.rational_number_after_dot(base)
}
'e' | 'E' => {
self.bump();
Expand All @@ -284,6 +280,18 @@ impl<'a> Cursor<'a> {
}
}

fn rational_number_after_dot(&mut self, base: Base) -> RawLiteralKind {
self.eat_decimal_digits();
let empty_exponent = match self.first() {
'e' | 'E' => {
self.bump();
!self.eat_exponent()
}
_ => false,
};
RawLiteralKind::Rational { base, empty_exponent }
}

fn maybe_string_prefix(&mut self, prefix: &str) -> Option<bool> {
debug_assert_eq!(self.prev(), prefix.chars().next().unwrap());
let prefix = &prefix[1..];
Expand Down
2 changes: 1 addition & 1 deletion crates/parse/src/lexer/cursor/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ pub enum RawTokenKind {
pub enum RawLiteralKind {
/// `123`, `0x123`; empty_int: `0x`
Int { base: Base, empty_int: bool },
/// `123.321`, `1.2e3`; empty_exponent: `2e`, `2.3e`
/// `123.321`, `1.2e3`, `.2e3`; empty_exponent: `2e`, `2.3e`, `.3e`
Rational { base: Base, empty_exponent: bool },
/// `"abc"`, `"abc`; `unicode"abc"`, `unicode"abc`
Str { terminated: bool, unicode: bool },
Expand Down
2 changes: 2 additions & 0 deletions crates/parse/src/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,8 @@ mod tests {
],
),
("0.0", &[(0..3, lit(Rational, "0.0"))]),
("0.", &[(0..2, lit(Rational, "0."))]),
(".0", &[(0..2, lit(Rational, ".0"))]),
("0.0e1", &[(0..5, lit(Rational, "0.0e1"))]),
("0.0e-1", &[(0..6, lit(Rational, "0.0e-1"))]),
("0e1", &[(0..3, lit(Rational, "0e1"))]),
Expand Down
Loading