Search in sources :

Example 6 with Chars

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;
        }
    });
}
Also used : Bytes(suite.primitive.Bytes) Chars(suite.primitive.Chars) BytesBuilder(suite.primitive.Bytes.BytesBuilder)

Example 7 with Chars

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);
    }
}
Also used : ArrayList(java.util.ArrayList) FTerminal(suite.node.parser.FactorizeResult.FTerminal) FactorizeResult(suite.node.parser.FactorizeResult) Chars(suite.primitive.Chars)

Example 8 with Chars

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);
}
Also used : Suite(suite.Suite) Singleton(suite.node.util.Singleton) Inspect(suite.inspect.Inspect) List_(suite.util.List_) To(suite.util.To) Chars(suite.primitive.Chars) Fun(suite.util.FunUtil.Fun) ArrayList(java.util.ArrayList) Node(suite.node.Node) CharsBuilder(suite.primitive.Chars.CharsBuilder) Iterate(suite.util.FunUtil.Iterate) List(java.util.List) ProverConstant(suite.lp.doer.ProverConstant) Rewrite(suite.node.util.Rewrite) Atom(suite.node.Atom) Nodify(suite.util.Nodify) Generalizer(suite.lp.doer.Generalizer) Dict(suite.node.Dict) Str(suite.node.Str) Str(suite.node.Str) Generalizer(suite.lp.doer.Generalizer) Dict(suite.node.Dict) Node(suite.node.Node)

Example 9 with Chars

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));
}
Also used : FTerminal(suite.node.parser.FactorizeResult.FTerminal) Chars(suite.primitive.Chars)

Example 10 with Chars

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);
}
Also used : Operator(suite.node.io.Operator) ArrayList(java.util.ArrayList) Chars(suite.primitive.Chars) Segment(suite.text.Segment)

Aggregations

Chars (suite.primitive.Chars)10 ArrayList (java.util.ArrayList)4 ProverConstant (suite.lp.doer.ProverConstant)2 Atom (suite.node.Atom)2 Dict (suite.node.Dict)2 Node (suite.node.Node)2 Str (suite.node.Str)2 FTerminal (suite.node.parser.FactorizeResult.FTerminal)2 Bytes (suite.primitive.Bytes)2 BytesBuilder (suite.primitive.Bytes.BytesBuilder)2 CharsBuilder (suite.primitive.Chars.CharsBuilder)2 Fun (suite.util.FunUtil.Fun)2 BufferedReader (java.io.BufferedReader)1 InputStreamReader (java.io.InputStreamReader)1 Paths (java.nio.file.Paths)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Entry (java.util.Map.Entry)1 Set (java.util.Set)1 Suite (suite.Suite)1