uwol / proleap-vb6-parser

ProLeap ANTLR4-based parser for Visual Basic 6.0
MIT License
79 stars 26 forks source link

Support named parameters ? #12

Closed fossamagna closed 7 years ago

fossamagna commented 7 years ago

Does vb6parser support for named parameters ? I attempted to retrieve Arg object form ArgValueAssignment. ex: follow to after

NamedParameters.cls

Sub studentInfo(ByVal name As String, Optional ByVal age As Integer = 0, Optional ByVal birth As Date = #1/1/2000#)
  Debug.Print "Name = " & name & "; age = " & CStr(age) & "; birth date = " & CStr(birth)
End Sub

Call studentInfo(age:=19, birth:=#9/21/1981#, name:="Mary")

NameParametersTest.java

  @Test
  public void test() throws Exception {
      final File inputFile = new File("src/test/resources/io/proleap/vb6/asg/call/arg/name/NamedParameters.cls");
      final Program program = new VbParserRunnerImpl().analyzeFile(inputFile);

      final Module module = program.getClazzModule("NamedParameters");

      final Sub studentInfo = module.getSub("studentInfo");
      assertNotNull(studentInfo);
      VisualBasic6BaseVisitor<Void> visitor = new VisualBasic6BaseVisitor<Void>() {

        @Override
        public Void visitArgsCall(ArgsCallContext ctx) {
          List<ArgCallContext> argCallCtxList = ctx.argCall();
          ASGElementRegistry registry = program.getASGElementRegistry();
          ArgCallContext argCallCtx = argCallCtxList.get(0);
          ArgValueAssignment argValueAssignment1 = (ArgValueAssignment) registry.getASGElement(argCallCtx);
          assertThat(argValueAssignment1.getArg().getName(), is("age")); // actual "name"

          ArgValueAssignment argValueAssignment2 = (ArgValueAssignment) registry.getASGElement(argCallCtx);
          assertThat(argValueAssignment2.getArg().getName(), is("birth")); // actual "age"

          ArgValueAssignment argValueAssignment3 = (ArgValueAssignment) registry.getASGElement(argCallCtx);
          assertThat(argValueAssignment3.getArg().getName(), is("name")); // actual "birth"
          return null;
        }
      };
      visitor.visit(module.getCtx());
  }

But, Actual Arg object mismatched to expected object. Also, I attempted Arg object from ValueAssignment. But it's always null.

uwol commented 7 years ago

You are right. In case of named parameters these were not resolved by the parser. Instead parameters were resolved only by index position in the calling statement.

I have committed a fix in 70e72b15676ccc6ec5ccee4c6007916b32288034 including a rewritten unit test based on the snippet provided by you. Hopefully, I got everything right. At least the test passes :-)

Please let me know if you find anything else and keep up the good work!