antlr / grammars-v4

Grammars written for ANTLR v4; expectation that the grammars are free of actions.
MIT License
10.12k stars 3.69k forks source link

New grammar request: VB.NET #3967

Open chenzimin opened 7 months ago

chenzimin commented 7 months ago

I tried to parse the vb6/examples/form1.vb file and got the same error in vb6/examples/form1.vb.errors, which is:

line 1:13 mismatched input 'Form1' expecting <EOF>

The vb6/examples/form1.vb file contains:

Public Class Form1

   Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
      ' Create two buttons to use as the accept and cancel buttons. 
      Dim button1 As New Button()
      Dim button2 As New Button()
      ' Set the text of button1 to "OK".
      button1.Text = "OK"
      ' Set the position of the button on the form.
      button1.Location = New Point(10, 10)
      ' Set the text of button2 to "Cancel".
      button2.Text = "Cancel"
      ' Set the position of the button based on the location of button1.
      button2.Location = _
         New Point(button1.Left, button1.Height + button1.Top + 10)
      ' Set the caption bar text of the form.   
      Me.Text = "tutorialspoint.com"
      ' Display a help button on the form.
      Me.HelpButton = True
     ' Define the border style of the form to a dialog box.
      Me.FormBorderStyle = FormBorderStyle.FixedDialog
      ' Set the MaximizeBox to false to remove the maximize box.
      Me.MaximizeBox = False
      ' Set the MinimizeBox to false to remove the minimize box.
      Me.MinimizeBox = False
      ' Set the accept button of the form to button1.
      Me.AcceptButton = button1
      ' Set the cancel button of the form to button2.
      Me.CancelButton = button2
      ' Set the start position of the form to the center of the screen.
      Me.StartPosition = FormStartPosition.CenterScreen
      ' Set window width and height
      Me.Height = 300
      Me.Width = 560
      ' Add button1 to the form.
      Me.Controls.Add(button1)
      ' Add button2 to the form.
      Me.Controls.Add(button2)
   End Sub
End Class

Anyway we can fix this?

kaby76 commented 7 months ago

VB6 does not have classes. Not sure why it's even in the examples/ directory if it's not VB6 valid. Perhaps we need a new version for VB.NET?

chenzimin commented 7 months ago

You are right, I was confused between VB6 and VB.NET. I wrongly thought that vb6/examples/form1.vb should be correctly parsed by VB6.

Do you know that if ANTLR4 have a grammar for VB.NET?

kaby76 commented 7 months ago

Do you know that if ANTLR4 have a grammar for VB.NET?

Pretty sure we don't have one. It should be added to the "to do" list.

If you can, rename the title of the issue to something like "New grammar request: VB.NET".

Beakerboy commented 7 months ago

I have a mostly complete grammar for VBA 7.1, which might be close to .Net https://github.com/Beakerboy/grammars-v4/tree/coverage/vba/vba7_1

PYPI has a python module for the Lexer and Parser as well: https://pypi.org/project/antlr4-vba/

I’m using this to create a Linting tool and precompiler for Office VBA.

chenzimin commented 7 months ago

I tried to parse vb6/examples/form1.vb with https://github.com/Beakerboy/grammars-v4/tree/coverage/vba/vba7_1, it failed with a different error message:

line 1:0 extraneous input 'Public' expecting {'ATTRIBUTE', 'VERSION', NEWLINE, REMCOMMENT, COMMENT, WS}
line 1:7 no viable alternative at input ' Class'

https://pypi.org/project/antlr4-vba/ seems to use the same vba grammar as in this repository, and parsing vb6/examples/form1.vb file with vba.g4 will also return line 1:13 mismatched input 'Form1' expecting <EOF>

Beakerboy commented 7 months ago

VBA files have a required module header similar to:

Attribute VB_Name = “Foo”

since the example is missing that, it will not parse. You can try it in lab.antlr.org with turning the header as optional instead of required

chenzimin commented 7 months ago

Thanks for the tip, but adding the header Attribute VB_Name = "Foo" to vb6/examples/form1.vb still results in line 3:13 mismatched input 'Form1' expecting <EOF>.

Beakerboy commented 7 months ago

~Is the file missing a newline character at the end?~ Ah, it’s a class. In VBA, class files have a different header.

VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
END
Attribute VB_Name = "Foo”

and the module portion of the file does not start with the word class.

maybe this won’t work, sorry.