Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

43
LINES

< > BotCompany Repo | #1014202 // parseDoubleArrowTreeCI

JavaX fragment (include)

static S parseDoubleArrowTreeCI_defaultKey = "<default>";

static Map<S, O> parseDoubleArrowTreeCI(S text) {
  L<S> lines = withoutEmpties(rtrimAll(lines(text)));
  if (empty(lines)) null;

  new L<Map<S, O>> nodes;
  new L<Int> indents; // indent of children
  
  Map<S, O> tree = ciMap();
  nodes.add(tree);
  indents.add(countIndent(first(lines)));
  
  for i over lines: {
    S s = lines.get(i);
    int indent = countIndent(s);
    
    while (indent < last(indents)) {
      removeLast(nodes);
      removeLast(indents);
    }
    
    L<S> x = splitAtDoubleArrow(s);
    if (l(x) == 2) { // leaf (or tree with default)
      S next = get(lines, i+1);
      if (next != null && countIndent(next) > indent) { // tree with default
        Map<S, O> newTree = ciMap();
        last(nodes).put(first(x), newTree);
        nodes.add(newTree);
        indents.add(indent+1);
        newTree.put(parseDoubleArrowTreeCI_defaultKey, second(x));
      } else // leaf
        last(nodes).put(first(x), second(x));
    } else {
      Map<S, O> newTree = ciMap();
      last(nodes).put(trim(s), newTree);
      nodes.add(newTree);
      indents.add(indent+1);
    }
  }
  
  ret tree;
}

Author comment

Began life as a copy of #1014190

download  show line numbers  debug dex  old transpilations   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1014202
Snippet name: parseDoubleArrowTreeCI
Eternal ID of this version: #1014202/10
Text MD5: 63720218c82f3b2dce5711c3fa3c29e0
Author: stefan
Category: javax / a.i.
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-04-16 00:16:03
Source code size: 1222 bytes / 43 lines
Pitched / IR pitched: No / No
Views / Downloads: 291 / 322
Version history: 9 change(s)
Referenced in: [show references]