Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix else if parsing problem #833

Merged
merged 1 commit into from
Nov 1, 2024

Conversation

radinshayanfar
Copy link
Contributor

Description

This PR fixes a problem related to parsing nested if/else in Colang 2. The problem is that whenever there is an if inside an else, the parser considers that as an elif node. However, this could be problematic, as the if might not meant to be an elif to the outer if. Consider the following examples:

flow main
  $t = 1
  if $t == 1
    print "t is 1"
  else
    if $t == 1
      print "t is really 1"
    else
      print "t may not be 1"

This example results in parsing error. This is because the else statement of the outer if, is combined with the if after that and is considered as elif. Therefore, the last else statement is not parseable, because there is no if related to that anymore! If you add anything between else and the if, even an optional : or a comment, it will fix the parsing issue.

In this PR, I changed the _ELSE_IF.1 definition, so it will capture any whitespace, except newline, between an else and if.

I have also attached the parsed tree, corresponding to the following code (removing the very last else from the above, so that it could be parsed).

flow main
  $t = 1
  if $t == 1
    print "t is 1"
  else
    if $t == 1
      print "t is really 1"

As you can see in the tree, the inner if is parsed as elif, not a nested if.
tree_incorrect

The correct form, which is the result of this PR, is this:
tree_fixed

Checklist

  • I've read the CONTRIBUTING guidelines.
  • I've updated the documentation if applicable.
  • I've added tests if applicable.
  • @mentions of the person or team responsible for reviewing proposed changes.
    @Pouyanpi @drazvan @schuellc-nvidia

@schuellc-nvidia
Copy link
Collaborator

Good catch! Thank you for the fix!

@schuellc-nvidia schuellc-nvidia merged commit 95f8418 into NVIDIA:develop Nov 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants