Skip to content

Variable assignments before an if statement may prevent it from being parsed correctly #342

Description

@dmitmel

Suppose we have the following minimal example:

handle_mime() {
  mime="$1" uncompressed_filename="$2"

  if [ -z "$decompress" ]; then
    case "$mime" in
    esac
  fi
}

It gets parsed as:

(program ; [0, 0] - [8, 0]
  (function_definition ; [0, 0] - [7, 1]
    name: (word) ; [0, 0] - [0, 11]
    body: (compound_statement ; [0, 14] - [7, 1]
      (command ; [1, 2] - [3, 25]
        (variable_assignment ; [1, 2] - [1, 11]
          name: (variable_name) ; [1, 2] - [1, 6]
          value: (string ; [1, 7] - [1, 11]
            (simple_expansion ; [1, 8] - [1, 10]
              (variable_name)))) ; [1, 9] - [1, 10]
        (variable_assignment ; [1, 12] - [1, 38]
          name: (variable_name) ; [1, 12] - [1, 33]
          value: (string ; [1, 34] - [1, 38]
            (simple_expansion ; [1, 35] - [1, 37]
              (variable_name)))) ; [1, 36] - [1, 37]
        name: (command_name ; [3, 2] - [3, 4]
          (word)) ; [3, 2] - [3, 4]
        argument: (word) ; [3, 5] - [3, 6]
        argument: (word) ; [3, 7] - [3, 9]
        argument: (string ; [3, 10] - [3, 23]
          (simple_expansion ; [3, 11] - [3, 22]
            (variable_name))) ; [3, 12] - [3, 22]
        argument: (word)) ; [3, 24] - [3, 25]
      (command ; [3, 27] - [3, 31]
        name: (command_name ; [3, 27] - [3, 31]
          (word))) ; [3, 27] - [3, 31]
      (case_statement ; [4, 4] - [5, 8]
        value: (string ; [4, 9] - [4, 16]
          (simple_expansion ; [4, 10] - [4, 15]
            (variable_name)))) ; [4, 11] - [4, 15]
      (command ; [6, 2] - [6, 4]
        name: (command_name ; [6, 2] - [6, 4]
          (word)))))) ; [6, 2] - [6, 4]

Notice the lack of an if_statement node. If I add a semicolon after the variable assignments on the second line, it becomes correctly parsed as:

(program ; [0, 0] - [8, 0]
  (function_definition ; [0, 0] - [7, 1]
    name: (word) ; [0, 0] - [0, 11]
    body: (compound_statement ; [0, 14] - [7, 1]
      (variable_assignments ; [1, 2] - [1, 38]
        (variable_assignment ; [1, 2] - [1, 11]
          name: (variable_name) ; [1, 2] - [1, 6]
          value: (string ; [1, 7] - [1, 11]
            (simple_expansion ; [1, 8] - [1, 10]
              (variable_name)))) ; [1, 9] - [1, 10]
        (variable_assignment ; [1, 12] - [1, 38]
          name: (variable_name) ; [1, 12] - [1, 33]
          value: (string ; [1, 34] - [1, 38]
            (simple_expansion ; [1, 35] - [1, 37]
              (variable_name))))) ; [1, 36] - [1, 37]
      (if_statement ; [3, 2] - [6, 4]
        condition: (test_command ; [3, 5] - [3, 25]
          (unary_expression ; [3, 7] - [3, 23]
            operator: (test_operator) ; [3, 7] - [3, 9]
            (string ; [3, 10] - [3, 23]
              (simple_expansion ; [3, 11] - [3, 22]
                (variable_name))))) ; [3, 12] - [3, 22]
        (case_statement ; [4, 4] - [5, 8]
          value: (string ; [4, 9] - [4, 16]
            (simple_expansion ; [4, 10] - [4, 15]
              (variable_name)))))))) ; [4, 11] - [4, 15]

The difference is, without a semicolon, all this text is mushed into a single command node:

  mime="$1" uncompressed_filename="$2"

  if [ -z "$decompress" ]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions