Skip to content

Commit

Permalink
Diagnostic refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
hydroper committed Apr 27, 2024
1 parent 28d2618 commit eb73c52
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 107 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion crates/parser/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "as3_parser"
version = "0.5.27"
version = "0.5.28"
edition = "2021"
authors = ["hydroper <[email protected]>"]
repository = "https://github.com/hydroper/as3parser"
Expand Down
36 changes: 12 additions & 24 deletions crates/parser/diagnostics/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub struct Diagnostic {
pub(crate) kind: DiagnosticKind,
pub(crate) is_warning: bool,
pub(crate) is_verify_error: bool,
pub(crate) arguments: Vec<DiagnosticArgument>,
pub(crate) arguments: Vec<Rc<dyn DiagnosticArgument>>,
pub(crate) custom_kind: RefCell<Option<Rc<dyn Any>>>,
}

Expand All @@ -41,7 +41,7 @@ impl PartialOrd for Diagnostic {
}

impl Diagnostic {
pub fn new_syntax_error(location: &Location, kind: DiagnosticKind, arguments: Vec<DiagnosticArgument>) -> Self {
pub fn new_syntax_error(location: &Location, kind: DiagnosticKind, arguments: Vec<Rc<dyn DiagnosticArgument>>) -> Self {
Self {
location: location.clone(),
kind,
Expand All @@ -52,7 +52,7 @@ impl Diagnostic {
}
}

pub fn new_verify_error(location: &Location, kind: DiagnosticKind, arguments: Vec<DiagnosticArgument>) -> Self {
pub fn new_verify_error(location: &Location, kind: DiagnosticKind, arguments: Vec<Rc<dyn DiagnosticArgument>>) -> Self {
Self {
location: location.clone(),
kind,
Expand All @@ -63,7 +63,7 @@ impl Diagnostic {
}
}

pub fn new_warning(location: &Location, kind: DiagnosticKind, arguments: Vec<DiagnosticArgument>) -> Self {
pub fn new_warning(location: &Location, kind: DiagnosticKind, arguments: Vec<Rc<dyn DiagnosticArgument>>) -> Self {
Self {
location: location.clone(),
kind,
Expand Down Expand Up @@ -98,7 +98,7 @@ impl Diagnostic {
self.is_verify_error
}

pub fn arguments(&self) -> Vec<DiagnosticArgument> {
pub fn arguments(&self) -> Vec<Rc<dyn DiagnosticArgument>> {
self.arguments.clone()
}

Expand Down Expand Up @@ -147,7 +147,7 @@ impl Diagnostic {
let mut string_arguments: HashMap<String, String> = hashmap!{};
let mut i = 1;
for argument in &self.arguments {
string_arguments.insert(i.to_string(), self.format_argument(argument.clone()));
string_arguments.insert(i.to_string(), argument.to_string());
i += 1;
}
use late_format::LateFormat;
Expand All @@ -157,31 +157,19 @@ impl Diagnostic {
};
msg.late_format(string_arguments)
}

fn format_argument(&self, argument: DiagnosticArgument) -> String {
match argument {
DiagnosticArgument::String(s) => s.clone(),
DiagnosticArgument::Token(t) => t.to_string(),
DiagnosticArgument::Dynamic(d) => d.to_string(),
}
}
}

/// The `diagarg![...]` literal is used for initializing
/// diagnostic arguments.
///
/// For example: `diagarg![Token(t1), String("foo".into())]`.
/// For example: `diagarg![token, "foo".into()]`.
pub macro diagarg {
($($variant:ident($value:expr)),*) => { vec![ $(DiagnosticArgument::$variant($value)),* ] },
($($value:expr),*) => { vec![ $(Rc::new($value)),* ] },
}

#[derive(Clone)]
pub enum DiagnosticArgument {
String(String),
Token(Token),
Dynamic(Rc<dyn DynamicDiagnosticArgument>),
pub trait DiagnosticArgument: Any + ToString + 'static {
}

pub trait DynamicDiagnosticArgument {
fn to_string(&self) -> String;
}
impl DiagnosticArgument for String {}

impl DiagnosticArgument for Token {}
32 changes: 16 additions & 16 deletions crates/parser/parser/css_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ impl<'input> CssParser<'input> {
self.locations.pop().unwrap().combine_with(self.previous_token.1.clone())
}

fn add_syntax_error(&self, location: &Location, kind: DiagnosticKind, arguments: Vec<DiagnosticArgument>) {
fn add_syntax_error(&self, location: &Location, kind: DiagnosticKind, arguments: Vec<Rc<dyn DiagnosticArgument>>) {
if self.compilation_unit().prevent_equal_offset_error(location) {
return;
}
self.compilation_unit().add_diagnostic(Diagnostic::new_syntax_error(location, kind, arguments));
}

fn _patch_syntax_error(&self, original: DiagnosticKind, location: &Location, kind: DiagnosticKind, arguments: Vec<DiagnosticArgument>) {
fn _patch_syntax_error(&self, original: DiagnosticKind, location: &Location, kind: DiagnosticKind, arguments: Vec<Rc<dyn DiagnosticArgument>>) {
if self.compilation_unit().diagnostics.borrow().is_empty() {
return;
}
Expand All @@ -63,7 +63,7 @@ impl<'input> CssParser<'input> {
}

/*
fn add_warning(&self, location: &Location, kind: DiagnosticKind, arguments: Vec<DiagnosticArgument>) {
fn add_warning(&self, location: &Location, kind: DiagnosticKind, arguments: Vec<Rc<dyn DiagnosticArgument>>) {
if self.compilation_unit().prevent_equal_offset_warning(location) {
return;
}
Expand Down Expand Up @@ -126,7 +126,7 @@ impl<'input> CssParser<'input> {
fn expect(&mut self, token: Token) {
if self.token.0 != token {
self.expecting_token_error = true;
self.add_syntax_error(&self.token_location(), DiagnosticKind::Expecting, diagarg![Token(token.clone()), Token(self.token.0.clone())]);
self.add_syntax_error(&self.token_location(), DiagnosticKind::Expecting, diagarg![token.clone(), self.token.0.clone()]);
} else {
self.expecting_token_error = false;
self.next();
Expand All @@ -141,7 +141,7 @@ impl<'input> CssParser<'input> {
(id, location)
} else {
self.expecting_token_error = true;
self.add_syntax_error(&self.token_location(), DiagnosticKind::ExpectingIdentifier, diagarg![Token(self.token.0.clone())]);
self.add_syntax_error(&self.token_location(), DiagnosticKind::ExpectingIdentifier, diagarg![self.token.0.clone()]);
(INVALIDATED_IDENTIFIER.to_owned(), self.tokenizer.cursor_location())
}
}
Expand All @@ -153,7 +153,7 @@ impl<'input> CssParser<'input> {
Some(value)
} else {
self.expecting_token_error = true;
self.add_syntax_error(&self.token_location(), DiagnosticKind::Unexpected, diagarg![Token(self.token.0.clone())]);
self.add_syntax_error(&self.token_location(), DiagnosticKind::Unexpected, diagarg![self.token.0.clone()]);
None
}
}
Expand All @@ -166,7 +166,7 @@ impl<'input> CssParser<'input> {
(v, location)
} else {
self.expecting_token_error = true;
self.add_syntax_error(&self.token_location(), DiagnosticKind::ExpectingStringLiteral, diagarg![Token(self.token.0.clone())]);
self.add_syntax_error(&self.token_location(), DiagnosticKind::ExpectingStringLiteral, diagarg![self.token.0.clone()]);
("".into(), self.tokenizer.cursor_location())
}
}
Expand Down Expand Up @@ -248,7 +248,7 @@ impl<'input> CssParser<'input> {
} else if self.peek(Token::CssAtFontFace) {
self.parse_font_face()
} else {
self.add_syntax_error(&self.token.1, DiagnosticKind::ExpectingDirective, diagarg![Token(self.token.0.clone())]);
self.add_syntax_error(&self.token.1, DiagnosticKind::ExpectingDirective, diagarg![self.token.0.clone()]);
let d = self.create_invalidated_directive(&self.tokenizer.cursor_location());
self.next();
d
Expand All @@ -263,15 +263,15 @@ impl<'input> CssParser<'input> {
if let Some(condition) = condition {
conditions.push(condition);
} else {
self.add_syntax_error(&self.token.1, DiagnosticKind::Unexpected, diagarg![Token(self.token.0.clone())]);
self.add_syntax_error(&self.token.1, DiagnosticKind::Unexpected, diagarg![self.token.0.clone()]);
}
loop {
if let Some(condition) = self.parse_opt_media_query_condition() {
conditions.push(condition);
} else if self.eof() || self.peek(Token::BlockOpen) {
break;
} else if !self.consume(Token::Comma) {
self.add_syntax_error(&self.token.1, DiagnosticKind::Unexpected, diagarg![Token(self.token.0.clone())]);
self.add_syntax_error(&self.token.1, DiagnosticKind::Unexpected, diagarg![self.token.0.clone()]);
self.next();
}
}
Expand All @@ -281,7 +281,7 @@ impl<'input> CssParser<'input> {
if let Some(rule) = self.parse_opt_rule() {
rules.push(Rc::new(rule));
} else {
self.add_syntax_error(&self.token.1, DiagnosticKind::Unexpected, diagarg![Token(self.token.0.clone())]);
self.add_syntax_error(&self.token.1, DiagnosticKind::Unexpected, diagarg![self.token.0.clone()]);
self.next();
}
}
Expand Down Expand Up @@ -342,7 +342,7 @@ impl<'input> CssParser<'input> {
right,
});
} else {
self.add_syntax_error(&self.token.1, DiagnosticKind::Unexpected, diagarg![Token(self.token.0.clone())]);
self.add_syntax_error(&self.token.1, DiagnosticKind::Unexpected, diagarg![self.token.0.clone()]);
base = Rc::new(CssMediaQueryCondition::And {
location: self.pop_location(),
left: base,
Expand All @@ -357,7 +357,7 @@ impl<'input> CssParser<'input> {

fn parse_arguments(&mut self) -> Result<CssParserFacade, ParserError> {
if !self.peek(Token::ParenOpen) {
self.add_syntax_error(&self.token.1, DiagnosticKind::Expecting, diagarg![Token(Token::ParenOpen), Token(self.token.0.clone())]);
self.add_syntax_error(&self.token.1, DiagnosticKind::Expecting, diagarg![Token::ParenOpen, self.token.0.clone()]);
return Err(ParserError::Common);
}
let (byte_range, token) = self.tokenizer.scan_arguments();
Expand All @@ -376,7 +376,7 @@ impl<'input> CssParser<'input> {
if let Some(s) = self.parse_opt_selector() {
selectors.push(s);
} else {
self.add_syntax_error(&self.token.1, DiagnosticKind::Unexpected, diagarg![Token(self.token.0.clone())]);
self.add_syntax_error(&self.token.1, DiagnosticKind::Unexpected, diagarg![self.token.0.clone()]);
}
}
let mut properties: Vec<Rc<CssProperty>> = vec![];
Expand Down Expand Up @@ -436,7 +436,7 @@ impl<'input> CssParser<'input> {

fn parse_selector_condition(&mut self) -> Rc<CssSelectorCondition> {
let Some(c) = self.parse_opt_selector_condition() else {
self.add_syntax_error(&self.token.1, DiagnosticKind::Unexpected, diagarg![Token(self.token.0.clone())]);
self.add_syntax_error(&self.token.1, DiagnosticKind::Unexpected, diagarg![self.token.0.clone()]);
return self.create_invalidated_selector_condition(&self.tokenizer.cursor_location());
};
c
Expand Down Expand Up @@ -537,7 +537,7 @@ impl<'input> CssParser<'input> {

fn parse_property_value(&mut self, min_precedence: CssOperatorPrecedence) -> Rc<CssPropertyValue> {
let Some(v) = self.parse_opt_property_value(min_precedence) else {
self.add_syntax_error(&self.token.1, DiagnosticKind::Unexpected, diagarg![Token(self.token.0.clone())]);
self.add_syntax_error(&self.token.1, DiagnosticKind::Unexpected, diagarg![self.token.0.clone()]);
return self.create_invalidated_property_value(&self.tokenizer.cursor_location());
};
v
Expand Down
8 changes: 4 additions & 4 deletions crates/parser/parser/css_tokenizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl<'input> CssTokenizer<'input> {
Location::with_offset(&self.compilation_unit, offset)
}

fn add_syntax_error(&self, location: &Location, kind: DiagnosticKind, arguments: Vec<DiagnosticArgument>) {
fn add_syntax_error(&self, location: &Location, kind: DiagnosticKind, arguments: Vec<Rc<dyn DiagnosticArgument>>) {
if self.compilation_unit().prevent_equal_offset_error(location) {
return;
}
Expand All @@ -59,7 +59,7 @@ impl<'input> CssTokenizer<'input> {

fn add_unexpected_error(&self) {
if self.characters.has_remaining() {
self.add_syntax_error(&self.character_ahead_location(), DiagnosticKind::UnexpectedCharacter, diagarg![String(self.characters.peek_or_zero().to_string())])
self.add_syntax_error(&self.character_ahead_location(), DiagnosticKind::UnexpectedCharacter, diagarg![self.characters.peek_or_zero().to_string()])
} else {
self.add_syntax_error(&self.cursor_location(), DiagnosticKind::UnexpectedEnd, vec![])
}
Expand Down Expand Up @@ -388,7 +388,7 @@ impl<'input> CssTokenizer<'input> {
if let Some(mv) = mv {
builder.push(mv);
} else {
self.add_syntax_error(&loc, DiagnosticKind::CssInvalidHexEscape, diagarg![String(digits)]);
self.add_syntax_error(&loc, DiagnosticKind::CssInvalidHexEscape, diagarg![digits]);
}
}
} else if self.characters.reached_end() {
Expand Down Expand Up @@ -422,7 +422,7 @@ impl<'input> CssTokenizer<'input> {
self.characters.next();
nesting += 1;
} else if self.characters.reached_end() {
self.add_syntax_error(&self.cursor_location(), DiagnosticKind::Expecting, diagarg![Token(Token::ParenClose), Token(Token::Eof)]);
self.add_syntax_error(&self.cursor_location(), DiagnosticKind::Expecting, diagarg![Token::ParenClose, Token::Eof]);
token = (Token::Eof, self.cursor_location());
break;
} else {
Expand Down
Loading

0 comments on commit eb73c52

Please sign in to comment.