package cn.infomany.test0;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;
/**
* @description: 范围访问器
* @author: zhanjinbing
* @data: 2020-03-03 15:17
*/
public class RangesVisitor extends TestBaseVisitor {
@Override
public Object visitAdd(TestParser.AddContext ctx) {
Set result = new HashSet<>();
Set result0 = (Set) visit(ctx.range(0));
Set result1 = (Set) visit(ctx.range(1));
result.addAll(result0);
result.addAll(result1);
return result;
}
@Override
public Object visitSE(TestParser.SEContext ctx) {
int speed = 1;
int start = (int) visit(ctx.start());
int end = (int) visit(ctx.end());
return visitSSE(start, speed, end,
ctx.start.getType() == TestLexer.MEDIUMBRACKETLEFT,
ctx.stop.getType() == TestLexer.MEDIUMBRACKETRIGHT);
}
private Set visitSSE(int start, int speed, int end, boolean hasStart, boolean hasEnd) {
Set result = new HashSet<>();
for (int i = start; i < end; i += speed) {
result.add(i);
}
if (!hasStart) {
result.remove(start);
}
if (hasEnd) {
result.add(end);
}
return result;
}
@Override
public Object visitSimple(TestParser.SimpleContext ctx) {
return visit(ctx.range());
}
@Override
public Object visitS(TestParser.SContext ctx) {
Set results = new HashSet<>();
if (ctx.start.getType() == TestLexer.SMALLBRACKETLEFT
|| ctx.stop.getType() == TestLexer.SMALLBRACKETRIGHT) {
return results;
}
results.add((Integer) visit(ctx.start()));
return results;
}
@Override
public Object visitSSE(TestParser.SSEContext ctx) {
Object speedObj = visit(ctx.speed());
int start = (int) visit(ctx.start());
int end = (int) visit(ctx.end());
Set results;
if (speedObj instanceof String) {
results = visitSSE(start, 1, end,
ctx.start.getType() == TestLexer.MEDIUMBRACKETLEFT,
ctx.stop.getType() == TestLexer.MEDIUMBRACKETRIGHT);
final Integer tmpInt = Integer.valueOf(((String) speedObj).substring(1));
results = results.stream().filter(it -> it % tmpInt == 0).collect(Collectors.toSet());
} else if (speedObj instanceof Integer) {
results = visitSSE(start, (Integer) speedObj, end,
ctx.start.getType() == TestLexer.MEDIUMBRACKETLEFT,
ctx.stop.getType() == TestLexer.MEDIUMBRACKETRIGHT);
} else {
throw new RuntimeException("[speed]格式有误");
}
return results;
}
@Override
public Object visitAddSub(TestParser.AddSubContext ctx) {
Set result = new HashSet<>();
Set result0 = (Set) visit(ctx.range(0));
Set result1 = (Set) visit(ctx.range(1));
result.addAll(result0);
if (ctx.op.getType() == TestLexer.ADDOPTION) {
result.addAll(result1);
} else {
result.removeAll(result1);
}
return result;
}
@Override
public Object visitAssoc(TestParser.AssocContext ctx) {
Set result = new HashSet<>();
Set result0 = (Set) visit(ctx.range(0));
Set result1 = (Set) visit(ctx.range(1));
result0.stream().forEach(it -> {
if (result1.contains(it)) {
result.add(it);
}
});
return result;
}
@Override
public Object visitStart(TestParser.StartContext ctx) {
return Integer.valueOf(ctx.INT().getText());
}
@Override
public Object visitSpeed(TestParser.SpeedContext ctx) {
// 检查是不是单纯的int
String reg = "\\d+";
if (ctx.getText().matches(reg)) {
return Integer.valueOf(ctx.INT().getText());
}
return ctx.getText();
}
@Override
public Object visitEnd(TestParser.EndContext ctx) {
return Integer.valueOf(ctx.INT().getText());
}
}