@@ -16,7 +16,7 @@ use crate::{
16
16
literals:: { AstLiteral , StringValue } ,
17
17
pre_processor,
18
18
provider:: IdProvider ,
19
- visitor :: { AstVisitor , Walker } ,
19
+ ser :: AstSerializer ,
20
20
} ;
21
21
22
22
use plc_source:: source_location:: * ;
@@ -1478,240 +1478,7 @@ impl AstNode {
1478
1478
}
1479
1479
1480
1480
pub fn as_string ( & self ) -> String {
1481
- let mut formatter = StringFormatter { result : String :: new ( ) } ;
1482
- formatter. visit ( self ) ;
1483
- formatter. result
1484
- }
1485
-
1486
- pub fn as_string_mut ( & mut self ) -> String {
1487
- let mut formatter = StringFormatter { result : String :: new ( ) } ;
1488
- formatter. visit ( self ) ;
1489
- formatter. result
1490
- }
1491
- }
1492
-
1493
- // TODO: Move into own file, rename to AstSerializer and implement some tests
1494
- struct StringFormatter {
1495
- result : String ,
1496
- }
1497
-
1498
- impl AstVisitor for StringFormatter {
1499
- fn visit ( & mut self , node : & AstNode ) {
1500
- node. walk ( self )
1501
- }
1502
-
1503
- fn visit_compilation_unit ( & mut self , _: & CompilationUnit ) {
1504
- unimplemented ! ( "for now only interested in individual nodes located in a POU body" )
1505
- }
1506
-
1507
- fn visit_implementation ( & mut self , _: & Implementation ) {
1508
- unimplemented ! ( "for now only interested in individual nodes located in a POU body" )
1509
- }
1510
-
1511
- fn visit_variable_block ( & mut self , _: & VariableBlock ) {
1512
- unimplemented ! ( "for now only interested in individual nodes located in a POU body" )
1513
- }
1514
-
1515
- fn visit_variable ( & mut self , _: & Variable ) {
1516
- unimplemented ! ( "for now only interested in individual nodes located in a POU body" )
1517
- }
1518
-
1519
- fn visit_config_variable ( & mut self , _: & ConfigVariable ) {
1520
- unimplemented ! ( "for now only interested in individual nodes located in a POU body" )
1521
- }
1522
-
1523
- fn visit_interface ( & mut self , _: & Interface ) {
1524
- unimplemented ! ( "for now only interested in individual nodes located in a POU body" )
1525
- }
1526
-
1527
- fn visit_property ( & mut self , _: & PropertyBlock ) {
1528
- unimplemented ! ( "for now only interested in individual nodes located in a POU body" )
1529
- }
1530
-
1531
- fn visit_enum_element ( & mut self , element : & AstNode ) {
1532
- element. walk ( self ) ;
1533
- }
1534
-
1535
- fn visit_data_type_declaration ( & mut self , _: & DataTypeDeclaration ) {
1536
- unimplemented ! ( "for now only interested in individual nodes located in a POU body" )
1537
- }
1538
-
1539
- fn visit_user_type_declaration ( & mut self , _: & UserTypeDeclaration ) {
1540
- unimplemented ! ( "for now only interested in individual nodes located in a POU body" )
1541
- }
1542
-
1543
- fn visit_data_type ( & mut self , _: & DataType ) {
1544
- unimplemented ! ( "for now only interested in individual nodes located in a POU body" )
1545
- }
1546
-
1547
- fn visit_pou ( & mut self , _: & Pou ) {
1548
- unimplemented ! ( "for now only interested in individual nodes located in a POU body" )
1549
- }
1550
-
1551
- fn visit_empty_statement ( & mut self , _stmt : & EmptyStatement , _node : & AstNode ) { }
1552
-
1553
- fn visit_default_value ( & mut self , _stmt : & DefaultValue , _node : & AstNode ) { }
1554
-
1555
- fn visit_literal ( & mut self , stmt : & AstLiteral , _node : & AstNode ) {
1556
- use crate :: literals:: AstLiteral ;
1557
- match stmt {
1558
- AstLiteral :: Integer ( value) => self . result . push_str ( & value. to_string ( ) ) ,
1559
- AstLiteral :: Real ( value) => self . result . push_str ( value) ,
1560
- AstLiteral :: Bool ( value) => self . result . push_str ( & value. to_string ( ) . to_uppercase ( ) ) ,
1561
- AstLiteral :: String ( string_value) => {
1562
- if string_value. is_wide {
1563
- self . result . push_str ( & format ! ( "\" {}\" " , string_value. value) ) ;
1564
- } else {
1565
- self . result . push_str ( & format ! ( "'{}'" , string_value. value) ) ;
1566
- }
1567
- }
1568
- AstLiteral :: Null => self . result . push_str ( "NULL" ) ,
1569
- _ => stmt. walk ( self ) , // Let other literals use their default walking behavior
1570
- }
1571
- }
1572
-
1573
- fn visit_multiplied_statement ( & mut self , stmt : & MultipliedStatement , _node : & AstNode ) {
1574
- stmt. walk ( self )
1575
- }
1576
-
1577
- fn visit_reference_expr ( & mut self , stmt : & ReferenceExpr , _node : & AstNode ) {
1578
- if let Some ( base) = & stmt. base {
1579
- base. walk ( self ) ;
1580
- }
1581
-
1582
- match & stmt. access {
1583
- ReferenceAccess :: Global ( reference) => {
1584
- self . result . push ( '.' ) ;
1585
- reference. walk ( self ) ;
1586
- }
1587
- ReferenceAccess :: Member ( reference) => {
1588
- if stmt. base . is_some ( ) {
1589
- self . result . push ( '.' ) ;
1590
- }
1591
- reference. walk ( self ) ;
1592
- }
1593
- ReferenceAccess :: Index ( index) => {
1594
- self . result . push ( '[' ) ;
1595
- index. walk ( self ) ;
1596
- self . result . push ( ']' ) ;
1597
- }
1598
- ReferenceAccess :: Cast ( reference) => {
1599
- self . result . push ( '#' ) ;
1600
- reference. walk ( self ) ;
1601
- }
1602
- ReferenceAccess :: Deref => {
1603
- self . result . push ( '^' ) ;
1604
- }
1605
- ReferenceAccess :: Address => {
1606
- self . result . insert_str ( 0 , "ADR(" ) ;
1607
- self . result . push ( ')' ) ;
1608
- }
1609
- }
1610
- }
1611
-
1612
- fn visit_identifier ( & mut self , stmt : & str , _node : & AstNode ) {
1613
- self . result . push_str ( stmt) ;
1614
- }
1615
-
1616
- fn visit_direct_access ( & mut self , stmt : & DirectAccess , _node : & AstNode ) {
1617
- stmt. walk ( self )
1618
- }
1619
-
1620
- fn visit_hardware_access ( & mut self , stmt : & HardwareAccess , _node : & AstNode ) {
1621
- stmt. walk ( self )
1622
- }
1623
-
1624
- fn visit_binary_expression ( & mut self , stmt : & BinaryExpression , _node : & AstNode ) {
1625
- stmt. left . walk ( self ) ;
1626
- self . result . push ( ' ' ) ;
1627
- self . result . push_str ( & stmt. operator . to_string ( ) ) ;
1628
- self . result . push ( ' ' ) ;
1629
- stmt. right . walk ( self ) ;
1630
- }
1631
-
1632
- fn visit_unary_expression ( & mut self , stmt : & UnaryExpression , _node : & AstNode ) {
1633
- self . result . push_str ( & stmt. operator . to_string ( ) ) ;
1634
- stmt. value . walk ( self ) ;
1635
- }
1636
-
1637
- fn visit_expression_list ( & mut self , stmt : & Vec < AstNode > , _node : & AstNode ) {
1638
- for ( i, node) in stmt. iter ( ) . enumerate ( ) {
1639
- if i > 0 {
1640
- self . result . push_str ( ", " ) ;
1641
- }
1642
- node. walk ( self ) ;
1643
- }
1644
- }
1645
-
1646
- fn visit_paren_expression ( & mut self , inner : & AstNode , _node : & AstNode ) {
1647
- self . result . push ( '(' ) ;
1648
- inner. walk ( self ) ;
1649
- self . result . push ( ')' ) ;
1650
- }
1651
-
1652
- fn visit_range_statement ( & mut self , stmt : & RangeStatement , _node : & AstNode ) {
1653
- stmt. walk ( self )
1654
- }
1655
-
1656
- fn visit_vla_range_statement ( & mut self , _node : & AstNode ) { }
1657
-
1658
- fn visit_assignment ( & mut self , stmt : & Assignment , _node : & AstNode ) {
1659
- stmt. left . walk ( self ) ;
1660
- self . result . push_str ( " := " ) ;
1661
- stmt. right . walk ( self ) ;
1662
- }
1663
-
1664
- fn visit_output_assignment ( & mut self , stmt : & Assignment , _node : & AstNode ) {
1665
- stmt. left . walk ( self ) ;
1666
- self . result . push_str ( " => " ) ;
1667
- stmt. right . walk ( self ) ;
1668
- }
1669
-
1670
- fn visit_ref_assignment ( & mut self , stmt : & Assignment , _node : & AstNode ) {
1671
- stmt. left . walk ( self ) ;
1672
- self . result . push_str ( " REF= " ) ;
1673
- stmt. right . walk ( self ) ;
1674
- }
1675
-
1676
- fn visit_call_statement ( & mut self , stmt : & CallStatement , _node : & AstNode ) {
1677
- stmt. operator . walk ( self ) ;
1678
- self . result . push ( '(' ) ;
1679
- if let Some ( opt) = stmt. parameters . as_ref ( ) {
1680
- opt. walk ( self )
1681
- }
1682
- self . result . push ( ')' ) ;
1683
- }
1684
-
1685
- fn visit_control_statement ( & mut self , stmt : & AstControlStatement , _node : & AstNode ) {
1686
- stmt. walk ( self )
1687
- }
1688
-
1689
- fn visit_case_condition ( & mut self , child : & AstNode , _node : & AstNode ) {
1690
- child. walk ( self )
1691
- }
1692
-
1693
- fn visit_exit_statement ( & mut self , _node : & AstNode ) { }
1694
-
1695
- fn visit_continue_statement ( & mut self , _node : & AstNode ) { }
1696
-
1697
- fn visit_return_statement ( & mut self , stmt : & ReturnStatement , _node : & AstNode ) {
1698
- stmt. walk ( self )
1699
- }
1700
-
1701
- fn visit_jump_statement ( & mut self , stmt : & JumpStatement , _node : & AstNode ) {
1702
- stmt. walk ( self )
1703
- }
1704
-
1705
- fn visit_label_statement ( & mut self , _stmt : & LabelStatement , _node : & AstNode ) { }
1706
-
1707
- fn visit_allocation ( & mut self , _stmt : & Allocation , _node : & AstNode ) { }
1708
-
1709
- fn visit_super ( & mut self , _stmt : & AstStatement , _node : & AstNode ) {
1710
- self . result . push_str ( "SUPER" ) ;
1711
- }
1712
-
1713
- fn visit_this ( & mut self , _stmt : & AstStatement , _node : & AstNode ) {
1714
- self . result . push_str ( "THIS" ) ;
1481
+ AstSerializer :: format ( self )
1715
1482
}
1716
1483
}
1717
1484
@@ -1831,9 +1598,7 @@ impl Operator {
1831
1598
1832
1599
#[ cfg( test) ]
1833
1600
mod tests {
1834
- use crate :: ast:: {
1835
- ArgumentProperty , AstFactory , AstNode , DeclarationKind , Operator , PouType , VariableBlockType ,
1836
- } ;
1601
+ use crate :: ast:: { ArgumentProperty , DeclarationKind , PouType , VariableBlockType } ;
1837
1602
1838
1603
#[ test]
1839
1604
fn display_pou ( ) {
@@ -1863,35 +1628,6 @@ mod tests {
1863
1628
assert_eq ! ( VariableBlockType :: Global . to_string( ) , "Global" ) ;
1864
1629
assert_eq ! ( VariableBlockType :: InOut . to_string( ) , "InOut" ) ;
1865
1630
}
1866
-
1867
- #[ test]
1868
- fn test_as_string ( ) {
1869
- use crate :: literals:: AstLiteral ;
1870
- use plc_source:: source_location:: SourceLocation ;
1871
-
1872
- // Test integer literal
1873
- let integer_node = AstNode :: new_integer ( 42 , 1 , SourceLocation :: internal ( ) ) ;
1874
- assert_eq ! ( integer_node. as_string( ) , "42" ) ;
1875
-
1876
- // Test identifier
1877
- let identifier_node = AstFactory :: create_identifier ( "myVar" , SourceLocation :: internal ( ) , 2 ) ;
1878
- assert_eq ! ( identifier_node. as_string( ) , "myVar" ) ;
1879
-
1880
- // Test binary expression: 3 + 5
1881
- let left = AstNode :: new_integer ( 3 , 3 , SourceLocation :: internal ( ) ) ;
1882
- let right = AstNode :: new_integer ( 5 , 4 , SourceLocation :: internal ( ) ) ;
1883
- let binary_expr = AstFactory :: create_binary_expression ( left, Operator :: Plus , right, 5 ) ;
1884
- assert_eq ! ( binary_expr. as_string( ) , "3 + 5" ) ;
1885
-
1886
- // Test parenthesized expression: (42)
1887
- let inner = AstNode :: new_integer ( 42 , 6 , SourceLocation :: internal ( ) ) ;
1888
- let paren_expr = AstFactory :: create_paren_expression ( inner, SourceLocation :: internal ( ) , 7 ) ;
1889
- assert_eq ! ( paren_expr. as_string( ) , "(42)" ) ;
1890
-
1891
- // Test boolean literal
1892
- let bool_node = AstNode :: new_literal ( AstLiteral :: Bool ( true ) , 8 , SourceLocation :: internal ( ) ) ;
1893
- assert_eq ! ( bool_node. as_string( ) , "TRUE" ) ;
1894
- }
1895
1631
}
1896
1632
1897
1633
pub struct AstFactory { }
0 commit comments