1919
2020/** A format defined by "tag start" and "tag end" chunks of text. */
2121public 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 };
0 commit comments