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

53
LINES

< > BotCompany Repo | #1000303 // tokenizeJava test

JavaX source code - run with: x30.jar

!636

!629 // standard functions
!1000302 // class Java20

main {
  psvm {
    String src = takeInput(args, "class main { String s; }");
    src = src.replace("\r\n", "\n");
    String[] tokens = tokenizeJava(src);
    for (String t : tokens)
      System.out.println(quote(t));
    String joined = join(tokens);
    saveTextFile("output/joined.txt", joined);
    if (!src.equals(joined)) {
      for (int i = 0; i < joined.length(); i++)
        if (src.charAt(i) != joined.charAt(i)) {
          System.out.println("Char diff at " + i + ": " + (int) + src.charAt(i) + " vs " + (int) joined.charAt(i));
          break;
        }
      throw new RuntimeException("Join fail");
    }
  }
  
  static String[] tokenizeJava(String src) {
    Java20 lex = new Java20();
    LineNumberReader source = new LineNumberReader(new StringReader(src));
    List<String> tokens = new ArrayList<String>();
    try {
      for (Object a; (a = lex.grab(source)) != lex.$;)
        tokens.add(lex.word());
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
    return tokens.toArray(new String[tokens.size()]);
  }
  
  static String takeInput(String[] args, String def) tex {
    if (args.length != 0) return loadSnippet(args[0]);
    return loadTextFile("input/input.txt", def);
  }
  
  static String join(String[] array) {
    StringBuilder buf = new StringBuilder();
    for (String s : array)
      buf.append(s);
    return buf.toString();
  }
  
  public static String quote(String s) {
    if (s == null) return "null";
    return "\"" + s.replace("\\", "\\\\").replace("\"", "\\\"").replace("\r", "\\r").replace("\n", "\\n") + "\"";
  }
}

Author comment

Began life as a copy of #648

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: #1000303
Snippet name: tokenizeJava test
Eternal ID of this version: #1000303/1
Text MD5: e9a82d5c200ed33dbc706fd7bf271ad9
Author: stefan
Category: javax
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-06-27 09:20:09
Source code size: 1703 bytes / 53 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 723 / 626
Referenced in: [show references]