Clean up, name the parsers, minor fixes

This commit is contained in:
Joscha 2019-06-20 10:30:58 +00:00
parent 3ded93fdb1
commit c03f660e02
8 changed files with 48 additions and 14 deletions

View file

@ -3,6 +3,7 @@ package de.plugh.compositeparse.parsers;
import de.plugh.compositeparse.Block; import de.plugh.compositeparse.Block;
import de.plugh.compositeparse.ParseException; import de.plugh.compositeparse.ParseException;
import de.plugh.compositeparse.Parser; import de.plugh.compositeparse.Parser;
import de.plugh.compositeparse.StringInput;
import java.util.List; import java.util.List;
import java.util.function.Function; import java.util.function.Function;
@ -31,7 +32,8 @@ public class QuotedString implements Parser<String> {
StringBuilder result = new StringBuilder(); StringBuilder result = new StringBuilder();
boolean escaped = false; boolean escaped = false;
while (true) { while (true) {
String s = block.getInput().read(1); StringInput input = block.getInput();
String s = input.read(1);
if (s.isEmpty()) { if (s.isEmpty()) {
throw new ParseException(block); throw new ParseException(block);
@ -39,6 +41,7 @@ public class QuotedString implements Parser<String> {
result.append(s); result.append(s);
escaped = false; escaped = false;
} else if (s.equals(quoteChar)) { } else if (s.equals(quoteChar)) {
input.move(-1);
break; break;
} else if (s.equals("\\")) { } else if (s.equals("\\")) {
escaped = true; escaped = true;
@ -47,6 +50,8 @@ public class QuotedString implements Parser<String> {
} }
} }
Literal.literally(quoteChar).parse(block);
return result.toString(); return result.toString();
} }

View file

@ -14,14 +14,12 @@ import java.util.Map;
public class AttributesParser implements Parser<Map<String, String>> { public class AttributesParser implements Parser<Map<String, String>> {
private static final String REGEX_NAME = "[:a-zA-Z_][:a-zA-Z0-9_.-]*";
@Override @Override
public Map<String, String> read(Block block) throws ParseException { public Map<String, String> read(Block block) throws ParseException {
List<Attribute> attributes = new Repeat<>(block1 -> { List<Attribute> attributes = new Repeat<>(block1 -> {
Repeat.atLeast(1, Literal.literally(" ")).parse(block1); new Expression("^\\s+").parse(block1);
String name = new Expression(REGEX_NAME).parse(block1); String name = new Expression(NodeParser.REGEX_NAME).parse(block1);
Literal.literally("=").parse(block1); Literal.literally("=").parse(block1);
String value = new QuotedString().parse(block1); String value = new QuotedString().parse(block1);

View file

@ -6,8 +6,16 @@ import de.plugh.compositeparse.Parser;
import de.plugh.compositeparse.parsers.Expression; import de.plugh.compositeparse.parsers.Expression;
import de.plugh.compositeparse.parsers.Literal; import de.plugh.compositeparse.parsers.Literal;
import java.util.List;
import java.util.function.Function;
public class CommentNodeParser implements Parser<CommentNode> { public class CommentNodeParser implements Parser<CommentNode> {
@Override
public Function<List<Block>, String> getNamingScheme() {
return Block.label("xml comment");
}
@Override @Override
public CommentNode read(Block block) throws ParseException { public CommentNode read(Block block) throws ParseException {
Literal.literally("<!--").parse(block); Literal.literally("<!--").parse(block);

View file

@ -7,9 +7,15 @@ import de.plugh.compositeparse.parsers.Default;
import de.plugh.compositeparse.parsers.Repeat; import de.plugh.compositeparse.parsers.Repeat;
import java.util.List; import java.util.List;
import java.util.function.Function;
public class DocumentParser implements Parser<Document> { public class DocumentParser implements Parser<Document> {
@Override
public Function<List<Block>, String> getNamingScheme() {
return Block.label("xml document");
}
@Override @Override
public Document read(Block block) throws ParseException { public Document read(Block block) throws ParseException {
Prolog prolog = new Default<>(null, new PrologParser()).parse(block); Prolog prolog = new Default<>(null, new PrologParser()).parse(block);

View file

@ -4,23 +4,24 @@ import de.plugh.compositeparse.Block;
import de.plugh.compositeparse.Pair; import de.plugh.compositeparse.Pair;
import de.plugh.compositeparse.ParseException; import de.plugh.compositeparse.ParseException;
import de.plugh.compositeparse.Parser; import de.plugh.compositeparse.Parser;
import de.plugh.compositeparse.parsers.Decision; import de.plugh.compositeparse.parsers.*;
import de.plugh.compositeparse.parsers.Expression;
import de.plugh.compositeparse.parsers.Literal;
import de.plugh.compositeparse.parsers.Repeat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.function.Function;
public class ElementNodeParser implements Parser<ElementNode> { public class ElementNodeParser implements Parser<ElementNode> {
private static final String REGEX_NAME = "[:a-zA-Z_][:a-zA-Z0-9_.-]*"; @Override
public Function<List<Block>, String> getNamingScheme() {
return Block.label("xml element node");
}
@Override @Override
public ElementNode read(Block block) throws ParseException { public ElementNode read(Block block) throws ParseException {
Literal.literally("<").parse(block); Literal.literally("<").parse(block);
String name = new Expression(REGEX_NAME).parse(block); String name = new Label<>("tag name", new Expression(NodeParser.REGEX_NAME)).parse(block);
Map<String, String> attributes = new AttributesParser().parse(block); Map<String, String> attributes = new AttributesParser().parse(block);
@ -39,7 +40,7 @@ public class ElementNodeParser implements Parser<ElementNode> {
Literal.literally(">"), Literal.literally(">"),
block1 -> { block1 -> {
List<Node> foundSubnodes = new Repeat<>(new NodeParser()).parse(block); List<Node> foundSubnodes = new Repeat<>(new NodeParser()).parse(block);
Literal.literally("</" + name + ">").parse(block1); new Label<>("closing tag", Literal.literally("</" + name + ">")).parse(block1);
return foundSubnodes; return foundSubnodes;
} }
) )

View file

@ -7,6 +7,8 @@ import de.plugh.compositeparse.parsers.Options;
public class NodeParser implements Parser<Node> { public class NodeParser implements Parser<Node> {
static final String REGEX_NAME = "[:a-zA-Z_][:a-zA-Z0-9_.-]*";
@Override @Override
public Node read(Block block) throws ParseException { public Node read(Block block) throws ParseException {
return new Options<>( return new Options<>(

View file

@ -10,10 +10,16 @@ public class TextNode extends Node {
@Override @Override
public String prettyPrint(String indent, boolean newline) { public String prettyPrint(String indent, boolean newline) {
String trimmed = text.trim();
if (trimmed.isEmpty()) {
return "";
}
String result = indent + trimmed;
if (newline) { if (newline) {
return indent + text.trim() + "\n"; return result + "\n";
} else { } else {
return indent + text.trim(); return result;
} }
} }

View file

@ -5,8 +5,16 @@ import de.plugh.compositeparse.ParseException;
import de.plugh.compositeparse.Parser; import de.plugh.compositeparse.Parser;
import de.plugh.compositeparse.parsers.Expression; import de.plugh.compositeparse.parsers.Expression;
import java.util.List;
import java.util.function.Function;
public class TextNodeParser implements Parser<TextNode> { public class TextNodeParser implements Parser<TextNode> {
@Override
public Function<List<Block>, String> getNamingScheme() {
return Block.label("text");
}
@Override @Override
public TextNode read(Block block) throws ParseException { public TextNode read(Block block) throws ParseException {
String text = new Expression("^[^<]+").parse(block); String text = new Expression("^[^<]+").parse(block);