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

114
LINES

< > BotCompany Repo | #1000642 // Find create statements in MySQL dump

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

Libraryless. Click here for Pure Java version (4005L/27K/73K).

!636
!sf
!multi-line strings
!1000635 // stringfunc {
!L<S>

!1000636 // compare strings with ==
!1000482 // quick cast
!JavaTok
!class _javax 19

interface StringFunc {
  S get(S s);
}

main {
  static O in;
  static L<S> markedStrings;
  static boolean debug;
  
  psvm {
    if (in == null) // may have been set by hotwire client
      in = "create table `bla`(hello int);";
    
    if (args.length != 0) in = loadSnippet(args[0]);
    
    S s = [[
      mysqltok input
      find "create" "table" ... ";"
    ]];
    L<S> ll = toLinesFullTrim(s);
    //System.out.println(ll);
    
    // remove comments
    ll = map(ll, stringfunc { s.replaceAll("//.*$", "").trim() });
    
    L<S> tok = null;
    
    for (S c : ll) {
      if (c == "mysqltok input")
        in = tok = mysqlTok((String) in);
      else if (c.startsWith("find")) {
        L<S> pat = JavaTok.split(c);
        pat.remove(0); pat.remove(0);
        L<S> inp = cast in;
        //System.out.println("inp: " + inp);
        //System.out.println("pat: " + pat);
        in = matchTokensList(inp, pat);
        System.out.println("Result: " + structure(in));
      }
    }
    
    if (in instanceof List) {
      List<int[]> list = cast in;
      markedStrings = getMarkedStrings(tok, list);
      markTokens(tok, list);
      in = JavaTok.join(tok);
    
      System.out.println("Markings found: " + size(list));
      for (int[] ij : list)
        System.out.println("Got: " + JavaTok.join(tok.subList(ij[0], ij[1]+1)));
    }
  }
  
  static List<String> getMarkedStrings(L<S> tok, L<int[]> markings) {
    new L<S> result;
    for (int i = 0; i < markings.size(); i++)
      result.add(JavaTok.join(tok.subList(markings.get(i)[0], markings.get(i)[1])));
    return result;
  }
  
  static int size(Object o) {
    return ((List) o).size();
  }
  
  static void markTokens(L<S> tok, L<int[]> marks) {
    System.out.println(tok.size());
    for (int i = 0; i < tok.size(); i++) {
      String t = tok.get(i);
      if (isStartMark(i, marks))
        t = "[[" + t;
      if (isEndMark(i, marks))
        t += "]]";
      tok.set(i, t);
    }
  }
  
  static boolean isStartMark(int i, L<int[]> marks) {
    for (int[] ij : marks)
      if (ij[0] == i)
        return true;
    return false;
  }
  
  static boolean isEndMark(int i, L<int[]> marks) {
    for (int[] ij : marks)
      if (ij[1] == i+1)
        return true;
    return false;
  }
  
  static S join(S glue, O o) {
    return join(glue, (L<S>) o);
  }
  
  !include #2000507 // original join function
 
  
  static L<S> map(L<S> l, StringFunc f) {
    new L<S> l2;
    for (S s : l)
      l2.add(f.get(s));
    return l2;
  }
}

Author comment

Began life as a copy of #1000634

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1000642
Snippet name: Find create statements in MySQL dump
Eternal ID of this version: #1000642/1
Text MD5: 0790405ded83f301a22849dfcc71e9ea
Transpilation MD5: 699f264f9ae09a20b0a708f48b7480e1
Author: stefan
Category:
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-08-22 12:38:33
Source code size: 2780 bytes / 114 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 710 / 1248
Referenced in: [show references]