mirror of
https://github.com/slint-ui/slint.git
synced 2025-08-04 18:58:36 +00:00
syntax_updater: Continue new syntax, add it to the parser
This re-adds inheritence with the "inherits" keyword
This commit is contained in:
parent
9d0c6bfc84
commit
a3ba7bf78a
4 changed files with 29 additions and 111 deletions
|
@ -57,17 +57,32 @@ pub fn parse_document(p: &mut impl Parser) -> bool {
|
|||
/// ```
|
||||
pub fn parse_component(p: &mut impl Parser) -> bool {
|
||||
let mut p = p.start_node(SyntaxKind::Component);
|
||||
let is_global = p.peek().as_str() == "global";
|
||||
if is_global {
|
||||
let simple_component = p.nth(1).kind() == SyntaxKind::ColonEqual;
|
||||
let is_global = !simple_component && p.peek().as_str() == "global";
|
||||
let is_new_component = !simple_component && p.peek().as_str() == "component";
|
||||
if is_global || is_new_component {
|
||||
p.consume();
|
||||
}
|
||||
if !p.start_node(SyntaxKind::DeclaredIdentifier).expect(SyntaxKind::Identifier) {
|
||||
drop(p.start_node(SyntaxKind::Element));
|
||||
return false;
|
||||
}
|
||||
if !p.expect(SyntaxKind::ColonEqual) {
|
||||
drop(p.start_node(SyntaxKind::Element));
|
||||
return false;
|
||||
if is_global {
|
||||
// ignore the `:=` (compatibility)
|
||||
p.test(SyntaxKind::ColonEqual);
|
||||
} else if !is_new_component {
|
||||
if !p.expect(SyntaxKind::ColonEqual) {
|
||||
drop(p.start_node(SyntaxKind::Element));
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
if p.peek().as_str() != "inherits" {
|
||||
p.error("Expected keyword 'inherits'");
|
||||
drop(p.start_node(SyntaxKind::Element));
|
||||
return false;
|
||||
} else {
|
||||
p.consume();
|
||||
}
|
||||
}
|
||||
|
||||
if is_global && p.peek().kind() == SyntaxKind::LBrace {
|
||||
|
|
|
@ -200,8 +200,6 @@ fn move_properties_to_root(
|
|||
return Ok(false);
|
||||
}
|
||||
let mut seen_brace = false;
|
||||
let mut generated_sub_element = false;
|
||||
let element = node.clone().into();
|
||||
for c in node.children_with_tokens() {
|
||||
let k = c.kind();
|
||||
if k == SyntaxKind::LBrace {
|
||||
|
@ -235,23 +233,10 @@ fn move_properties_to_root(
|
|||
seen_brace = false;
|
||||
}
|
||||
|
||||
if args.new_component_declaration {
|
||||
if super::new_component_declaration::process_component_node(
|
||||
&element,
|
||||
&c,
|
||||
file,
|
||||
&mut generated_sub_element,
|
||||
state,
|
||||
args,
|
||||
)? {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if k == SyntaxKind::RBrace {
|
||||
file.write_all(
|
||||
&**std::mem::take(&mut state.lookup_change.extra_component_stuff).borrow(),
|
||||
)?;
|
||||
file.write_all(&*std::mem::take(
|
||||
&mut *state.lookup_change.extra_component_stuff.borrow_mut(),
|
||||
))?;
|
||||
}
|
||||
crate::visit_node_or_token(c, file, state, args)?;
|
||||
}
|
||||
|
|
|
@ -2,11 +2,7 @@
|
|||
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-commercial
|
||||
|
||||
use crate::Cli;
|
||||
use i_slint_compiler::{
|
||||
langtype::Type,
|
||||
object_tree::ElementRc,
|
||||
parser::{syntax_nodes, NodeOrToken, SyntaxKind, SyntaxNode},
|
||||
};
|
||||
use i_slint_compiler::parser::{SyntaxKind, SyntaxNode};
|
||||
use std::io::Write;
|
||||
|
||||
pub(crate) fn fold_node(
|
||||
|
@ -17,7 +13,7 @@ pub(crate) fn fold_node(
|
|||
) -> std::io::Result<bool> {
|
||||
debug_assert!(args.new_component_declaration);
|
||||
let kind = node.kind();
|
||||
if kind == SyntaxKind::Component {
|
||||
if kind == SyntaxKind::Component && node.child_token(SyntaxKind::ColonEqual).is_some() {
|
||||
let is_global =
|
||||
node.child_token(SyntaxKind::Identifier).map_or(false, |t| t.text() == "global");
|
||||
if !is_global {
|
||||
|
@ -25,7 +21,9 @@ pub(crate) fn fold_node(
|
|||
}
|
||||
for n in node.children_with_tokens() {
|
||||
if n.kind() == SyntaxKind::ColonEqual {
|
||||
// ignore that
|
||||
if !is_global {
|
||||
write!(file, " inherits ")?;
|
||||
}
|
||||
} else {
|
||||
crate::visit_node_or_token(n, file, state, args)?;
|
||||
}
|
||||
|
@ -34,83 +32,3 @@ pub(crate) fn fold_node(
|
|||
}
|
||||
Ok(false)
|
||||
}
|
||||
|
||||
/// Called for each node directly under SyntaxNode::Element in a component
|
||||
///
|
||||
/// returns true if we should not print this
|
||||
pub(crate) fn process_component_node(
|
||||
element: &syntax_nodes::Element,
|
||||
node: &NodeOrToken,
|
||||
file: &mut impl Write,
|
||||
generated_sub_element: &mut bool,
|
||||
state: &mut crate::State,
|
||||
args: &Cli,
|
||||
) -> std::io::Result<bool> {
|
||||
let k = node.kind();
|
||||
if node.kind() == SyntaxKind::QualifiedName {
|
||||
return Ok(true);
|
||||
}
|
||||
|
||||
if !*generated_sub_element
|
||||
&& matches!(
|
||||
k,
|
||||
SyntaxKind::SubElement | SyntaxKind::ChildrenPlaceholder | SyntaxKind::RBrace
|
||||
)
|
||||
{
|
||||
*generated_sub_element = true;
|
||||
|
||||
if let Some(e) = &state.current_elem {
|
||||
add_base_two_way_bindings(e.clone(), args, file)?;
|
||||
}
|
||||
|
||||
if let Some(base) = element.QualifiedName() {
|
||||
write!(file, "__old_root := \n ")?;
|
||||
crate::visit_node(base.into(), file, state, args)?;
|
||||
write!(file, "{{ \n ")?;
|
||||
}
|
||||
}
|
||||
|
||||
if k == SyntaxKind::RBrace {
|
||||
write!(file, "}}")?;
|
||||
}
|
||||
|
||||
Ok(false)
|
||||
}
|
||||
|
||||
fn add_base_two_way_bindings(
|
||||
mut e: ElementRc,
|
||||
args: &Cli,
|
||||
file: &mut impl Write,
|
||||
) -> std::io::Result<()> {
|
||||
loop {
|
||||
let base_type = e.borrow().base_type.clone();
|
||||
match base_type {
|
||||
Type::Component(c) => {
|
||||
e = c.root_element.clone();
|
||||
for p in &e.borrow().property_declarations {
|
||||
if args.input_output_properties {
|
||||
write!(file, "inout ")?;
|
||||
}
|
||||
write!(file, "property {0} <=> __old_root.{0};\n ", p.0)?;
|
||||
}
|
||||
}
|
||||
Type::Builtin(b) => {
|
||||
for p in &b.properties {
|
||||
if matches!(p.0.as_str(), "x" | "y") {
|
||||
continue;
|
||||
}
|
||||
if matches!(p.0.as_str(), "height" | "width") {
|
||||
write!(file, "{0} <=> __old_root.{0};\n ", p.0)?;
|
||||
continue;
|
||||
}
|
||||
if args.input_output_properties {
|
||||
write!(file, "inout ")?;
|
||||
}
|
||||
write!(file, "property {0} <=> __old_root.{0};\n ", p.0)?;
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
_ => return Ok(()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -313,7 +313,7 @@ fn fold_node(
|
|||
{
|
||||
return Ok(true);
|
||||
}
|
||||
if (args.fully_qualify || args.move_declaration || args.new_component_declaration)
|
||||
if (args.fully_qualify || args.move_declaration)
|
||||
&& experiments::lookup_changes::fold_node(node, file, state, args)?
|
||||
{
|
||||
return Ok(true);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue