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    Thin,
105    Scaled,
106    From,
107    To,
108    At,
109    With,
110    By,
111    Then,
112    Continue,
113    Chop,
114    Same,
115    Cw,
116    Ccw,
117    Of,
118    The,
119    Way,
120    Between,
121    And,
122    Here,
123    Last,
124    Fill,
125    Nth, // ordinal marker: st / nd / rd / th
126    Print,
127    Copy,
128    Reset,
129    Exec,
130    Sh,
131    Command,
132    Define,
133    Undef,
134    Rand,
135    If,
136    Else,
137    For,
138    Do,
139    Sprintf,
140    // rpic animation extension (not in classic pic)
141    Animate,
142    After,
143    Delay,
144    Repeat,
145    Yoyo,
146    Ease,
147    Along,
148    Stagger,
149    Out,
150    Scroll,
151    Into,
152}
153
154/// Compass / named corners of an object.
155#[derive(Debug, Clone, Copy, PartialEq, Eq)]
156pub enum Corner {
157    N,
158    S,
159    E,
160    W,
161    Ne,
162    Se,
163    Nw,
164    Sw,
165    Start,
166    End,
167    Center,
168}
169
170/// Dotted attribute accessors: `.ht .wid .rad .diam .thick .len`.
171#[derive(Debug, Clone, Copy, PartialEq, Eq)]
172pub enum Param {
173    Height,
174    Width,
175    Radius,
176    Diameter,
177    Thickness,
178    Length,
179}
180
181/// Single-argument math functions.
182#[derive(Debug, Clone, Copy, PartialEq, Eq)]
183pub enum Func1 {
184    Abs,
185    Acos,
186    Asin,
187    Cos,
188    Exp,
189    Expe,
190    Int,
191    Log,
192    Loge,
193    Sign,
194    Sin,
195    Sqrt,
196    Tan,
197    Floor,
198}
199
200/// Two-argument math functions.
201#[derive(Debug, Clone, Copy, PartialEq, Eq)]
202pub enum Func2 {
203    Atan2,
204    Max,
205    Min,
206    Pmod,
207}
208
209/// Line style attributes.
210#[derive(Debug, Clone, Copy, PartialEq, Eq)]
211pub enum LineType {
212    Solid,
213    Dotted,
214    Dashed,
215    Invis,
216}
217
218/// Text justification attributes.
219#[derive(Debug, Clone, Copy, PartialEq, Eq)]
220pub enum TextPos {
221    Center,
222    Ljust,
223    Rjust,
224    Above,
225    Below,
226}
227
228/// Arrowhead specifiers.
229#[derive(Debug, Clone, Copy, PartialEq, Eq)]
230pub enum Arrow {
231    Left,   // <-
232    Right,  // ->
233    Double, // <->
234}
235
236/// Direction-of-motion words.
237#[derive(Debug, Clone, Copy, PartialEq, Eq)]
238pub enum Dir {
239    Up,
240    Down,
241    Right,
242    Left,
243}
244
245/// Drawable primitive objects.
246#[derive(Debug, Clone, Copy, PartialEq, Eq)]
247pub enum Prim {
248    Box,
249    Circle,
250    Ellipse,
251    Arc,
252    Line,
253    Arrow,
254    Move,
255    Spline,
256}
257
258/// Color / outline / shade attribute keyword.
259#[derive(Debug, Clone, Copy, PartialEq, Eq)]
260pub enum Color {
261    Colored,
262    Outlined,
263    Shaded,
264}
265
266/// Built-in environment variables (default dimensions & globals).
267#[derive(Debug, Clone, Copy, PartialEq, Eq)]
268pub enum EnvVar {
269    Arcrad,
270    Arrowht,
271    Arrowwid,
272    Boxht,
273    Boxrad,
274    Boxwid,
275    Circlerad,
276    Dashwid,
277    Ellipseht,
278    Ellipsewid,
279    Lineht,
280    Linewid,
281    Moveht,
282    Movewid,
283    Textht,
284    Textoffset,
285    Textwid,
286    Arrowhead,
287    Fillval,
288    Linethick,
289    Maxpsht,
290    Maxpswid,
291    Scale,
292    Margin,
293    Topmargin,
294    Rightmargin,
295    Bottommargin,
296    Leftmargin,
297    Texlabels,
298    Dotrad,
299    Maxanimrepeat,
300    Maxanimseconds,
301}