Skip to content

Commit

Permalink
* Fix Parser failing on nested initializer lists and on attributes…
Browse files Browse the repository at this point in the history
… found inside `enum` declarations
  • Loading branch information
saudet committed Dec 4, 2023
1 parent 2d03a8d commit 9855bf6
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

* Fix `Parser` failing on nested initializer lists and on attributes found inside `enum` declarations
* Fix `Parser` for basic containers like `std::optional<std::pair<int,int> >` ([issue #718](https://github.com/bytedeco/javacpp/issues/718))
* Add support for `std::basic_string` basic container ([issue bytedeco/javacpp-presets#1311](https://github.com/bytedeco/javacpp-presets/issues/1311))
* Enhance `Parser` by adding downcast constructors for polymorphic classes ([pull #700](https://github.com/bytedeco/javacpp/pull/700))
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/bytedeco/javacpp/tools/InfoMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public class InfoMap extends HashMap<String,List<Info>> {
"std::function", "std::basic_string"))
.put(new Info("basic/types").cppTypes("signed", "unsigned", "char", "short", "int", "long", "bool", "float", "double",
"__int8", "__int16", "__int32", "__int64", "_Bool", "_Complex", "_Imaginary", "complex", "imaginary"))
.put(new Info("deprecated").annotations("@Deprecated"))
.put(new Info("noexcept").annotations("@NoException(true)"))

.put(new Info("__COUNTER__").cppText("#define __COUNTER__ 0"))
Expand Down
26 changes: 16 additions & 10 deletions src/main/java/org/bytedeco/javacpp/tools/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -2294,8 +2294,8 @@ boolean function(Context context, DeclarationList declList) throws ParserExcepti
count--;
}

// consider { ... } preceded by an identifier or `>` as an initializer list
if (count == 0 && !token.match(Token.IDENTIFIER, '>') && tokens.get(1).match('{')) {
// consider { ... } preceded by an identifier, a comma, or `>` as an initializer list
if (count == 0 && !token.match(Token.IDENTIFIER, ',', '>') && tokens.get(1).match('{')) {
tokens.next();
break;
}
Expand Down Expand Up @@ -2426,8 +2426,8 @@ boolean function(Context context, DeclarationList declList) throws ParserExcepti
count--;
}

// consider { ... } preceded by an identifier or `>` as an initializer list
if (count == 0 && !token.match(Token.IDENTIFIER, '>') && tokens.get(1).match('{')) {
// consider { ... } preceded by an identifier, a comma, or `>` as an initializer list
if (count == 0 && !token.match(Token.IDENTIFIER, ',', '>') && tokens.get(1).match('{')) {
tokens.next();
break;
}
Expand Down Expand Up @@ -2506,8 +2506,8 @@ boolean function(Context context, DeclarationList declList) throws ParserExcepti
count--;
}

// consider { ... } preceded by an identifier or `>` as an initializer list
if (count == 0 && !token.match(Token.IDENTIFIER, '>') && tokens.get(1).match('{')) {
// consider { ... } preceded by an identifier, a comma, or `>` as an initializer list
if (count == 0 && !token.match(Token.IDENTIFIER, ',', '>') && tokens.get(1).match('{')) {
tokens.next();
break;
}
Expand Down Expand Up @@ -4065,8 +4065,15 @@ boolean enumeration(Context context, DeclarationList declList) throws ParserExce
} else if (info == null) {
infoMap.put(info = new Info(cppName).cppText("").translate());
}
tokens.next();
String annotations = "";
Attribute attr = attribute(true);
while (attr != null && attr.annotation) {
annotations += attr.javaName;
attr = attribute(true);
}
String spacing2 = "";
if (tokens.next().match('=')) {
if (tokens.get().match('=')) {
spacing2 = tokens.get().spacing;
if (spacing2.length() > 0 && spacing2.charAt(0) == ' ') {
spacing2 = spacing2.substring(1);
Expand Down Expand Up @@ -4104,7 +4111,6 @@ boolean enumeration(Context context, DeclarationList declList) throws ParserExce
if (separator.equals(",")) {
separator = ";";
}
String annotations = "";
if (!javaName.equals(cppName)){
annotations += "@Name(\"" + cppName + "\") ";
} else if (context.namespace != null && context.javaName == null) {
Expand Down Expand Up @@ -4142,12 +4148,12 @@ boolean enumeration(Context context, DeclarationList declList) throws ParserExce
spacing = " ";
}
String cast = javaType.equals("byte") || javaType.equals("short") ? "(" + javaType + ")(" : "";
text += spacing + javaName + spacing2 + " = " + cast + countPrefix;
text += spacing + annotations + javaName + spacing2 + " = " + cast + countPrefix;
String countPrefix2 = countPrefix;
for (String key : enumeratorMap.keySet()) {
countPrefix2 = countPrefix2.replaceAll("\\b" + key + "\\b", key + ".value");
}
text2 += spacing + javaName + spacing2 + "(" + cast + countPrefix2;
text2 += spacing + annotations + javaName + spacing2 + "(" + cast + countPrefix2;
if (countPrefix.trim().length() > 0) {
if (count > 0) {
text += " + " + count;
Expand Down

0 comments on commit 9855bf6

Please sign in to comment.