Add Beautifier as a stage

This commit is contained in:
aNNiMON 2023-10-05 14:08:56 +03:00 committed by Victor Melnik
parent c5810f0deb
commit fb6a8b8ea1
2 changed files with 13 additions and 15 deletions

View File

@ -2,7 +2,7 @@ package com.annimon.ownlang;
import com.annimon.ownlang.exceptions.OwnLangParserException;
import com.annimon.ownlang.exceptions.StoppedException;
import com.annimon.ownlang.parser.Beautifier;
import com.annimon.ownlang.parser.BeautifierStage;
import com.annimon.ownlang.parser.Token;
import com.annimon.ownlang.parser.ast.Statement;
import com.annimon.ownlang.parser.error.ParseErrorsFormatterStage;
@ -97,9 +97,10 @@ public final class Main {
}
}
if (options.beautifyMode) {
String input = new SourceLoaderStage()
String result = new SourceLoaderStage()
.then(new BeautifierStage())
.perform(new StagesDataMap(), options.toInputSource());
System.out.println(Beautifier.beautify(input));
System.out.println(result);
return;
}
run(options);

View File

@ -1,6 +1,8 @@
package com.annimon.ownlang.parser;
import com.annimon.ownlang.Console;
import com.annimon.ownlang.stages.Stage;
import com.annimon.ownlang.stages.StagesData;
import java.util.HashMap;
import java.util.Map;
@ -8,11 +10,7 @@ import java.util.Map;
*
* @author aNNiMON
*/
public final class Beautifier {
public static String beautify(String input) {
return new Beautifier(input).beautify();
}
public final class BeautifierStage implements Stage<String, String> {
private enum OperatorMode {
SPACES, RSPACES, TRIM, RTRIM, AS_SOURCE,
@ -77,21 +75,20 @@ public final class Beautifier {
OPERATORS.put(">>>", OperatorMode.SPACES);
}
private final String input;
private final int length;
private final StringBuilder beautifiedCode, buffer;
private String input;
private int length;
private StringBuilder beautifiedCode, buffer;
private int pos;
private int indentLevel;
public Beautifier(String input) {
@Override
public String perform(StagesData stagesData, String input) {
this.input = input;
length = input.length();
beautifiedCode = new StringBuilder();
buffer = new StringBuilder();
indentLevel = 0;
return beautify();
}
public String beautify() {