Skip to content

Commit eb73c52

Browse files
author
hydroper
committed
Diagnostic refactor
1 parent 28d2618 commit eb73c52

8 files changed

Lines changed: 117 additions & 107 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/parser/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "as3_parser"
3-
version = "0.5.27"
3+
version = "0.5.28"
44
edition = "2021"
55
authors = ["hydroper <matheusdiasdesouzads@gmail.com>"]
66
repository = "https://github.com/hydroper/as3parser"

crates/parser/diagnostics/diagnostics.rs

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub struct Diagnostic {
1515
pub(crate) kind: DiagnosticKind,
1616
pub(crate) is_warning: bool,
1717
pub(crate) is_verify_error: bool,
18-
pub(crate) arguments: Vec<DiagnosticArgument>,
18+
pub(crate) arguments: Vec<Rc<dyn DiagnosticArgument>>,
1919
pub(crate) custom_kind: RefCell<Option<Rc<dyn Any>>>,
2020
}
2121

@@ -41,7 +41,7 @@ impl PartialOrd for Diagnostic {
4141
}
4242

4343
impl Diagnostic {
44-
pub fn new_syntax_error(location: &Location, kind: DiagnosticKind, arguments: Vec<DiagnosticArgument>) -> Self {
44+
pub fn new_syntax_error(location: &Location, kind: DiagnosticKind, arguments: Vec<Rc<dyn DiagnosticArgument>>) -> Self {
4545
Self {
4646
location: location.clone(),
4747
kind,
@@ -52,7 +52,7 @@ impl Diagnostic {
5252
}
5353
}
5454

55-
pub fn new_verify_error(location: &Location, kind: DiagnosticKind, arguments: Vec<DiagnosticArgument>) -> Self {
55+
pub fn new_verify_error(location: &Location, kind: DiagnosticKind, arguments: Vec<Rc<dyn DiagnosticArgument>>) -> Self {
5656
Self {
5757
location: location.clone(),
5858
kind,
@@ -63,7 +63,7 @@ impl Diagnostic {
6363
}
6464
}
6565

66-
pub fn new_warning(location: &Location, kind: DiagnosticKind, arguments: Vec<DiagnosticArgument>) -> Self {
66+
pub fn new_warning(location: &Location, kind: DiagnosticKind, arguments: Vec<Rc<dyn DiagnosticArgument>>) -> Self {
6767
Self {
6868
location: location.clone(),
6969
kind,
@@ -98,7 +98,7 @@ impl Diagnostic {
9898
self.is_verify_error
9999
}
100100

101-
pub fn arguments(&self) -> Vec<DiagnosticArgument> {
101+
pub fn arguments(&self) -> Vec<Rc<dyn DiagnosticArgument>> {
102102
self.arguments.clone()
103103
}
104104

@@ -147,7 +147,7 @@ impl Diagnostic {
147147
let mut string_arguments: HashMap<String, String> = hashmap!{};
148148
let mut i = 1;
149149
for argument in &self.arguments {
150-
string_arguments.insert(i.to_string(), self.format_argument(argument.clone()));
150+
string_arguments.insert(i.to_string(), argument.to_string());
151151
i += 1;
152152
}
153153
use late_format::LateFormat;
@@ -157,31 +157,19 @@ impl Diagnostic {
157157
};
158158
msg.late_format(string_arguments)
159159
}
160-
161-
fn format_argument(&self, argument: DiagnosticArgument) -> String {
162-
match argument {
163-
DiagnosticArgument::String(s) => s.clone(),
164-
DiagnosticArgument::Token(t) => t.to_string(),
165-
DiagnosticArgument::Dynamic(d) => d.to_string(),
166-
}
167-
}
168160
}
169161

170162
/// The `diagarg![...]` literal is used for initializing
171163
/// diagnostic arguments.
172164
///
173-
/// For example: `diagarg![Token(t1), String("foo".into())]`.
165+
/// For example: `diagarg![token, "foo".into()]`.
174166
pub macro diagarg {
175-
($($variant:ident($value:expr)),*) => { vec![ $(DiagnosticArgument::$variant($value)),* ] },
167+
($($value:expr),*) => { vec![ $(Rc::new($value)),* ] },
176168
}
177169

178-
#[derive(Clone)]
179-
pub enum DiagnosticArgument {
180-
String(String),
181-
Token(Token),
182-
Dynamic(Rc<dyn DynamicDiagnosticArgument>),
170+
pub trait DiagnosticArgument: Any + ToString + 'static {
183171
}
184172

185-
pub trait DynamicDiagnosticArgument {
186-
fn to_string(&self) -> String;
187-
}
173+
impl DiagnosticArgument for String {}
174+
175+
impl DiagnosticArgument for Token {}

crates/parser/parser/css_parser.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,14 @@ impl<'input> CssParser<'input> {
4545
self.locations.pop().unwrap().combine_with(self.previous_token.1.clone())
4646
}
4747

48-
fn add_syntax_error(&self, location: &Location, kind: DiagnosticKind, arguments: Vec<DiagnosticArgument>) {
48+
fn add_syntax_error(&self, location: &Location, kind: DiagnosticKind, arguments: Vec<Rc<dyn DiagnosticArgument>>) {
4949
if self.compilation_unit().prevent_equal_offset_error(location) {
5050
return;
5151
}
5252
self.compilation_unit().add_diagnostic(Diagnostic::new_syntax_error(location, kind, arguments));
5353
}
5454

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

6565
/*
66-
fn add_warning(&self, location: &Location, kind: DiagnosticKind, arguments: Vec<DiagnosticArgument>) {
66+
fn add_warning(&self, location: &Location, kind: DiagnosticKind, arguments: Vec<Rc<dyn DiagnosticArgument>>) {
6767
if self.compilation_unit().prevent_equal_offset_warning(location) {
6868
return;
6969
}
@@ -126,7 +126,7 @@ impl<'input> CssParser<'input> {
126126
fn expect(&mut self, token: Token) {
127127
if self.token.0 != token {
128128
self.expecting_token_error = true;
129-
self.add_syntax_error(&self.token_location(), DiagnosticKind::Expecting, diagarg![Token(token.clone()), Token(self.token.0.clone())]);
129+
self.add_syntax_error(&self.token_location(), DiagnosticKind::Expecting, diagarg![token.clone(), self.token.0.clone()]);
130130
} else {
131131
self.expecting_token_error = false;
132132
self.next();
@@ -141,7 +141,7 @@ impl<'input> CssParser<'input> {
141141
(id, location)
142142
} else {
143143
self.expecting_token_error = true;
144-
self.add_syntax_error(&self.token_location(), DiagnosticKind::ExpectingIdentifier, diagarg![Token(self.token.0.clone())]);
144+
self.add_syntax_error(&self.token_location(), DiagnosticKind::ExpectingIdentifier, diagarg![self.token.0.clone()]);
145145
(INVALIDATED_IDENTIFIER.to_owned(), self.tokenizer.cursor_location())
146146
}
147147
}
@@ -153,7 +153,7 @@ impl<'input> CssParser<'input> {
153153
Some(value)
154154
} else {
155155
self.expecting_token_error = true;
156-
self.add_syntax_error(&self.token_location(), DiagnosticKind::Unexpected, diagarg![Token(self.token.0.clone())]);
156+
self.add_syntax_error(&self.token_location(), DiagnosticKind::Unexpected, diagarg![self.token.0.clone()]);
157157
None
158158
}
159159
}
@@ -166,7 +166,7 @@ impl<'input> CssParser<'input> {
166166
(v, location)
167167
} else {
168168
self.expecting_token_error = true;
169-
self.add_syntax_error(&self.token_location(), DiagnosticKind::ExpectingStringLiteral, diagarg![Token(self.token.0.clone())]);
169+
self.add_syntax_error(&self.token_location(), DiagnosticKind::ExpectingStringLiteral, diagarg![self.token.0.clone()]);
170170
("".into(), self.tokenizer.cursor_location())
171171
}
172172
}
@@ -248,7 +248,7 @@ impl<'input> CssParser<'input> {
248248
} else if self.peek(Token::CssAtFontFace) {
249249
self.parse_font_face()
250250
} else {
251-
self.add_syntax_error(&self.token.1, DiagnosticKind::ExpectingDirective, diagarg![Token(self.token.0.clone())]);
251+
self.add_syntax_error(&self.token.1, DiagnosticKind::ExpectingDirective, diagarg![self.token.0.clone()]);
252252
let d = self.create_invalidated_directive(&self.tokenizer.cursor_location());
253253
self.next();
254254
d
@@ -263,15 +263,15 @@ impl<'input> CssParser<'input> {
263263
if let Some(condition) = condition {
264264
conditions.push(condition);
265265
} else {
266-
self.add_syntax_error(&self.token.1, DiagnosticKind::Unexpected, diagarg![Token(self.token.0.clone())]);
266+
self.add_syntax_error(&self.token.1, DiagnosticKind::Unexpected, diagarg![self.token.0.clone()]);
267267
}
268268
loop {
269269
if let Some(condition) = self.parse_opt_media_query_condition() {
270270
conditions.push(condition);
271271
} else if self.eof() || self.peek(Token::BlockOpen) {
272272
break;
273273
} else if !self.consume(Token::Comma) {
274-
self.add_syntax_error(&self.token.1, DiagnosticKind::Unexpected, diagarg![Token(self.token.0.clone())]);
274+
self.add_syntax_error(&self.token.1, DiagnosticKind::Unexpected, diagarg![self.token.0.clone()]);
275275
self.next();
276276
}
277277
}
@@ -281,7 +281,7 @@ impl<'input> CssParser<'input> {
281281
if let Some(rule) = self.parse_opt_rule() {
282282
rules.push(Rc::new(rule));
283283
} else {
284-
self.add_syntax_error(&self.token.1, DiagnosticKind::Unexpected, diagarg![Token(self.token.0.clone())]);
284+
self.add_syntax_error(&self.token.1, DiagnosticKind::Unexpected, diagarg![self.token.0.clone()]);
285285
self.next();
286286
}
287287
}
@@ -342,7 +342,7 @@ impl<'input> CssParser<'input> {
342342
right,
343343
});
344344
} else {
345-
self.add_syntax_error(&self.token.1, DiagnosticKind::Unexpected, diagarg![Token(self.token.0.clone())]);
345+
self.add_syntax_error(&self.token.1, DiagnosticKind::Unexpected, diagarg![self.token.0.clone()]);
346346
base = Rc::new(CssMediaQueryCondition::And {
347347
location: self.pop_location(),
348348
left: base,
@@ -357,7 +357,7 @@ impl<'input> CssParser<'input> {
357357

358358
fn parse_arguments(&mut self) -> Result<CssParserFacade, ParserError> {
359359
if !self.peek(Token::ParenOpen) {
360-
self.add_syntax_error(&self.token.1, DiagnosticKind::Expecting, diagarg![Token(Token::ParenOpen), Token(self.token.0.clone())]);
360+
self.add_syntax_error(&self.token.1, DiagnosticKind::Expecting, diagarg![Token::ParenOpen, self.token.0.clone()]);
361361
return Err(ParserError::Common);
362362
}
363363
let (byte_range, token) = self.tokenizer.scan_arguments();
@@ -376,7 +376,7 @@ impl<'input> CssParser<'input> {
376376
if let Some(s) = self.parse_opt_selector() {
377377
selectors.push(s);
378378
} else {
379-
self.add_syntax_error(&self.token.1, DiagnosticKind::Unexpected, diagarg![Token(self.token.0.clone())]);
379+
self.add_syntax_error(&self.token.1, DiagnosticKind::Unexpected, diagarg![self.token.0.clone()]);
380380
}
381381
}
382382
let mut properties: Vec<Rc<CssProperty>> = vec![];
@@ -436,7 +436,7 @@ impl<'input> CssParser<'input> {
436436

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

538538
fn parse_property_value(&mut self, min_precedence: CssOperatorPrecedence) -> Rc<CssPropertyValue> {
539539
let Some(v) = self.parse_opt_property_value(min_precedence) else {
540-
self.add_syntax_error(&self.token.1, DiagnosticKind::Unexpected, diagarg![Token(self.token.0.clone())]);
540+
self.add_syntax_error(&self.token.1, DiagnosticKind::Unexpected, diagarg![self.token.0.clone()]);
541541
return self.create_invalidated_property_value(&self.tokenizer.cursor_location());
542542
};
543543
v

crates/parser/parser/css_tokenizer.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ impl<'input> CssTokenizer<'input> {
5050
Location::with_offset(&self.compilation_unit, offset)
5151
}
5252

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

6060
fn add_unexpected_error(&self) {
6161
if self.characters.has_remaining() {
62-
self.add_syntax_error(&self.character_ahead_location(), DiagnosticKind::UnexpectedCharacter, diagarg![String(self.characters.peek_or_zero().to_string())])
62+
self.add_syntax_error(&self.character_ahead_location(), DiagnosticKind::UnexpectedCharacter, diagarg![self.characters.peek_or_zero().to_string()])
6363
} else {
6464
self.add_syntax_error(&self.cursor_location(), DiagnosticKind::UnexpectedEnd, vec![])
6565
}
@@ -388,7 +388,7 @@ impl<'input> CssTokenizer<'input> {
388388
if let Some(mv) = mv {
389389
builder.push(mv);
390390
} else {
391-
self.add_syntax_error(&loc, DiagnosticKind::CssInvalidHexEscape, diagarg![String(digits)]);
391+
self.add_syntax_error(&loc, DiagnosticKind::CssInvalidHexEscape, diagarg![digits]);
392392
}
393393
}
394394
} else if self.characters.reached_end() {
@@ -422,7 +422,7 @@ impl<'input> CssTokenizer<'input> {
422422
self.characters.next();
423423
nesting += 1;
424424
} else if self.characters.reached_end() {
425-
self.add_syntax_error(&self.cursor_location(), DiagnosticKind::Expecting, diagarg![Token(Token::ParenClose), Token(Token::Eof)]);
425+
self.add_syntax_error(&self.cursor_location(), DiagnosticKind::Expecting, diagarg![Token::ParenClose, Token::Eof]);
426426
token = (Token::Eof, self.cursor_location());
427427
break;
428428
} else {

0 commit comments

Comments
 (0)