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

88
LINES

< > BotCompany Repo | #1020318 // Record Declaration Bug [fixed]

JavaX source code (desktop) [tags: use-pretranspiled] - run with: x30.jar

Download Jar. Libraryless. Click here for Pure Java version (7319L/51K).

!7

static void tok_recordDecls(L<S> tok) {
  int i;
  bool re = false;
  while ((i = jfind(tok, "record <id>(")) >= 0) {
    int argsFrom = i+6, argsTo = findCodeTokens(tok, i, false, ")");
    int idx = findCodeTokens(tok, argsTo, false, "{");
    int j = findEndOfBracketPart(tok, idx);
    S name = tok.get(i+2);
    bool noEq = eq(get(tok, i-2), "noeq");
    if (noEq) tok.set(i-2, "");
    bool noToString = eq(get(tok, i-2), "noToString");
    if (noToString) tok.set(i-2, "");
    
    new StringBuilder buf;
    
    L<S> tArgs = subList(tok, argsFrom-1, argsTo);
    L<Pair<S>> args = tok_typesAndNamesOfParams(tArgs);
    L<S> contents = subList(tok, idx+1, j);
    
    for (Pair<S> p : args)
      buf.append("\n  " + jreplace(p.a, "...", "[]") + " " + p.b + ";");
      
    buf.append("\n  *() {}");
    buf.append("\n  *(" + joinWithComma(map(args, func(Pair<S> p) -> S {
      dropPrefix("new ", p.a) + " *" + p.b })) + ") {}");
      
    if (!noToString && !(jfind(contents, "toString {") >= 0 || jfind(contents, "toString()") >= 0))
      buf.append("\n  toString { ret " 
        + quote(name + "(")
        + " + "
        + join([[ + ", " + ]], secondOfPairs(args))
        + " + \")\"; }");
      
    if (!noEq) {
      //buf.append("\n  [stdEq]");
      buf.append(tok_genEqualsAndHashCode(name, args));
    }
    
    tok.set(idx, "{" + buf);
    tok.set(i, "class");
    clearTokens(tok, argsFrom-2, argsTo+1);
    reTok(tok, i, idx+1);
    tok_addFieldOrder(tok, i);
    set re;
  }
  if (re) reTok(tok);
}

static bool allVarNames_debug = true;

static L<S> allVarNames(L<S> tok) {
  print(join(tok));
  bool debug = allVarNames_debug;
  Map<Int, Int> bracketMap = getBracketMap(tok);
  new L<S> varNames;
  for (int i = 3; i < tok.size(); i += 2) {
    S t = tok.get(i);
    print("Got token: " + t);
    if (eqOneOf(t, "=", ";", ",")) {
      S v = tok.get(i-2);
      if (isIdentifier(v))
        varNames.add(v);
      if (eq(t, "=")) {
        i = scanToEndOfInitializer(tok, bracketMap, i)+1;
        assertTrue(odd(i));
      }
    } else if (eq(t, "<")) {
      i = findEndOfTypeArgs(tok, i)+1;
      if (debug)
        print("Skipped type args: " + struct(subList(tok, i)));
    } else if (eqOneOf(t, "{", "(")) {
      int j = findEndOfBracketPart(tok, i)-1; // skip function/class bodies
      if (debug) print("Skipping body " + (j-i)/2);
      i = j;
    }
  }
  return varNames;
}

p-exp {
  LS tok = javaTok([[record Definition(S word, S definition) {}]]);
  tok_recordDecls(tok);
  print(join(tok));
  assertTrue(join(tok).contains([[_fieldOrder = "word definition"]]));
  print("OK");
}

download  show line numbers  debug dex  old transpilations   

Travelled to 8 computer(s): bhatertpkbcr, cfunsshuasjs, gwrvuhgaqvyk, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1020318
Snippet name: Record Declaration Bug [fixed]
Eternal ID of this version: #1020318/8
Text MD5: e7ea6e35637e23084dc796e66c036936
Transpilation MD5: 41cde6c94b065aec540e7559eb19a93a
Author: stefan
Category: javax / transpiling
Type: JavaX source code (desktop)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-12-14 01:48:15
Source code size: 2720 bytes / 88 lines
Pitched / IR pitched: No / No
Views / Downloads: 252 / 676
Version history: 7 change(s)
Referenced in: [show references]