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

View file

@ -14,14 +14,12 @@ import java.util.Map;
public class AttributesParser implements Parser<Map<String, String>> {
private static final String REGEX_NAME = "[:a-zA-Z_][:a-zA-Z0-9_.-]*";
@Override
public Map<String, String> read(Block block) throws ParseException {
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);
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.Literal;
import java.util.List;
import java.util.function.Function;
public class CommentNodeParser implements Parser<CommentNode> {
@Override
public Function<List<Block>, String> getNamingScheme() {
return Block.label("xml comment");
}
@Override
public CommentNode read(Block block) throws ParseException {
Literal.literally("<!--").parse(block);

View file

@ -7,9 +7,15 @@ import de.plugh.compositeparse.parsers.Default;
import de.plugh.compositeparse.parsers.Repeat;
import java.util.List;
import java.util.function.Function;
public class DocumentParser implements Parser<Document> {
@Override
public Function<List<Block>, String> getNamingScheme() {
return Block.label("xml document");
}
@Override
public Document read(Block block) throws ParseException {
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.ParseException;
import de.plugh.compositeparse.Parser;
import de.plugh.compositeparse.parsers.Decision;
import de.plugh.compositeparse.parsers.Expression;
import de.plugh.compositeparse.parsers.Literal;
import de.plugh.compositeparse.parsers.Repeat;
import de.plugh.compositeparse.parsers.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
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
public ElementNode read(Block block) throws ParseException {
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);
@ -39,7 +40,7 @@ public class ElementNodeParser implements Parser<ElementNode> {
Literal.literally(">"),
block1 -> {
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;
}
)

View file

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

View file

@ -10,10 +10,16 @@ public class TextNode extends Node {
@Override
public String prettyPrint(String indent, boolean newline) {
String trimmed = text.trim();
if (trimmed.isEmpty()) {
return "";
}
String result = indent + trimmed;
if (newline) {
return indent + text.trim() + "\n";
return result + "\n";
} 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.parsers.Expression;
import java.util.List;
import java.util.function.Function;
public class TextNodeParser implements Parser<TextNode> {
@Override
public Function<List<Block>, String> getNamingScheme() {
return Block.label("text");
}
@Override
public TextNode read(Block block) throws ParseException {
String text = new Expression("^[^<]+").parse(block);