Source code: jpicedt/format/latex/parser/PicEndExpression.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.format.latex.parser;
32
33 import jpicedt.graphic.io.parser.*;
34 import java.util.*;
35 import gnu.regexp.*;
36
37 /**
38 * Match "%End XXXX"-like expressions at the beginning of a line (or preceeded by whitespaces),
39 * after skipping as many lines as necessary.
40 */
41 public class PicEndExpression extends AbstractRegularExpression {
42
43 private String literal;
44 private RE re;
45
46 /**
47 * @param literal e.g. "%End Line"
48 */
49 public PicEndExpression(String literal){
50 try {this.re = new RE("\n *" + literal);}
51 catch (REException e){e.printStackTrace();}
52 this.literal = literal;
53 }
54
55 /**
56 * @return TRUE in any case, else throw an exception (@EOF) ; (this means that exception throwing is
57 * the only way to get out of the this expression...)
58 * in case of success, call action() with value=leading String
59 * @throws ParserException.NotFoundInFile if EOF was reached before this expression could be found
60 */
61 public boolean interpret(Context context) throws ParserException {
62
63 context.mark();
64 REMatch match = context.getMatch(re); // move caret past matched string if found
65 if (match != null){
66 context.reset();
67 String skipped = context.readTo(match.getEndIndex()-literal.length());
68 context.readTo(match.getEndIndex());
69 action(new ParserEvent(this, context, true, skipped));
70 return true;
71 }
72 throw new ParserException.NotFoundInFile(context, this); //EOF=>stop here
73 }
74
75 /**
76 *
77 */
78 public String toString(){
79 return "[PicEndExpression:" + literal + "]";
80 }
81 }