Skip to content

Commit

Permalink
chore(regular_expression): Extract diagnostics (#5287)
Browse files Browse the repository at this point in the history
- Extract `Diagnostic::error()`s to separate file
- Align error message prefix
  • Loading branch information
leaysgur committed Aug 28, 2024
1 parent d6b84ec commit 15b87ad
Show file tree
Hide file tree
Showing 6 changed files with 739 additions and 602 deletions.
155 changes: 155 additions & 0 deletions crates/oxc_regular_expression/src/body_parser/diagnostics.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
use oxc_diagnostics::OxcDiagnostic;
use oxc_span::Span;

const PREFIX: &str = "Invalid regular expression:";

#[cold]
pub fn duplicated_capturing_group_names(spans: Vec<Span>) -> OxcDiagnostic {
OxcDiagnostic::error(format!("{PREFIX} Duplicated capturing group names")).with_labels(spans)
}

#[cold]
pub fn too_may_capturing_groups(span0: Span) -> OxcDiagnostic {
OxcDiagnostic::error(format!("{PREFIX} Too many capturing groups")).with_label(span0)
}

#[cold]
pub fn parse_pattern_incomplete(span0: Span) -> OxcDiagnostic {
OxcDiagnostic::error(format!("{PREFIX} Could not parse the entire pattern")).with_label(span0)
}

#[cold]
pub fn lone_quantifier(span0: Span, kind: &str) -> OxcDiagnostic {
OxcDiagnostic::error(format!("{PREFIX} Lone quantifier found, expected with `{kind}`"))
.with_label(span0)
}

#[cold]
pub fn unterminated_pattern(span0: Span, kind: &str) -> OxcDiagnostic {
OxcDiagnostic::error(format!("{PREFIX} Unterminated {kind}")).with_label(span0)
}

#[cold]
pub fn invalid_extended_atom_escape(span0: Span) -> OxcDiagnostic {
OxcDiagnostic::error(format!("{PREFIX} Invalid extended atom escape")).with_label(span0)
}

#[cold]
pub fn invalid_braced_quantifier(span0: Span) -> OxcDiagnostic {
OxcDiagnostic::error(format!("{PREFIX} Invalid braced quantifier")).with_label(span0)
}

#[cold]
pub fn invalid_indexed_reference(span0: Span) -> OxcDiagnostic {
OxcDiagnostic::error(format!("{PREFIX} Invalid indexed reference")).with_label(span0)
}

#[cold]
pub fn empty_group_specifier(span0: Span) -> OxcDiagnostic {
OxcDiagnostic::error(format!("{PREFIX} Group specifier is empty")).with_label(span0)
}

#[cold]
pub fn invalid_named_reference(span0: Span) -> OxcDiagnostic {
OxcDiagnostic::error(format!("{PREFIX} Invalid named reference")).with_label(span0)
}

#[cold]
pub fn invalid_unicode_property_name_negative_strings(span0: Span, name: &str) -> OxcDiagnostic {
OxcDiagnostic::error(format!(
"{PREFIX} Invalid property name `{name}`(negative + property of strings)"
))
.with_label(span0)
}

#[cold]
pub fn invalid_character_class(span0: Span) -> OxcDiagnostic {
OxcDiagnostic::error(format!("{PREFIX} Invalid character class with strings unicode property"))
.with_label(span0)
}

#[cold]
pub fn character_class_range_out_of_order(span0: Span, kind: &str) -> OxcDiagnostic {
OxcDiagnostic::error(format!("{PREFIX} Character {kind} range out of order")).with_label(span0)
}

#[cold]
pub fn character_class_range_invalid_atom(span0: Span) -> OxcDiagnostic {
OxcDiagnostic::error(format!("{PREFIX} Character class range with invalid atom"))
.with_label(span0)
}

#[cold]
pub fn invalid_class_atom(span0: Span) -> OxcDiagnostic {
OxcDiagnostic::error(format!("{PREFIX} Invalid class atom")).with_label(span0)
}

#[cold]
pub fn empty_class_set_expression(span0: Span) -> OxcDiagnostic {
OxcDiagnostic::error(format!("{PREFIX} Expected nonempty class set expression"))
.with_label(span0)
}

#[cold]
pub fn class_intersection_unexpected_ampersand(span0: Span) -> OxcDiagnostic {
OxcDiagnostic::error(format!("{PREFIX} Unexpected `&` inside of class intersection"))
.with_label(span0)
}

#[cold]
pub fn class_set_expression_invalid_character(span0: Span, kind: &str) -> OxcDiagnostic {
OxcDiagnostic::error(format!("{PREFIX} Unexpected character inside of {kind}"))
.with_label(span0)
}

#[cold]
pub fn character_class_contents_invalid_operands(span0: Span) -> OxcDiagnostic {
OxcDiagnostic::error(format!(
"{PREFIX} Invalid class operands inside of character class contents"
))
.with_label(span0)
}

#[cold]
pub fn missing_capturing_group_name(span0: Span) -> OxcDiagnostic {
OxcDiagnostic::error(format!("{PREFIX} Missing capturing group name")).with_label(span0)
}

#[cold]
pub fn too_large_number_in_braced_quantifier(span0: Span) -> OxcDiagnostic {
OxcDiagnostic::error(format!("{PREFIX} Number is too large in braced quantifier"))
.with_label(span0)
}

#[cold]
pub fn braced_quantifier_out_of_order(span0: Span) -> OxcDiagnostic {
OxcDiagnostic::error(format!("{PREFIX} Numbers out of order in braced quantifier"))
.with_label(span0)
}

#[cold]
pub fn too_large_number_digits(span0: Span, kind: &str) -> OxcDiagnostic {
OxcDiagnostic::error(format!("{PREFIX} Number is too large in {kind} digits")).with_label(span0)
}

#[cold]
pub fn invalid_unicode_property(span0: Span, kind: &str) -> OxcDiagnostic {
OxcDiagnostic::error(format!("{PREFIX} Invalid unicode property {kind}")).with_label(span0)
}

#[cold]
pub fn invalid_unicode_property_of_strings(span0: Span, name: &str) -> OxcDiagnostic {
OxcDiagnostic::error(format!("{PREFIX} Invalid unicode property `{name}`"))
.with_help("Enable `UnicodeSetsMode` to use this property")
.with_label(span0)
}

#[cold]
pub fn invalid_unicode_escape_sequence(span0: Span) -> OxcDiagnostic {
OxcDiagnostic::error(format!("{PREFIX} Invalid unicode escape sequence")).with_label(span0)
}

#[cold]
pub fn invalid_surrogate_pair(span0: Span) -> OxcDiagnostic {
OxcDiagnostic::error(format!("{PREFIX} Invalid surrogate pair")).with_label(span0)
}
1 change: 1 addition & 0 deletions crates/oxc_regular_expression/src/body_parser/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod diagnostics;
mod parser;
mod reader;
mod state;
Expand Down
Loading

0 comments on commit 15b87ad

Please sign in to comment.