Skip to content

Commit 16de37b

Browse files
committed
0.23.2 - Add 'classSignature.comments' parsing and AST output.
1 parent 8dcac1d commit 16de37b

19 files changed

+149
-25
lines changed

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@ This project does its best to adhere to [Semantic Versioning](http://semver.org/
44

55

66
--------
7-
### [0.23.1](N/A) - 2021-08-14
7+
### [0.23.2](N/A) - 2021-08-21
8+
#### Added
9+
* `classSignature.comments` parsing and AST output added
10+
11+
12+
--------
13+
### [0.23.1](https://github.com/TeamworkGuy2/JParseCode/commit/8dcac1d6a2dfe305dcd286a6d97068120f4e022e) - 2021-08-14
814
#### Changed
915
* Expanded C# and Java field parsing tests
1016

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ JSON Result (printed to System.out):
7676
"classSignature": {
7777
"access": "PUBLIC",
7878
"name": "ParserExamples.Samples.SimpleCs",
79-
"declarationType": "class"
79+
"declarationType": "class",
80+
"comments": [" <summary>\n", " A simple class to test parsing.\n", " </summary>\n"]
8081
},
8182
"blockType": "CLASS",
8283
"using": [],

bin/jparse_code-with-tests.jar

1.89 KB
Binary file not shown.

bin/jparse_code.jar

1.81 KB
Binary file not shown.

package-lib.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version" : "0.23.1",
2+
"version" : "0.23.2",
33
"name" : "jparse-code",
44
"description" : "An in-progress suite of parsing/transpilation tools for C#, Java, and TypeScript code. Generates simple JSON ASTs.",
55
"homepage" : "https://github.com/TeamworkGuy2/JParseCode",

src/twg2/ast/interm/classes/ClassSigResolved.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,14 @@ public class ClassSigResolved implements ClassSig {
2626
private final @Getter Keyword accessModifier;
2727
/** The block's annotations */
2828
private final @Getter List<AnnotationSig> annotations;
29+
private final @Getter List<String> comments;
2930
/** The block's type (i.e. 'interface', 'class', 'enum', etc.) */
3031
private final @Getter String declarationType;
3132
private final @Getter TypeSig.TypeSigResolved extendClass;
3233
private final @Getter List<TypeSig.TypeSigResolved> implementInterfaces;
3334

3435

35-
public ClassSigResolved(List<String> fullName, List<? extends TypeSigResolved> params, Keyword accessModifier, List<? extends AnnotationSig> annotations,
36+
public ClassSigResolved(List<String> fullName, List<? extends TypeSigResolved> params, Keyword accessModifier, List<? extends AnnotationSig> annotations, List<String> comments,
3637
String declarationType, TypeSigResolved extendClass, List<? extends TypeSigResolved> implementInterfaces) {
3738
@SuppressWarnings("unchecked")
3839
var paramsCast = (List<TypeSigResolved>)params;
@@ -45,6 +46,7 @@ public ClassSigResolved(List<String> fullName, List<? extends TypeSigResolved> p
4546
this.params = paramsCast;
4647
this.accessModifier = accessModifier;
4748
this.annotations = annotationsCast;
49+
this.comments = comments;
4850
this.declarationType = declarationType;
4951
this.extendClass = extendClass;
5052
this.implementInterfaces = implementInterfacesCast;
@@ -91,6 +93,11 @@ public void toJson(Appendable dst, WriteSettings st) throws IOException {
9193
.toArrayConsume(annotations, dst, (a) -> a.toJson(dst, st));
9294
}
9395

96+
if(comments.size() > 0) {
97+
json.comma(dst).propName("comments", dst)
98+
.toStringArray(comments, dst);
99+
}
100+
94101
dst.append(" }");
95102
}
96103

src/twg2/ast/interm/classes/ClassSigSimple.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@ public class ClassSigSimple implements ClassSig {
2525
/** The block's type (i.e. 'interface', 'class', 'enum', etc.) */
2626
private final @Getter String declarationType;
2727
private final @Getter List<String> extendImplementSimpleNames;
28+
private final @Getter List<String> comments;
2829

2930

3031

31-
public ClassSigSimple(List<String> fullName, List<? extends TypeSigSimple> params, Keyword accessModifier, List<? extends AnnotationSig> annotations, String declarationType, List<String> extendImplementSimpleNames) {
32+
public ClassSigSimple(List<String> fullName, List<? extends TypeSigSimple> params, Keyword accessModifier, List<? extends AnnotationSig> annotations, List<String> comments, String declarationType, List<String> extendImplementSimpleNames) {
3233
@SuppressWarnings("unchecked")
3334
var paramsCast = (List<TypeSigSimple>)params;
3435
@SuppressWarnings("unchecked")
@@ -38,6 +39,7 @@ public ClassSigSimple(List<String> fullName, List<? extends TypeSigSimple> param
3839
this.params = paramsCast;
3940
this.accessModifier = accessModifier;
4041
this.annotations = annotationsCast;
42+
this.comments = comments;
4143
this.declarationType = declarationType;
4244
this.extendImplementSimpleNames = extendImplementSimpleNames;
4345
}
@@ -82,6 +84,11 @@ public void toJson(Appendable dst, WriteSettings st) throws IOException {
8284
.toArrayConsume(annotations, dst, (a) -> a.toJson(dst, st));
8385
}
8486

87+
if(comments.size() > 0) {
88+
json.comma(dst).propName("comments", dst)
89+
.toStringArray(comments, dst);
90+
}
91+
8592
dst.append(" }");
8693
}
8794

src/twg2/parser/codeParser/csharp/CsBlockParser.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public AstParser<List<AnnotationSig>> createAnnotationParser(BlockAst<CsBlock> b
6767
@Override
6868
public AstParser<List<String>> createCommentParser(BlockAst<CsBlock> block) {
6969
var lang = CodeLanguageOptions.C_SHARP;
70-
return new CommentBlockExtractor(lang.displayName(), block);
70+
return new CommentBlockExtractor(lang.displayName());
7171
}
7272

7373

@@ -97,7 +97,8 @@ public List<Entry<SimpleTree<CodeToken>, ClassAst.SimpleImpl<CsBlock>>> extractC
9797
public List<BlockAst<CsBlock>> extractBlocks(List<String> nameScope, SimpleTree<CodeToken> astTree, BlockAst<CsBlock> parentScope) {
9898
List<BlockAst<CsBlock>> blocks = new ArrayList<>();
9999
var annotationExtractor = new CsAnnotationExtractor();
100-
extractBlocksFromTree(nameScope, astTree, 0, null, parentScope, annotationExtractor, blocks);
100+
var commentExtractor = createCommentParser(parentScope);
101+
extractBlocksFromTree(nameScope, astTree, 0, null, parentScope, annotationExtractor, commentExtractor, blocks);
101102
return blocks;
102103
}
103104

@@ -108,8 +109,8 @@ public List<BlockAst<CsBlock>> extractBlocks(List<String> nameScope, SimpleTree<
108109
* @param depth the current blockTree's depth within the tree (0=root node, 1=child of root, etc.)
109110
* @param parentNode the current blockTree's parent node or null if the parent is null (only possible if blockTree is a child of a tree with a null root or blockTree is the root and has no parent)
110111
*/
111-
public static void extractBlocksFromTree(List<String> nameScope, SimpleTree<CodeToken> blockTree,
112-
int depth, SimpleTree<CodeToken> parentNode, BlockAst<CsBlock> parentScope, CsAnnotationExtractor annotationExtractor, List<BlockAst<CsBlock>> blocks) {
112+
public static void extractBlocksFromTree(List<String> nameScope, SimpleTree<CodeToken> blockTree, int depth, SimpleTree<CodeToken> parentNode, BlockAst<CsBlock> parentScope,
113+
AstParser<List<AnnotationSig>> annotationExtractor, AstParser<List<String>> commentExtractor, List<BlockAst<CsBlock>> blocks) {
113114
var lang = CodeLanguageOptions.C_SHARP;
114115
var keywordUtil = lang.getKeywordUtil();
115116
var children = blockTree.getChildren();
@@ -124,6 +125,7 @@ public static void extractBlocksFromTree(List<String> nameScope, SimpleTree<Code
124125
var token = child.getData();
125126

126127
boolean annotAccepted = annotationExtractor.acceptNext(child);
128+
boolean commentAccepted = commentExtractor.acceptNext(child) || annotAccepted; // keep comments if annotation was accepted so that comments before an annotation carry over if there is a block following the annotation(s)
127129

128130
int addBlockCount = 0;
129131
BlockAst<CsBlock> newestBlock = null;
@@ -152,8 +154,9 @@ public static void extractBlocksFromTree(List<String> nameScope, SimpleTree<Code
152154
var blockTypes = blockSig.isGeneric() ? blockSig.getParams() : Collections.<TypeSig.TypeSigSimple>emptyList();
153155
var blockFqName = NameUtil.splitFqName(blockSig.getTypeName());
154156
var annotations = new ArrayList<>(annotationExtractor.getParserResult());
157+
var comments = new ArrayList<>(commentExtractor.getParserResult());
155158

156-
newestBlock = new BlockAst<>(new ClassSigSimple(blockFqName, blockTypes, access, annotations, blockTypeStr, nameCompoundRes.getValue()), child, blockType);
159+
newestBlock = new BlockAst<>(new ClassSigSimple(blockFqName, blockTypes, access, annotations, comments, blockTypeStr, nameCompoundRes.getValue()), child, blockType);
157160
blocks.add(newestBlock);
158161
}
159162

@@ -164,8 +167,11 @@ public static void extractBlocksFromTree(List<String> nameScope, SimpleTree<Code
164167
// a valid block must have 2 or more children: a 'name' and a '{...}' block
165168
// create a separate annotation extractor when extracting blocks within an annotation (not sure if this could ever happen)
166169
if(child.size() > 1) {
167-
extractBlocksFromTree(nameScope, child, depth + 1, blockTree, newestBlock != null ? newestBlock : parentScope, (annotAccepted ? annotationExtractor.copy() : annotationExtractor.recycle()), blocks);
170+
var childAnnotExtractor = annotAccepted ? annotationExtractor.copy() : annotationExtractor.recycle();
171+
var childCommentExtractor = commentAccepted ? commentExtractor.copy() : commentExtractor.recycle();
172+
extractBlocksFromTree(nameScope, child, depth + 1, blockTree, newestBlock != null ? newestBlock : parentScope, childAnnotExtractor, childCommentExtractor, blocks);
168173
if(!annotAccepted) { annotationExtractor.recycle(); }
174+
if(!commentAccepted) { commentExtractor.recycle(); }
169175
}
170176

171177
while(addBlockCount > 0) {

src/twg2/parser/codeParser/extractors/CommentBlockExtractor.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
import java.util.ArrayList;
44
import java.util.List;
55

6-
import twg2.ast.interm.block.BlockAst;
7-
import twg2.parser.codeParser.BlockType;
86
import twg2.parser.fragment.CodeToken;
97
import twg2.parser.fragment.CodeTokenType;
108
import twg2.parser.stateMachine.AstParserReusableBase;
@@ -25,16 +23,14 @@ static enum State {
2523
}
2624

2725

28-
BlockAst<? extends BlockType> parentBlock;
2926
List<String> comments = new ArrayList<>();
3027
boolean multiLine;
3128
String langName;
3229

3330

34-
public CommentBlockExtractor(String langName, BlockAst<? extends BlockType> parentBlock) {
31+
public CommentBlockExtractor(String langName) {
3532
super(langName + " field", State.COMPLETE, State.FAILED);
3633
this.langName = langName;
37-
this.parentBlock = parentBlock;
3834
this.state = State.INIT;
3935
}
4036

@@ -121,7 +117,7 @@ public CommentBlockExtractor recycle() {
121117

122118
@Override
123119
public CommentBlockExtractor copy() {
124-
return new CommentBlockExtractor(this.langName, this.parentBlock);
120+
return new CommentBlockExtractor(this.langName);
125121
}
126122

127123

src/twg2/parser/codeParser/java/JavaBlockParser.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public AstParser<List<AnnotationSig>> createAnnotationParser(BlockAst<JavaBlock>
6565
@Override
6666
public AstParser<List<String>> createCommentParser(BlockAst<JavaBlock> block) {
6767
var lang = CodeLanguageOptions.JAVA;
68-
return new CommentBlockExtractor(lang.displayName(), block);
68+
return new CommentBlockExtractor(lang.displayName());
6969
}
7070

7171

@@ -97,14 +97,15 @@ public List<Entry<SimpleTree<CodeToken>, ClassAst.SimpleImpl<JavaBlock>>> extrac
9797
public List<BlockAst<JavaBlock>> extractBlocks(List<String> nameScope, SimpleTree<CodeToken> astTree, BlockAst<JavaBlock> parentScope) {
9898
List<BlockAst<JavaBlock>> blocks = new ArrayList<>();
9999
var annotationExtractor = new JavaAnnotationExtractor();
100+
var commentExtractor = createCommentParser(parentScope);
100101
// parse package name and push it into the name scope
101102
String pkgName = parsePackageDeclaration(astTree);
102103
if(pkgName == null) {
103104
pkgName = "(default package)";
104105
}
105106
nameScope.add(pkgName);
106107

107-
extractBlocksFromTree(nameScope, astTree, 0, null, parentScope, annotationExtractor, blocks);
108+
extractBlocksFromTree(nameScope, astTree, 0, null, parentScope, annotationExtractor, commentExtractor, blocks);
108109
return blocks;
109110
}
110111

@@ -115,8 +116,8 @@ public List<BlockAst<JavaBlock>> extractBlocks(List<String> nameScope, SimpleTre
115116
* @param depth the current blockTree's depth within the tree (0=root node, 1=child of root, etc.)
116117
* @param parentNode the current blockTree's parent node or null if the parent is null (only possible if blockTree is a child of a tree with a null root or blockTree is the root and has no parent)
117118
*/
118-
public static void extractBlocksFromTree(List<String> nameScope, SimpleTree<CodeToken> blockTree,
119-
int depth, SimpleTree<CodeToken> parentNode, BlockAst<JavaBlock> parentScope, JavaAnnotationExtractor annotationExtractor, List<BlockAst<JavaBlock>> blocks) {
119+
public static void extractBlocksFromTree(List<String> nameScope, SimpleTree<CodeToken> blockTree, int depth, SimpleTree<CodeToken> parentNode, BlockAst<JavaBlock> parentScope,
120+
AstParser<List<AnnotationSig>> annotationExtractor, AstParser<List<String>> commentExtractor, List<BlockAst<JavaBlock>> blocks) {
120121
var lang = CodeLanguageOptions.JAVA;
121122
var keywordUtil = lang.getKeywordUtil();
122123
var children = blockTree.getChildren();
@@ -129,6 +130,7 @@ public static void extractBlocksFromTree(List<String> nameScope, SimpleTree<Code
129130
var token = child.getData();
130131

131132
boolean annotAccepted = annotationExtractor.acceptNext(child);
133+
boolean commentAccepted = commentExtractor.acceptNext(child) || annotAccepted; // keep comments if annotation was accepted so that comments before an annotation carry over if there is a block following the annotation(s)
132134

133135
int addBlockCount = 0;
134136
BlockAst<JavaBlock> newestBlock = null;
@@ -157,8 +159,9 @@ public static void extractBlocksFromTree(List<String> nameScope, SimpleTree<Code
157159
var blockTypes = blockSig.isGeneric() ? blockSig.getParams() : Collections.<TypeSig.TypeSigSimple>emptyList();
158160
var blockFqName = NameUtil.splitFqName(blockSig.getTypeName());
159161
var annotations = new ArrayList<>(annotationExtractor.getParserResult());
162+
var comments = new ArrayList<>(commentExtractor.getParserResult());
160163

161-
newestBlock = new BlockAst<>(new ClassSigSimple(blockFqName, blockTypes, access, annotations, blockTypeStr, nameCompoundRes.getValue()), child, blockType);
164+
newestBlock = new BlockAst<>(new ClassSigSimple(blockFqName, blockTypes, access, annotations, comments, blockTypeStr, nameCompoundRes.getValue()), child, blockType);
162165
blocks.add(newestBlock);
163166
}
164167

@@ -168,8 +171,11 @@ public static void extractBlocksFromTree(List<String> nameScope, SimpleTree<Code
168171

169172
// a valid block must have 2 or more children: a 'name' and a '{...}' block
170173
if(child.size() > 1) {
171-
extractBlocksFromTree(nameScope, child, depth + 1, blockTree, newestBlock != null ? newestBlock : parentScope, (annotAccepted ? annotationExtractor.copy() : annotationExtractor.recycle()), blocks);
174+
var childAnnotExtractor = annotAccepted ? annotationExtractor.copy() : annotationExtractor.recycle();
175+
var childCommentExtractor = commentAccepted ? commentExtractor.copy() : commentExtractor.recycle();
176+
extractBlocksFromTree(nameScope, child, depth + 1, blockTree, newestBlock != null ? newestBlock : parentScope, childAnnotExtractor, childCommentExtractor, blocks);
172177
if(!annotAccepted) { annotationExtractor.recycle(); }
178+
if(!commentAccepted) { commentExtractor.recycle(); }
173179
}
174180

175181
while(addBlockCount > 0) {

0 commit comments

Comments
 (0)