OSDN Git Service

* pa64-hpux.h (LIB_SPEC): Fix library specification used with GNU ld.
[pf3gnuchains/gcc-fork.git] / libjava / gnu / regexp / RESyntax.java
1 /* gnu/regexp/RESyntax.java
2    Copyright (C) 1998-2002, 2004 Free Software Foundation, Inc.
3
4 This file is part of GNU Classpath.
5
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING.  If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
20
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library.  Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
25
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module.  An independent module is a module which is not derived from
33 or based on this library.  If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so.  If you do not wish to do so, delete this
36 exception statement from your version. */
37
38
39 package gnu.regexp;
40 import java.io.Serializable;
41 import java.util.BitSet;
42
43 /**
44  * An RESyntax specifies the way a regular expression will be compiled.
45  * This class provides a number of predefined useful constants for
46  * emulating popular regular expression syntaxes.  Additionally the
47  * user may construct his or her own syntax, using any combination of the
48  * syntax bit constants.  The syntax is an optional argument to any of the
49  * matching methods on class RE.
50  *
51  * @author <A HREF="mailto:wes@cacas.org">Wes Biggs</A>
52  */
53
54 public final class RESyntax implements Serializable {
55     static final String DEFAULT_LINE_SEPARATOR = System.getProperty("line.separator");
56
57     private static final String SYNTAX_IS_FINAL = RE.getLocalizedMessage("syntax.final");
58
59     private BitSet bits;
60
61     // true for the constant defined syntaxes
62     private boolean isFinal = false;
63
64     private String lineSeparator = DEFAULT_LINE_SEPARATOR;
65
66   // Values for constants are bit indexes
67
68   /**
69    * Syntax bit. Backslash is an escape character in lists.
70    */
71   public static final int RE_BACKSLASH_ESCAPE_IN_LISTS =  0;
72
73   /**
74    * Syntax bit. Use \? instead of ? and \+ instead of +.
75    */
76   public static final int RE_BK_PLUS_QM                =  1;
77
78   /**
79    * Syntax bit. POSIX character classes ([:...:]) in lists are allowed.
80    */
81   public static final int RE_CHAR_CLASSES              =  2;
82
83   /**
84    * Syntax bit. ^ and $ are special everywhere.
85    * <B>Not implemented.</B>
86    */
87   public static final int RE_CONTEXT_INDEP_ANCHORS     =  3; 
88
89   /**
90    * Syntax bit. Repetition operators are only special in valid positions.
91    * <B>Not implemented.</B>
92    */
93   public static final int RE_CONTEXT_INDEP_OPS         =  4; 
94
95   /**
96    * Syntax bit. Repetition and alternation operators are invalid
97    * at start and end of pattern and other places. 
98    * <B>Not implemented</B>.
99    */
100   public static final int RE_CONTEXT_INVALID_OPS       =  5; 
101
102   /**
103    * Syntax bit. Match-any-character operator (.) matches a newline.
104    */
105   public static final int RE_DOT_NEWLINE               =  6;
106
107   /**
108    * Syntax bit. Match-any-character operator (.) does not match a null.
109    */
110   public static final int RE_DOT_NOT_NULL              =  7;
111
112   /**
113    * Syntax bit. Intervals ({x}, {x,}, {x,y}) are allowed.
114    */
115   public static final int RE_INTERVALS                 =  8;
116
117   /**
118    * Syntax bit. No alternation (|), match one-or-more (+), or 
119    * match zero-or-one (?) operators.
120    */
121   public static final int RE_LIMITED_OPS               =  9;
122
123   /**
124    * Syntax bit. Newline is an alternation operator.
125    */
126   public static final int RE_NEWLINE_ALT               = 10; // impl.
127
128   /**
129    * Syntax bit. Intervals use { } instead of \{ \}
130    */
131   public static final int RE_NO_BK_BRACES              = 11; 
132
133   /**
134    * Syntax bit. Grouping uses ( ) instead of \( \).
135    */
136   public static final int RE_NO_BK_PARENS              = 12;
137
138   /**
139    * Syntax bit. Backreferences not allowed.
140    */
141   public static final int RE_NO_BK_REFS                = 13;
142
143   /**
144    * Syntax bit. Alternation uses | instead of \|
145    */
146   public static final int RE_NO_BK_VBAR                = 14;
147
148   /**
149    * Syntax bit. <B>Not implemented</B>.
150    */
151   public static final int RE_NO_EMPTY_RANGES           = 15;
152
153   /**
154    * Syntax bit. An unmatched right parenthesis (')' or '\)', depending
155    * on RE_NO_BK_PARENS) will throw an exception when compiling.
156    */
157   public static final int RE_UNMATCHED_RIGHT_PAREN_ORD = 16;
158
159   /**
160    * Syntax bit. <B>Not implemented.</B>
161    */
162   public static final int RE_HAT_LISTS_NOT_NEWLINE     = 17;
163
164   /**
165    * Syntax bit.  Stingy matching is allowed (+?, *?, ??, {x,y}?).
166    */
167   public static final int RE_STINGY_OPS                = 18;
168
169   /**
170    * Syntax bit. Allow character class escapes (\d, \D, \s, \S, \w, \W).
171    */
172   public static final int RE_CHAR_CLASS_ESCAPES        = 19;
173
174   /**
175    * Syntax bit. Allow use of (?:xxx) grouping (subexpression is not saved).
176    */
177   public static final int RE_PURE_GROUPING             = 20;
178
179   /**
180    * Syntax bit. Allow use of (?=xxx) and (?!xxx) apply the subexpression
181    * to the text following the current position without consuming that text.
182    */
183   public static final int RE_LOOKAHEAD                 = 21;
184
185   /**
186    * Syntax bit. Allow beginning- and end-of-string anchors (\A, \Z).
187    */
188   public static final int RE_STRING_ANCHORS            = 22;
189
190   /**
191    * Syntax bit. Allow embedded comments, (?#comment), as in Perl5.
192    */
193   public static final int RE_COMMENTS                  = 23;
194
195   /**
196    * Syntax bit. Allow character class escapes within lists, as in Perl5.
197    */
198   public static final int RE_CHAR_CLASS_ESC_IN_LISTS   = 24;
199
200   private static final int BIT_TOTAL                   = 25;
201
202   /**
203    * Predefined syntax.
204    * Emulates regular expression support in the awk utility.
205    */
206   public static final RESyntax RE_SYNTAX_AWK;
207
208   /**
209    * Predefined syntax.
210    * Emulates regular expression support in the ed utility.
211    */
212   public static final RESyntax RE_SYNTAX_ED;
213
214   /**
215    * Predefined syntax.
216    * Emulates regular expression support in the egrep utility.
217    */
218   public static final RESyntax RE_SYNTAX_EGREP;
219
220   /**
221    * Predefined syntax.
222    * Emulates regular expression support in the GNU Emacs editor.
223    */
224   public static final RESyntax RE_SYNTAX_EMACS;
225
226   /**
227    * Predefined syntax.
228    * Emulates regular expression support in the grep utility.
229    */
230   public static final RESyntax RE_SYNTAX_GREP;
231
232   /**
233    * Predefined syntax.
234    * Emulates regular expression support in the POSIX awk specification.
235    */
236   public static final RESyntax RE_SYNTAX_POSIX_AWK;
237
238   /**
239    * Predefined syntax.
240    * Emulates POSIX basic regular expression support.
241    */
242   public static final RESyntax RE_SYNTAX_POSIX_BASIC;
243
244   /**
245    * Predefined syntax.
246    * Emulates regular expression support in the POSIX egrep specification.
247    */
248   public static final RESyntax RE_SYNTAX_POSIX_EGREP;
249
250   /**
251    * Predefined syntax.
252    * Emulates POSIX extended regular expression support.
253    */
254   public static final RESyntax RE_SYNTAX_POSIX_EXTENDED;
255
256   /**
257    * Predefined syntax.
258    * Emulates POSIX basic minimal regular expressions.
259    */
260   public static final RESyntax RE_SYNTAX_POSIX_MINIMAL_BASIC;
261
262   /**
263    * Predefined syntax.
264    * Emulates POSIX extended minimal regular expressions.
265    */
266   public static final RESyntax RE_SYNTAX_POSIX_MINIMAL_EXTENDED;
267
268   /**
269    * Predefined syntax.
270    * Emulates regular expression support in the sed utility.
271    */
272   public static final RESyntax RE_SYNTAX_SED;
273
274   /**
275    * Predefined syntax.
276    * Emulates regular expression support in Larry Wall's perl, version 4,
277    */
278   public static final RESyntax RE_SYNTAX_PERL4;
279
280   /**
281    * Predefined syntax.
282    * Emulates regular expression support in Larry Wall's perl, version 4,
283    * using single line mode (/s modifier).
284    */
285   public static final RESyntax RE_SYNTAX_PERL4_S; // single line mode (/s)
286
287   /**
288    * Predefined syntax.
289    * Emulates regular expression support in Larry Wall's perl, version 5.
290    */
291   public static final RESyntax RE_SYNTAX_PERL5;  
292
293   /**
294    * Predefined syntax.
295    * Emulates regular expression support in Larry Wall's perl, version 5,
296    * using single line mode (/s modifier).
297    */
298   public static final RESyntax RE_SYNTAX_PERL5_S;
299
300     /**
301      * Predefined syntax.
302      * Emulates regular expression support in Java 1.4's java.util.regex
303      * package.
304      */
305     public static final RESyntax RE_SYNTAX_JAVA_1_4;
306
307   static {
308       // Define syntaxes
309       
310       RE_SYNTAX_EMACS = new RESyntax().makeFinal();
311       
312       RESyntax RE_SYNTAX_POSIX_COMMON = new RESyntax()
313           .set(RE_CHAR_CLASSES)
314           .set(RE_DOT_NEWLINE)
315           .set(RE_DOT_NOT_NULL)
316           .set(RE_INTERVALS)
317           .set(RE_NO_EMPTY_RANGES)
318           .makeFinal();
319       
320       RE_SYNTAX_POSIX_BASIC = new RESyntax(RE_SYNTAX_POSIX_COMMON)
321           .set(RE_BK_PLUS_QM)
322           .makeFinal();
323       
324       RE_SYNTAX_POSIX_EXTENDED = new RESyntax(RE_SYNTAX_POSIX_COMMON)
325           .set(RE_CONTEXT_INDEP_ANCHORS)
326           .set(RE_CONTEXT_INDEP_OPS)
327           .set(RE_NO_BK_BRACES)
328           .set(RE_NO_BK_PARENS)
329           .set(RE_NO_BK_VBAR)
330           .set(RE_UNMATCHED_RIGHT_PAREN_ORD)
331           .makeFinal();
332
333       RE_SYNTAX_AWK = new RESyntax()
334           .set(RE_BACKSLASH_ESCAPE_IN_LISTS)
335           .set(RE_DOT_NOT_NULL)
336           .set(RE_NO_BK_PARENS)
337           .set(RE_NO_BK_REFS)
338           .set(RE_NO_BK_VBAR)
339           .set(RE_NO_EMPTY_RANGES)
340           .set(RE_UNMATCHED_RIGHT_PAREN_ORD)
341           .makeFinal();
342       
343       RE_SYNTAX_POSIX_AWK = new RESyntax(RE_SYNTAX_POSIX_EXTENDED)
344           .set(RE_BACKSLASH_ESCAPE_IN_LISTS)
345           .makeFinal();
346       
347       RE_SYNTAX_GREP = new RESyntax()
348           .set(RE_BK_PLUS_QM)
349           .set(RE_CHAR_CLASSES)
350           .set(RE_HAT_LISTS_NOT_NEWLINE)
351           .set(RE_INTERVALS)
352           .set(RE_NEWLINE_ALT)
353           .makeFinal();
354       
355       RE_SYNTAX_EGREP = new RESyntax()
356           .set(RE_CHAR_CLASSES)
357           .set(RE_CONTEXT_INDEP_ANCHORS)
358           .set(RE_CONTEXT_INDEP_OPS)
359           .set(RE_HAT_LISTS_NOT_NEWLINE)
360           .set(RE_NEWLINE_ALT)
361           .set(RE_NO_BK_PARENS)
362           .set(RE_NO_BK_VBAR)
363           .makeFinal();
364     
365       RE_SYNTAX_POSIX_EGREP = new RESyntax(RE_SYNTAX_EGREP)
366           .set(RE_INTERVALS)
367           .set(RE_NO_BK_BRACES)
368           .makeFinal();
369     
370       /* P1003.2/D11.2, section 4.20.7.1, lines 5078ff.  */
371     
372       RE_SYNTAX_ED = new RESyntax(RE_SYNTAX_POSIX_BASIC)
373           .makeFinal();
374     
375       RE_SYNTAX_SED = new RESyntax(RE_SYNTAX_POSIX_BASIC)
376           .makeFinal();
377       
378       RE_SYNTAX_POSIX_MINIMAL_BASIC = new RESyntax(RE_SYNTAX_POSIX_COMMON)
379           .set(RE_LIMITED_OPS)
380           .makeFinal();
381       
382       /* Differs from RE_SYNTAX_POSIX_EXTENDED in that RE_CONTEXT_INVALID_OPS
383          replaces RE_CONTEXT_INDEP_OPS and RE_NO_BK_REFS is added. */
384       
385       RE_SYNTAX_POSIX_MINIMAL_EXTENDED = new RESyntax(RE_SYNTAX_POSIX_COMMON)
386           .set(RE_CONTEXT_INDEP_ANCHORS)
387           .set(RE_CONTEXT_INVALID_OPS)
388           .set(RE_NO_BK_BRACES)
389           .set(RE_NO_BK_PARENS)
390           .set(RE_NO_BK_REFS)
391           .set(RE_NO_BK_VBAR)
392           .set(RE_UNMATCHED_RIGHT_PAREN_ORD)
393           .makeFinal();
394       
395       /* There is no official Perl spec, but here's a "best guess" */
396       
397       RE_SYNTAX_PERL4 = new RESyntax()
398           .set(RE_BACKSLASH_ESCAPE_IN_LISTS)
399           .set(RE_CONTEXT_INDEP_ANCHORS)
400           .set(RE_CONTEXT_INDEP_OPS)          // except for '{', apparently
401           .set(RE_INTERVALS)
402           .set(RE_NO_BK_BRACES)
403           .set(RE_NO_BK_PARENS)
404           .set(RE_NO_BK_VBAR)
405           .set(RE_NO_EMPTY_RANGES)
406           .set(RE_CHAR_CLASS_ESCAPES)    // \d,\D,\w,\W,\s,\S
407           .makeFinal();
408       
409       RE_SYNTAX_PERL4_S = new RESyntax(RE_SYNTAX_PERL4)
410           .set(RE_DOT_NEWLINE)
411           .makeFinal();
412       
413       RE_SYNTAX_PERL5 = new RESyntax(RE_SYNTAX_PERL4)
414           .set(RE_PURE_GROUPING)          // (?:)
415           .set(RE_STINGY_OPS)             // *?,??,+?,{}?
416           .set(RE_LOOKAHEAD)              // (?=)(?!)
417           .set(RE_STRING_ANCHORS)         // \A,\Z
418           .set(RE_CHAR_CLASS_ESC_IN_LISTS)// \d,\D,\w,\W,\s,\S within []
419           .set(RE_COMMENTS)              // (?#)
420           .makeFinal();
421       
422       RE_SYNTAX_PERL5_S = new RESyntax(RE_SYNTAX_PERL5)
423           .set(RE_DOT_NEWLINE)
424           .makeFinal();
425
426       RE_SYNTAX_JAVA_1_4 = new RESyntax(RE_SYNTAX_PERL5)
427           // XXX
428           .makeFinal();
429   }
430
431   /**
432    * Construct a new syntax object with all bits turned off.
433    * This is equivalent to RE_SYNTAX_EMACS.
434    */
435   public RESyntax() {
436     bits = new BitSet(BIT_TOTAL);
437   }
438
439     /**
440      * Called internally when constructing predefined syntaxes
441      * so their interpretation cannot vary.  Conceivably useful
442      * for your syntaxes as well.  Causes IllegalAccessError to
443      * be thrown if any attempt to modify the syntax is made.
444      *
445      * @return this object for convenient chaining
446      */
447     public RESyntax makeFinal() {
448         isFinal = true;
449         return this;
450     }
451
452   /**
453    * Construct a new syntax object with all bits set the same 
454    * as the other syntax.
455    */
456   public RESyntax(RESyntax other) {
457     bits = (BitSet) other.bits.clone();
458   }
459
460   /**
461    * Check if a given bit is set in this syntax.
462    */
463   public boolean get(int index) {
464     return bits.get(index);
465   }
466
467   /**
468    * Set a given bit in this syntax. 
469    *
470    * @param index the constant (RESyntax.RE_xxx) bit to set.
471    * @return a reference to this object for easy chaining.
472    */
473   public RESyntax set(int index) {
474       if (isFinal) throw new IllegalAccessError(SYNTAX_IS_FINAL);
475     bits.set(index);
476     return this;
477   }
478
479   /**
480    * Clear a given bit in this syntax. 
481    *
482    * @param index the constant (RESyntax.RE_xxx) bit to clear.
483    * @return a reference to this object for easy chaining.
484    */
485   public RESyntax clear(int index) {
486       if (isFinal) throw new IllegalAccessError(SYNTAX_IS_FINAL);
487       bits.clear(index);
488       return this;
489   }
490
491     /**
492      * Changes the line separator string for regular expressions
493      * created using this RESyntax.  The default separator is the
494      * value returned by the system property "line.separator", which
495      * should be correct when reading platform-specific files from a
496      * filesystem.  However, many programs may collect input from
497      * sources where the line separator is differently specified (for
498      * example, in the applet environment, the text box widget
499      * interprets line breaks as single-character newlines,
500      * regardless of the host platform.
501      *
502      * Note that setting the line separator to a character or
503      * characters that have specific meaning within the current syntax
504      * can cause unexpected chronosynclastic infundibula.
505      *
506      * @return this object for convenient chaining 
507      */
508     public RESyntax setLineSeparator(String aSeparator) {
509         if (isFinal) throw new IllegalAccessError(SYNTAX_IS_FINAL);
510         lineSeparator = aSeparator;
511         return this;
512     }
513
514     /**
515      * Returns the currently active line separator string.  The default
516      * is the platform-dependent system property "line.separator".
517      */
518     public String getLineSeparator() {
519         return lineSeparator;
520     }
521 }