cromwell icon indicating copy to clipboard operation
cromwell copied to clipboard

Parsing issue with if then else expression and == operator

Open freeseek opened this issue 5 years ago • 3 comments

Consider the following WDL using the if then else construct:

version 1.0

workflow main {
  call main {
    input:
      x = 1
  }
}

task main {
  input {
    Int x
  }

  command <<<
    echo ~{if x == 1 then 1 else 0}
  >>>
}

when I parse it:

$ java -jar womtool-52.jar validate main.wdl 
ERROR: Unexpected symbol (line 20, col 15) when parsing 'e'.

Expected then, got "".

    echo ~{if x == 1 then 1 else 0}
              ^

$e = :if $e :then $e :else $e -> TernaryIf( cond=$1, iftrue=$3, iffalse=$5 )

The following equivalent WDL instead:

version 1.0

workflow main {
  call main {
    input:
      x = 1
  }
}

task main {
  input {
    Int x
  }

  Int y	= if x == 1 then 1 else 0
  command <<<
    echo ~{y}
  >>>
}

when I parse it:

$ java -jar womtool-52.jar validate main.wdl 
Success!

Similarly this equivalent WDL:

version 1.0

workflow main {
  call main {
    input:
      x = 1
  }
}

task main {
  input {
    Int x
  }

  command <<<
    echo ~{if !(x != 1) then 1 else 0}
  >>>
}

when I parse it:

$ java -jar womtool-52.jar validate main.wdl 
Success!

It seems like the parser does not accept the == operator in the condition of the TernaryIf for some reasons, but only in the case it is included in a command <<< >>> section.

freeseek avatar Jul 31 '20 00:07 freeseek

Can you try with parens?

command <<<
    echo ~{if (x == 1) then 1 else 0}
>>>

The parser does seem to be out of spec, but maybe we can give it a nudge in the right direction this way.

aednichols avatar Jul 31 '20 15:07 aednichols

That was actually my first thought. But with this:

version 1.0

workflow main {
  call main {
    input:
      x = 1
  }
}

task main {
  input {
    Int x
  }

  command <<<
    echo ~{if (x == 1) then 1 else 0}
  >>>
}

when I parse it:

$ java -jar womtool-52.jar validate main.wdl 
ERROR: Unexpected symbol (line 16, col 16) when parsing '_gen23'.

Expected rparen, got "".

    echo ~{if (x == 1) then 1 else 0}
               ^

$e = :lparen $_gen23 :rparen -> TupleLiteral( values=$1 )

I was under the impression that Cromwell automatically adds parentheses but I am not really sure how it actually works.

freeseek avatar Jul 31 '20 17:07 freeseek

Also the following WDL:

version 1.0

workflow main {
}

task main {
  command <<<
    echo ~{if 0 < 0.0 then "yes" else "no"}
  >>>
}

gives similarly inexplicable error messages (with Cromwell 85):

$ java -jar womtool-85.jar validate main.wdl 
ERROR: Unexpected symbol (line 8, col 21) when parsing 'e'.

Expected identifier, got "0".

    echo ~{if 0 < 0.0 then "yes" else "no"}
                    ^

$e = $e <=> :dot :identifier -> MemberAccess( value=$0, member=$2 )

It almost seems like Cromwell does not like the 0.0 representation of 0 within the command <<< ... >>> section of a task

freeseek avatar Jun 20 '23 18:06 freeseek