Rubberduck: Parse Error : Comment Line with Underscore

Created on 16 Jul 2018  路  7Comments  路  Source: rubberduck-vba/Rubberduck

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
bug parse-tree-processing

Most helpful comment

@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

All 7 comments

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

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

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.

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.

@MDoerner this is a bug also, slight variation:

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

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

@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
Was this page helpful?
0 / 5 - 0 ratings