Skip to content

Commit

Permalink
🚧 work in progress on the new YAML parser
Browse files Browse the repository at this point in the history
  • Loading branch information
arnaudroques committed Feb 8, 2025
1 parent 14be338 commit 5fdc068
Show file tree
Hide file tree
Showing 10 changed files with 708 additions and 185 deletions.
70 changes: 35 additions & 35 deletions src/net/sourceforge/plantuml/style/parser/StyleParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@
import net.sourceforge.plantuml.style.ValueImpl;
import net.sourceforge.plantuml.utils.BlocLines;
import net.sourceforge.plantuml.utils.CharInspector;
import net.sourceforge.plantuml.utils.Inspector;
import net.sourceforge.plantuml.utils.InspectorUtils;
import net.sourceforge.plantuml.utils.Peeker;
import net.sourceforge.plantuml.utils.PeekerUtils;

public class StyleParser {

Expand All @@ -73,9 +73,9 @@ public static Collection<Style> parse(BlocLines lines, AutomaticCounter counter)
// for (StyleToken t : tokens)
// System.err.println(t);

for (Inspector<StyleToken> ins = InspectorUtils.inspector(tokens); ins.peek(0) != null;) {
final StyleToken token = ins.peek(0);
ins.jump();
for (Peeker<StyleToken> peeker = PeekerUtils.peeker(tokens); peeker.peek(0) != null;) {
final StyleToken token = peeker.peek(0);
peeker.jump();
if (token.getType() == StyleTokenType.NEWLINE)
continue;
if (token.getType() == StyleTokenType.SEMICOLON)
Expand All @@ -86,33 +86,33 @@ public static Collection<Style> parse(BlocLines lines, AutomaticCounter counter)
if (token.getType() == StyleTokenType.STRING && token.getData().equalsIgnoreCase("</style>"))
continue;

if (ins.peek(0).getType() == StyleTokenType.COMMA) {
final String full = token.getData() + readWithComma(ins);
skipNewLines(ins);
if (ins.peek(0).getType() == StyleTokenType.OPEN_BRACKET) {
if (peeker.peek(0).getType() == StyleTokenType.COMMA) {
final String full = token.getData() + readWithComma(peeker);
skipNewLines(peeker);
if (peeker.peek(0).getType() == StyleTokenType.OPEN_BRACKET) {
context = context.push(full);
ins.jump();
peeker.jump();
continue;
}
throw new IllegalStateException();
}
if (token.getType() == StyleTokenType.STRING) {
String full = token.getData();
if (ins.peek(0).getType() == StyleTokenType.STAR) {
ins.jump();
if (peeker.peek(0).getType() == StyleTokenType.STAR) {
peeker.jump();
full += StyleSignatureBasic.STAR;
}
skipNewLines(ins);
if (ins.peek(0).getType() == StyleTokenType.OPEN_BRACKET) {
skipNewLines(peeker);
if (peeker.peek(0).getType() == StyleTokenType.OPEN_BRACKET) {
context = context.push(full);
ins.jump();
peeker.jump();
continue;
}
skipColon(ins);
skipColon(peeker);
if (token.getData().startsWith("--")) {
variables.learn(token.getData(), readValue(ins));
} else if (ins.peek(0).getType() == StyleTokenType.STRING) {
final String valueString = variables.value(readValue(ins));
variables.learn(token.getData(), readValue(peeker));
} else if (peeker.peek(0).getType() == StyleTokenType.STRING) {
final String valueString = variables.value(readValue(peeker));
final String keyString = token.getData();
final PName key = PName.getFromName(keyString, scheme);
if (key == null) {
Expand All @@ -135,21 +135,21 @@ public static Collection<Style> parse(BlocLines lines, AutomaticCounter counter)
} else if (token.getType() == StyleTokenType.AROBASE_MEDIA) {
scheme = StyleScheme.DARK;
continue;
} else if (token.getType() == StyleTokenType.COLON && ins.peek(0).getType() == StyleTokenType.STRING
&& ins.peek(1).getType() == StyleTokenType.OPEN_BRACKET) {
final String full = token.getData() + ins.peek(0).getData();
} else if (token.getType() == StyleTokenType.COLON && peeker.peek(0).getType() == StyleTokenType.STRING
&& peeker.peek(1).getType() == StyleTokenType.OPEN_BRACKET) {
final String full = token.getData() + peeker.peek(0).getData();
context = context.push(full);
ins.jump();
ins.jump();
peeker.jump();
peeker.jump();
continue;
} else if (token.getType() == StyleTokenType.COLON && ins.peek(0).getType() == StyleTokenType.STRING
&& ins.peek(1).getType() == StyleTokenType.STAR
&& ins.peek(2).getType() == StyleTokenType.OPEN_BRACKET) {
final String full = token.getData() + ins.peek(0).getData() + ins.peek(1).getData();
} else if (token.getType() == StyleTokenType.COLON && peeker.peek(0).getType() == StyleTokenType.STRING
&& peeker.peek(1).getType() == StyleTokenType.STAR
&& peeker.peek(2).getType() == StyleTokenType.OPEN_BRACKET) {
final String full = token.getData() + peeker.peek(0).getData() + peeker.peek(1).getData();
context = context.push(full);
ins.jump();
ins.jump();
ins.jump();
peeker.jump();
peeker.jump();
peeker.jump();
continue;
} else if (token.getType() == StyleTokenType.OPEN_BRACKET) {
throw new StyleParsingException("Invalid open bracket");
Expand All @@ -160,7 +160,7 @@ public static Collection<Style> parse(BlocLines lines, AutomaticCounter counter)
return Collections.unmodifiableList(result);
}

private static String readWithComma(Inspector<StyleToken> ins) {
private static String readWithComma(Peeker<StyleToken> ins) {
final StringBuilder result = new StringBuilder();
while (ins.peek(0) != null) {
final StyleToken current = ins.peek(0);
Expand All @@ -172,7 +172,7 @@ private static String readWithComma(Inspector<StyleToken> ins) {
return result.toString();
}

private static String readValue(Inspector<StyleToken> ins) throws StyleParsingException {
private static String readValue(Peeker<StyleToken> ins) throws StyleParsingException {
final StringBuilder result = new StringBuilder();
while (ins.peek(0) != null) {
final StyleToken current = ins.peek(0);
Expand Down Expand Up @@ -203,7 +203,7 @@ private static String readValue(Inspector<StyleToken> ins) throws StyleParsingEx
return result.toString();
}

private static void skipNewLines(Inspector<StyleToken> ins) {
private static void skipNewLines(Peeker<StyleToken> ins) {
while (true) {
final StyleToken token = ins.peek(0);
if (token == null || token.getType() != StyleTokenType.NEWLINE)
Expand All @@ -212,7 +212,7 @@ private static void skipNewLines(Inspector<StyleToken> ins) {
}
}

private static void skipColon(Inspector<StyleToken> ins) {
private static void skipColon(Peeker<StyleToken> ins) {
while (true) {
final StyleToken token = ins.peek(0);
if (token == null || token.getType() != StyleTokenType.COLON)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
*/
package net.sourceforge.plantuml.utils;

public interface Inspector<O> {
public interface Peeker<O> {
// ::remove file when __HAXE__
O peek(int ahead);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@

import java.util.List;

public abstract class InspectorUtils {
public abstract class PeekerUtils {
// ::remove file when __HAXE__

private InspectorUtils() {
private PeekerUtils() {

}

public static <O> Inspector<O> inspector(final List<O> list) {
return new Inspector<O>() {
public static <O> Peeker<O> peeker(final List<O> list) {
return new Peeker<O>() {

private int pos = 0;

Expand All @@ -61,6 +61,13 @@ public O peek(int ahead) {
public void jump() {
pos++;
}

@Override
public String toString() {
if (peek(0) == null)
return super.toString();
return peek(0).toString();
}
};
}
}
11 changes: 6 additions & 5 deletions src/net/sourceforge/plantuml/yaml/YamlDiagramFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,12 @@ public Diagram createSystem(UmlSource source, Previous previous, PreprocessingAr
list.add(line);
}
yaml = new SimpleYamlParser().parse(list);
// yaml = new YamlParser().parse(list);
} catch (Exception e) {
Logme.error(e);
}
final JsonDiagram result = new JsonDiagram(source, UmlDiagramType.YAML, yaml, highlighted, styleExtractor, preprocessing);
final JsonDiagram result = new JsonDiagram(source, UmlDiagramType.YAML, yaml, highlighted, styleExtractor,
preprocessing);
if (styleExtractor != null) {
try {
styleExtractor.applyStyles(result.getSkinParam());
Expand All @@ -96,16 +98,15 @@ public Diagram createSystem(UmlSource source, Previous previous, PreprocessingAr
}
final String title = styleExtractor.getTitle();
if (title != null)
result.setTitle(DisplayPositioned.single(Display.getWithNewlines(result.getPragma(), title), HorizontalAlignment.CENTER,
VerticalAlignment.CENTER));
result.setTitle(DisplayPositioned.single(Display.getWithNewlines(result.getPragma(), title),
HorizontalAlignment.CENTER, VerticalAlignment.CENTER));
}
return result;
}

@Override
public UmlDiagramType getUmlDiagramType() {
return UmlDiagramType.YAML;
}


}
88 changes: 67 additions & 21 deletions src/net/sourceforge/plantuml/yaml/parser/YamlBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public class YamlBuilder {

private final Deque<JsonValue> stack = new ArrayDeque<>();
private JsonValue current;
private JsonArray currentArray;
private String lastEmpiledKey;

public YamlBuilder() {
Expand All @@ -57,8 +58,15 @@ public void increaseIndentation() {

}

public void indentationDecrease() {
public void decreaseIndentation() {
stack.removeLast();
current = new JsonObject();
}

private void onNoKeyOnlyText(String value) {
final JsonObject pending = getPending();
pending.set(lastEmpiledKey, value);

}

public void onOnlyKey(String key) {
Expand All @@ -76,54 +84,92 @@ public void onKeyAndValue(String key, String value) {
}

private JsonObject getPending() {
final JsonObject pending;
if (stack.getLast() instanceof JsonArray) {
final JsonArray array = (JsonArray) stack.getLast();
pending = array.get(array.size() - 1).asObject();
} else {
pending = (JsonObject) stack.getLast();
return array.get(array.size() - 1).asObject();
}
return pending;
return (JsonObject) stack.getLast();
}

public void onListItemPlainDash() {
if (areWeJustStarting()) {
currentArray = new JsonArray();
stack.removeLast();
stack.addLast(currentArray);
current = new JsonObject();
currentArray.add(current);
stack.addLast(current);
return;
}

if (areWeBuildingAnArray())
decreaseIndentation();
else
increaseIndentation();

updateCurrentArray();
currentArray.add(current);
}

private boolean areWeJustStarting() {
if (stack.size() != 1)
return false;
final JsonValue first = stack.getLast();
if (first instanceof JsonObject == false)
return false;
return ((JsonObject) first).size() == 0;
}

private boolean areWeBuildingAnArray() {
if (stack.size() < 2)
return false;
if (currentArray.size() < 1)
return false;
return stack.getLast() == currentArray.get(currentArray.size() - 1);
}

public void onListItemKeyAndValue(String key, String value) {
final JsonArray array = getCurrentArray();
updateCurrentArray();
current = new JsonObject();
array.add(current);
currentArray.add(current);
((JsonObject) current).add(key, value);
}

public void onListItemOnlyKey(String key) {
final JsonArray array = getCurrentArray();
updateCurrentArray();
current = new JsonObject();
final JsonObject firstTableElement = new JsonObject();
array.add(firstTableElement);
currentArray.add(firstTableElement);
firstTableElement.add(key, current);
}

public void onListItemOnlyValue(String value) {
final JsonArray array = getCurrentArray();
array.add(value);
updateCurrentArray();
currentArray.add(value);

}

private JsonArray getCurrentArray() {
final JsonArray array;
private void updateCurrentArray() {
if (areWeJustStarting()) {
currentArray = new JsonArray();
stack.removeLast();
stack.addLast(currentArray);
return;
}
if (stack.getLast() instanceof JsonObject && ((JsonObject) stack.getLast()).size() == 0) {
array = new JsonArray();
currentArray = new JsonArray();
stack.removeLast();
final JsonObject parent = (JsonObject) stack.getLast();
parent.set(lastEmpiledKey, array);
stack.addLast(array);
parent.set(lastEmpiledKey, currentArray);
stack.addLast(currentArray);
} else if (stack.getLast() instanceof JsonArray) {
array = (JsonArray) stack.getLast();
currentArray = (JsonArray) stack.getLast();
} else
throw new IllegalStateException("wip");
return array;
}

public JsonObject getResult() {
return (JsonObject) stack.getFirst();
public JsonValue getResult() {
return stack.getFirst();
}

}
Loading

0 comments on commit 5fdc068

Please sign in to comment.