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

1  
!636
2  
!629 // standard functions
3  
!1000302 // class Java20
4  
5  
main {
6  
  psvm {
7  
    String src = takeInput(args, "class main { String s; }");
8  
    src = src.replace("\r\n", "\n");
9  
    String[] tokens = tokenizeJava(src);
10  
    for (String t : tokens)
11  
      System.out.println(quote(t));
12  
    String joined = join(tokens);
13  
    saveTextFile("output/joined.txt", joined);
14  
    if (!src.equals(joined)) {
15  
      for (int i = 0; i < joined.length(); i++)
16  
        if (src.charAt(i) != joined.charAt(i)) {
17  
          System.out.println("Char diff at " + i + ": " + (int) + src.charAt(i) + " vs " + (int) joined.charAt(i));
18  
          break;
19  
        }
20  
      throw new RuntimeException("Join fail");
21  
    }
22  
  }
23  
  
24  
  static String[] tokenizeJava(String src) {
25  
    Java20 lex = new Java20();
26  
    LineNumberReader source = new LineNumberReader(new StringReader(src));
27  
    List<String> tokens = new ArrayList<String>();
28  
    try {
29  
      for (Object a; (a = lex.grab(source)) != lex.$;)
30  
        tokens.add(lex.word());
31  
    } catch (Exception e) {
32  
      throw new RuntimeException(e);
33  
    }
34  
    return tokens.toArray(new String[tokens.size()]);
35  
  }
36  
  
37  
  static String takeInput(String[] args, String def) tex {
38  
    if (args.length != 0) return loadSnippet(args[0]);
39  
    return loadTextFile("input/input.txt", def);
40  
  }
41  
  
42  
  static String join(String[] array) {
43  
    StringBuilder buf = new StringBuilder();
44  
    for (String s : array)
45  
      buf.append(s);
46  
    return buf.toString();
47  
  }
48  
  
49  
  public static String quote(String s) {
50  
    if (s == null) return "null";
51  
    return "\"" + s.replace("\\", "\\\\").replace("\"", "\\\"").replace("\r", "\\r").replace("\n", "\\n") + "\"";
52  
  }
53  
}

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: 724 / 627
Referenced in: [show references]