Advertisement
Ladies_Man

#COMPLR 4 Position, Fragment, Message

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