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
24    // ---- structural --------------------------------------------------------
25    /// End of statement: newline or `;`.
26    Newline,
27    /// `.PS` picture start (optionally followed by width/height terms).
28    DotPS,
29    /// `.PE` picture end.
30    DotPE,
31    /// End of input.
32    Eof,
33
34    // ---- punctuation & operators ------------------------------------------
35    Lt,         // <
36    Lparen,     // (
37    Rparen,     // )
38    Mult,       // *
39    Plus,       // +
40    Minus,      // -
41    Div,        // /
42    Percent,    // %
43    Caret,      // ^
44    Not,        // !
45    AndAnd,     // &&
46    OrOr,       // ||
47    Ampersand,  // &
48    Comma,      // ,
49    Colon,      // :
50    LeftBrack,  // [
51    RightBrack, // ]
52    LeftBrace,  // {
53    RightBrace, // }
54    Dot,        // .
55    Block,      // []  (empty-block reference)
56    LeftQuote,  // `
57    RightQuote, // '
58    Eq,         // =
59    ColonEq,    // :=
60    PlusEq,     // +=
61    MinusEq,    // -=
62    MultEq,     // *=
63    DivEq,      // /=
64    RemEq,      // %=
65    EqEq,       // ==
66    Neq,        // !=
67    Ge,         // >=
68    Le,         // <=
69    Gt,         // >
70    DotX,       // .x
71    DotY,       // .y
72
73    // ---- grouped keyword classes ------------------------------------------
74    Kw(Kw),
75    Corner(Corner),
76    Param(Param),
77    Func1(Func1),
78    Func2(Func2),
79    LineType(LineType),
80    TextPos(TextPos),
81    Arrow(Arrow),
82    Dir(Dir),
83    Prim(Prim),
84    Color(Color),
85    EnvVar(EnvVar),
86}
87
88/// General keywords: attributes, ordinals, control words, commands.
89#[derive(Debug, Clone, Copy, PartialEq, Eq)]
90pub enum Kw {
91    Ht,
92    Wid,
93    Rad,
94    Diam,
95    Thick,
96    Scaled,
97    From,
98    To,
99    At,
100    With,
101    By,
102    Then,
103    Continue,
104    Chop,
105    Same,
106    Cw,
107    Ccw,
108    Of,
109    The,
110    Way,
111    Between,
112    And,
113    Here,
114    Last,
115    Fill,
116    Nth, // ordinal marker: st / nd / rd / th
117    Print,
118    Copy,
119    Reset,
120    Exec,
121    Sh,
122    Command,
123    Define,
124    Undef,
125    Rand,
126    If,
127    Else,
128    For,
129    Do,
130    Sprintf,
131    // rpic animation extension (not in classic pic)
132    Animate,
133    After,
134    Delay,
135}
136
137/// Compass / named corners of an object.
138#[derive(Debug, Clone, Copy, PartialEq, Eq)]
139pub enum Corner {
140    N,
141    S,
142    E,
143    W,
144    Ne,
145    Se,
146    Nw,
147    Sw,
148    Start,
149    End,
150    Center,
151}
152
153/// Dotted attribute accessors: `.ht .wid .rad .diam .thick .len`.
154#[derive(Debug, Clone, Copy, PartialEq, Eq)]
155pub enum Param {
156    Height,
157    Width,
158    Radius,
159    Diameter,
160    Thickness,
161    Length,
162}
163
164/// Single-argument math functions.
165#[derive(Debug, Clone, Copy, PartialEq, Eq)]
166pub enum Func1 {
167    Abs,
168    Acos,
169    Asin,
170    Cos,
171    Exp,
172    Expe,
173    Int,
174    Log,
175    Loge,
176    Sign,
177    Sin,
178    Sqrt,
179    Tan,
180    Floor,
181}
182
183/// Two-argument math functions.
184#[derive(Debug, Clone, Copy, PartialEq, Eq)]
185pub enum Func2 {
186    Atan2,
187    Max,
188    Min,
189    Pmod,
190}
191
192/// Line style attributes.
193#[derive(Debug, Clone, Copy, PartialEq, Eq)]
194pub enum LineType {
195    Solid,
196    Dotted,
197    Dashed,
198    Invis,
199}
200
201/// Text justification attributes.
202#[derive(Debug, Clone, Copy, PartialEq, Eq)]
203pub enum TextPos {
204    Center,
205    Ljust,
206    Rjust,
207    Above,
208    Below,
209}
210
211/// Arrowhead specifiers.
212#[derive(Debug, Clone, Copy, PartialEq, Eq)]
213pub enum Arrow {
214    Left,   // <-
215    Right,  // ->
216    Double, // <->
217}
218
219/// Direction-of-motion words.
220#[derive(Debug, Clone, Copy, PartialEq, Eq)]
221pub enum Dir {
222    Up,
223    Down,
224    Right,
225    Left,
226}
227
228/// Drawable primitive objects.
229#[derive(Debug, Clone, Copy, PartialEq, Eq)]
230pub enum Prim {
231    Box,
232    Circle,
233    Ellipse,
234    Arc,
235    Line,
236    Arrow,
237    Move,
238    Spline,
239}
240
241/// Color / outline / shade attribute keyword.
242#[derive(Debug, Clone, Copy, PartialEq, Eq)]
243pub enum Color {
244    Colored,
245    Outlined,
246    Shaded,
247}
248
249/// Built-in environment variables (default dimensions & globals).
250#[derive(Debug, Clone, Copy, PartialEq, Eq)]
251pub enum EnvVar {
252    Arcrad,
253    Arrowht,
254    Arrowwid,
255    Boxht,
256    Boxrad,
257    Boxwid,
258    Circlerad,
259    Dashwid,
260    Ellipseht,
261    Ellipsewid,
262    Lineht,
263    Linewid,
264    Moveht,
265    Movewid,
266    Textht,
267    Textoffset,
268    Textwid,
269    Arrowhead,
270    Fillval,
271    Linethick,
272    Maxpsht,
273    Maxpswid,
274    Scale,
275}