Source code: jpicedt/graphic/io/parser/RepeatExpression.java
1 /* jPicEdt version 1.3.2, a picture editor for LaTeX.
2 Copyright (C) 1999-2002 Sylvain Reynal
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 Sylvain Reynal
19 Département de Physique
20 Ecole Nationale Supérieure de l'Electronique et de ses Applications (ENSEA)
21 6, avenue du Ponceau
22 95014 CERGY CEDEX
23 FRANCE
24
25 Tel : 00 +33 130 736 245
26 Fax : 00 +33 130 736 667
27 e-mail : reynal@ensea.fr
28 jPicEdt web page : http://www.jpicedt.org
29 */
30
31 package jpicedt.graphic.io.parser;
32
33 import java.util.*;
34
35 /**
36 * an expression that represents a pattern repeating a given number of times
37 */
38 public class RepeatExpression extends AbstractRegularExpression {
39
40
41 private AbstractRegularExpression expr;
42 private int repeat;
43 private int mode;
44
45 /**
46 * @param repeat number of times this expression has to be found sequentially ;
47 * @param mode EXACTLY, AT_LEAST or AT_MOST
48 */
49 public RepeatExpression(AbstractRegularExpression expr, int repeat, int mode){
50
51 this.expr=expr;
52 this.repeat = repeat;
53 this.mode=mode;
54 }
55
56 /**
57 * set the base-pattern to the given expression
58 */
59 public void setPattern(AbstractRegularExpression expr){ this.expr=expr;}
60
61
62 /**
63 * @return TRUE if and only if the given "expr" has been found as many times as specified by
64 * the "mode" given in the constructor.
65 * If TRUE, calls action with : key="*", value=new Integer(number of effective repeat found)
66 */
67 public boolean interpret(Context c) throws ParserException {
68
69 int i=0;
70 switch(mode){
71 case AT_LEAST:
72 for(i=0; i<repeat; i++){
73 if (expr.interpret(c) == false) {
74 action(new ParserEvent(this, c, false, new Integer(i)));
75 return false;
76 }
77 }
78 while(expr.interpret(c)){i++;} // <-- "swallow" remaining expr occurences
79 action(new ParserEvent(this, c, true, new Integer(i)));
80 return true;
81 case AT_MOST:
82 for(i=0; i<repeat; i++){
83 if (expr.interpret(c) == false) {
84 action(new ParserEvent(this, c, true, new Integer(i)));
85 return true; // <-- break as soon as no more occurence of expr
86 }
87 }
88 if (expr.interpret(c) == true) {
89 i++;
90 action(new ParserEvent(this, c, false, new Integer(i)));
91 return false; // <-- now, repeat count has been exceeded
92 }
93 action(new ParserEvent(this, c, true, new Integer(i)));
94 return true;
95 case EXACTLY:
96 default: // EXACTLY
97 for(i=0; i<repeat; i++){
98 if (expr.interpret(c) == false) {
99 action(new ParserEvent(this, c, false, new Integer(i)));
100 return false;
101 }
102 }
103 action(new ParserEvent(this, c, true, new Integer(i)));
104 return true;
105 }
106 }
107
108 /**
109 *
110 */
111 public String toString(){
112
113 String modeStr;
114 switch(mode){
115 case AT_LEAST :
116 modeStr="AT_LEAST";
117 break;
118 case AT_MOST :
119 modeStr="AT_MOST";
120 break;
121 default:
122 modeStr="EXACT";
123 break;
124 }
125 return "[Repeat:" + modeStr + " " + repeat + " " + expr + "]";
126 }
127 }