Advertisement
Ladies_Man

#COMPLR 5 Position, Fragment

Apr 19th, 2016
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.85 KB | None | 0 0
  1. public class Position implements Comparable<Position> {
  2.  
  3.     private String text;
  4.     private int line, pos, index;
  5.     private int Cp;
  6.  
  7.     Position(String text) {
  8.         this.text = text;
  9.         line = pos = 1;
  10.         index = 0;
  11.     }
  12.  
  13.     public int getLine() {
  14.         return line;
  15.     }
  16.  
  17.     public void setLine(int line) {
  18.         this.line = line;
  19.     }
  20.  
  21.     public int getPos() {
  22.         return pos;
  23.     }
  24.  
  25.     public void setPos(int pos) {
  26.         this.pos = pos;
  27.     }
  28.  
  29.     public int getIndex() {
  30.         return index;
  31.     }
  32.  
  33.     public void setIndex(int index) {
  34.         this.index = index;
  35.     }
  36.  
  37.     public int getCp() {
  38.         return (index == text.length()) ?
  39.                 -1 : Character.codePointAt(text.toCharArray(), index);
  40.     }
  41.  
  42.     public Boolean isWhiteSpace() {
  43.         return index != text.length() &&
  44.                 Character.isWhitespace(this.getCp());
  45.     }
  46.  
  47.     public Boolean isLetter() {
  48.         return index != text.length() &&
  49.                 Character.isLetter(this.getCp());
  50.     }
  51.  
  52.     public Boolean isDigit() {
  53.         return index != text.length() &&
  54.                 Character.isDigit(this.getCp());
  55.     }
  56.  
  57.     public Boolean isLetterOrDigit() {
  58.         return index != text.length() &&
  59.                 Character.isLetterOrDigit(this.getCp());
  60.     }
  61.  
  62.     public Boolean isNewLine() {
  63.         if (index == text.length())
  64.             return true;
  65.  
  66.         if ('\r' == text.charAt(index) && index + 1 < text.length())
  67.             return ('\n' == text.charAt(index + 1));
  68.  
  69.         return ('\n' == text.charAt(index));
  70.     }
  71.  
  72.  
  73.     public Position nxt() {
  74.         if (index < text.length()) {
  75.             if (isNewLine()) {
  76.                 if ('\r' == text.charAt(index)) {
  77.                     index++;
  78.                 }
  79.                 line++;
  80.                 pos = 1;
  81.             } else {
  82.                 if (Character.isHighSurrogate(text.charAt(index))) {
  83.                     index++;
  84.                 }
  85.                 pos++;
  86.             }
  87.             index++;
  88.         }
  89.         return this;
  90.     }
  91.  
  92.     @Override
  93.     public int compareTo(Position o) {
  94.         return Integer.compare(this.index, o.index);
  95.     }
  96.  
  97.     @Override
  98.     public String toString() {
  99.         return "(" + line + ", " + pos + ")";
  100.     }
  101.  
  102.     public Position copy() {
  103.         Position tmp = new Position(this.text);
  104.         tmp.setPos(this.pos);
  105.         tmp.setLine(this.line);
  106.         tmp.setIndex(this.index);
  107.  
  108.         return tmp;
  109.     }
  110. }
  111.  
  112.  
  113.  
  114.  
  115. Fragment:
  116. public class Fragment {
  117.     public final Position starting, following;
  118.  
  119.     Fragment(Position starting, Position following) {
  120.         this.starting = starting;
  121.         this.following = following;
  122.     }
  123.  
  124.     @Override
  125.     public String toString() {
  126.         return starting.toString() + "-" + following.toString();
  127.     }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement