// Add initialization of Sub for the current Structure public override void ExitTypeStmt([NotNull] BosParser.TypeStmtContext context) { var currentType = TypesStack.Pop(); if (InitStructures.ContainsKey(currentType) && InitStructures[currentType].Text.Length > 0) { StreamRewriter.InsertBefore(context.Stop, InitStructures[currentType].Text); StructuresWithInit.Add(currentType); } else { InitStructures.Remove(currentType); } //base.ExitTypeStmt(context); }
//transform a Type in a Structure // typeStmt rule (VBA.g4 Line 502) public override void EnterTypeStmt([NotNull] BosParser.TypeStmtContext context) { // Find the type name or in the grammar the identifier var typeName = context.ambiguousIdentifier().GetText(); TypesStack.Push(typeName); // Store it in the stack, helpful when recreating the type // Used to create the new type InitStructures.Add(typeName, new StructureInitializer(typeName)); StreamRewriter.Replace(context.TYPE().Symbol, "Structure"); StreamRewriter.Replace(context.END_TYPE().Symbol, "End Structure"); string visibility = context.visibility().GetText(); foreach (var st in context.typeStmt_Element()) { StreamRewriter.InsertBefore(st.Start, $"{visibility} "); } }
// Cannot initialize elements inside a Structure // since VBA Types are transformed in VB.NET Structures // Remove the initialization of array public override void ExitTypeStmt_Element([NotNull] BosParser.TypeStmt_ElementContext context) { // Peek: See the next element, without removing it from stack var currentType = TypesStack.Peek(); // subscripts are the (1 TO 10) part of the arrays if (context.subscripts() != null && context.subscripts().IsEmpty) { string nameArray = context.ambiguousIdentifier().GetText(); string subscriptsArray = context.subscripts().GetText(); InitStructures[currentType].Add( $"ReDim {nameArray} ({subscriptsArray})"); StringBuilder commas = new StringBuilder(); Enumerable.Range(0, context.subscripts().subscript().Length - 1) .ToList() .ForEach(x => commas.Append(",")); StreamRewriter.Replace(context.subscripts().Start, context.subscripts().Stop, $"{ commas.ToString() }"); } }