Skip to content

Commit

Permalink
Issue #1023: Comment at CodeGen
Browse files Browse the repository at this point in the history
  • Loading branch information
Brochato committed Aug 31, 2018
1 parent 25bb3ef commit e7a7cb4
Show file tree
Hide file tree
Showing 4 changed files with 259 additions and 7 deletions.
62 changes: 62 additions & 0 deletions Codegen/src/Generators/LinearNodeSourceCodeMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1115,6 +1115,9 @@ public void Accept(Node node)
//Create All SourceTextBuffer Content associated to Nodes
CreateNodeSourceTextBufferContents();

// Comment specific parts (Formalized Comments)
CommentSpecificParts(node);

//Now Complete Function Declaration Lines relocation.
CompleteFunctionDeclarationLinesRelocation();

Expand Down Expand Up @@ -1269,6 +1272,65 @@ public static bool HasIntersection<A>(List<A> l1, List<A> l2)
return false;
}

public Tuple<Token, Token> GetFormalizedCommentEnclosingTokensIfAny(Node node)
{
Token formalizedCommentStartToken =
node.CodeElement?.ConsumedTokens.FirstOrDefault(t => t.TokenType == TokenType.FormalizedCommentsStart);
Token formalizedCommentStopToken =
node.CodeElement?.ConsumedTokens.FirstOrDefault(t => t.TokenType == TokenType.FormalizedCommentsStop);
if (formalizedCommentStartToken != null && formalizedCommentStopToken != null)
return new Tuple<Token, Token>(formalizedCommentStartToken, formalizedCommentStopToken);
return null;
}

public void CommentSpecificParts(Node node)
{
if (node is Compiler.CodeModel.Program)
{
// Formalised Comments
var formalizedCommentsToken = GetFormalizedCommentEnclosingTokensIfAny(node);
if (formalizedCommentsToken != null)
{// The node have a formalized Comment
NodeData nodeData = Nodes.FirstOrDefault(n => n.node == node);
if (nodeData != null)
{
// Split the Buffer to get an array of string representing the lines
string content = new string(nodeData.Buffer.ToArray());
string[] lines = content.Split(
new[] { "\r\n", "\r", "\n" },
StringSplitOptions.None
);

int linestart = formalizedCommentsToken.Item1.Line;
int linestop = formalizedCommentsToken.Item2.Line;

int tempoCommentedLines = 0;
int tempoTestedLines = 0;
// for each lines, if it is inside the formalized comment then comment them (replace the 6th character by a '*')
for (int i = 0; i < lines.Length - 1; i++)
{
tempoTestedLines++;
if (nodeData.Positions.Item4[i] >= linestart && nodeData.Positions.Item4[i] <= linestop &&
lines[i].Length >= 7)
{
tempoCommentedLines++;
lines[i] = lines[i].Substring(0, 6) + '*' + lines[i].Substring(7, lines[i].Length - 7);
}
}

// Replace the buffer content by the new one commented
string newContent = string.Join(Environment.NewLine, lines);
nodeData.Buffer.Insert(newContent, 0, newContent.Length);
}
}
}

foreach (var child in node.Children)
{
CommentSpecificParts(child);
}
}

/// <summary>
/// Dump All Structures.
/// </summary>
Expand Down
23 changes: 16 additions & 7 deletions Codegen/test/TestTypeCobolCodegen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -761,15 +761,24 @@ public void DeclarativesWithProcedures2()
CodegenTestUtils.ParseGenerateCompare(Path.Combine("TypeCobol", "DeclarativesWithProcedures2") + ".tcbl", skeletons, false, "TestTypeCobolVersion");
}

[TestMethod]
[TestCategory("Codegen")]
[TestProperty("Time", "fast")]
public void DeclarativesWithInstructionsWithinTest()
{
var skeletons = CodegenTestUtils.ParseConfig(Path.Combine("TypeCobol", "skeletons") + ".xml");
CodegenTestUtils.ParseGenerateCompare(Path.Combine("TypeCobol", "DeclarativesWithInstructionsWithin") + ".rdz.tcbl", skeletons, false, "TestTypeCobolVersion");
[TestMethod]
[TestCategory("Codegen")]
[TestProperty("Time", "fast")]
public void DeclarativesWithInstructionsWithinTest()
{
var skeletons = CodegenTestUtils.ParseConfig(Path.Combine("TypeCobol", "skeletons") + ".xml");
CodegenTestUtils.ParseGenerateCompare(Path.Combine("TypeCobol", "DeclarativesWithInstructionsWithin") + ".rdz.tcbl", skeletons, false, "TestTypeCobolVersion");
}

[TestMethod]
[TestCategory("Codegen")]
[TestProperty("Time", "fast")]
public void FormalizedCommentsTest()
{
var skeletons = CodegenTestUtils.ParseConfig(Path.Combine("TypeCobol", "skeletons") + ".xml");
CodegenTestUtils.ParseGenerateCompare(Path.Combine("TypeCobol", "FormalizedComments") + ".tcbl", skeletons, false, "TestTypeCobolVersion");
}


#if EUROINFO_RULES
[TestMethod]
Expand Down
55 changes: 55 additions & 0 deletions Codegen/test/resources/input/TypeCobol/FormalizedComments.tcbl
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@

*<<< My program
@ Description : description
@deprecated:
@ replacedBy : MyFonction2
@ rEsTrIcTiOn : Do not Use BOOL var
@ need : some needs
- description
@ see : Thank you for your attention
@ todo :
- Add BOOL support
- implement a call counter
*>>>
IDENTIFICATION DIVISION.
PROGRAM-ID. SandBox.

DATA DIVISION.
LOCAL-STORAGE SECTION.

*<<<
@ description : inline typedef
*>>>
01 myType TYPEDEF STRICT PUBLIC pic X(01).

*<<< Vect2D *>>>
01 Vect2D TYPEDEF STRICT.
02 Coord2d.
03 X PIC 9(4).
03 Y PIC 9(4).

PROCEDURE DIVISION.

*<<< MyProc info
@ deprec : It is
deprecated
- need : long need
@ todo:
- todo1
- todo 2
@ params:
- myDate: just a date
- bla: bla < 2
- toto: toto
-blu: will be ignored *>>>
DECLARE PROCEDURE MyProc PRIVATE
INPUT myDate TYPE Date
bla Pic S9(1)V9(12)
IN-OUT myBool TYPE BOOL
OUTPUT toto TYPE BOOL
bli Pic PPP999PPP.
PROCEDURE DIVISION.
CONTINUE.
END-DECLARE.

END PROGRAM SandBox.
126 changes: 126 additions & 0 deletions Codegen/test/resources/output/TypeCobol/FormalizedComments.tcbl
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
 *TypeCobol_Version:TestTypeCobolVersion

*<<< My program
* @ Description : description
* @deprecated:
* @ replacedBy : MyFonction2
* @ rEsTrIcTiOn : Do not Use BOOL var
* @ need : some needs
* - description
* @ see : Thank you for your attention
* @ todo :
* - Add BOOL support
* - implement a call counter
*>>>
IDENTIFICATION DIVISION.
PROGRAM-ID. SandBox.

DATA DIVISION.
LOCAL-STORAGE SECTION.

*<<<
* @ description : inline typedef
* *>>>
*01 myType TYPEDEF STRICT PUBLIC pic X(01).
*




*<<< Vect2D *>>>
*01 Vect2D TYPEDEF STRICT.
*

* 02 Coord2d.
* 03 X PIC 9(4).
* 03 Y PIC 9(4).

PROCEDURE DIVISION.

*<<< MyProc info
* @ deprec : It is
*deprecated
* - need : long need
* @ todo:
* - todo1
* - todo 2
* @ params:
* - myDate: just a date
* - bla: bla < 2
* - toto: toto
* -blu: will be ignored *>>>
*DECLARE PROCEDURE MyProc PRIVATE
* INPUT myDate TYPE Date
* bla Pic S9(1)V9(12)
* IN-OUT myBool TYPE BOOL
* OUTPUT toto TYPE BOOL
* bli Pic PPP999PPP.
*


















END PROGRAM SandBox.
*
*<<< MyProc info
* @ deprec : It is
*deprecated
* - need : long need
* @ todo:
* - todo1
* - todo 2
* @ params:
* - myDate: just a date
* - bla: bla < 2
* - toto: toto
* -blu: will be ignored *>>>
*DECLARE PROCEDURE MyProc PRIVATE
* INPUT myDate TYPE Date
* bla Pic S9(1)V9(12)
* IN-OUT myBool TYPE BOOL
* OUTPUT toto TYPE BOOL
* bli Pic PPP999PPP.
*_________________________________________________________________
IDENTIFICATION DIVISION.
PROGRAM-ID. a726b68dMyProc.
DATA DIVISION.
LINKAGE SECTION.
01 myDate.
02 YYYY PIC 9(4).
02 MM PIC 9(2).
02 DD PIC 9(2).
01 bla Pic S9(1)V9(12).
01 myBool-value PIC X VALUE LOW-VALUE.
88 myBool VALUE 'T'.
88 myBool-false VALUE 'F'
X'00' thru 'S'
'U' thru X'FF'.
01 toto-value PIC X VALUE LOW-VALUE.
88 toto VALUE 'T'.
88 toto-false VALUE 'F'
X'00' thru 'S'
'U' thru X'FF'.
01 bli Pic PPP999PPP.
PROCEDURE DIVISION
USING BY REFERENCE myDate
BY REFERENCE bla
BY REFERENCE myBool-value
BY REFERENCE toto-value
BY REFERENCE bli
.
CONTINUE.
END PROGRAM a726b68dMyProc.

0 comments on commit e7a7cb4

Please sign in to comment.