Skip to main content

rpic_core/
token.rs

1//! Token set for the pic language, mirroring dpic's `dpic.toks`.
2//!
3//! Open-ended lexical classes (numbers, strings, identifiers, labels, macro
4//! args) carry their payload; the large but finite keyword vocabulary is folded
5//! into grouped enums ([`Kw`], [`Corner`], [`Param`], [`Func1`], [`Func2`],
6//! [`LineType`], [`TextPos`], [`Arrow`], [`Dir`], [`Prim`], [`Color`],
7//! [`EnvVar`]) so the parser can branch on them directly.
8
9/// A single lexical token.
10#[derive(Debug, Clone, PartialEq)]
11pub enum Token {
12    // ---- literals & open classes -------------------------------------------
13    /// Numeric literal (pic has only floating-point numbers).
14    Float(f64),
15    /// `"…"` string literal (quotes stripped, escapes resolved).
16    Str(String),
17    /// Lower-initial identifier / variable name.
18    Name(String),
19    /// Upper-initial place label (e.g. `Start`).
20    Label(String),
21    /// `$n` macro argument reference.
22    Arg(u32),
23    /// `$+` — the number of arguments passed to the current macro.
24    ArgCount,
25    /// A literal `$` not introducing a macro argument (e.g. `$f$` LaTeX text
26    /// passed unquoted as a macro argument). Carried as text by the macro layer.
27    Dollar,
28    /// A literal `\` that is not a line continuation (e.g. `\beta` LaTeX text
29    /// passed unquoted as a macro argument). Carried as text by the macro layer.
30    Backslash,
31
32    // ---- structural --------------------------------------------------------
33    /// End of statement: newline or `;`.
34    Newline,
35    /// `.PS` picture start (optionally followed by width/height terms).
36    DotPS,
37    /// `.PE` picture end.
38    DotPE,
39    /// End of input.
40    Eof,
41
42    // ---- punctuation & operators ------------------------------------------
43    Lt,         // <
44    Lparen,     // (
45    Rparen,     // )
46    Mult,       // *
47    Plus,       // +
48    Minus,      // -
49    Div,        // /
50    Percent,    // %
51    Caret,      // ^
52    Not,        // !
53    AndAnd,     // &&
54    OrOr,       // ||
55    Ampersand,  // &
56    Comma,      // ,
57    Colon,      // :
58    LeftBrack,  // [
59    RightBrack, // ]
60    LeftBrace,  // {
61    RightBrace, // }
62    Dot,        // .
63    Block,      // []  (empty-block reference)
64    LeftQuote,  // `
65    RightQuote, // '
66    Eq,         // =
67    ColonEq,    // :=
68    PlusEq,     // +=
69    MinusEq,    // -=
70    MultEq,     // *=
71    DivEq,      // /=
72    RemEq,      // %=
73    EqEq,       // ==
74    Neq,        // !=
75    Ge,         // >=
76    Le,         // <=
77    Gt,         // >
78    DotX,       // .x
79    DotY,       // .y
80
81    // ---- grouped keyword classes ------------------------------------------
82    Kw(Kw),
83    Corner(Corner),
84    Param(Param),
85    Func1(Func1),
86    Func2(Func2),
87    LineType(LineType),
88    TextPos(TextPos),
89    Arrow(Arrow),
90    Dir(Dir),
91    Prim(Prim),
92    Color(Color),
93    EnvVar(EnvVar),
94}
95
96/// General keywords: attributes, ordinals, control words, commands.
97#[derive(Debug, Clone, Copy, PartialEq, Eq)]
98pub enum Kw {
99    Ht,
100    Wid,
101    Rad,
102    Diam,
103    Thick,
104    Scaled,
105    From,
106    To,
107    At,
108    With,
109    By,
110    Then,
111    Continue,
112    Chop,
113    Same,
114    Cw,
115    Ccw,
116    Of,
117    The,
118    Way,
119    Between,
120    And,
121    Here,
122    Last,
123    Fill,
124    Nth, // ordinal marker: st / nd / rd / th
125    Print,
126    Copy,
127    Reset,
128    Exec,
129    Sh,
130    Command,
131    Define,
132    Undef,
133    Rand,
134    If,
135    Else,
136    For,
137    Do,
138    Sprintf,
139    // rpic animation extension (not in classic pic)
140    Animate,
141    After,
142    Delay,
143}
144
145/// Compass / named corners of an object.
146#[derive(Debug, Clone, Copy, PartialEq, Eq)]
147pub enum Corner {
148    N,
149    S,
150    E,
151    W,
152    Ne,
153    Se,
154    Nw,
155    Sw,
156    Start,
157    End,
158    Center,
159}
160
161/// Dotted attribute accessors: `.ht .wid .rad .diam .thick .len`.
162#[derive(Debug, Clone, Copy, PartialEq, Eq)]
163pub enum Param {
164    Height,
165    Width,
166    Radius,
167    Diameter,
168    Thickness,
169    Length,
170}
171
172/// Single-argument math functions.
173#[derive(Debug, Clone, Copy, PartialEq, Eq)]
174pub enum Func1 {
175    Abs,
176    Acos,
177    Asin,
178    Cos,
179    Exp,
180    Expe,
181    Int,
182    Log,
183    Loge,
184    Sign,
185    Sin,
186    Sqrt,
187    Tan,
188    Floor,
189}
190
191/// Two-argument math functions.
192#[derive(Debug, Clone, Copy, PartialEq, Eq)]
193pub enum Func2 {
194    Atan2,
195    Max,
196    Min,
197    Pmod,
198}
199
200/// Line style attributes.
201#[derive(Debug, Clone, Copy, PartialEq, Eq)]
202pub enum LineType {
203    Solid,
204    Dotted,
205    Dashed,
206    Invis,
207}
208
209/// Text justification attributes.
210#[derive(Debug, Clone, Copy, PartialEq, Eq)]
211pub enum TextPos {
212    Center,
213    Ljust,
214    Rjust,
215    Above,
216    Below,
217}
218
219/// Arrowhead specifiers.
220#[derive(Debug, Clone, Copy, PartialEq, Eq)]
221pub enum Arrow {
222    Left,   // <-
223    Right,  // ->
224    Double, // <->
225}
226
227/// Direction-of-motion words.
228#[derive(Debug, Clone, Copy, PartialEq, Eq)]
229pub enum Dir {
230    Up,
231    Down,
232    Right,
233    Left,
234}
235
236/// Drawable primitive objects.
237#[derive(Debug, Clone, Copy, PartialEq, Eq)]
238pub enum Prim {
239    Box,
240    Circle,
241    Ellipse,
242    Arc,
243    Line,
244    Arrow,
245    Move,
246    Spline,
247}
248
249/// Color / outline / shade attribute keyword.
250#[derive(Debug, Clone, Copy, PartialEq, Eq)]
251pub enum Color {
252    Colored,
253    Outlined,
254    Shaded,
255}
256
257/// Built-in environment variables (default dimensions & globals).
258#[derive(Debug, Clone, Copy, PartialEq, Eq)]
259pub enum EnvVar {
260    Arcrad,
261    Arrowht,
262    Arrowwid,
263    Boxht,
264    Boxrad,
265    Boxwid,
266    Circlerad,
267    Dashwid,
268    Ellipseht,
269    Ellipsewid,
270    Lineht,
271    Linewid,
272    Moveht,
273    Movewid,
274    Textht,
275    Textoffset,
276    Textwid,
277    Arrowhead,
278    Fillval,
279    Linethick,
280    Maxpsht,
281    Maxpswid,
282    Scale,
283    Margin,
284    Topmargin,
285    Rightmargin,
286    Bottommargin,
287    Leftmargin,
288    Texlabels,
289}