Skip to content

Commit 59caaab

Browse files
committed
Introduced Parser.ChunkHandler to simplify Parser.compile.ErrorFormatter.
1 parent 404c8ab commit 59caaab

File tree

3 files changed

+29
-34
lines changed

3 files changed

+29
-34
lines changed

src/main/java/com/diffplug/freshmark/Parser.java

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,21 @@
1919

2020
/** A format defined by "tag start" and "tag end" chunks of text. */
2121
public abstract class Parser {
22+
/** Interface which can compile a single section of a FreshMark document. */
23+
@FunctionalInterface
24+
public interface SectionCompiler {
25+
String compileSection(String section, String script, String input);
26+
}
27+
28+
/** Interface for passing a chunk of the document. */
29+
@FunctionalInterface
30+
protected interface ChunkHandler {
31+
/**
32+
* @param startIdxFromRaw index of the start of content, relative to the beginning of the raw input
33+
* @param content the content to be handled
34+
*/
35+
void handle(int startIdxFromRaw, String content);
36+
}
2237

2338
/**
2439
* Given an input string, parses out the body sections from the tag sections.
@@ -27,7 +42,7 @@ public abstract class Parser {
2742
* @param body called for every chunk of text outside a tag
2843
* @param tag called for every chunk of text inside a tag
2944
*/
30-
protected abstract void bodyAndTags(String rawInput, Consumer<String> body, Consumer<String> tag);
45+
protected abstract void bodyAndTags(String rawInput, ChunkHandler body, ChunkHandler tag);
3146

3247
/**
3348
* Reassembles a section/script/output chunk back into
@@ -40,12 +55,6 @@ public abstract class Parser {
4055
*/
4156
protected abstract String reassemble(String section, String script, String output);
4257

43-
/** Interface which can compile a single section of a FreshMark document. */
44-
@FunctionalInterface
45-
public interface SectionCompiler {
46-
String compileSection(String section, String program, String in);
47-
}
48-
4958
/**
5059
* Compiles an input string to an output string, using the given compiler to compile each section.
5160
*
@@ -56,27 +65,14 @@ public interface SectionCompiler {
5665
public String compile(String fullInput, SectionCompiler compiler) {
5766
StringBuilder result = new StringBuilder(fullInput.length() * 3 / 2);
5867
/** Associates errors with the part of the input that caused it. */
68+
@edu.umd.cs.findbugs.annotations.SuppressFBWarnings(value = "SIC_INNER_SHOULD_BE_STATIC_ANON", justification = "It's a bug in FindBugs. TODO: report")
5969
class ErrorFormatter {
60-
int numReadSoFar = 0;
61-
62-
Consumer<String> wrap(Consumer<String> action) {
63-
return input -> {
70+
ChunkHandler wrap(Consumer<String> action) {
71+
return (int startIdxFromRaw, String content) -> {
6472
try {
65-
action.accept(input);
66-
String toRead = fullInput.substring(numReadSoFar);
67-
if (toRead.startsWith(input)) {
68-
// body
69-
numReadSoFar += input.length();
70-
} else {
71-
// tag
72-
// TODO: we don't have enough information to do line-based
73-
// error checking, so it's just turned off entirely
74-
//String tag = intron + input + exon;
75-
//assert(toRead.startsWith(tag));
76-
//numReadSoFar += tag.length();
77-
}
73+
action.accept(content);
7874
} catch (Throwable e) {
79-
long problemStart = 1 + countNewlines(fullInput.substring(0, numReadSoFar));
75+
long problemStart = 1 + countNewlines(fullInput.substring(0, startIdxFromRaw));
8076
throw new RuntimeException("Error on line " + problemStart + ": " + e.getMessage(), e);
8177
}
8278
};

src/main/java/com/diffplug/freshmark/ParserIntronExon.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
package com.diffplug.freshmark;
1717

18-
import java.util.function.Consumer;
1918
import java.util.regex.Matcher;
2019
import java.util.regex.Pattern;
2120

@@ -56,18 +55,18 @@ public ParserIntronExon(String intron, String exon, String regex) {
5655
* @param tag called for every chunk of text inside a tag
5756
*/
5857
@Override
59-
protected void bodyAndTags(String rawInput, Consumer<String> body, Consumer<String> tag) {
58+
protected void bodyAndTags(String rawInput, ChunkHandler body, ChunkHandler tag) {
6059
Matcher matcher = pattern.matcher(rawInput);
6160
int last = 0;
6261
while (matcher.find()) {
6362
if (matcher.start() > last) {
64-
body.accept(rawInput.substring(last, matcher.start()));
63+
body.handle(last, rawInput.substring(last, matcher.start()));
6564
}
66-
tag.accept(matcher.group(1));
65+
tag.handle(matcher.start(1), matcher.group(1));
6766
last = matcher.end();
6867
}
6968
if (last < rawInput.length()) {
70-
body.accept(rawInput.substring(last));
69+
body.handle(last, rawInput.substring(last));
7170
}
7271
}
7372

src/test/java/com/diffplug/freshmark/ParserTest.java renamed to src/test/java/com/diffplug/freshmark/ParserIntronExonTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import org.junit.Assert;
2121
import org.junit.Test;
2222

23-
public class ParserTest {
23+
public class ParserIntronExonTest {
2424
static final Parser freshmarkParser = new FreshMark(null, null).parser;
2525

2626
@Test
@@ -32,15 +32,15 @@ public void testBodyAndTags() {
3232
"simple.txt",
3333
"unclosed.txt",
3434
"unclosedthenstuff.txt")
35-
.forEach(ParserTest::testCaseBodyAndTags);
35+
.forEach(ParserIntronExonTest::testCaseBodyAndTags);
3636
}
3737

3838
static void testCaseBodyAndTags(String file) {
3939
String raw = TestResource.getTestResource(file);
4040
StringBuilder result = new StringBuilder(raw.length());
41-
freshmarkParser.bodyAndTags(raw, body -> {
41+
freshmarkParser.bodyAndTags(raw, (startIdx, body) -> {
4242
result.append(body);
43-
}, tag -> {
43+
}, (startIdx, tag) -> {
4444
result.append("<!---freshmark");
4545
result.append(tag);
4646
result.append("-->");

0 commit comments

Comments
 (0)