================================================================================
Simple FOR loops
================================================================================

*** Test Cases ***
Example
    FOR    ${animal}    IN    cat    dog
        Log    ${animal}
        Log    2nd keyword
    END
    Log    Outside loop

Second Example
    FOR    ${var}    IN    one    two    ${3}    four    ${five}
    ...    kuusi    7    eight    nine    ${last}
        Log    ${var}
    END

Example
    FOR    ${element}    IN    @{ELEMENTS}
        Start Element    ${element}

        CONTINUE

        BREAK
    END

--------------------------------------------------------------------------------

(source_file
  (section
    (test_cases_section
      (section_header)
      (test_case_definition
        (name)
        (body
          (statement
            (for_statement
              left: (variable_list
                (scalar_variable
                  (variable_name)))
              right: (in
                (arguments
                  (argument
                    (text_chunk))
                  (argument
                    (text_chunk))))
              body: (block
                (statement
                  (keyword_invocation
                    (keyword)
                    (arguments
                      (argument
                        (scalar_variable
                          (variable_name))))))
                (statement
                  (keyword_invocation
                    (keyword)
                    (arguments
                      (argument
                        (text_chunk))))))))
          (statement
            (keyword_invocation
              (keyword)
              (arguments
                (argument
                  (text_chunk)))))))
      (test_case_definition
        (name)
        (body
          (statement
            (for_statement
              left: (variable_list
                (scalar_variable
                  (variable_name)))
              right: (in
                (arguments
                  (argument
                    (text_chunk))
                  (argument
                    (text_chunk))
                  (argument
                    (scalar_variable
                      (variable_name)))
                  (argument
                    (text_chunk))
                  (argument
                    (scalar_variable
                      (variable_name)))
                  (continuation
                    (ellipses)
                    (argument
                      (text_chunk))
                    (argument
                      (text_chunk))
                    (argument
                      (text_chunk))
                    (argument
                      (text_chunk))
                    (argument
                      (scalar_variable
                        (variable_name))))))
              body: (block
                (statement
                  (keyword_invocation
                    (keyword)
                    (arguments
                      (argument
                        (scalar_variable
                          (variable_name)))))))))))
      (test_case_definition
        (name)
        (body
          (statement
            (for_statement
              left: (variable_list
                (scalar_variable
                  (variable_name)))
              right: (in
                (arguments
                  (argument
                    (list_variable
                      (variable_name)))))
              body: (block
                (statement
                  (keyword_invocation
                    (keyword)
                    (arguments
                      (argument
                        (scalar_variable
                          (variable_name))))))
                (statement
                  (continue_statement))
                (statement
                  (break_statement))))))))))

================================================================================
Nested FOR loops
================================================================================

*** Test Cases ***
Multiple nesting levels
    FOR    ${root}    IN    r1    r2
        FOR    ${child}    IN    c1   c2    c3
            FOR    ${grandchild}    IN    g1    g2
                Log Many    ${root}    ${child}    ${grandchild}
            END
        END
        FOR    ${sibling}    IN    s1    s2    s3
            IF    '${sibling}' != 's2'
                Log Many    ${root}    ${sibling}
            END
        END
    END

--------------------------------------------------------------------------------

(source_file
  (section
    (test_cases_section
      (section_header)
      (test_case_definition
        (name)
        (body
          (statement
            (for_statement
              left: (variable_list
                (scalar_variable
                  (variable_name)))
              right: (in
                (arguments
                  (argument
                    (text_chunk))
                  (argument
                    (text_chunk))))
              body: (block
                (statement
                  (for_statement
                    left: (variable_list
                      (scalar_variable
                        (variable_name)))
                    right: (in
                      (arguments
                        (argument
                          (text_chunk))
                        (argument
                          (text_chunk))
                        (argument
                          (text_chunk))))
                    body: (block
                      (statement
                        (for_statement
                          left: (variable_list
                            (scalar_variable
                              (variable_name)))
                          right: (in
                            (arguments
                              (argument
                                (text_chunk))
                              (argument
                                (text_chunk))))
                          body: (block
                            (statement
                              (keyword_invocation
                                (keyword)
                                (arguments
                                  (argument
                                    (scalar_variable
                                      (variable_name)))
                                  (argument
                                    (scalar_variable
                                      (variable_name)))
                                  (argument
                                    (scalar_variable
                                      (variable_name))))))))))))
                (statement
                  (for_statement
                    left: (variable_list
                      (scalar_variable
                        (variable_name)))
                    right: (in
                      (arguments
                        (argument
                          (text_chunk))
                        (argument
                          (text_chunk))
                        (argument
                          (text_chunk))))
                    body: (block
                      (statement
                        (if_statement
                          condition: (argument
                            (text_chunk)
                            (scalar_variable
                              (variable_name))
                            (text_chunk))
                          consequence: (block
                            (statement
                              (keyword_invocation
                                (keyword)
                                (arguments
                                  (argument
                                    (scalar_variable
                                      (variable_name)))
                                  (argument
                                    (scalar_variable
                                      (variable_name))))))))))))))))))))

================================================================================
Several loop variables
================================================================================

*** Test Cases ***
Multiple loop variables
    FOR    ${index}    ${english}    ${finnish}    IN
    ...    1           cat           kissa
    ...    2           dog           koira
    ...    3           horse         hevonen
        Add Translation    ${english}    ${finnish}    ${index}
    END
    FOR    ${name}    ${id}    IN    @{EMPLOYERS}
        Create    ${name}    ${id}
    END

--------------------------------------------------------------------------------

(source_file
  (section
    (test_cases_section
      (section_header)
      (test_case_definition
        (name)
        (body
          (statement
            (for_statement
              left: (variable_list
                (scalar_variable
                  (variable_name))
                (scalar_variable
                  (variable_name))
                (scalar_variable
                  (variable_name)))
              right: (in
                (arguments
                  (continuation
                    (ellipses)
                    (argument
                      (text_chunk))
                    (argument
                      (text_chunk))
                    (argument
                      (text_chunk)))
                  (continuation
                    (ellipses)
                    (argument
                      (text_chunk))
                    (argument
                      (text_chunk))
                    (argument
                      (text_chunk)))
                  (continuation
                    (ellipses)
                    (argument
                      (text_chunk))
                    (argument
                      (text_chunk))
                    (argument
                      (text_chunk)))))
              body: (block
                (statement
                  (keyword_invocation
                    (keyword)
                    (arguments
                      (argument
                        (scalar_variable
                          (variable_name)))
                      (argument
                        (scalar_variable
                          (variable_name)))
                      (argument
                        (scalar_variable
                          (variable_name)))))))))
          (statement
            (for_statement
              left: (variable_list
                (scalar_variable
                  (variable_name))
                (scalar_variable
                  (variable_name)))
              right: (in
                (arguments
                  (argument
                    (list_variable
                      (variable_name)))))
              body: (block
                (statement
                  (keyword_invocation
                    (keyword)
                    (arguments
                      (argument
                        (scalar_variable
                          (variable_name)))
                      (argument
                        (scalar_variable
                          (variable_name))))))))))))))

================================================================================
FOR ... IN RANGE syntax
================================================================================

*** Keywords ***

Only upper limit
    [Documentation]    Loops over values from 0 to 9.
    FOR    ${index}    IN RANGE    10
        Log    ${index}
    END

Start and end
    [Documentation]    Loops over values from 1 to 10.
    FOR    ${index}    IN RANGE    1    11
        Log    ${index}
    END

Also step given
    [Documentation]    Loops over values 5, 15, and 25.
    FOR    ${index}    IN RANGE    5    26    10
        Log    ${index}
    END

--------------------------------------------------------------------------------

(source_file
  (section
    (keywords_section
      (section_header)
      (keyword_definition
        (name)
        (body
          (keyword_setting
            (arguments
              (argument
                (text_chunk))))
          (statement
            (for_statement
              left: (variable_list
                (scalar_variable
                  (variable_name)))
              right: (in_range
                (argument
                  (text_chunk)))
              body: (block
                (statement
                  (keyword_invocation
                    (keyword)
                    (arguments
                      (argument
                        (scalar_variable
                          (variable_name)))))))))))
      (keyword_definition
        (name)
        (body
          (keyword_setting
            (arguments
              (argument
                (text_chunk))))
          (statement
            (for_statement
              left: (variable_list
                (scalar_variable
                  (variable_name)))
              right: (in_range
                (argument
                  (text_chunk))
                (argument
                  (text_chunk)))
              body: (block
                (statement
                  (keyword_invocation
                    (keyword)
                    (arguments
                      (argument
                        (scalar_variable
                          (variable_name)))))))))))
      (keyword_definition
        (name)
        (body
          (keyword_setting
            (arguments
              (argument
                (text_chunk))))
          (statement
            (for_statement
              left: (variable_list
                (scalar_variable
                  (variable_name)))
              right: (in_range
                (argument
                  (text_chunk))
                (argument
                  (text_chunk))
                (argument
                  (text_chunk)))
              body: (block
                (statement
                  (keyword_invocation
                    (keyword)
                    (arguments
                      (argument
                        (scalar_variable
                          (variable_name))))))))))))))

================================================================================
FOR ... IN ENUMERATE syntax
================================================================================

***Keywords***

FOR-IN-ENUMERATE
    FOR    ${index}    ${item}    IN ENUMERATE    @{LIST}
        My Keyword    ${index}    ${item}
    END

FOR-IN-ENUMERATE with two values per iteration
    FOR    ${index}    ${en}    ${fi}    IN ENUMERATE
    ...    cat      kissa
    ...    dog      koira
    ...    horse    hevonen
        Log    "${en}" in English is "${fi}" in Finnish (index: ${index})
    END

--------------------------------------------------------------------------------

(source_file
  (section
    (keywords_section
      (section_header)
      (keyword_definition
        (name)
        (body
          (statement
            (for_statement
              left: (variable_list
                (scalar_variable
                  (variable_name))
                (scalar_variable
                  (variable_name)))
              right: (in_enumerate
                (arguments
                  (argument
                    (list_variable
                      (variable_name)))))
              body: (block
                (statement
                  (keyword_invocation
                    (keyword)
                    (arguments
                      (argument
                        (scalar_variable
                          (variable_name)))
                      (argument
                        (scalar_variable
                          (variable_name)))))))))))
      (keyword_definition
        (name)
        (body
          (statement
            (for_statement
              left: (variable_list
                (scalar_variable
                  (variable_name))
                (scalar_variable
                  (variable_name))
                (scalar_variable
                  (variable_name)))
              right: (in_enumerate
                (arguments
                  (continuation
                    (ellipses)
                    (argument
                      (text_chunk))
                    (argument
                      (text_chunk)))
                  (continuation
                    (ellipses)
                    (argument
                      (text_chunk))
                    (argument
                      (text_chunk)))
                  (continuation
                    (ellipses)
                    (argument
                      (text_chunk))
                    (argument
                      (text_chunk)))))
              body: (block
                (statement
                  (keyword_invocation
                    (keyword)
                    (arguments
                      (argument
                        (text_chunk)
                        (scalar_variable
                          (variable_name))
                        (text_chunk)
                        (scalar_variable
                          (variable_name))
                        (text_chunk)
                        (scalar_variable
                          (variable_name))
                        (text_chunk)))))))))))))

================================================================================
FOR ... IN ZIP syntax
================================================================================

*** Keywords ***

FOR-IN-ZIP with multiple lists
    FOR    ${a}    ${x}    ${n}    IN ZIP    ${ABC}    ${XYZ}    ${NUM}
        Log Many    ${a}    ${x}    ${n}
    END

FOR-IN-ZIP with one variable
    FOR    ${items}    IN ZIP    ${ABC}    ${XYZ}    ${NUM}
        Length Should Be    ${items}    3
        Log Many    ${items}[0]    ${items}[1]    ${items}[2]
    END

LONGEST mode with custom fill value
    [Documentation]    This loop executes five times.
    ...                On last two rounds `${n}` has value `0`.
    FOR    ${c}    ${n}    IN ZIP    ${CHARACTERS}    ${NUMBERS}    mode=LONGEST    fill=0
        Log    ${c}: ${n}
    END

--------------------------------------------------------------------------------

(source_file
  (section
    (keywords_section
      (section_header)
      (keyword_definition
        (name)
        (body
          (statement
            (for_statement
              left: (variable_list
                (scalar_variable
                  (variable_name))
                (scalar_variable
                  (variable_name))
                (scalar_variable
                  (variable_name)))
              right: (in_zip
                (arguments
                  (argument
                    (scalar_variable
                      (variable_name)))
                  (argument
                    (scalar_variable
                      (variable_name)))
                  (argument
                    (scalar_variable
                      (variable_name)))))
              body: (block
                (statement
                  (keyword_invocation
                    (keyword)
                    (arguments
                      (argument
                        (scalar_variable
                          (variable_name)))
                      (argument
                        (scalar_variable
                          (variable_name)))
                      (argument
                        (scalar_variable
                          (variable_name)))))))))))
      (keyword_definition
        (name)
        (body
          (statement
            (for_statement
              left: (variable_list
                (scalar_variable
                  (variable_name)))
              right: (in_zip
                (arguments
                  (argument
                    (scalar_variable
                      (variable_name)))
                  (argument
                    (scalar_variable
                      (variable_name)))
                  (argument
                    (scalar_variable
                      (variable_name)))))
              body: (block
                (statement
                  (keyword_invocation
                    (keyword)
                    (arguments
                      (argument
                        (scalar_variable
                          (variable_name)))
                      (argument
                        (text_chunk)))))
                (statement
                  (keyword_invocation
                    (keyword)
                    (arguments
                      (argument
                        (scalar_variable
                          (variable_name))
                        (text_chunk))
                      (argument
                        (scalar_variable
                          (variable_name))
                        (text_chunk))
                      (argument
                        (scalar_variable
                          (variable_name))
                        (text_chunk))))))))))
      (keyword_definition
        (name)
        (body
          (keyword_setting
            (arguments
              (argument
                (text_chunk))
              (continuation
                (ellipses)
                (argument
                  (text_chunk)
                  (scalar_variable
                    (variable_name))
                  (text_chunk)))))
          (statement
            (for_statement
              left: (variable_list
                (scalar_variable
                  (variable_name))
                (scalar_variable
                  (variable_name)))
              right: (in_zip
                (arguments
                  (argument
                    (scalar_variable
                      (variable_name)))
                  (argument
                    (scalar_variable
                      (variable_name)))
                  (argument
                    (text_chunk))
                  (argument
                    (text_chunk))))
              body: (block
                (statement
                  (keyword_invocation
                    (keyword)
                    (arguments
                      (argument
                        (scalar_variable
                          (variable_name))
                        (text_chunk)
                        (scalar_variable
                          (variable_name))))))))))))))
