Advertisement
Guest User

Untitled

a guest
May 28th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.95 KB | None | 0 0
  1. else if (cur.isDecimalDigit()) {
  2.                         // Number: [0-9]{1,3}(.[0-9]{3})*
  3.                         String val = "";
  4.  
  5.                         //get number
  6.                         do {
  7.                             val += (cur.cp() == '.') ? "." : cur.cp() - '0';
  8.                             cur.incPosition();
  9.                         }
  10.                         while (cur.isDecimalDigit() || cur.cp() == '.');
  11.  
  12.                         //check dot position
  13.                         for (int i = val.length() - 1, c = 0; i >= 0; i--, c++) {
  14.                             /*
  15.                             . && c!=3 err
  16.                             . && c==3 c=0
  17.                             d && c==3 err
  18.                             d && c!=3 ok
  19.                              */
  20.                             if (val.charAt(i) == '.' && c == 3) {
  21.                                 c = -1; // c++ = 0
  22.  
  23.                             } else if (c == 3 || val.charAt(i) == '.') {
  24.                                 compiler.addMessage(
  25.                                         true,
  26.                                         start,
  27.                                         "wrong position of delimiters in number");
  28.                                 break;
  29.  
  30.                             }
  31.  
  32.                         }
  33.                         //remove not num chars
  34.                         val = val.replaceAll("\\D", "");
  35.  
  36.                         //calculate value of number
  37.                         long num = 0;
  38.  
  39.                         try {
  40.                             for (char c: val.toCharArray()) {
  41.                                 num = Math.addExact(Math.multiplyExact(num, 10), (c - '0'));
  42.                             }
  43.                         } catch (ArithmeticException e) {
  44.  
  45.                             compiler.addMessage(
  46.                                     true,
  47.                                     start,
  48.                                     "integral constant is too large");
  49.                         }
  50.                         val = "" + num;
  51.  
  52.                         //add dots
  53.                         String res = "";
  54.                         for (int i = val.length() - 1, c = 0; i >= 0; i--, c++) {
  55.                             if (c == 3) {
  56.                                 c = 0;
  57.                                 res = "." + res;
  58.                             }
  59.                             res = val.charAt(i) + res;
  60.                         }
  61.                         val = res;
  62.  
  63.                         if (cur.isLetter()) {
  64.                             compiler.addMessage(
  65.                                     true,
  66.                                     new Position(cur),
  67.                                     "delimiter required");
  68.                         }
  69.                         return new NumberToken(
  70.                                 val,
  71.                                 start,
  72.                                 new Position(cur));
  73.                     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement