df_ls_structure 0.3.0-rc.1

A language server for Dwarf Fortress RAW files
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
use crate::{
    AmmoToken, ArmorToken, BuildingToken, FoodToken, GlovesToken, HelmToken, InstrumentToken,
    ItemReferenceArg, ItemSpecificEnum, KeyBindEnum, MaterialTokenArgWithNoneNone,
    MaterialTokenArgWithReagentMat, NoneEnum, PantsToken, ShieldToken, ShoesToken, SiegeAmmoToken,
    SkillEnum, ToolToken, ToyToken, TrapCompToken, WeaponToken,
};
use df_ls_core::{AllowEmpty, Choose, Clamp, Reference, ReferenceTo, Referenceable};
use df_ls_diagnostics::DiagnosticsInfo;
use df_ls_syntax_analysis::{Token, TokenDeserialize, TryFromArgumentGroup};
use serde::{Deserialize, Serialize};

#[derive(
    Serialize, Deserialize, Clone, Debug, Default, TokenDeserialize, PartialEq, Eq, Referenceable,
)]
pub struct ReactionToken {
    /// Defines a new reaction
    #[token_de(token = "REACTION", on_duplicate_to_parent, primary_token)]
    #[referenceable(self_reference)]
    pub reference: Option<ReferenceTo<Self>>,
    /// Defines the name used by the reaction in-game.
    #[token_de(token = "NAME")]
    pub name: Option<String>,
    /// Provides a text description of the reaction ingame when it is highlighted in the building UI.
    /// Multiple `DESCRIPTION` tokens can be defined in a reaction, and each will appear on a new line.
    /// The pop-up box that contains the description is limited to 325 characters total.
    ///
    /// Alternatively, this token can reference a `DESCRIPTION` token in an existing tool definition
    /// by replacing string with `USE_TOOL:<ITEM_SUBTYPE>`; or an existing instrument definition with
    /// `USE_INSTRUMENT:<ITEM_SUBTYPE>`.
    #[token_de(token = "DESCRIPTION")]
    pub description: Vec<AllowEmpty<Choose<ReactionDescriptionUseItemTokenArg, String>>>,
    /// This version of the reaction is not used by dwarves at home in a fortress, but rather
    /// the wanderers of Adventure Mode. When using this token, it will be allowed for adventurers
    /// of any race, without editing Entity files.
    #[token_de(token = "ADVENTURE_MODE_ENABLED")]
    pub adventure_mode_enabled: Option<()>,
    /// Amount of attributes given per skill improvement.
    ///
    /// Default is 10.
    ///
    /// Adding since: v0.47.01
    // TODO always positive?
    #[token_de(token = "ATTRIBUTE_IP")]
    pub attribute_ip: Option<i32>,
    /// The reaction will be queued automatically if the reaction reagents are all present.
    #[token_de(token = "AUTOMATIC")]
    pub automatic: Option<()>,
    /// Sets the building that the reaction will be performed in, and the button used to queue
    /// the reaction once that building's menu is accessed in-game
    #[token_de(token = "BUILDING")]
    pub building: Vec<(ReferenceTo<BuildingToken>, Choose<KeyBindEnum, NoneEnum>)>, // 2nd param is KeyBind
    /// Puts the reaction in a category. Categories are custom submenus for reaction menus.
    /// The category ID is a unique identifier for the category. It is only used in the raws, and
    /// will not appear in the game.
    ///
    /// If you're defining multiple categories within the same reaction - for example, if you
    /// intend the reaction to be nested two deep, and haven't yet defined the super-category -
    /// the last CATEGORY token within the reaction definition is the one that the reaction will
    /// appear in.
    #[token_de(token = "CATEGORY")]
    pub category: Option<ReactionCategoryToken>,
    /// Requires that the reaction either use up a unit of coal or charcoal or
    /// else be performed at a magma workshop
    #[token_de(token = "FUEL")]
    pub fuel: Option<()>,
    /// Sets the maximum number of times a reaction is allowed to run when using stacked reagents.
    /// This can be used to ensure that the reaction doesn't repeat until
    /// the entire stack is depleted.
    #[token_de(token = "MAX_MULTIPLIER")]
    pub max_multiplier: Option<u32>,
    /// Skill used by the reaction
    #[token_de(token = "SKILL")]
    pub skill: Option<SkillEnum>,
    /// Amount of skill given per product made.
    ///
    /// Default is 30.
    ///
    /// Adding since: v0.47.01
    // TODO always positive?
    #[token_de(token = "SKILL_IP")]
    pub skill_ip: Option<i32>,
    /// Proportion of how much the skill level effects the outcome (example: quality) of the result.
    ///
    /// The skill roll is `random(basic range) + random((skill level * multiplier)/2 + 1) +
    /// random((skill level * multiplier)/2 + 1)`.
    /// random(x) returns a number between 0 and x-1, so basic range is always 1 or more.
    /// Higher skill rolls give better results.
    ///
    /// Arguments:
    /// - Basic Range: The maximum value for the basic range. Need to be equal or bigger then `1`.
    /// - Skill level multiplier: Used to increase effectiveness of the skill level.
    ///
    /// Default is `[SKILL_ROLL_RANGE:11:5]`
    ///
    /// Adding since: v0.47.01
    #[token_de(token = "SKILL_ROLL_RANGE")]
    pub skill_roll_range: Option<(Clamp<u32, 1, { u32::MAX as isize }>, u32)>,
    /// Reagent must contain writing.
    /// Arguments are: `<chance>:<reagent/product target>:<type of improvement>:<mat tokens>`đđ
    /// `<mat tokens>` might consists of 3 arguments,
    /// for example: `GET_MATERIAL_FROM_REAGENT:reagent:REACTION_PRODUCT_ID`
    /// Other times it has 2 extra arguments like: `INORGANIC:BRONZE_COATING`
    // |-----0-----|-1-|----2-----|----------3----------|---------------------4--------------------|
    // [IMPROVEMENT:100:instrument:INSTRUMENT_PIECE:BODY:GET_MATERIAL_FROM_REAGENT:drum  :NONE     ]
    // [IMPROVEMENT:100:jug       :GLAZED               :GET_MATERIAL_FROM_REAGENT:glaze :GLAZE_MAT]
    // [IMPROVEMENT:100:a         :INSTRUMENT_PIECE:BODY:METAL                    :FRAME           ]
    // [IMPROVEMENT:100:target    :SPIKES               :GET_MATERIAL_FROM_REAGENT:gem   :NONE     ]
    #[token_de(token = "IMPROVEMENT")]
    pub improvement: Vec<(
        u8,                             // chance
        Reference,                      // ReferenceTo<ReagentToken> // reagent
        ImprovementTypeTokenArg,        // improvement_type
        MaterialTokenArgWithReagentMat, // Material arguments
    )>,
    /// Requires a given reagent as an input for a reaction
    #[token_de(token = "REAGENT")]
    pub reagents: Vec<ReagentToken>,
    /// See description on `ProductToken`
    #[token_de(token = "PRODUCT")]
    pub products: Vec<ProductToken>,
}

#[derive(
    Serialize, Deserialize, Clone, Debug, Default, TokenDeserialize, PartialEq, Eq, Referenceable,
)]
pub struct ReagentToken {
    /// Requires a given reagent as an input for a reaction
    /// Arguments are: `<name/id>:<quantity>:<item token>:<mat tokens>`
    /// `<mat tokens>` and `<item token>` might consists of 2 arguments,
    /// for example: `BOULDER:NO_SUBTYPE` or `INORGANIC:COAL_BITUMINOUS`
    #[token_de(token = "REAGENT", on_duplicate_to_parent, primary_token)]
    #[referenceable(self_reference)]
    // [REAGENT:A            :1  :NONE       :NONE      :NONE     :NONE           ]
    // [REAGENT:lye          :150:LIQUID_MISC:NONE      :LYE                      ]
    // [REAGENT:lye container:1  :NONE       :NONE      :NONE     :NONE           ]
    // [REAGENT:A            :1  :BOULDER    :NO_SUBTYPE:INORGANIC:COAL_BITUMINOUS]
    pub reference: Option<(
        ReferenceTo<Self>,                              // Name/id
        u32,                                            // Quantity
        Choose<(NoneEnum, NoneEnum), ItemReferenceArg>, // Item token
        MaterialTokenArgWithNoneNone,                   // Material token
    )>,
    /// Reagent material must have the `[BONE]` token.
    #[token_de(token = "ANY_BONE_MATERIAL")]
    pub any_bone_material: Option<()>,
    /// Reagent material must have the `[HORN]` token.
    #[token_de(token = "ANY_HORN_MATERIAL")]
    pub any_horn_material: Option<()>,
    /// Reagent material must have the `[LEATHER]` token.
    #[token_de(token = "ANY_LEATHER_MATERIAL")]
    pub any_leather_material: Option<()>,
    /// Reagent material must have the `[PEARL]` token.
    #[token_de(token = "ANY_PEARL_MATERIAL")]
    pub any_pearl_material: Option<()>,
    /// Reagent material must be subordinate to a `PLANT` object.
    #[token_de(token = "ANY_PLANT_MATERIAL")]
    pub any_plant_material: Option<()>,
    /// Reagent material must have the `[SHELL]` token.
    #[token_de(token = "ANY_SHELL_MATERIAL")]
    pub any_shell_material: Option<()>,
    /// Reagent material must have the `[SILK]` token.
    #[token_de(token = "ANY_SILK_MATERIAL")]
    pub any_silk_material: Option<()>,
    /// Reagent material must have the `[SOAP]` token.
    #[token_de(token = "ANY_SOAP_MATERIAL")]
    pub any_soap_material: Option<()>,
    /// Reagent is made of a tissue having `[TISSUE_SHAPE:STRANDS]`, intended for matching hair and
    /// wool. Must be used with `[USE_BODY_COMPONENT]`.
    #[token_de(token = "ANY_STRAND_TISSUE")]
    pub any_strand_tissue: Option<()>,
    /// Reagent material must have the `[TOOTH]` token.
    #[token_de(token = "ANY_TOOTH_MATERIAL")]
    pub any_tooth_material: Option<()>,
    /// Reagent material must have the `[YARN]` token.
    #[token_de(token = "ANY_YARN_MATERIAL")]
    pub any_yarn_material: Option<()>,
    /// Reagent material must have the `[SOIL_SAND]` token.
    #[token_de(token = "IS_SAND_MATERIAL")]
    pub is_sand_material: Option<()>,
    /// Reagent material must have the `[ITEMS_HARD]` token.
    #[token_de(token = "HARD_ITEM_MATERIAL")]
    pub hard_item_material: Option<()>,
    /// Reagent material must have the `[ITEMS_METAL]` token.
    #[token_de(token = "METAL_ITEM_MATERIAL")]
    pub metal_item_material: Option<()>,
    /// Reagent has to be a bag. Intended to be used with an item type of `BOX`, to prevent chests,
    /// coffers, and other containers from being used instead.
    #[token_de(token = "BAG")]
    pub bag: Option<()>,
    /// Reagent is able to be used to build structures (Stone, Wood, Blocks, Bars?).
    #[token_de(token = "BUILDMAT")]
    pub build_material: Option<()>,
    /// Reagent can be an Artifact. Using `[PRESERVE_REAGENT]` with this is strongly advised.
    #[token_de(token = "CAN_USE_ARTIFACT")]
    pub can_use_artifact: Option<()>,
    /// Allows the reagent to be an item that is otherwise reserved for use by a hospital.
    #[token_de(token = "CAN_USE_HOSPITAL_RESERVED")]
    pub can_use_hospital_reserved: Option<()>,
    /// Allows the reagent to be an item that is otherwise reserved for use by a location.
    #[token_de(token = "CAN_USE_LOCATION_RESERVED")]
    pub can_use_location_reserved: Option<()>,
    /// Reagent is a container that holds the specified reagent.
    #[token_de(token = "CONTAINS")]
    pub contains: Option<Reference>,
    /// Reagent must be a `BARREL` or `TOOL` which contains at least one item of type `LIQUID_MISC` made of `LYE`.
    /// Use of this token is discouraged, as it does not work with buckets (instead, use `[CONTAINS:lye]`
    /// and a corresponding lye reagent `[REAGENT:lye:150:LIQUID_MISC:NONE:LYE]`).
    #[token_de(token = "CONTAINS_LYE", alias = "POTASHABLE")]
    pub contains_lye: Option<Reference>,
    /// Reagent material must have `[ABSORPTION:0]`
    #[token_de(token = "DOES_NOT_ABSORB")]
    pub does_not_absorb: Option<()>,
    /// Performing a reaction with large stacks of inputs can allow multiple sets of outputs to be
    /// produced. Setting this flag causes the reagent to be ignored in this process -
    /// for example, with the reaction "1 plant + 1 barrel -> 5 alcohol (into barrel)",
    /// using this on the barrel allows the reaction to be performed as
    /// "5 plant + 1 barrel -> 25 alcohol" instead of "5 plant + 5 barrel -> 25 alcohol".
    #[token_de(token = "DOES_NOT_DETERMINE_PRODUCT_AMOUNT")]
    pub does_not_determine_product_amount: Option<()>,
    /// If the reagent is a container, it must be empty.
    #[token_de(token = "EMPTY")]
    pub empty: Option<()>,
    /// Reagent must be considered fire-safe (stable temperature below 11000 °U )
    /// - i.e. not wood, and not coal.
    #[token_de(token = "FIRE_BUILD_SAFE")]
    pub fire_build_safe: Option<()>,
    /// Reagent must be a barrel or any non-absorbing tool with `[TOOL_USE:FOOD_STORAGE]`
    #[token_de(token = "FOOD_STORAGE_CONTAINER")]
    pub food_storage_container: Option<()>,
    /// Reagent material has `[IS_GLASS]`.
    #[token_de(token = "GLASS_MATERIAL")]
    pub glass_material: Option<()>,
    /// Similar to `HAS_MATERIAL_REACTION_PRODUCT`, but requires the reagent's material to
    /// have a matching `ITEM_REACTION_PRODUCT` entry.
    #[token_de(token = "HAS_ITEM_REACTION_PRODUCT")]
    pub has_item_reaction_product: Option<Reference>, // TODO See REACTION_CLASS in MaterialDefinitionToken
    /// Similar to `REACTION_CLASS`, but requires the reagents material to have a matching
    /// `MATERIAL_REACTION_PRODUCT` entry. Intended for reactions which transform one class of
    /// material into another, such as skin->leather and fat->tallow.
    #[token_de(token = "HAS_MATERIAL_REACTION_PRODUCT")]
    pub has_material_reaction_product: Option<Reference>, // TODO See REACTION_CLASS in MaterialDefinitionToken
    /// Reagent must be a tool with the specific `TOOL_USE` value.
    /// The reagents item type must be `TOOL:NONE` for this to make any sense.
    #[token_de(token = "HAS_TOOL_USE")]
    /// Reference to `ITEM_TOOL` but the category like: `LIQUID_CONTAINER`.
    /// The item type must be `TOOL:NONE` for this to make any sense.
    pub has_tool_use: Option<Reference>, // TODO
    /// Reagent must contain writing.
    #[token_de(token = "HAS_WRITING_IMPROVEMENT")]
    pub has_writing_improvement: Option<()>,
    /// *Currently broken*
    ///
    /// Reagent must be considered magma-safe (stable temperature below 12000 °U ).
    #[token_de(token = "MAGMA_BUILD_SAFE")]
    pub magma_build_safe: Option<()>,
    /// Reagent material must be an ore of the specified metal.
    #[token_de(token = "METAL_ORE")]
    pub metal_ore: Option<Reference>, // TODO reference to Inorganic material
    /// Reagent's item dimension must be at least this large. The reagent's item type must be
    /// `BAR`, `POWDER_MISC`, `LIQUID_MISC`, `DRINK`, `THREAD`, `CLOTH`, or `GLOB` for this to work.
    #[token_de(token = "MIN_DIMENSION")]
    pub min_dimension: Option<u32>,
    /// Item must not have an edge, so must be blunt.
    /// Sharp stones (produced using knapping) and most types of weapon/ammo
    /// can not be used with this token.
    #[token_de(token = "NO_EDGE_ALLOWED")]
    pub no_edge_allowed: Option<()>,
    /// Reagent must be sharpened (used for carving).
    #[token_de(token = "HAS_EDGE")]
    pub has_edge: Option<()>,
    /// If the item is a container, it must not contain lye or milk.
    /// Not necessary if specifying `[EMPTY]`.
    #[token_de(token = "NOT_CONTAIN_BARREL_ITEM")]
    pub not_contain_barrel_item: Option<()>,
    /// Reagent can not be engraved. For example, a memorial slab can not be engraved.
    #[token_de(token = "NOT_ENGRAVED")]
    pub not_engraved: Option<()>,
    /// Reagent has not been decorated.
    #[token_de(token = "NOT_IMPROVED")]
    pub not_improved: Option<()>,
    /// Reagent must not be in the `SOLID_PRESSED` state.
    #[token_de(token = "NOT_PRESSED")]
    pub not_pressed: Option<()>,
    /// Reagent must be "collected" - used with `THREAD:NONE` to exclude webs.
    #[token_de(token = "NOT_WEB")]
    pub not_web: Option<()>,
    /// Reagent is not destroyed, which is the normal effect, at the completion of the reaction.
    /// Typically used for containers.
    #[token_de(token = "PRESERVE_REAGENT")]
    pub preserve_reagent: Option<()>,
    /// Requires the reagents material to have a matching `REACTION_CLASS` entry.
    /// Intended for reactions which accept a variety of materials but where the input material
    /// does not determine the output material, such as `FLUX` (for making pig iron and steel)
    /// and GYPSUM (for producing plaster powder).
    #[token_de(token = "REACTION_CLASS")]
    pub reaction_class: Option<Reference>, // TODO
    /// Reagent must not be rotten, mainly for organic materials.
    #[token_de(token = "UNROTTEN")]
    pub unrotten: Option<()>,
    /// Reagent material must come off a creature's body (`CORPSE` or `CORPSEPIECE`).
    #[token_de(token = "USE_BODY_COMPONENT")]
    pub use_body_component: Option<()>,
    /// Reagent must be "undisturbed" - used with `THREAD:NONE` to gather webs.
    #[token_de(token = "WEB_ONLY")]
    pub web_only: Option<()>,
    /// Reagent is made of an non-economic stone.
    #[token_de(token = "WORTHLESS_STONE_ONLY")]
    pub worthless_stone_only: Option<()>,
}

#[derive(Serialize, Deserialize, Clone, Debug, Default, TokenDeserialize, PartialEq, Eq)]
pub struct ProductToken {
    /// Defines a thing that comes out of the reaction. `GET_MATERIAL_FROM_REAGENT` and
    /// `GET_ITEM_DATA_FROM_REAGENT` can be used to defer the choice of material and/or item to
    /// the appropriate tag in a given reagent's material - the former comes in place of the
    /// material token, the latter replaces both the item and material tokens.
    /// Arguments are:
    ///    `<probability of success(0-100)>:<quantity>:<item token>:<item subtype>:<mat tokens>`
    /// `<mat tokens>` might consists of 3 arguments,
    /// for example: `GET_MATERIAL_FROM_REAGENT:A:TAN_MAT`
    /// Other times it has 2 extra arguments like: `INORGANIC:BRONZE_COATING`
    // Tarn comment:
    // > In the product, if you want to use the reagent's material itself,
    // > use NONE instead of a reaction product class (TAN_MAT in this example).
    // |---0---|-1-|2|-----------------3--------------------|---------------------4-------------------|
    // [PRODUCT:100:1:SKIN_TANNED:NONE                      :GET_MATERIAL_FROM_REAGENT:A     :TAN_MAT ]
    // [PRODUCT:100:1:BAR        :NONE                      :GET_MATERIAL_FROM_REAGENT:tallow:SOAP_MAT]
    // [PRODUCT:100:4:BAR        :NO_SUBTYPE                :METAL                    :STERLING_SILVER]
    // [PRODUCT:100:1:WEAPON     :ITEM_WEAPON_SPEAR_TRAINING:GET_MATERIAL_FROM_REAGENT:log   :NONE    ]
    #[token_de(token = "PRODUCT", on_duplicate_to_parent, primary_token)]
    pub reference: Option<(
        u8,  // probability_success
        u32, // quantity
        Choose<
            (ItemReferenceArg, MaterialTokenArgWithReagentMat), // item token, then material token
            (
                GetItemDataFromReagentEnum, // signals to use specific reagent's item and material
                ReferenceTo<ReagentToken>,  // signals which LOCALLY defined reagent you mean
                Choose<NoneEnum, Reference>, // TODO ref is to a MATERIAL_REACTION_PRODUCT reaction class
            ),
        >,
    )>,
    /// Product is given a sharp edge. Used for knapping.
    #[token_de(token = "FORCE_EDGE")]
    pub force_edge: Option<()>,
    /// Specifies the size of the product.
    /// A size of 150 is typical for `BAR`, `POWDER_MISC`, `LIQUID_MISC`, `DRINK`, and `GLOB`.
    /// A size of 15000 is typical for `THREAD`,
    /// and a size of 10000 is typical for `CLOTH`.
    #[token_de(token = "PRODUCT_DIMENSION")]
    pub product_dimension: Option<u32>,
    /// Product is created in the `SOLID_PASTE` state.
    #[token_de(token = "PRODUCT_PASTE")]
    pub product_paste: Option<()>,
    /// Product is created in the `SOLID_PRESSED` state.
    #[token_de(token = "PRODUCT_PRESSED")]
    pub product_pressed: Option<()>,
    /// Places the product in a container; `<id>` must be the name of a reagent with
    /// the `PRESERVE_REAGENT` token and a container item type.
    #[token_de(token = "PRODUCT_TO_CONTAINER")]
    pub product_to_container: Option<Reference>, // TODO refer to `[REAGENT:<This_value>:..]`
    /// Allows the product to be referred to by the given name, for the purpose of being passed
    /// down as argument in other tokens.
    #[token_de(token = "PRODUCT_TOKEN")]
    pub product_token: Option<Reference>, // TODO refer to `[REAGENT:<This_value>:..]`
    /// Transfers artifact status from the reagent to the product.
    #[token_de(token = "TRANSFER_ARTIFACT_STATUS")]
    pub transfer_artifact_status: Option<()>,
}

#[derive(
    Serialize, Deserialize, Clone, Debug, Default, TokenDeserialize, PartialEq, Eq, Referenceable,
)]
pub struct ReactionCategoryToken {
    /// Argument 1 of `[CATEGORY:...]`
    #[token_de(token = "CATEGORY", on_duplicate_to_parent, primary_token)]
    #[referenceable(self_reference)]
    pub reference: Option<ReferenceTo<Self>>,
    /// The name of the category as displayed in-game.
    #[token_de(token = "CATEGORY_NAME")]
    pub category_name: Option<String>,
    /// If present, when the category is highlighted in a building menu, this string will be
    /// displayed in the Helpful Hint box.
    #[token_de(token = "CATEGORY_DESCRIPTION")]
    pub category_description: Option<String>,
    /// If present, when the category is highlighted in a building menu, this string will be
    /// displayed in the Helpful Hint box.
    #[token_de(token = "CATEGORY_PARENT")]
    pub category_parent: Option<ReferenceTo<Self>>,
    /// If present, this category can be selected from its parent menu (whether a building or a
    /// parent category) using the given hotkey.
    #[token_de(token = "CATEGORY_KEY")]
    pub category_key: Option<Choose<KeyBindEnum, NoneEnum>>, // param is KeyBind
}

#[derive(Serialize, Deserialize, Clone, Debug, TokenDeserialize, PartialEq, Eq)]
#[token_de(enum_value)]
pub enum GetItemDataFromReagentEnum {
    #[token_de(token = "GET_ITEM_DATA_FROM_REAGENT")]
    GetItemDataFromReagent,
}
impl Default for GetItemDataFromReagentEnum {
    fn default() -> Self {
        Self::GetItemDataFromReagent
    }
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
pub enum ImprovementTypeTokenArg {
    // TODO: token should be marked as deprecated/unused/ignored by the game, see #83
    // #[token_de(token = "ART_IMAGE")]
    // ArtImage,
    // #[token_de(token = "COVERED")]
    Covered,
    //#[token_de(token = "GLAZED")]
    Glazed,
    // #[token_de(token = "RINGS_HANGING")]
    RingsHanging,
    // #[token_de(token = "BANDS")]
    Bands,
    // #[token_de(token = "SPIKES")]
    Spikes,
    // #[token_de(token = "SPECIFIC", alias = "ITEMSPECIFIC")]
    Specific(ItemSpecificEnum),
    // TODO: token should be marked as deprecated/unused/ignored by the game, see #83
    // #[token_de(token = "THREAD")]
    // Thread,
    // TODO: token should be marked as deprecated/unused/ignored by the game, see #83
    // #[token_de(token = "CLOTH")]
    // Cloth,
    // TODO: token should be marked as deprecated/unused/ignored by the game, see #83
    // #[token_de(token = "SEWN_IMAGE")]
    // SewnImage,
    // #[token_de(token = "PAGES")]
    Pages,
    // TODO: token should be marked as deprecated/unused/ignored by the game, see #83
    // #[token_de(token = "ILLUSTRATION")]
    // Illustration,
    // #[token_de(token = "INSTRUMENT_PIECE")]
    InstrumentPiece(Reference), // Ref to INSTRUMENT PIECE, distinct from ITEM_INSTRUMENT
    // #[token_de(token = "WRITING")]
    Writing,
    // TODO: token should be marked as deprecated/unused/ignored by the game, see #83
    // #[token_de(token = "IMAGE_SET")]
    // ImageSet,
}
impl Default for ImprovementTypeTokenArg {
    fn default() -> Self {
        Self::Covered
    }
}

// Deserialize a token with following pattern: `[REF:improvement_type_token_arg:...]`
df_ls_syntax_analysis::token_deserialize_unary_token!(ImprovementTypeTokenArg);

impl TryFromArgumentGroup for ImprovementTypeTokenArg {
    fn try_from_argument_group(
        token: &mut Token,
        source: &str,
        diagnostics: &mut DiagnosticsInfo,
        add_diagnostics_on_err: bool,
    ) -> Result<Self, ()> {
        // Safe first argument for error case
        let arg0 = match token.get_current_arg() {
            Ok(arg) => Ok(arg.clone()),
            Err(err) => Err(err),
        };
        let reference_arg0 =
            Reference::try_from_argument_group(token, source, diagnostics, add_diagnostics_on_err)?;
        let bp_criteria = match reference_arg0.0.as_ref() {
            "COVERED" => ImprovementTypeTokenArg::Covered,
            "GLAZED" => ImprovementTypeTokenArg::Glazed,
            "RINGS_HANGING" => ImprovementTypeTokenArg::RingsHanging,
            "BANDS" => ImprovementTypeTokenArg::Bands,
            "SPIKES" => ImprovementTypeTokenArg::Spikes,
            // TODO: add alias warning
            "SPECIFIC" | "ITEMSPECIFIC" => {
                let specific = <ItemSpecificEnum>::try_from_argument_group(
                    token,
                    source,
                    diagnostics,
                    add_diagnostics_on_err,
                )?;
                ImprovementTypeTokenArg::Specific(specific)
            }
            "PAGES" => ImprovementTypeTokenArg::Pages,
            "INSTRUMENT_PIECE" => {
                let instrument_piece = <Reference>::try_from_argument_group(
                    token,
                    source,
                    diagnostics,
                    add_diagnostics_on_err,
                )?;
                ImprovementTypeTokenArg::InstrumentPiece(instrument_piece)
            }
            "WRITING" => ImprovementTypeTokenArg::Writing,
            _ => {
                Self::diagnostics_wrong_enum_type(
                    &arg0?,
                    vec![
                        "COVERED",
                        "GLAZED",
                        "RINGS_HANGING",
                        "BANDS",
                        "SPIKES",
                        "SPECIFIC",
                        "ITEMSPECIFIC",
                        "PAGES",
                        "INSTRUMENT_PIECE",
                        "WRITING",
                    ],
                    source,
                    diagnostics,
                    add_diagnostics_on_err,
                );
                return Err(());
            }
        };
        Ok(bp_criteria)
    }
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
pub enum ReactionDescriptionUseItemTokenArg {
    // TODO find out if all item types can actually be used
    UseAmmo(ReferenceTo<AmmoToken>),
    UseArmor(ReferenceTo<ArmorToken>),
    UseFood(ReferenceTo<FoodToken>),
    UseGloves(ReferenceTo<GlovesToken>),
    UseHelm(ReferenceTo<HelmToken>),
    UseInstrument(ReferenceTo<InstrumentToken>),
    UsePants(ReferenceTo<PantsToken>),
    UseShield(ReferenceTo<ShieldToken>),
    UseShoes(ReferenceTo<ShoesToken>),
    UseSiegeAmmo(ReferenceTo<SiegeAmmoToken>),
    UseTool(ReferenceTo<ToolToken>),
    UseToy(ReferenceTo<ToyToken>),
    UseTrapComp(ReferenceTo<TrapCompToken>),
    UseWeapon(ReferenceTo<WeaponToken>),
}
impl Default for ReactionDescriptionUseItemTokenArg {
    fn default() -> Self {
        Self::UseTool(ReferenceTo::new(String::default()))
    }
}

// Deserialize a token with following pattern: `[REF:reaction_description_use_item:...]`
df_ls_syntax_analysis::token_deserialize_unary_token!(ReactionDescriptionUseItemTokenArg);

impl TryFromArgumentGroup for ReactionDescriptionUseItemTokenArg {
    fn try_from_argument_group(
        token: &mut Token,
        source: &str,
        diagnostics: &mut DiagnosticsInfo,
        add_diagnostics_on_err: bool,
    ) -> Result<Self, ()> {
        // Safe first argument for error case
        let arg0 = match token.get_current_arg() {
            Ok(arg) => Ok(arg.clone()),
            Err(err) => Err(err),
        };
        let reference_arg0 =
            Reference::try_from_argument_group(token, source, diagnostics, add_diagnostics_on_err)?;
        let bp_criteria = match reference_arg0.0.as_ref() {
            "USE_AMMO" => {
                let item_ref = <ReferenceTo<AmmoToken>>::try_from_argument_group(
                    token,
                    source,
                    diagnostics,
                    add_diagnostics_on_err,
                )?;
                ReactionDescriptionUseItemTokenArg::UseAmmo(item_ref)
            }
            "USE_ARMOR" => {
                let item_ref = <ReferenceTo<ArmorToken>>::try_from_argument_group(
                    token,
                    source,
                    diagnostics,
                    add_diagnostics_on_err,
                )?;
                ReactionDescriptionUseItemTokenArg::UseArmor(item_ref)
            }
            "USE_FOOD" => {
                let item_ref = <ReferenceTo<FoodToken>>::try_from_argument_group(
                    token,
                    source,
                    diagnostics,
                    add_diagnostics_on_err,
                )?;
                ReactionDescriptionUseItemTokenArg::UseFood(item_ref)
            }
            "USE_GLOVES" => {
                let item_ref = <ReferenceTo<GlovesToken>>::try_from_argument_group(
                    token,
                    source,
                    diagnostics,
                    add_diagnostics_on_err,
                )?;
                ReactionDescriptionUseItemTokenArg::UseGloves(item_ref)
            }
            "USE_HELM" => {
                let item_ref = <ReferenceTo<HelmToken>>::try_from_argument_group(
                    token,
                    source,
                    diagnostics,
                    add_diagnostics_on_err,
                )?;
                ReactionDescriptionUseItemTokenArg::UseHelm(item_ref)
            }
            "USE_INSTRUMENT" => {
                let item_ref = <ReferenceTo<InstrumentToken>>::try_from_argument_group(
                    token,
                    source,
                    diagnostics,
                    add_diagnostics_on_err,
                )?;
                ReactionDescriptionUseItemTokenArg::UseInstrument(item_ref)
            }
            "USE_PANTS" => {
                let item_ref = <ReferenceTo<PantsToken>>::try_from_argument_group(
                    token,
                    source,
                    diagnostics,
                    add_diagnostics_on_err,
                )?;
                ReactionDescriptionUseItemTokenArg::UsePants(item_ref)
            }
            "USE_SHIELD" => {
                let item_ref = <ReferenceTo<ShieldToken>>::try_from_argument_group(
                    token,
                    source,
                    diagnostics,
                    add_diagnostics_on_err,
                )?;
                ReactionDescriptionUseItemTokenArg::UseShield(item_ref)
            }
            "USE_SHOES" => {
                let item_ref = <ReferenceTo<ShoesToken>>::try_from_argument_group(
                    token,
                    source,
                    diagnostics,
                    add_diagnostics_on_err,
                )?;
                ReactionDescriptionUseItemTokenArg::UseShoes(item_ref)
            }
            "USE_SIEGEAMMO" => {
                let item_ref = <ReferenceTo<SiegeAmmoToken>>::try_from_argument_group(
                    token,
                    source,
                    diagnostics,
                    add_diagnostics_on_err,
                )?;
                ReactionDescriptionUseItemTokenArg::UseSiegeAmmo(item_ref)
            }
            "USE_TOOL" => {
                let item_ref = <ReferenceTo<ToolToken>>::try_from_argument_group(
                    token,
                    source,
                    diagnostics,
                    add_diagnostics_on_err,
                )?;
                ReactionDescriptionUseItemTokenArg::UseTool(item_ref)
            }
            "USE_TRAPCOMP" => {
                let item_ref = <ReferenceTo<TrapCompToken>>::try_from_argument_group(
                    token,
                    source,
                    diagnostics,
                    add_diagnostics_on_err,
                )?;
                ReactionDescriptionUseItemTokenArg::UseTrapComp(item_ref)
            }
            "USE_WEAPON" => {
                let item_ref = <ReferenceTo<WeaponToken>>::try_from_argument_group(
                    token,
                    source,
                    diagnostics,
                    add_diagnostics_on_err,
                )?;
                ReactionDescriptionUseItemTokenArg::UseWeapon(item_ref)
            }
            _ => {
                Self::diagnostics_wrong_enum_type(
                    &arg0?,
                    vec!["USE_INSTRUMENT", "USE_TOOL"],
                    source,
                    diagnostics,
                    add_diagnostics_on_err,
                );
                return Err(());
            }
        };
        Ok(bp_criteria)
    }
}