Skip to content

Commit

Permalink
Qualified ASDoc instance reference
Browse files Browse the repository at this point in the history
  • Loading branch information
hydroper committed Apr 18, 2024
1 parent e17117c commit 6dc46e8
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 12 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.0"
version = "0.5.1"
edition = "2021"
authors = ["hydroper <[email protected]>"]
repository = "https://github.com/hydroper/as3parser"
Expand Down
30 changes: 29 additions & 1 deletion crates/parser/parser/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4603,7 +4603,7 @@ impl<'input> Parser<'input> {
}
let mut base: Option<Rc<Expression>> = None;
let base_text: String = split[0].to_owned();
let instance_property: Option<(String, Location)> = split.get(1).and_then(|&f| if f.is_empty() { None } else {
let instance_property_text: Option<(String, Location)> = split.get(1).and_then(|&f| if f.is_empty() { None } else {
Some((f.to_owned(), Location::with_offsets(self.compilation_unit(), reference_loc.first_offset() + base_text.len() + 1, reference_loc.last_offset())))
});

Expand All @@ -4620,6 +4620,20 @@ impl<'input> Parser<'input> {
}
}

let mut instance_property: Option<Rc<QualifiedIdentifier>> = None;
if let Some(text) = instance_property_text {
let parser_options = ParserOptions {
byte_range: Some((text.1.first_offset(), text.1.last_offset())),
..default()
};
if let Some(exp) = ParserFacade(parser_options).parse_qualified_identifier(self.compilation_unit()) {
instance_property = Some(Rc::new(exp));
} else {
self.add_syntax_error(&tag_location, DiagnosticKind::FailedParsingAsDocTag, diagnostic_arguments![String(tag_name.to_owned())]);
return None;
}
}

if base.is_none() && instance_property.is_none() {
self.add_syntax_error(&tag_location, DiagnosticKind::FailedParsingAsDocTag, diagnostic_arguments![String(tag_name.to_owned())]);
return None;
Expand Down Expand Up @@ -5118,6 +5132,20 @@ impl ParserFacade {
}
}

/// Parses a qualified identifier and expects end-of-file.
pub fn parse_qualified_identifier(&self, compilation_unit: &Rc<CompilationUnit>) -> Option<QualifiedIdentifier> {
let mut parser = self.create_parser(compilation_unit);
if parser.next().is_ok() {
let exp = parser.parse_qualified_identifier().ok();
if exp.is_some() {
let _ = parser.expect_eof();
}
exp
} else {
None
}
}

/// Parses `TypeExpression` and expects end-of-file.
pub fn parse_type_expression(&self, compilation_unit: &Rc<CompilationUnit>) -> Option<Rc<Expression>> {
let mut parser = self.create_parser(compilation_unit);
Expand Down
2 changes: 1 addition & 1 deletion crates/parser/tree/asdoc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,5 @@ pub struct AsDocReference {
/// Base expression.
pub base: Option<Rc<Expression>>,
/// Instance property fragment following the hash character.
pub instance_property: Option<(String, Location)>,
pub instance_property: Option<Rc<QualifiedIdentifier>>,
}
Binary file modified demo/dist/as3_parser_demo_bg.wasm
Binary file not shown.
30 changes: 22 additions & 8 deletions tests/parser/ASDoc.ast.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,17 @@
}
}
},
"instance_property": [
"x",
"3:13-3:14"
]
"instance_property": {
"location": "3:13-3:14",
"attribute": false,
"qualifier": null,
"id": {
"Id": [
"x",
"3:13-3:14"
]
}
}
}
},
"3:4-3:14"
Expand All @@ -57,10 +64,17 @@
}
}
},
"instance_property": [
"x",
"5:12-5:13"
]
"instance_property": {
"location": "5:12-5:13",
"attribute": false,
"qualifier": null,
"id": {
"Id": [
"x",
"5:12-5:13"
]
}
}
},
"display_text": "X description"
}
Expand Down

0 comments on commit 6dc46e8

Please sign in to comment.