better exception handling

proper floating numbers support
This commit is contained in:
rusefillc 2021-11-25 20:17:12 -05:00
parent 0605c618fd
commit 917c866235
3 changed files with 33 additions and 13 deletions

View File

@ -1,11 +1,6 @@
package neoe.formatter.lua;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
@ -156,14 +151,14 @@ public class LuaFormatter {
StringBuilder sb;
LuaTokens tokens;
public String format(String txt, Env env) throws Exception {
public String format(String txt, Env env) {
sb = new StringBuilder();
tokens = new LuaTokens(txt);
loop(null, null, null, env);
return sb.toString();
}
private void loop(LuaTokenType preType, String until, LuaTokenType operator, Env env) throws Exception {
private void loop(LuaTokenType preType, String until, LuaTokenType operator, Env env) {
env.lastType = preType;
while (true) {
LuaTokens.TypeAndValue tt = tokens.next();
@ -176,8 +171,12 @@ public class LuaFormatter {
break;
if (DEBUG) {
debug.write(String.format("t:%s,v:%s\n", type, token));
debug.flush();
try {
debug.write(String.format("t:%s,v:%s\n", type, token));
debug.flush();
} catch (IOException e) {
System.out.println("Unexpected e");
}
}
addToken(env, tt);
@ -201,7 +200,7 @@ public class LuaFormatter {
Stack stack = new Stack();
}
private void addToken(Env env, LuaTokens.TypeAndValue tt) throws Exception {
private void addToken(Env env, LuaTokens.TypeAndValue tt) {
LuaTokenType type = tt.getType();
String token = tt.getValue();
env.forcedChangeLine = false;
@ -421,6 +420,7 @@ public class LuaFormatter {
if (currentToken != null) {
if (")".equals(currentToken.getValue()) ||
"(".equals(currentToken.getValue()) ||
".".equals(currentToken.getValue()) ||
",".equals(currentToken.getValue()) ||
"[".equals(currentToken.getValue()) ||
"]".equals(currentToken.getValue())

View File

@ -0,0 +1,20 @@
package neoe.formatter.lua;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class FloatNumberTest {
@Test
public void test() {
String code = "function onCanRx()\n" +
"id = 0.1\n" +
"end";
String formatted = new LuaFormatter().format(code, new LuaFormatter.Env());
assertEquals("function onCanRx()\n" +
"\tid = 0.1\n" +
"end\n" +
"\n", formatted);
}
}

View File

@ -6,7 +6,7 @@ import static org.junit.Assert.assertEquals;
public class LuaFormatterTest {
@Test
public void testMethodArgumentsFormatting() throws Exception {
public void testMethodArgumentsFormatting() {
String code = "function onCanRx(bus, id)\n" +
"end";
String formatted = new LuaFormatter().format(code, new LuaFormatter.Env());
@ -16,7 +16,7 @@ public class LuaFormatterTest {
}
@Test
public void testArrayFormatting() throws Exception {
public void testArrayFormatting() {
String code = "function onCanRx()\n" +
" canState = data[1]\n" +
"end";