19
19
20
20
/** A format defined by "tag start" and "tag end" chunks of text. */
21
21
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
+ }
22
37
23
38
/**
24
39
* Given an input string, parses out the body sections from the tag sections.
@@ -27,7 +42,7 @@ public abstract class Parser {
27
42
* @param body called for every chunk of text outside a tag
28
43
* @param tag called for every chunk of text inside a tag
29
44
*/
30
- protected abstract void bodyAndTags (String rawInput , Consumer < String > body , Consumer < String > tag );
45
+ protected abstract void bodyAndTags (String rawInput , ChunkHandler body , ChunkHandler tag );
31
46
32
47
/**
33
48
* Reassembles a section/script/output chunk back into
@@ -40,12 +55,6 @@ public abstract class Parser {
40
55
*/
41
56
protected abstract String reassemble (String section , String script , String output );
42
57
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
-
49
58
/**
50
59
* Compiles an input string to an output string, using the given compiler to compile each section.
51
60
*
@@ -56,27 +65,14 @@ public interface SectionCompiler {
56
65
public String compile (String fullInput , SectionCompiler compiler ) {
57
66
StringBuilder result = new StringBuilder (fullInput .length () * 3 / 2 );
58
67
/** 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" )
59
69
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 ) -> {
64
72
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 );
78
74
} catch (Throwable e ) {
79
- long problemStart = 1 + countNewlines (fullInput .substring (0 , numReadSoFar ));
75
+ long problemStart = 1 + countNewlines (fullInput .substring (0 , startIdxFromRaw ));
80
76
throw new RuntimeException ("Error on line " + problemStart + ": " + e .getMessage (), e );
81
77
}
82
78
};
0 commit comments