mirror of
https://github.com/slint-ui/slint.git
synced 2025-10-02 22:54:36 +00:00
Parse property declarations
For now this uses the syntax property<qualified type name> name;
This commit is contained in:
parent
5accea4cdd
commit
32b99547f1
5 changed files with 54 additions and 0 deletions
|
@ -34,6 +34,8 @@ fn fill_token_vec(stream: TokenStream, vec: &mut Vec<parser::Token>) {
|
||||||
';' => SyntaxKind::Semicolon,
|
';' => SyntaxKind::Semicolon,
|
||||||
'!' => SyntaxKind::Bang,
|
'!' => SyntaxKind::Bang,
|
||||||
'.' => SyntaxKind::Dot,
|
'.' => SyntaxKind::Dot,
|
||||||
|
'<' => SyntaxKind::LAngle,
|
||||||
|
'>' => SyntaxKind::RAngle,
|
||||||
_ => SyntaxKind::Error,
|
_ => SyntaxKind::Error,
|
||||||
};
|
};
|
||||||
vec.push(parser::Token {
|
vec.push(parser::Token {
|
||||||
|
|
|
@ -74,6 +74,7 @@ pub struct Element {
|
||||||
|
|
||||||
/// This should probably be in the Component instead
|
/// This should probably be in the Component instead
|
||||||
pub signals_declaration: Vec<String>,
|
pub signals_declaration: Vec<String>,
|
||||||
|
pub property_declarations: Vec<PropertyDeclaration>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Element {
|
impl Element {
|
||||||
|
@ -169,6 +170,10 @@ impl Element {
|
||||||
r.signals_declaration.push(name);
|
r.signals_declaration.push(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for prop_decl in node.children().filter(|n| n.kind() == SyntaxKind::PropertyDeclaration) {
|
||||||
|
r.property_declarations.push(PropertyDeclaration::from_node(prop_decl));
|
||||||
|
}
|
||||||
|
|
||||||
for se in node.children() {
|
for se in node.children() {
|
||||||
if se.kind() == SyntaxKind::SubElement {
|
if se.kind() == SyntaxKind::SubElement {
|
||||||
let id = se.child_text(SyntaxKind::Identifier).unwrap_or_default();
|
let id = se.child_text(SyntaxKind::Identifier).unwrap_or_default();
|
||||||
|
@ -185,6 +190,27 @@ impl Element {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Default, Debug, Clone)]
|
||||||
|
pub struct PropertyDeclaration {
|
||||||
|
property_type: QualifiedTypeName,
|
||||||
|
name: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PropertyDeclaration {
|
||||||
|
pub fn from_node(node: SyntaxNode) -> Self {
|
||||||
|
debug_assert_eq!(node.kind(), SyntaxKind::PropertyDeclaration);
|
||||||
|
Self {
|
||||||
|
property_type: QualifiedTypeName::from_node(
|
||||||
|
node.children()
|
||||||
|
.filter(|n| n.kind() == SyntaxKind::QualifiedTypeName)
|
||||||
|
.nth(0)
|
||||||
|
.unwrap(),
|
||||||
|
),
|
||||||
|
name: node.child_text(SyntaxKind::Identifier).unwrap(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Default, Debug, Clone)]
|
#[derive(Default, Debug, Clone)]
|
||||||
pub struct QualifiedTypeName {
|
pub struct QualifiedTypeName {
|
||||||
members: Vec<String>,
|
members: Vec<String>,
|
||||||
|
|
|
@ -45,6 +45,7 @@ macro_rules! declare_token_kind {
|
||||||
RepeatedElement,
|
RepeatedElement,
|
||||||
SignalDeclaration,
|
SignalDeclaration,
|
||||||
SignalConnection,
|
SignalConnection,
|
||||||
|
PropertyDeclaration,
|
||||||
Binding,
|
Binding,
|
||||||
CodeStatement,
|
CodeStatement,
|
||||||
CodeBlock,
|
CodeBlock,
|
||||||
|
@ -74,6 +75,8 @@ declare_token_kind! {
|
||||||
RBrace -> r"\}",
|
RBrace -> r"\}",
|
||||||
LParent -> r"\(",
|
LParent -> r"\(",
|
||||||
RParent -> r"\)",
|
RParent -> r"\)",
|
||||||
|
LAngle -> r"<",
|
||||||
|
RAngle -> r">",
|
||||||
ColonEqual -> ":=",
|
ColonEqual -> ":=",
|
||||||
FatArrow -> r"=>",
|
FatArrow -> r"=>",
|
||||||
Equal -> r"=",
|
Equal -> r"=",
|
||||||
|
|
|
@ -66,6 +66,7 @@ pub fn parse_element(p: &mut impl Parser) -> bool {
|
||||||
/// for xx in model: Sub {}
|
/// for xx in model: Sub {}
|
||||||
/// clicked => {}
|
/// clicked => {}
|
||||||
/// signal foobar;
|
/// signal foobar;
|
||||||
|
/// property<int> width;
|
||||||
/// ```
|
/// ```
|
||||||
fn parse_element_content(p: &mut impl Parser) {
|
fn parse_element_content(p: &mut impl Parser) {
|
||||||
loop {
|
loop {
|
||||||
|
@ -82,6 +83,9 @@ fn parse_element_content(p: &mut impl Parser) {
|
||||||
SyntaxKind::Identifier if p.peek().as_str() == "signal" => {
|
SyntaxKind::Identifier if p.peek().as_str() == "signal" => {
|
||||||
parse_signal_declaration(&mut *p);
|
parse_signal_declaration(&mut *p);
|
||||||
}
|
}
|
||||||
|
SyntaxKind::LAngle if p.peek().as_str() == "property" => {
|
||||||
|
parse_property_declaration(&mut *p);
|
||||||
|
}
|
||||||
_ => {
|
_ => {
|
||||||
p.consume();
|
p.consume();
|
||||||
p.error("FIXME");
|
p.error("FIXME");
|
||||||
|
@ -219,3 +223,18 @@ fn parse_signal_declaration(p: &mut impl Parser) {
|
||||||
p.expect(SyntaxKind::Identifier);
|
p.expect(SyntaxKind::Identifier);
|
||||||
p.expect(SyntaxKind::Semicolon);
|
p.expect(SyntaxKind::Semicolon);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg_attr(test, parser_test)]
|
||||||
|
/// ```test
|
||||||
|
/// property<int> foobar;
|
||||||
|
/// ```
|
||||||
|
fn parse_property_declaration(p: &mut impl Parser) {
|
||||||
|
debug_assert_eq!(p.peek().as_str(), "property");
|
||||||
|
let mut p = p.start_node(SyntaxKind::PropertyDeclaration);
|
||||||
|
p.consume(); // property
|
||||||
|
p.expect(SyntaxKind::LAngle);
|
||||||
|
parse_qualified_type_name(&mut *p);
|
||||||
|
p.expect(SyntaxKind::RAngle);
|
||||||
|
p.expect(SyntaxKind::Identifier);
|
||||||
|
p.expect(SyntaxKind::Semicolon);
|
||||||
|
}
|
||||||
|
|
4
sixtyfps_compiler/tests/basic/property_declaration.60
Normal file
4
sixtyfps_compiler/tests/basic/property_declaration.60
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
|
||||||
|
Test := Rectangle {
|
||||||
|
property<int> foo;
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue