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

171
LINES

< > BotCompany Repo | #1002529 // class Explain (parser explainer)

JavaX fragment (include)

// parser explainer
static class Explain {
  static L<S> primitiveClasses = litlist("quoted", "identifier", "any", "int");
  
  O parseResult;
  L<S> tok;
  L e;
  new L<Explain> subs;
  
  // XXX - applies to full parse always
  L<S> fullMatchClasses() { ret (L<S>) call(parseResult, "fullMatchClasses"); }
  
  static boolean debug;
  
  *(O *parseResult, L *e) {
    tok = (L) get(parseResult, "tok");
    _makeSubExplains();
  }
  
  void _makeSubExplains() {
    for (int i = 4; i < l(e); i++) {
      L sub = cast get(e, i);
      int t1 = (int) get(sub, 0);
      int t2 = (int) get(sub, 1);
      S className = getString(sub, 2);
      L subE = sub;
      if (debug)
        print("subE first = " + sfu(subE));
      if (!primitiveClasses.contains(className))
        subE = (L) call(parseResult, "explainFull", t1, t2, className);
      if (debug)
        printF("Explaining for " + className() + ": * * * => *", t1, t2, className, subE);
      if (subE == null)
        subs.add(null);
      else
        subs.add(new Explain(parseResult, subE));
    }
  }
  
  S className() {
    ret getString(e, 2);
  }
  
  int fromToken() {
    ret (int) get(e, 0);
  }
  
  int toToken() {
    ret (int) get(e, 1);
  }
  
  // return tokens, padded with empty non-code tokens first and last
  // to make actual CNC
  L<S> tok() {
    ret concatLists(
      litlist(""),
      subList(tok, fromToken(), toToken()-1),
      litlist(""));
  }
  
  S string() {
    ret join(subList(tok, fromToken(), toToken()-1));
  }
  
  boolean containsToken(S t) {
    ret main containsToken(tok(), t);
  }
  
  void findAll(S className, L<S> out) {
    if (eq(className, className()))
      out.add(string());
    else // << this is new - don't recurse
      for (Explain e : subs)
        if (e != null)
          e.findAll(className, out);
  }
  
  L<S> findAll(S className) {
    new L<S> l;
    findAll(className, l);
    ret l;
  }
  
  // short for findFirst
  Explain find(S className) {
    ret findFirst(className);
  }
  
  Explain findFirst(S className) {
    if (eq(className, className()))
      ret this;
    ret findFirstSub(className);
  }
  
  // find class, but exclude myself
  Explain findFirstSub(S className) {
    for (Explain e : subs)
      if (e != null) {
        Explain result = e.findFirst(className);
        if (result != null) ret result;
      }
    ret null;
  }
  
  boolean is(S className) {
    ret eq(className(), className);
  }
  
  boolean has(S className) {
    ret findFirst(className) != null;
  }
  
  boolean hasSub(S className) {
    ret findFirstSub(className) != null;
  }
  
  void findAll2(S className, L<Explain> out) {
    if (is(className))
      out.add(this);
    for (Explain e : subs)
      if (e != null)
        e.findAll2(className, out);
  }
  
  L<Explain> findAll2(S className) {
    new L<Explain> l;
    findAll2(className, l);
    ret l;
  }
  
  // short for pruneSubs
  Explain prune(S className) {
    ret pruneSubs(className);
  }
  
  // returns self after pruning
  Explain pruneSubs(S className) {
    for (int i = 0; i < l(subs); ) {
      Explain e = sub(i);
      if (e == null) ++i;
      else if (eq(e.className(), className))
        subs.remove(i);
      else {
        e.pruneSubs(className);
        ++i;
      }
    }
    ret this;
  }
  
  Explain sub(int i) { ret get(subs, i); }
  S str(int i) {
    // ret sub(i).string(); // sub might be null
    L sub = cast get(e, 4+i);
    int t1 = (int) get(sub, 0);
    int t2 = (int) get(sub, 1);
    ret join(subList(tok, t1, t2-1));
  }
  
  L<Explain> subs() { ret subs; }
  
  bool singleEqualChild() {
    if (l(subs) != 1 || first(subs) == null) false;
    Explain e = first(subs);
    ret fromToken() == e.fromToken() && toToken() == e.toToken();
  }
  
  S prettierAnalysis() {
    ret (S) call(parseResult, "prettierAnalysis");
  }
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1002529
Snippet name: class Explain (parser explainer)
Eternal ID of this version: #1002529/15
Text MD5: b38407010ad06a76a90040b6842a0e1d
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-11-14 21:45:03
Source code size: 4028 bytes / 171 lines
Pitched / IR pitched: No / No
Views / Downloads: 727 / 3550
Version history: 14 change(s)
Referenced in: [show references]