use of suite.primitive.Chars in project suite by stupidsing.
the class As method utf8encode.
public static Outlet<Bytes> utf8encode(Outlet<Chars> charsOutlet) {
Source<Chars> source = charsOutlet.source();
return Outlet.of(new Source<>() {
public Bytes source() {
Chars chars = source.source();
if (chars != null) {
BytesBuilder bb = new BytesBuilder();
for (int i = 0; i < chars.size(); i++) {
char ch = chars.get(i);
if (ch < 0x80)
bb.append((byte) ch);
else if (ch < 0x800) {
bb.append((byte) (0xC0 + ((ch >> 6) & 0x1F)));
bb.append((byte) (0x80 + ((ch >> 0) & 0x3F)));
} else if (ch < 0x10000) {
bb.append((byte) (0xE0 + ((ch >> 12) & 0x0F)));
bb.append((byte) (0x80 + ((ch >> 6) & 0x3F)));
bb.append((byte) (0x80 + ((ch >> 0) & 0x3F)));
} else {
bb.append((byte) (0xF0 + ((ch >> 18) & 0x07)));
bb.append((byte) (0x80 + ((ch >> 12) & 0x3F)));
bb.append((byte) (0x80 + ((ch >> 6) & 0x3F)));
bb.append((byte) (0x80 + ((ch >> 0) & 0x3F)));
}
}
return bb.toBytes();
} else
return null;
}
});
}
use of suite.primitive.Chars in project suite by stupidsing.
the class Ebnf method toFactorizeResult.
private FactorizeResult toFactorizeResult(char[] cs, int p0, int px, Ast ast) {
List<Ast> children = ast.children;
int size = children.size();
if (0 < size) {
List<FactorizeResult> frs = new ArrayList<>();
int pos = p0;
for (int i = 0; i < size; i++) {
Ast child = children.get(i);
int pos0 = pos;
pos = i != size - 1 ? child.end : px;
frs.add(toFactorizeResult(cs, pos0, pos, child));
}
return FactorizeResult.merge(ast.entity, frs);
} else {
Chars pre = Chars.of(cs, p0, ast.start);
Chars mid = Chars.of(cs, ast.start, ast.end);
Chars post = Chars.of(cs, ast.end, px);
return new FactorizeResult(pre, new FTerminal(mid), post);
}
}
use of suite.primitive.Chars in project suite by stupidsing.
the class FactorizeResult method rewrite.
public static FactorizeResult rewrite(FactorizeResult frfrom, FactorizeResult frto, FactorizeResult fr0) {
Generalizer generalizer = new Generalizer();
Iterate<Node> rewrite = n0 -> {
Node[] m = Suite.pattern(FTerminal.class.getName() + ":.0").match(n0);
Node n1 = m != null ? m[0] : null;
Node n2 = n1 instanceof Dict ? ((Dict) n1).map.get(Atom.of("chars")) : null;
Node n3 = n2 != null ? n2.finalNode() : null;
String s = n3 instanceof Str ? ((Str) n3).value : null;
boolean b = s != null && s.startsWith(ProverConstant.variablePrefix) && s.substring(1).matches("[0-9]*");
return b ? generalizer.generalize(Atom.of(s)) : n0;
};
Fun<FactorizeResult, Node> parse = fr -> rw.rewrite(rewrite, nodify.nodify(FNode.class, fr.node));
Node nodeFrom = parse.apply(frfrom);
Node nodeTo = parse.apply(frto);
FNode fn0 = fr0.node;
Node node0 = nodify.nodify(FNode.class, fn0);
Node nodex = rw.rewrite(nodeFrom, nodeTo, node0);
FNode fnx = nodify.unnodify(FNode.class, nodex);
return new FactorizeResult(fr0.pre, fnx, fr0.post);
}
use of suite.primitive.Chars in project suite by stupidsing.
the class RecursiveFactorizer method term.
private FactorizeResult term(Chars chars) {
Chars chars1 = chars.trim();
int p0 = reverser.reverse(chars.start);
int p1 = reverser.reverse(chars1.start);
int p2 = reverser.reverse(chars1.end);
int px = reverser.reverse(chars.end);
return new FactorizeResult(Chars.of(in.cs, p0, p1), new FTerminal(Chars.of(in.cs, p1, p2)), Chars.of(in.cs, p2, px));
}
use of suite.primitive.Chars in project suite by stupidsing.
the class RecursiveFactorizer method parse_.
private FactorizeResult parse_(Chars chars, int fromOp) {
Chars chars1 = chars.trim();
if (0 < chars1.size()) {
char first = chars1.get(0);
char last = chars1.get(-1);
for (int i = fromOp; i < operators.length; i++) {
Operator operator = operators[i];
Chars range = operator != TermOp.TUPLE_ ? chars : chars1;
Segment ops = ParseUtil.searchPosition(chars.cs, Segment.of(range.start, range.end), operator);
if (ops == null)
continue;
Chars left = Chars.of(chars.cs, chars.start, ops.start);
Chars middle = Chars.of(chars.cs, ops.start, ops.end);
Chars right = Chars.of(chars.cs, ops.end, chars.end);
Chars post = null;
int li, ri;
if (operator == TermOp.BRACES) {
if (chars1.end < ops.start || last != '}')
continue;
right = Chars.of(chars.cs, ops.end, chars1.end - 1);
post = Chars.of(chars.cs, chars1.end - 1, chars.end);
li = 0;
ri = 0;
} else {
if (operator == TermOp.TUPLE_)
if (left.isWhitespaces() || right.isWhitespaces())
continue;
boolean isLeftAssoc = operator.getAssoc() == Assoc.LEFT;
li = fromOp + (isLeftAssoc ? 0 : 1);
ri = fromOp + (isLeftAssoc ? 1 : 0);
}
List<FactorizeResult> list = new ArrayList<>(4);
list.add(parse_(left, li));
list.add(term(middle));
list.add(parse_(right, ri));
if (post != null)
list.add(term(post));
return FactorizeResult.merge(operator.toString(), list);
}
if (//
first == '(' && last == ')' || //
first == '[' && last == ']' || first == '`' && last == '`') {
Chars left = Chars.of(chars.cs, chars.start, chars1.start + 1);
Chars middle = Chars.of(chars.cs, chars1.start + 1, chars1.end - 1);
Chars right = Chars.of(chars.cs, chars1.end - 1, chars.end);
return FactorizeResult.merge("" + first, List.of(term(left), parse_(middle, 0), term(right)));
}
}
return term(chars);
}
Aggregations