Skip to content

Commit 404c8ab

Browse files
committed
Refactored ParserIntronExon out of Parser. This should allow things like Java-style comments for code-generation frameworks.
1 parent 6b1be81 commit 404c8ab

File tree

5 files changed

+116
-74
lines changed

5 files changed

+116
-74
lines changed

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

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

18-
import java.util.regex.Pattern;
19-
2018
import javax.script.ScriptEngine;
2119
import javax.script.ScriptException;
2220

@@ -52,33 +50,18 @@
5250
*/
5351
public abstract class CommentScript {
5452
/**
55-
* Creates a CommentScript with the given comment intron/exon pair.
56-
* <p>
57-
* Comment blocks will be parsed using the following regex:
58-
* <pre>
59-
* Pattern.quote(intron) + "(.*?)" + Pattern.quote(exon)
60-
* </pre>
61-
* */
62-
protected CommentScript(String intron, String exon) {
63-
this(intron, exon, Pattern.quote(intron) + "(.*?)" + Pattern.quote(exon));
64-
}
65-
66-
/**
67-
* Creates a CommentScript with the given comment intron/exon pair, as well
68-
* as a custom regex.
69-
* <p>
70-
* Usually, you should use the {@link #CommentScript(String, String)} constructor,
71-
* unless there are some special rules for how comment blocks are parsed.
53+
* Creates a CommentScript using the given parser to
54+
* delineate and combine comment blocks.
7255
*/
73-
protected CommentScript(String intron, String exon, String regex) {
74-
parser = new Parser(intron, exon, regex);
56+
protected CommentScript(Parser parser) {
57+
this.parser = parser;
7558
}
7659

7760
/** Parser which splits up the raw document into structured tags which get passed to the compiler. */
7861
final Parser parser;
7962

8063
/** Compiles a single section/script/input combo into the appropriate output. */
81-
final Parser.SectionCompiler compiler = new Parser.SectionCompiler() {
64+
final ParserIntronExon.SectionCompiler compiler = new ParserIntronExon.SectionCompiler() {
8265
@Override
8366
public String compileSection(String section, String script, String input) {
8467
return Errors.rethrow().get(() -> {

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,8 @@
4848
*/
4949
public abstract class CommentScriptMustache extends CommentScript {
5050
/** @see CommentScript#CommentScript(String, String) */
51-
protected CommentScriptMustache(String intron, String exon) {
52-
super(intron, exon);
53-
}
54-
55-
/** @see CommentScript#CommentScript(String, String, String) */
56-
protected CommentScriptMustache(String intron, String exon, String regex) {
57-
super(intron, exon, regex);
51+
protected CommentScriptMustache(Parser parser) {
52+
super(parser);
5853
}
5954

6055
/** Replaces whatever is inside of {@code &#123;&#123;key&#125;&#125;} tags using the {@code keyToValue} function. */

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
import com.diffplug.jscriptbox.JScriptBox;
3131
import com.diffplug.jscriptbox.Language;
3232

33-
/** The defaault implementation. */
33+
/** The default implementation. */
3434
public class FreshMark extends CommentScriptMustache {
3535
private static final String INTRON = "<!---freshmark";
3636
private static final String EXON = "-->";
@@ -39,7 +39,7 @@ public class FreshMark extends CommentScriptMustache {
3939
private final Consumer<String> warningStream;
4040

4141
public FreshMark(Map<String, ?> properties, Consumer<String> warningStream) {
42-
super(INTRON, EXON, Pattern.quote(INTRON) + "(.*?)" + Pattern.quote(EXON));
42+
super(new ParserIntronExon(INTRON, EXON));
4343
this.properties = properties;
4444
this.warningStream = warningStream;
4545
}

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

Lines changed: 8 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,9 @@
1616
package com.diffplug.freshmark;
1717

1818
import java.util.function.Consumer;
19-
import java.util.regex.Matcher;
20-
import java.util.regex.Pattern;
2119

2220
/** A format defined by "tag start" and "tag end" chunks of text. */
23-
class Parser {
24-
final String intron, exon;
25-
final Pattern pattern;
26-
27-
Parser(String intron, String exon, String regex) {
28-
this.intron = intron;
29-
this.exon = exon;
30-
pattern = Pattern.compile(regex, Pattern.DOTALL);
31-
}
21+
public abstract class Parser {
3222

3323
/**
3424
* Given an input string, parses out the body sections from the tag sections.
@@ -37,20 +27,7 @@ class Parser {
3727
* @param body called for every chunk of text outside a tag
3828
* @param tag called for every chunk of text inside a tag
3929
*/
40-
protected void bodyAndTags(String rawInput, Consumer<String> body, Consumer<String> tag) {
41-
Matcher matcher = pattern.matcher(rawInput);
42-
int last = 0;
43-
while (matcher.find()) {
44-
if (matcher.start() > last) {
45-
body.accept(rawInput.substring(last, matcher.start()));
46-
}
47-
tag.accept(matcher.group(1));
48-
last = matcher.end();
49-
}
50-
if (last < rawInput.length()) {
51-
body.accept(rawInput.substring(last));
52-
}
53-
}
30+
protected abstract void bodyAndTags(String rawInput, Consumer<String> body, Consumer<String> tag);
5431

5532
/**
5633
* Reassembles a section/script/output chunk back into
@@ -61,21 +38,7 @@ protected void bodyAndTags(String rawInput, Consumer<String> body, Consumer<Stri
6138
* @param output
6239
* @return
6340
*/
64-
protected String reassemble(String section, String script, String output) {
65-
// make sure that the compiled output starts and ends with a newline,
66-
// so that the tags stay separated separated nicely
67-
if (!output.startsWith("\n")) {
68-
output = "\n" + output;
69-
}
70-
if (!output.endsWith("\n")) {
71-
output = output + "\n";
72-
}
73-
return intron + " " + section + "\n" +
74-
script +
75-
exon +
76-
output +
77-
intron + " /" + section + " " + exon;
78-
}
41+
protected abstract String reassemble(String section, String script, String output);
7942

8043
/** Interface which can compile a single section of a FreshMark document. */
8144
@FunctionalInterface
@@ -106,9 +69,11 @@ Consumer<String> wrap(Consumer<String> action) {
10669
numReadSoFar += input.length();
10770
} else {
10871
// tag
109-
String tag = intron + input + exon;
110-
assert(toRead.startsWith(tag));
111-
numReadSoFar += tag.length();
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();
11277
}
11378
} catch (Throwable e) {
11479
long problemStart = 1 + countNewlines(fullInput.substring(0, numReadSoFar));
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* Copyright 2015 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.freshmark;
17+
18+
import java.util.function.Consumer;
19+
import java.util.regex.Matcher;
20+
import java.util.regex.Pattern;
21+
22+
/** A format defined by "tag start" and "tag end" chunks of text. */
23+
public class ParserIntronExon extends Parser {
24+
final String intron, exon;
25+
final Pattern pattern;
26+
27+
/**
28+
* A Parser which uses simple intron / exon string to delimit comments.
29+
* <p>
30+
* Comment blocks will be parsed using the following regex:
31+
* <pre>
32+
* Pattern.quote(intron) + "(.*?)" + Pattern.quote(exon)
33+
* </pre>
34+
*/
35+
public ParserIntronExon(String intron, String exon) {
36+
this(intron, exon, Pattern.quote(intron) + "(.*?)" + Pattern.quote(exon));
37+
}
38+
39+
/**
40+
* A Parser with the given comment intron/exon pair, with a custom regex.
41+
* <p>
42+
* Usually, you should use the {@link #Parser(String, String)} constructor,
43+
* unless there are some special rules for how comment blocks are parsed.
44+
*/
45+
public ParserIntronExon(String intron, String exon, String regex) {
46+
this.intron = intron;
47+
this.exon = exon;
48+
pattern = Pattern.compile(regex, Pattern.DOTALL);
49+
}
50+
51+
/**
52+
* Given an input string, parses out the body sections from the tag sections.
53+
*
54+
* @param rawInput the raw input string
55+
* @param body called for every chunk of text outside a tag
56+
* @param tag called for every chunk of text inside a tag
57+
*/
58+
@Override
59+
protected void bodyAndTags(String rawInput, Consumer<String> body, Consumer<String> tag) {
60+
Matcher matcher = pattern.matcher(rawInput);
61+
int last = 0;
62+
while (matcher.find()) {
63+
if (matcher.start() > last) {
64+
body.accept(rawInput.substring(last, matcher.start()));
65+
}
66+
tag.accept(matcher.group(1));
67+
last = matcher.end();
68+
}
69+
if (last < rawInput.length()) {
70+
body.accept(rawInput.substring(last));
71+
}
72+
}
73+
74+
/**
75+
* Reassembles a section/script/output chunk back into
76+
* the full file.
77+
*
78+
* @param section
79+
* @param script
80+
* @param output
81+
* @return
82+
*/
83+
@Override
84+
protected String reassemble(String section, String script, String output) {
85+
// make sure that the compiled output starts and ends with a newline,
86+
// so that the tags stay separated separated nicely
87+
if (!output.startsWith("\n")) {
88+
output = "\n" + output;
89+
}
90+
if (!output.endsWith("\n")) {
91+
output = output + "\n";
92+
}
93+
return intron + " " + section + "\n" +
94+
script +
95+
exon +
96+
output +
97+
intron + " /" + section + " " + exon;
98+
}
99+
}

0 commit comments

Comments
 (0)