Skip to content

Commit b2727d8

Browse files
committed
Refactored StringConditions, CharConditions, CharCompoundConditions, CharMultiConditionParser, to sub-class instead of weird function parameters as sub-classes. Added name() to ParserCondition. Added test for parsing IntermMethod.
1 parent 493287a commit b2727d8

33 files changed

+733
-624
lines changed

rsc/ITrackSearchService.cs

-1
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System.Runtime.Serialization;
2+
3+
namespace ParserExamples.Models {
4+
5+
/// <summary>
6+
/// A class representing a group of tracks.
7+
/// </summary>
8+
/// <threadsafety>
9+
/// This class is mutable. And it is not thread-safe.
10+
/// </threadsafety>
11+
[DataContract]
12+
public class AlbumInfo {
13+
14+
/// <value>The track name.</value>
15+
[DataMember]
16+
public string AlbumName { get; set; }
17+
18+
/// <value>The track duration in milliseconds</value>
19+
public IList<TrackInfo> Tracks { get; set }
20+
21+
}
22+
23+
}

rsc/TrackInfo.cs rsc/csharp/ParserExamples/Models/TrackInfo.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System.Runtime.Serialization;
22

3-
namespace ParserTools.Entities {
3+
namespace ParserExamples.Models {
44

55
/// <summary>
66
/// A class representing a Track.

rsc/csharp/ParserExamples/Services/ITrackSearchService.cs

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
using System.ServiceModel;using System.ServiceModel.Web;using ParserExamples.Models;using ParserExamples.Searching;namespace ParserExamples.Services{ /// <summary> /// This interface provides the contract for track searching. /// </summary> /// <remarks> /// Implementations are expected to be effectively thread-safe. /// </remarks> [ServiceContract] public interface ITrackSearchService { /// <summary> /// Searches tracks. /// </summary> /// <param name="criteria">The search criteria</param> /// <returns>The search result</returns> [OperationContract] [WebInvoke(Method = "POST", UriTemplate = "/TrackSearch", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)] [TransactionFlow(TransactionFlowOption.Allowed)] SearchResult<TrackInfo> Search(TrackSearchCriteria criteria) { ; } /// <summary> /// Searches tracks that have past due date. /// </summary> /// <param name="albumName">The album name</param> /// <returns>The search result</returns> [OperationContract] [WebInvoke(Method = "POST", UriTemplate = "/GetAlbumTracks?albumName={albumName}", ResponseFormat = WebMessageFormat.Json)] [TransactionFlow(TransactionFlowOption.Allowed)] SearchResult<IDictionary<AlbumInfo, IList<TrackInfo>>> GetAlbumTracks(string albumName) { content of block; } }}

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

+7
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ static enum State {
2626

2727
List<AnnotationSig> annotations = new ArrayList<>();
2828
State state = State.INIT;
29+
String name = "C# annotation";
30+
31+
32+
@Override
33+
public String name() {
34+
return name;
35+
}
2936

3037

3138
@Override

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

+10-10
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ public static CodeFileSrc<DocumentFragmentText<CodeFragmentType>, CodeLanguageOp
4949

5050

5151
static CharPrecondition createAnnotationParser() {
52-
CharPrecondition annotationParser = new StringBoundedParserBuilder()
53-
.addStartEndNotPrecededByMarkers('[', '[', ']', Inclusion.INCLUDE)
52+
CharPrecondition annotationParser = new StringBoundedParserBuilder("C# annotation")
53+
.addStartEndNotPrecededByMarkers("block [ ]", '[', '[', ']', Inclusion.INCLUDE)
5454
.isCompound(true)
5555
.build();
5656
return annotationParser;
@@ -59,22 +59,22 @@ static CharPrecondition createAnnotationParser() {
5959

6060
// TODO only partially implemented
6161
static CharPrecondition createOperatorParser() {
62-
CharPrecondition operatorParser = new StringParserBuilder()
63-
.addCharLiteralMarker('+')
64-
.addCharLiteralMarker('-')
65-
.addCharLiteralMarker('=')
66-
.addCharLiteralMarker('?')
67-
.addCharLiteralMarker(':')
62+
CharPrecondition operatorParser = new StringParserBuilder("C# operator")
63+
.addCharLiteralMarker("+", '+')
64+
.addCharLiteralMarker("-", '-')
65+
.addCharLiteralMarker("=", '=')
66+
.addCharLiteralMarker("?", '?')
67+
.addCharLiteralMarker(":", ':')
6868
.build();
6969
return operatorParser;
7070
}
7171

7272

7373
// TODO couldn't get this working with identifier parser which needs to parse ', ' in strings like 'Map<String, String>'
7474
static CharPrecondition createSeparatorParser() {
75-
CharPrecondition annotationParser = new StringBoundedParserBuilder()
75+
CharPrecondition annotationParser = new StringBoundedParserBuilder(";")
7676
//.addCharLiteralMarker(',')
77-
.addCharLiteralMarker(';')
77+
.addCharLiteralMarker(";", ';')
7878
.isCompound(false)
7979
.build();
8080
return annotationParser;

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

+7
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ static enum State {
4040
String fieldName;
4141
CsDataTypeExtractor typeParser = new CsDataTypeExtractor(false);
4242
State state = State.INIT;
43+
String name = "C# field";
4344

4445

4546
public CsDataModelFieldExtractor(IntermBlock<? extends JsonWritableSig, ? extends CompoundBlock> parentBlock, CsAnnotationExtractor annotationParser) {
@@ -48,6 +49,12 @@ public CsDataModelFieldExtractor(IntermBlock<? extends JsonWritableSig, ? extend
4849
}
4950

5051

52+
@Override
53+
public String name() {
54+
return name;
55+
}
56+
57+
5158
@Override
5259
public boolean acceptNext(SimpleTree<DocumentFragmentText<CodeFragmentType>> tokenNode) {
5360
if(state == State.COMPLETE || state == State.FAILED) {

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

+7
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ static enum State {
3939
private State state = State.INIT;
4040
private boolean allowVoid;
4141
private boolean prevNodeWasBlockId;
42+
private String name = "C# data type";
4243

4344

4445
/**
@@ -49,6 +50,12 @@ public CsDataTypeExtractor(boolean allowVoid) {
4950
}
5051

5152

53+
@Override
54+
public String name() {
55+
return name;
56+
}
57+
58+
5259
@Override
5360
public boolean acceptNext(SimpleTree<DocumentFragmentText<CodeFragmentType>> tokenNode) {
5461
if(state == State.COMPLETE || state == State.FAILED) {

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

+7
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ static enum State {
3939
List<IntermMethodSig.SimpleImpl> methods = new ArrayList<>();
4040
CsDataTypeExtractor typeParser = new CsDataTypeExtractor(true);
4141
State state = State.INIT;
42+
String name = "C# method signature";
43+
44+
45+
@Override
46+
public String name() {
47+
return name;
48+
}
4249

4350

4451
/**

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

+7
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ static enum State {
3131

3232
List<List<String>> usingStatements = new ArrayList<>();
3333
State state = State.INIT;
34+
String name = "C# import statement";
35+
36+
37+
@Override
38+
public String name() {
39+
return name;
40+
}
3441

3542

3643
@Override

src/twg2/parser/codeParser/parsers/CodeBlockParser.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ public static CharPrecondition createBlockParser() {
1616

1717

1818
public static CharPrecondition createBlockParser(char startChar, char endChar) {
19-
CharPrecondition commentParser = new StringBoundedParserBuilder()
20-
.addStartEndMarkers(startChar, endChar, Inclusion.INCLUDE)
19+
CharPrecondition commentParser = new StringBoundedParserBuilder("block")
20+
.addStartEndMarkers("block " + startChar + " " + endChar, startChar, endChar, Inclusion.INCLUDE)
2121
.isCompound(true)
2222
.build();
2323
return commentParser;

src/twg2/parser/codeParser/parsers/CodeStringParser.java

+9-9
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,28 @@ public final class CodeStringParser {
1414

1515

1616
public static final CharPrecondition createStringParserForJava() {
17-
CharPrecondition stringParser = new StringBoundedParserBuilder()
18-
.addStartEndNotPrecededByMarkers('"', '\\', '"', Inclusion.INCLUDE)
19-
.addStartEndNotPrecededByMarkers('\'', '\\', '\'', Inclusion.INCLUDE)
17+
CharPrecondition stringParser = new StringBoundedParserBuilder("Java string")
18+
.addStartEndNotPrecededByMarkers("string literal", '"', '\\', '"', Inclusion.INCLUDE)
19+
.addStartEndNotPrecededByMarkers("char literal", '\'', '\\', '\'', Inclusion.INCLUDE)
2020
.build();
2121
return stringParser;
2222
}
2323

2424

2525
// TODO make parser work with all types of C# string literals
2626
public static final CharPrecondition createStringParserForCSharp() {
27-
CharPrecondition stringParser = new StringBoundedParserBuilder()
28-
.addStartEndNotPrecededByMarkers('"', '\\', '"', Inclusion.INCLUDE)
29-
.addStartEndNotPrecededByMarkers('\'', '\\', '\'', Inclusion.INCLUDE)
27+
CharPrecondition stringParser = new StringBoundedParserBuilder("C# string")
28+
.addStartEndNotPrecededByMarkers("string literal", '"', '\\', '"', Inclusion.INCLUDE)
29+
.addStartEndNotPrecededByMarkers("char literal", '\'', '\\', '\'', Inclusion.INCLUDE)
3030
.build();
3131
return stringParser;
3232
}
3333

3434

3535
public static final CharPrecondition createStringParserForJavascript() {
36-
CharPrecondition stringParser = new StringBoundedParserBuilder()
37-
.addStartEndNotPrecededByMarkers('"', '\\', '"', Inclusion.INCLUDE)
38-
.addStartEndNotPrecededByMarkers('\'', '\\', '\'', Inclusion.INCLUDE)
36+
CharPrecondition stringParser = new StringBoundedParserBuilder("JS string")
37+
.addStartEndNotPrecededByMarkers("string literal", '"', '\\', '"', Inclusion.INCLUDE)
38+
.addStartEndNotPrecededByMarkers("char literal", '\'', '\\', '\'', Inclusion.INCLUDE)
3939
.build();
4040
return stringParser;
4141
}

src/twg2/parser/codeParser/parsers/CommentParser.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,28 @@ public final class CommentParser {
1717

1818

1919
public static final CharPrecondition createCommentParserForJava() {
20-
CharPrecondition commentParser = new StringBoundedParserBuilder()
21-
.addStartEndMarkers("/*", "*/", Inclusion.INCLUDE)
22-
.addStartEndMarkers("//", '\n', Inclusion.EXCLUDE)
20+
CharPrecondition commentParser = new StringBoundedParserBuilder("comment")
21+
.addStartEndMarkers("multi-line comment", "/*", "*/", Inclusion.INCLUDE)
22+
.addStartEndMarkers("single-line comment", "//", '\n', Inclusion.EXCLUDE)
2323
.build();
2424
return commentParser;
2525
}
2626

2727

2828
public static final CharPrecondition createCommentParser(EnumSet<CommentStyle> style) {
29-
StringBoundedParserBuilder commentParser = new StringBoundedParserBuilder();
29+
StringBoundedParserBuilder commentParser = new StringBoundedParserBuilder("comment " + style);
3030
int markerCount = 0;
3131

3232
if(style.contains(CommentStyle.MULTILINE_C_STYLE)) {
33-
commentParser.addStartEndMarkers("/*", "*/", Inclusion.INCLUDE);
33+
commentParser.addStartEndMarkers("multi-line comment", "/*", "*/", Inclusion.INCLUDE);
3434
markerCount++;
3535
}
3636
if(style.contains(CommentStyle.END_OF_LINE)) {
37-
commentParser.addStartEndMarkers("//", '\n', Inclusion.EXCLUDE);
37+
commentParser.addStartEndMarkers("single-line comment", "//", '\n', Inclusion.EXCLUDE);
3838
markerCount++;
3939
}
4040
if(style.contains(CommentStyle.XML_COMMENT)) {
41-
commentParser.addStartEndMarkers("<!--", "-->", Inclusion.INCLUDE);
41+
commentParser.addStartEndMarkers("XML comment", "<!--", "-->", Inclusion.INCLUDE);
4242
markerCount++;
4343
}
4444

src/twg2/parser/codeParser/parsers/GenericTypeParser.java

+8-6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import java.util.function.Supplier;
55

66
import lombok.val;
7+
import twg2.collections.primitiveCollections.CharArrayList;
8+
import twg2.parser.Inclusion;
79
import twg2.parser.text.CharConditionPipe;
810
import twg2.parser.text.CharConditions;
911
import twg2.parser.text.CharParserCondition;
@@ -30,15 +32,15 @@ public static CharParserCondition _createGenericTypeStatementCondition(int recur
3032

3133
val requiredParser = Arrays.asList(singleIdentifierParserConstructor.get());
3234
// TODO only matches generic types in the format '<a, b>', allow whitespace between '<'/'>' and after ','
33-
val optionalParser = Arrays.asList(CharConditionPipe.createPipeAllRequired(Arrays.asList(
34-
CharConditions.charLiteralFactory().create('<'),
35-
CharConditionPipe.createPipeRepeatableSeparator(
35+
val optionalParser = Arrays.asList(CharConditionPipe.createPipeAllRequired("generic type signature", Arrays.asList(
36+
new CharConditions.CharLiteralFilter("<", CharArrayList.of('<'), Inclusion.INCLUDE),
37+
CharConditionPipe.createPipeRepeatableSeparator("generic type params",
3638
Arrays.asList(nestedGenericTypeIdentifierCond),
37-
Arrays.asList(StringConditions.stringLiteralFactory().create(", "))
39+
Arrays.asList(new StringConditions.StringLiteralFilter("separator", new String[] { ", " }, Inclusion.INCLUDE))
3840
),
39-
CharConditions.charLiteralFactory().create('>')
41+
new CharConditions.CharLiteralFilter(">", CharArrayList.of('>'), Inclusion.INCLUDE)
4042
)));
41-
return CharConditionPipe.createPipeOptionalSuffix(requiredParser, optionalParser);
43+
return CharConditionPipe.createPipeOptionalSuffix("type parser", requiredParser, optionalParser);
4244
}
4345

4446
}

src/twg2/parser/codeParser/parsers/IdentifierParser.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.util.Arrays;
44

55
import lombok.val;
6+
import twg2.collections.primitiveCollections.CharArrayList;
67
import twg2.parser.Inclusion;
78
import twg2.parser.text.CharConditionPipe;
89
import twg2.parser.text.CharConditions;
@@ -17,16 +18,16 @@
1718
* @since 2015-11-27
1819
*/
1920
public class IdentifierParser {
20-
static int genericTypeDepth = 2;
21+
static int genericTypeDepth = 3;
2122

2223
public static CharPrecondition createIdentifierWithGenericTypeParser() {
23-
CharPrecondition identifierWithGenericTypeParser = new CharPreconditionImpl<>(false, GenericTypeParser.createGenericTypeStatementCondition(genericTypeDepth, IdentifierParser::createCompoundIdentifierCondition));
24+
CharPrecondition identifierWithGenericTypeParser = new CharPreconditionImpl<>("compound identifier with optional generic type", false, GenericTypeParser.createGenericTypeStatementCondition(genericTypeDepth, IdentifierParser::createCompoundIdentifierCondition));
2425
return identifierWithGenericTypeParser;
2526
}
2627

2728

2829
public static CharPrecondition createIdentifierParser() {
29-
CharPrecondition identifierParser = new StringParserBuilder()
30+
CharPrecondition identifierParser = new StringParserBuilder("identifier")
3031
.addConditionMatcher(createIdentifierCondition())
3132
.build();
3233
return identifierParser;
@@ -46,8 +47,7 @@ public static CharConditions.BaseCharFilter createIdentifierCondition() {
4647
CharSearchSet charSet = firstCharSet.copy();
4748
charSet.addRange('0', '9');
4849

49-
val cond = new CharConditions.BaseCharFilter(charSet::contains, firstCharSet::contains, charSet.toCharList().toArray(), Inclusion.INCLUDE, charSet);
50-
CharConditions.setupContainsCharFirstSpecialFilter(cond);
50+
val cond = new CharConditions.ContainsCharFirstSpecialFilter("identifier", charSet::contains, firstCharSet::contains, charSet.toCharList().toArray(), Inclusion.INCLUDE, charSet);
5151
return cond;
5252
}
5353

@@ -57,8 +57,8 @@ public static CharConditions.BaseCharFilter createIdentifierCondition() {
5757
*/
5858
public static CharParserCondition createCompoundIdentifierCondition() {
5959
val identifierParser = Arrays.asList(IdentifierParser.createIdentifierCondition());
60-
val separatorParser = Arrays.asList(CharConditions.charLiteralFactory().create('.'));
61-
return CharConditionPipe.createPipeRepeatableSeparator(identifierParser, separatorParser);
60+
val separatorParser = Arrays.asList(new CharConditions.CharLiteralFilter("identifier namespace separator", CharArrayList.of('.'), Inclusion.INCLUDE));
61+
return CharConditionPipe.createPipeRepeatableSeparator("compound identifier", identifierParser, separatorParser);
6262
}
6363

6464
}

src/twg2/parser/condition/ParserCondition.java

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
public interface ParserCondition {
1717

18+
public String name();
19+
1820
/**
1921
* @return true if this precondition filter has been successfully completed/matched, false if not
2022
* @see #isFailed()

src/twg2/parser/main/MainParser.java

+4-17
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,6 @@ public class MainParser {
2828

2929

3030
public static void parseAndValidProjectCsClasses() throws IOException {
31-
val files1Order = Arrays.asList(
32-
);
33-
val files2Order = Arrays.asList(
34-
);
35-
3631
val fileSet = new ProjectClassSet<CodeFileSrc<DocumentFragmentText<CodeFragmentType>, CodeLanguage>, IntermClass.SimpleImpl<CsBlock>>();
3732
val files1 = CsMain.getFilesByExtension(Paths.get("/server/Services"), 1, "cs");
3833
val files2 = CsMain.getFilesByExtension(Paths.get("/server/Entities"), 3, "cs");
@@ -45,25 +40,17 @@ public static void parseAndValidProjectCsClasses() throws IOException {
4540
val resFileSet = ProjectClassSet.resolveClasses(fileSet, CsBlock.CLASS, missingNamespaces);
4641

4742
val writeSettings = new WriteSettings(true, false, false);
48-
val res = resFileSet.getCompilationUnitsStartWith(Arrays.asList(""));
43+
val res = resFileSet.getCompilationUnitsStartWith(Arrays.asList("Corningstone", "Entities"));
4944

5045
// get a subset of all the parsed files
5146
List<String> resFiles = new ArrayList<>();
5247
List<IntermClass.ResolvedImpl<CsBlock>> resClasses = new ArrayList<>();
5348

5449
// fill indices with null so we can random access any valid index
55-
for(int i = 0, size = files1Order.size(); i < size; i++) {
56-
resFiles.add(null);
57-
resClasses.add(null);
58-
}
59-
6050
for(val classInfo : res) {
61-
String classFqName = NameUtil.joinFqName(classInfo.getValue().getSignature().getFullyQualifyingName());
62-
int idx = -1;
63-
if((idx = files1Order.indexOf(classFqName)) > -1) {
64-
resClasses.set(idx, classInfo.getValue());
65-
resFiles.set(idx, classInfo.getKey().getSrcName());
66-
}
51+
//String classFqName = NameUtil.joinFqName(classInfo.getValue().getSignature().getFullyQualifyingName());
52+
resClasses.add(classInfo.getValue());
53+
resFiles.add(classInfo.getKey().getSrcName());
6754
}
6855
resClasses.sort((c1, c2) -> NameUtil.joinFqName(c1.getSignature().getFullyQualifyingName()).compareTo(NameUtil.joinFqName(c2.getSignature().getFullyQualifyingName())));
6956

0 commit comments

Comments
 (0)