JAL-1807 still testing
[jalviewjs.git] / unused / com / stevesoft / pat / StrPos.java
1 //\r
2 // This software is now distributed according to\r
3 // the Lesser Gnu Public License.  Please see\r
4 // http://www.gnu.org/copyleft/lesser.txt for\r
5 // the details.\r
6 //    -- Happy Computing!\r
7 //\r
8 package com.stevesoft.pat;\r
9 \r
10 /**\r
11  Shareware: package pat\r
12  <a href="copyright.html">Copyright 2001, Steven R. Brandt</a>\r
13  */\r
14 /**\r
15  * StrPos is used internally by regex to parse the regular expression.\r
16  */\r
17 public class StrPos\r
18 {\r
19   String s;\r
20 \r
21   int pos;\r
22 \r
23   /** Return the position in the string pointed to */\r
24   public int pos()\r
25   {\r
26     return pos;\r
27   }\r
28 \r
29   /** This contains the escape character, which is \ by default. */\r
30   public char esc = Pattern.ESC;\r
31 \r
32   char c;\r
33 \r
34   /** Returns the current, possibly escaped, character. */\r
35   public char thisChar()\r
36   {\r
37     return c;\r
38   }\r
39 \r
40   boolean dontMatch, eos;\r
41 \r
42   /** tell whether we are at end of string */\r
43   public boolean eos()\r
44   {\r
45     return eos;\r
46   }\r
47 \r
48   /** initialize a StrPos from another StrPos. */\r
49   public StrPos(StrPos sp)\r
50   {\r
51     dup(sp);\r
52   }\r
53 \r
54   /** copy a StrPos from sp to this. */\r
55   public void dup(StrPos sp)\r
56   {\r
57     s = sp.s;\r
58     pos = sp.pos;\r
59     c = sp.c;\r
60     dontMatch = sp.dontMatch;\r
61     eos = sp.eos;\r
62   }\r
63 \r
64   /**\r
65    * Initialize a StrPos by giving it a String, and a position within the\r
66    * String.\r
67    */\r
68   public StrPos(String s, int pos)\r
69   {\r
70     this.s = s;\r
71     this.pos = pos - 1;\r
72     inc();\r
73   }\r
74 \r
75   /**\r
76    * Advance the place where StrPos points within the String. Counts a backslash\r
77    * as part of the next character.\r
78    */\r
79   public StrPos inc()\r
80   {\r
81     pos++;\r
82     if (pos >= s.length())\r
83     {\r
84       eos = true;\r
85       return this;\r
86     }\r
87     eos = false;\r
88     c = s.charAt(pos);\r
89     if (c == esc && pos + 1 < s.length())\r
90     {\r
91       pos++;\r
92       c = s.charAt(pos);\r
93       if (c != esc)\r
94       {\r
95         dontMatch = true;\r
96       }\r
97       else\r
98       {\r
99         dontMatch = false;\r
100       }\r
101     }\r
102     else\r
103     {\r
104       dontMatch = false;\r
105     }\r
106     return this;\r
107   }\r
108 \r
109   /**\r
110    * Compare the (possibly escaped) character pointed to by StrPos. Return true\r
111    * if they are the same, but lways return if character pointed to is escaped.\r
112    */\r
113   public boolean match(char ch)\r
114   {\r
115     if (dontMatch || eos)\r
116     {\r
117       return false;\r
118     }\r
119     return c == ch;\r
120   }\r
121 \r
122   /** As match, but only matches if the character is escaped. */\r
123   public boolean escMatch(char ch)\r
124   {\r
125     if (!dontMatch || eos)\r
126     {\r
127       return false;\r
128     }\r
129     return c == ch;\r
130   }\r
131 \r
132   /**\r
133    * Returns true if the current character is escaped (preceeded by "\").\r
134    */\r
135   public boolean escaped()\r
136   {\r
137     return dontMatch;\r
138   }\r
139 \r
140   /**\r
141    * Increment the string pointer by each character in\r
142    * \r
143    * <pre>\r
144    * st\r
145    * </pre>\r
146    * \r
147    * that matches a non-escaped character.\r
148    */\r
149   public boolean incMatch(String st)\r
150   {\r
151     StrPos sp = new StrPos(this);\r
152     int i;\r
153     for (i = 0; i < st.length(); i++)\r
154     {\r
155       if (!sp.match(st.charAt(i)))\r
156       {\r
157         return false;\r
158       }\r
159       sp.inc();\r
160     }\r
161     dup(sp);\r
162     return true;\r
163   }\r
164 \r
165   /** Read in an integer. */\r
166   public patInt getPatInt()\r
167   {\r
168     if (incMatch("inf"))\r
169     {\r
170       return new patInf();\r
171     }\r
172     int i, cnt = 0;\r
173     StrPos sp = new StrPos(this);\r
174     for (i = 0; !sp.eos && sp.c >= '0' && sp.c <= '9'; i++)\r
175     {\r
176       cnt = 10 * cnt + sp.c - '0';\r
177       sp.inc();\r
178     }\r
179     if (i == 0)\r
180     {\r
181       return null;\r
182     }\r
183     dup(sp);\r
184     return new patInt(cnt);\r
185   }\r
186 \r
187   /** get the string that we are processing. */\r
188   public String getString()\r
189   {\r
190     return s;\r
191   }\r
192 };\r