1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
pub mod ast;
mod ast_context_iter;
mod ast_iter;
pub mod error;
mod parser;

pub use ast::SyntaxNode;
pub use error::TwigParseError;

use crate::parser::general::{document_node_all, Input};
use nom::combinator::all_consuming;

/// Parses a template into an AST of the [SyntaxNode] type.
/// If it fails it will return a [TwigParseError] which contains a function
/// to generate a human readable error message [TwigParseError::pretty_helpful_error_string].
pub fn parse(input: Input) -> Result<SyntaxNode, TwigParseError<Input>> {
    let (_, result) = all_consuming(document_node_all)(input)?;

    Ok(result)
}

#[cfg(test)]
mod tests {
    use super::*;
    use ast::*;

    /*
    The input or output data for testing purposes is partially from the following sources and under copyright!
    It is not included in the built binaries. Keep the licenses in mind if you use these strings (MIT as of 12.12.2020)!

    Copyright (c) shopware AG (https://github.com/shopware/platform)
    Copyright (c) shopware AG (https://github.com/shopware/SwagMigrationAssistant)
     */

    #[test]
    fn it_works() {
        let result = parse("{% block swag_migration_index_main_page_modal_abort_migration_confirmDialog_message_hint %}
                <p class=\"swag-migration-index-modal-abort-migration-confirm-dialog-hint\">
                    Hello world
                </p>
                {% endblock %}");

        let attributes = vec![TagAttribute::HtmlAttribute(HtmlAttribute {
            name: "class".to_string(),
            value: Some(
                "swag-migration-index-modal-abort-migration-confirm-dialog-hint".to_string(),
            ),
        })];

        assert_eq!(
            result,
            Ok(SyntaxNode::Root(vec![
                SyntaxNode::TwigStructure(TwigStructure::TwigBlock(TwigBlock{
                        name: "swag_migration_index_main_page_modal_abort_migration_confirmDialog_message_hint".to_string(),
                        children: vec![
                            SyntaxNode::Whitespace,
                            SyntaxNode::Tag(Tag {
                                name: "p".to_string(),
                                self_closed: false,
                                attributes,
                                children: vec![
                                    SyntaxNode::Whitespace,
                                    SyntaxNode::Plain(Plain { plain: "Hello world".to_string() }),
                                    SyntaxNode::Whitespace,
                                ]
                            }),
                            SyntaxNode::Whitespace
                        ]
                    }))
                ])
            )
        )
    }

    #[test]
    fn test_missing_closing_tag_error() {
        let input = "{% block swag_migration_index_main_page_modal_abort_migration_confirmDialog_message_hint %}
                <p class=\"swag-migration-index-modal-abort-migration-confirm-dialog-hint\">
                    Hello world
                <div>
                </div>
                {% endblock %}";
        let result = parse(input).unwrap_err();

        let pretty = result.pretty_helpful_error_string(input);
        //println!("{}", pretty);
        assert_eq!(pretty, "Parsing goes wrong in line 6 and column 17 :\n                {% endblock %}\n                ^\n                |\nMissing closing tag for opening tag \'p\' with attributes [ class=\"swag-migration-index-modal-abort-migration-confirm-dialog-hint\" ]");
    }

    #[test]
    fn test_missing_closing_block_error() {
        let input = "{% block swag_migration_index_main_page_modal_abort_migration_confirmDialog_message_hint %}
                <p class=\"swag-migration-index-modal-abort-migration-confirm-dialog-hint\">
                    Hello world
                </p>";
        let result = parse(input).unwrap_err();

        let pretty = result.pretty_helpful_error_string(input);
        //println!("{}", pretty);
        assert_eq!(pretty, "Parsing goes wrong in line 4 and column 21 :\n                </p>\n                    ^\n                    |\nMissing endblock for \'swag_migration_index_main_page_modal_abort_migration_confirmDialog_message_hint\' twig block");
    }

    #[test]
    fn test_missing_closing_tag_error_nested() {
        let input = "{% block swag_migration_index_main_page_modal_abort_migration_confirmDialog_message_hint %}
                <div>
                <p class=\"swag-migration-index-modal-abort-migration-confirm-dialog-hint\">

                    Hello world
                <p></p>
                </div>
                {% endblock %}";
        let result = parse(input).unwrap_err();

        let pretty = result.pretty_helpful_error_string(input);
        //println!("{}", pretty);
        assert_eq!(pretty, "Parsing goes wrong in line 7 and column 19 :\n                </div>\n                  ^\n                  |\nMissing closing tag for opening tag \'p\' with attributes [ class=\"swag-migration-index-modal-abort-migration-confirm-dialog-hint\" ]");
    }

    #[test]
    fn test_missing_closing_tag_error_with_twig_if_in_attributes() {
        let input = "{% block swag_migration_index_main_page_modal_abort_migration_confirmDialog_message_hint %}
                <p class=\"swag-migration-index-modal-abort-migration-confirm-dialog-hint\" {# this is a comment #}
                   {% if isHidden %}
                       style=\"display: none;\"
                   {% endif %}>
                    Hello world
                <div>
                </div>
                {% endblock %}";
        let result = parse(input).unwrap_err();

        let pretty = result.pretty_helpful_error_string(input);
        //println!("{}", pretty);
        assert_eq!(pretty, "Parsing goes wrong in line 9 and column 17 :\n                {% endblock %}\n                ^\n                |\nMissing closing tag for opening tag \'p\' with attributes [ class=\"swag-migration-index-modal-abort-migration-confirm-dialog-hint\" {# this is a comment #} {% if isHidden %} style=\"display: none;\" {% endif %} ]");
    }

    #[test]
    fn test_missing_closing_tag_error_with_twig_if_elseif_else_in_attributes() {
        let input = "{% block swag_migration_index_main_page_modal_abort_migration_confirmDialog_message_hint %}
                <p class=\"swag-migration-index-modal-abort-migration-confirm-dialog-hint\" {# this is a comment #}
                   {% if isHidden %}
                       style=\"display: none;\"
                   {% elseif isInline %}
                       style=\"display: inline;\"
                   {% else %}
                       style=\"display: block;\"
                   {% endif %}>
                    Hello world
                <div>
                </div>
                {% endblock %}";
        let result = parse(input).unwrap_err();

        let pretty = result.pretty_helpful_error_string(input);
        //println!("{}", pretty);
        assert_eq!(pretty, "Parsing goes wrong in line 13 and column 17 :\n                {% endblock %}\n                ^\n                |\nMissing closing tag for opening tag \'p\' with attributes [ class=\"swag-migration-index-modal-abort-migration-confirm-dialog-hint\" {# this is a comment #} {% if isHidden %} style=\"display: none;\" {% elseif isInline %} style=\"display: inline;\" {% else %} style=\"display: block;\" {% endif %} ]");
    }

    #[test]
    fn test_missing_quote_in_tag_argument() {
        let input = "
                <p class=swag-migration-index-modal-abort-migration-confirm-dialog-hint\">
                    Hello world
                </p>";
        let result = parse(input).unwrap_err();

        let pretty = result.pretty_helpful_error_string(input);
        //println!("{}", pretty);
        assert_eq!(pretty, "Parsing goes wrong in line 1 and column 26 :\n                <p class=swag-migration-index-modal-abort-migration-confirm-dialog-hint\">\n                         ^\n                         |\nmissing \'\"\' quote");
    }

    #[test]
    fn test_missing_quote_in_tag_argument_end() {
        let input = "<sw-button size=\"small @click=\"onCloseModal\">
                click me
            </sw-button>";
        let result = parse(input).unwrap_err();

        let pretty = result.pretty_helpful_error_string(input);
        //println!("{}", pretty);
        assert_eq!(pretty, "Parsing goes wrong in line 1 and column 44 :\n<sw-button size=\"small @click=\"onCloseModal\">\n                                           ^\n                                           |\ninvalid attribute name");
    }
}