Source code: jpicedt/format/pstricks/parser/PstricksParser.java
1 /*
2 PstricksParser.java - July 24, 2002 - jPicEdt 1.3.2, a picture editor for LaTeX.
3 Copyright (C) 1999-2002 Sylvain Reynal
4
5 Département de Physique
6 Ecole Nationale Supérieure de l'Electronique et de ses Applications (ENSEA)
7 6, avenue du Ponceau
8 F-95014 CERGY CEDEX
9
10 Tel : +33 130 736 245
11 Fax : +33 130 736 667
12 e-mail : reynal@ensea.fr
13 jPicEdt web page : http://www.jpicedt.org/
14
15 This program is free software; you can redistribute it and/or
16 modify it under the terms of the GNU General Public License
17 as published by the Free Software Foundation; either version 2
18 of the License, or any later version.
19
20 This program is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
24
25 You should have received a copy of the GNU General Public License
26 along with this program; if not, write to the Free Software
27 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28 */
29
30 package jpicedt.format.pstricks.parser;
31
32 import jpicedt.graphic.io.parser.RootExpression;
33 import jpicedt.graphic.io.parser.EnclosingExpression;
34 import jpicedt.graphic.io.parser.Pool;
35 import jpicedt.graphic.io.parser.ExpressionConstants;
36
37 import jpicedt.graphic.PicPoint;
38 import jpicedt.graphic.model.*;
39
40 import java.awt.Color;
41 import java.io.*;
42 import java.util.*;
43
44 /**
45 * Root expression for the Pstricks parser, containing grammar rules for the pstricks format.
46 * Pstricks support is pretty ok, though add-ons (like pst-nodes.sty) are still missing.
47 * <p>
48 * This class consists of grammar rules, wrapped in a RootExpression which can itself be directly added
49 * to the main tree (for instance, using DefaultParser.addGrammar()).
50 * <P>
51 * Note : the current parser used across the
52 * JPicEdt application can be retrieved using jpicedt.JPicEdt.getParser().
53 * <p>
54 * [TODO] : \psgrid, \psaxes, arrow size, dot style.
55 * @author $Author: reynal $
56 * @version $Id: PstricksParser.java,v 1.2 2002/08/05 16:44:10 reynal Exp $
57 *
58 */
59 public class PstricksParser extends RootExpression implements ExpressionConstants {
60
61 /** key associated with \\psxunit register (double) */
62 public static final Pool.Key KEY_X_UNIT = new Pool.Key("pst.x-unit");
63 /** key associated with \\psyunit register (double) */
64 public static final Pool.Key KEY_Y_UNIT = new Pool.Key("pst.y-unit");
65 /** key associated with \\psrunit register (double) */
66 public static final Pool.Key KEY_R_UNIT = new Pool.Key("pst.r-unit");
67 /** key associated with the attribute set which stores PsTricks default graphical parameters (PicAttributeSet) */
68 public static final Pool.Key KEY_ATTRIBUTES = new Pool.Key("pst.attributes");
69 /** user-defined colours (a HashMap which gets filled by UserDefinedColorsExpression, see \\newgray, \\newrgbcolor,... in PsTricks documentation p.5) */
70 public static final Pool.Key KEY_USER_COLOURS = new Pool.Key("pst.user-colours");
71 /** \\newpsobject : key associated with the HashMap that associates pairs "macroName -> param_string",
72 * e.g. "\\myline" -> "linecolor=green,filltype=solid" (HashMap) */
73 public static final Pool.Key KEY_NEWPSOBJECTS = new Pool.Key("pst.newpsobjects");
74
75 private Pool pool;
76
77 /**
78 * Creates a new PstricksParser, and build grammar rules
79 */
80 public PstricksParser(Pool pool) {
81
82 this.pool = pool;
83
84 // 0°) begin picture
85 add(new BeginPsPictureExpression()); // not used (but otherwise, it's considered a non-parsable exp)
86
87 // 1°) parameters
88 add(new EnclosingExpression("\\psset{", new PSTParametersExpression(pool, KEY_ATTRIBUTES), "}")); // \\psset{...}
89 add(new UserDefinedColorExpression(pool)); // \\newrgbcolor{...}() or similar commands
90
91 // 2°) lines and polygons :
92 add(new PsQLineExpression(pool)); // \\qline (PsTricks)
93 add(new PsPolygonExpression(pool,PsPolygonExpression.POLYGON)); // \\pspolygon (closed)
94 add(new PsPolygonExpression(pool,PsPolygonExpression.LINE)); //\\psline (open)
95 add(new PsPolygonExpression(pool,PsPolygonExpression.DOTS)); //\\psdots
96
97 // 3°) frames
98 add(new PsFrameExpression(pool));
99
100 // 4°) ellipses and arcs
101 add(new PsEllipseExpression(pool)); // \\psellipse
102 add(new PsCircleExpression(pool)); // \\pscircle
103 add(new PsArcExpression(pool,PsArcExpression.ARC)); // \\psarc
104 add(new PsArcExpression(pool,PsArcExpression.WEDGE)); // \\pswedge
105 add(new PsQDiskExpression(pool)); // \\qdisk
106 //add(new PsEllipticalArcExpression(pool)); // special trick to build elliptic arc with PsTricks
107
108 // 5°) splines
109 add(new PsBezierExpression(pool)); // \\psbezier
110
111 // 6°) text and misc.
112 add(new PsRPutExpression(pool)); // \\rput...
113
114 // 7°) comment
115 add(new jpicedt.graphic.io.parser.CommentExpression("%"));
116
117 // 8°) \\newpsobject
118 add(new PsObjectExpression(this,pool));
119
120 // throw ParserException.EndOfPicture to signals that a \end{pspicture} was found
121 // and that the parsing process ends up here (otherwise, we stop at EOF)
122 add(new EndPsPictureExpression());
123
124 //System.out.println("PsTricks grammar : \n" + this.toString());
125 }
126
127
128 /**
129 * reinit shared parameters belonging to the Pool
130 */
131 public void reinit(){
132 //System.out.println("Reinit' PstricksParser...");
133 double pstXunit,pstYunit,pstRunit;
134 pstXunit = pstYunit = pstRunit = 10.0; // PsTricks's default = 1cm
135 pool.put(KEY_X_UNIT, new Double(pstXunit));
136 pool.put(KEY_Y_UNIT, new Double(pstYunit));
137 pool.put(KEY_R_UNIT, new Double(pstRunit));
138 pool.put(KEY_ATTRIBUTES, new PicAttributeSet()); // PsTricks's registers
139 pool.put(KEY_NEWPSOBJECTS, new HashMap());
140 pool.put(KEY_USER_COLOURS, new HashMap());
141 }
142 }