Skip to content

Commit

Permalink
nanorc parser: the reader is not closed if exception is thrown
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Jan 27, 2022
1 parent f988d35 commit d3aa7dc
Showing 1 changed file with 63 additions and 60 deletions.
123 changes: 63 additions & 60 deletions builtins/src/main/java/org/jline/builtins/SyntaxHighlighter.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2002-2021, the original author or authors.
* Copyright (c) 2002-2022, the original author or authors.
*
* This software is distributable under the BSD license. See the terms of the
* BSD license in the documentation provided with this software.
Expand Down Expand Up @@ -480,80 +480,83 @@ public NanorcParser(InputStream in, String name, String target) {
public void parse() throws IOException {
String line;
int idx = 0;
while ((line = reader.readLine()) != null) {
idx++;
line = line.trim();
if (line.length() > 0 && !line.startsWith("#")) {
List<String> parts = RuleSplitter.split(fixRegexes(line));
if (parts.get(0).equals("syntax")) {
syntaxName = parts.get(1);
List<Pattern> filePatterns = new ArrayList<>();
if (name != null) {
if (name.equals(syntaxName)) {
matches = true;
} else {
break;
}
} else if (target != null) {
for (int i = 2; i < parts.size(); i++) {
filePatterns.add(Pattern.compile(parts.get(i)));
}
for (Pattern p : filePatterns) {
if (p.matcher(target).find()) {
try {
while ((line = reader.readLine()) != null) {
idx++;
line = line.trim();
if (line.length() > 0 && !line.startsWith("#")) {
List<String> parts = RuleSplitter.split(fixRegexes(line));
if (parts.get(0).equals("syntax")) {
syntaxName = parts.get(1);
List<Pattern> filePatterns = new ArrayList<>();
if (name != null) {
if (name.equals(syntaxName)) {
matches = true;
} else {
break;
}
} else if (target != null) {
for (int i = 2; i < parts.size(); i++) {
filePatterns.add(Pattern.compile(parts.get(i)));
}
for (Pattern p : filePatterns) {
if (p.matcher(target).find()) {
matches = true;
break;
}
}
if (!matches && !syntaxName.equals(DEFAULT_SYNTAX)) {
break;
}
} else {
matches = true;
}
if (!matches && !syntaxName.equals(DEFAULT_SYNTAX)) {
break;
}
} else {
matches = true;
}
} else if (parts.get(0).startsWith("$")) {
String key = themeKey(parts.get(0));
if (colorTheme.containsKey(key)) {
if (parser == null) {
parser = new Parser();
}
String[] args = parts.get(1).split(",\\s*");
boolean validKey = true;
if (key.startsWith("$BLOCK_COMMENT")) {
parser.setBlockCommentDelimiters(key, args);
} else if (key.startsWith("$LINE_COMMENT")) {
parser.setLineCommentDelimiters(key, args);
} else if (key.startsWith("$BALANCED_DELIMITERS")) {
parser.setBalancedDelimiters(key, args);
} else if (parts.get(0).startsWith("$")) {
String key = themeKey(parts.get(0));
if (colorTheme.containsKey(key)) {
if (parser == null) {
parser = new Parser();
}
String[] args = parts.get(1).split(",\\s*");
boolean validKey = true;
if (key.startsWith("$BLOCK_COMMENT")) {
parser.setBlockCommentDelimiters(key, args);
} else if (key.startsWith("$LINE_COMMENT")) {
parser.setLineCommentDelimiters(key, args);
} else if (key.startsWith("$BALANCED_DELIMITERS")) {
parser.setBalancedDelimiters(key, args);
} else {
Log.warn("Unknown token type: ", key);
validKey = false;
}
if (validKey) {
if (!highlightRules.containsKey(key)) {
highlightRules.put(key, new ArrayList<>());
}
for (String l : colorTheme.get(key).split("\\\\n")) {
idx++;
addHighlightRule(RuleSplitter.split(fixRegexes(l)), idx, key);
}
}
} else {
Log.warn("Unknown token type: ", key);
validKey = false;
}
if (validKey) {
if (!highlightRules.containsKey(key)) {
highlightRules.put(key, new ArrayList<>());
}
} else if (!addHighlightRule(parts, idx, TOKEN_NANORC) && parts.get(0).matches("\\+" + REGEX_TOKEN_NAME)) {
String key = themeKey(parts.get(0));
if (colorTheme.containsKey(key)) {
for (String l : colorTheme.get(key).split("\\\\n")) {
idx++;
addHighlightRule(RuleSplitter.split(fixRegexes(l)), idx, key);
addHighlightRule(RuleSplitter.split(fixRegexes(l)), idx, TOKEN_NANORC);
}
} else {
Log.warn("Unknown token type: ", key);
}
} else {
Log.warn("Unknown token type: ", key);
}
} else if (!addHighlightRule(parts, idx, TOKEN_NANORC) && parts.get(0).matches("\\+" + REGEX_TOKEN_NAME)) {
String key = themeKey(parts.get(0));
if (colorTheme.containsKey(key)) {
for (String l : colorTheme.get(key).split("\\\\n")) {
idx++;
addHighlightRule(RuleSplitter.split(fixRegexes(l)), idx, TOKEN_NANORC);
}
} else {
Log.warn("Unknown token type: ", key);
}
}
}
} finally {
reader.close();
}
reader.close();
}

private String fixRegexes(String line) {
Expand Down

0 comments on commit d3aa7dc

Please sign in to comment.