Advertisement
cd62131

Print Indentifier

Nov 8th, 2013
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 2.83 KB | None | 0 0
  1. import java.io.*;
  2.  
  3. public class Main {
  4.   private final PushbackReader in;
  5.   private final StringBuilder sb;
  6.  
  7.   public Main(PushbackReader in) {
  8.     this.in = in;
  9.     sb = new StringBuilder();
  10.   }
  11.  
  12.   private char read() {
  13.     int read_in = 0;
  14.     try {
  15.       read_in = in.read();
  16.     }
  17.     catch (Exception e) {
  18.       System.exit(1);
  19.     }
  20.     if (read_in == -1) return Character.MIN_VALUE;
  21.     return (char )read_in;
  22.   }
  23.  
  24.   private void pushBack(char c) {
  25.     try {
  26.       in.unread(c);
  27.     }
  28.     catch (Exception e) {
  29.       System.exit(1);
  30.     }
  31.   }
  32.  
  33.   public void parse() {
  34.     while (true) {
  35.       char c = read();
  36.       if (c == Character.MIN_VALUE) break;
  37.       if (isLineComment(c)) {
  38.         lineComment();
  39.         continue;
  40.       }
  41.       if (isBlockComment(c)) {
  42.         blockComment();
  43.         continue;
  44.       }
  45.       if (isBreakable(c)) {
  46.         sb.append("\n");
  47.         continue;
  48.       }
  49.       sb.append(c);
  50.     }
  51.   }
  52.  
  53.   private boolean isBlockComment(char c1) {
  54.     return isComment(c1, '*');
  55.   }
  56.  
  57.   private boolean isLineComment(char c1) {
  58.     return isComment(c1, '/');
  59.   }
  60.  
  61.   private boolean isComment(char c1, char comment_char) {
  62.     if (c1 != '/') return false;
  63.     char c2 = read();
  64.     if (c2 != comment_char) {
  65.       pushBack(c2);
  66.       return false;
  67.     }
  68.     sb.append(c1); // c1 is /
  69.     sb.append(c2); // c2 is / or *
  70.     return true;
  71.   }
  72.  
  73.   private boolean isBreakable(char c) {
  74.     if (Character.isUnicodeIdentifierStart(c) ||
  75.       Character.isUnicodeIdentifierPart(c)) return false;
  76.     return true;
  77.   }
  78.  
  79.   private void blockComment() {
  80.     char prev = Character.MAX_VALUE;
  81.     do {
  82.       char c1 = read();
  83.       if (c1 == Character.MIN_VALUE) break;
  84.       sb.append(c1);
  85.       if (prev == '*' && c1 == '/') break;
  86.       char c2 = read();
  87.       if (c2 == Character.MIN_VALUE) break;
  88.       sb.append(c2);
  89.       if (c1 == '*' && c2 == '/') break;
  90.       prev = c2;
  91.     } while (true);
  92.     sb.append("\n");
  93.   }
  94.  
  95.   private void lineComment() {
  96.     while (true) {
  97.       char c = read();
  98.       if (c == Character.MIN_VALUE || c == Character.LINE_SEPARATOR ||
  99.         c == '\r' || c == '\n') break;
  100.       sb.append(c);
  101.     }
  102.     sb.append("\n");
  103.   }
  104.  
  105.   @Override
  106.   public String toString() {
  107.     return sb.toString();
  108.   }
  109.  
  110.   public static void main(String[] args) {
  111.     PushbackReader in = null;
  112.     if (args.length > 0) {
  113.       File file = null;
  114.       try {
  115.         file = new File(args[0]);
  116.       }
  117.       catch (Exception e) {
  118.         System.exit(1);
  119.       }
  120.       try {
  121.         in = new PushbackReader(new FileReader(file));
  122.       }
  123.       catch (Exception e) {
  124.         System.exit(1);
  125.       }
  126.     }
  127.     else in = new PushbackReader(new InputStreamReader(System.in));
  128.     Main m = new Main(in);
  129.     m.parse();
  130.     System.out.println(m);
  131.   }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement