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

Parse Error : Comment Line with Underscore #4196

Closed
mmaysami opened this issue Jul 16, 2018 · 7 comments · Fixed by #4208
Closed

Parse Error : Comment Line with Underscore #4196

mmaysami opened this issue Jul 16, 2018 · 7 comments · Fixed by #4208
Assignees
Labels
bug Identifies work items for known bugs parse-tree-processing

Comments

@mmaysami
Copy link

mmaysami commented Jul 16, 2018

I have a VBA project and trying to take advantage of rubberduck features. However, it seems that rubberduck doesn't handle one of the end-ifs in the following function.

Error Context reads as : mismatched input 'End If' expecting PABS, ANY, ARRAY, CBOOL, CBYTE, CCUR, ...}

I have also isolated only the function below into a blank/new workbook and same error happens with rubberduck. The source of issue seems to be a commented header line for if condition over line break '' character ! I tried to remove the comment header with '' in it and no more error was generated.

P.S. Option Explicit is included in the module in case that might be a question.

image

Version 2.2.0.3505
OS: Microsoft Windows NT 10.0.16299.0, x64
Host Product: Microsoft Office 2016 x86
Host Version: 16.0.4705.1000
Host Executable: EXCEL.EXE

Option Explicit

'Check for invalid characters in name (return message if any found blank otherwise)
Public Function CheckNameInvalidCharacters(ByVal name As String) As String
    Dim foundInvalidChar As Boolean
    Dim returnMessage As String
    
    returnMessage = "Invalid characters found: "
    foundInvalidChar = False
    'Single quote can't be first character
    If Left(name, 1) = "'" Then
        returnMessage = returnMessage & "' "
        foundInvalidChar = True
    End If
    '=
    If InStr(name, "=") > 0 Then
        returnMessage = returnMessage & "= "
        foundInvalidChar = True
    End If
    'Double quotes "
    If InStr(name, Chr(34)) > 0 Then
        returnMessage = returnMessage & Chr(34) & " "
        foundInvalidChar = True
    End If
    '_                                '<======================= Actual Cause of Error
    '_  Underscore            '<================= No More Error if I add Text after Underscore
    If InStr(name, "_") > 0 Then
        returnMessage = returnMessage & "_ "
        foundInvalidChar = True
    End If                         '<======================= RubberDuck Error Line
    '.
    If InStr(name, ".") > 0 Then
        returnMessage = returnMessage & ". "
        foundInvalidChar = True
    End If
    
    If foundInvalidChar Then
        CheckNameInvalidCharacters = returnMessage
    Else
        CheckNameInvalidCharacters = ""
    End If
End Function
@mmaysami mmaysami changed the title Parse Error : Parse Error : Comment Line with Underscore Jul 16, 2018
@comintern
Copy link
Contributor

Just to be clear, I'm assuming that the lines with '<======================= Actual Cause of Error and '<======================= RubberDuck Error Line are not actually in the code when you get the parser error.

In this code:

    ' _
    If InStr(name, "_") > 0 Then
        returnMessage = returnMessage & "_ "
        foundInvalidChar = True
    End If

The End If is the error line - the comment has a line continuation, so the If statement below it is part of the comment. the next two lines are perfectly valid code outside of an If block, so the first line that contains a syntax error is the End If. This is exactly how the VBA compiler reports it also:

image

@comintern comintern added support Whether you're using Rubberduck or you've forked it and have questions, never hesitate to ask! status-bydesign "It's not a bug, it's a feature!" labels Jul 16, 2018
@mmaysami
Copy link
Author

Sorry, I was playing around with the _ line to get it fixed and copied one with space. Originally there is no space between ' and _ otherwise you are correct as it will be detected as line break and fails the entire function. Regarding the extra comment pointing the error lines, you are correct it is not part of the original code.

So, if line below is in code which is detected by VBA as a comment line with no break., the error is raised:
'_

I tried a few cases and as soon as I added a text after underscore, there is no error.
'_ Underscore

@bclothier
Copy link
Contributor

I can reproduce this. Here's a much smaller MVCE:

This will result in a parser error even though the code can compile.

Public Sub foo()
    '_
    If True Then
    End If
End Sub

OTOH, this compiles and parses just fine:

Public Sub foo()
    '_ x
    If True Then
    End If
End Sub

In contrast, a space makes the code un-compilable with End If without block If

Public Sub foo()
    ' _ 
    If True Then
    End If
End Sub

Finally, if you put in a character, the code becomes compilable and is also parsable:

Public Sub foo()
    ' _ a
    If True Then
    End If
End Sub

In conclusion, I suspect there's a issue with the grammar for handling line continuations with the comment.

@bclothier bclothier reopened this Jul 17, 2018
@comintern comintern added bug Identifies work items for known bugs parse-tree-processing and removed status-bydesign "It's not a bug, it's a feature!" support Whether you're using Rubberduck or you've forked it and have questions, never hesitate to ask! labels Jul 17, 2018
@MDoerner MDoerner self-assigned this Jul 17, 2018
@MDoerner
Copy link
Contributor

This is a grammar issue, indeed. I already have an idea how to fix this edge case. I will just need a bit of time to test it.

@ThunderFrame
Copy link
Member

ThunderFrame commented Jul 17, 2018

@MDoerner this is a bug also, slight variation:

Sub test()
If True Then
'_ _
_
End If
End Sub

@ThunderFrame
Copy link
Member

And of course a non breaking space followed by an underscore followed by a new line, is NOT a line continuation.

@ThunderFrame
Copy link
Member

@MDoerner This might prove useful when testing the change:

Option Explicit

Sub test()

'_
If True Then
End If
    
'_ _
_
If True Then
End If

Dim dic1 As _
Dictionary

Dim dic2 As _
Scripting _
. _
Dictionary

Dim dic3 As Scripting _
  . _
  Dictionary

Debug.Print dic1. _
Item("a")

Debug.Print dic2 _
. _
Item("a")

Debug.Print dic3 _
  . _
  Item("a")

Dim x, x_
x = dic1 _
!a

x = dic2 _
! _
a

x = dic3 _
  ! _
  a
    
End Sub

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Identifies work items for known bugs parse-tree-processing
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants