Expression language syntax reference - DW2MC/DW2ModLoader GitHub Wiki

Syntax Reference

This document describes the syntax of expressions used inside of YAML to perform lookups, selections or calculations.

Typically this is found after $where ($where: item.Amount is 0), inside of collection key expressions (surrounded by parentheses, (item.Amount is 0):), after numeric members Amount: 32 * Scale, or in explicit expression members ($Description: SharedDescription concat "\nThis one is for Humans.")

Literals

Booleans

true and false

Numbers

Normal base 10 decimal rules.

Example: 10, 12.34e+5

/[-+]?[0-9]*\.?[0-9]+(?:e[-+]?[0-9]+)/

Strings

Normal string rules, double quotes.

Example: "Example.", "New\nlines\n"

/(?<![""\\])""(?:[^""\\]|\\.)*?""(?!=[""\\])/

Escape sequences

These are the same escape sequences as C#.

Sequence Name Code
\' Single quote 0x27
\" Double quote 0x22
\\ Backslash 0x5C
\0 Null 0x00
\a Alert 0x07
\b Backspace 0x08
\f Form feed 0x0C
\n New line 0x0A
\r Carriage return 0x0D
\t Horizontal tab 0x09
\v Vertical tab 0x0B
\xH[H][H][H] UTF-16, 0 - FFFF 0xHHHH
\uHHHH UTF-16, 0000 - FFFF 0xHHHH
\U00HHHHHH UTF-32, 000000 - 10FFFF 0x00HHHHHH

Special Symbols

Symbol Description Value
Pi Pi (double precision) 3.14159265358979
ฯ€ Same as above
Tau .NET 6: Tau (double precision), 4: reserved 6.28318530717959
ฯ„ Same as above
+Inf Positive infinity +โˆž
+Infinity Same as above
+โˆž Same as above
-Inf Negative infinity -โˆž
-Infinity Same as above
-โˆž Same as above
NaN Not a number Probably a quiet NaN
e Natural logarithmic base 2.71828182845905

Conditional and reserved symbols

def

In the context of a definition, refers to the definition object. So for example, in a Government: context, any fields on the Government definition type are valid members.

  • The ID of the government, def.GovernmentId
  • The government's name, def.Name
  • The government's first bonus' type, def.Bonuses[0].Type

Otherwise this symbol's name should be reserved.

collection

In the context of a collection, refers to the collection itself.

So for example, in a Government: ... Bonuses: collection context, you have access to the collection itself.

  • The government's first bonus, collection[0]
  • The type of the first bonus, collection[0].Type

Otherwise this symbol's name should be reserved.

item

In the context of a collection, refers to the collection item.

In the context of a selection expression, (e.g. list item selection, e.g. (...)) refers to the current item.

So for example, in a Government: ... Bonuses: collection context, any fields on the Bonus type are valid members.

  • The type of the bonus, item.Type

Otherwise this symbol's name should be reserved.

value

In the context of a field member, refers to the previous value.

In the context of a selection expression, (e.g. list item selection, e.g. (...)) refers to the current value.

For example, when you're updating a value using an update or update-all instruction,

  • To get the previous value, you can just refer to value
  • To replace some text in a previous Description: field, you can express it as,
    $Description: value replace "Technocracy government type is only available to the Ackdarian race." with "All your base are belong to us."

Otherwise this symbol's name should be reserved.

empire

In the context of an empire specific definition (like EmpirePolicy), refers to the Empire object.

Currently this is only applicable to the EmpirePolicy: context.

Since this definition type can apply to multiple empires, you may want to express different possibilities.

This should always be of a Empire type object while in the context of an empire specific type.

Otherwise this symbol's name should be reserved.

game

This refers to the DWGame object. You can use this to get and compare the Game's version.

Example: v(game.Version) in versions "(,1.1)"

galaxy

This refers to the Galaxy object.

Generally this is only available in the context of a running game, such as during empire creation and updating of EmpirePolicy.

Otherwise this symbol's name should be reserved.

loader

This refers to the ModManager object. You can use this to get and compare the Mod Loader's version.

Example: loader.Version in versions "[1.2,)"

You may theoretically also use this to check for the existence of other mods. (Needs to be documented.)

Objects and Collections

Member resolution

. is used to resolve member fields and properties.

Example: A.B will resolve the B member of A.

Index resolution

[expression] where expression can be a number or full expression, performs an index resolution operation as necessary to access a collection member. The majority of indexes are zero based, it is probably safe to assume all of them are. This may be referred to as a subexpression or nested expression.

Example: A[11] (or A[8+3], etc.) will resolve index 11 in the collection of A. If A is a string, this will return the 12 character, otherwise it may return the 12th item.

Operators

Expression Description Arity
A + B Add Binary
A - B Subtract Binary
A โˆ’ B Same as above Binary
A * B Multiply Binary
A โˆ™ B Same as above (not a period) Binary
A ร— B Same as above Binary
A / B Divide Binary
A รท B Same as above Binary
A โˆถ B Same as above (not a colon) Binary
A โˆ• B Same as above Binary
A % B Modulus Binary
A ^ B Raise to power Binary
A < B Less than comparison Binary
A > B Less than comparison Binary
A <= B Less than or equal to comparison Binary
A >= B Less than or equal to comparison Binary
A โ‰ค B Less than or equal to comparison Binary
A โ‰ฅ B Greater than or equal to comparison Binary
A is B Equal to comparison Binary
A is not B Not equal to comparison Binary
A and B Logical and boolean combination Binary
A and not B Logical and not boolean combination Binary
A or B Logical or boolean combination Binary
A or not B Logical or boolean combination Binary
A .. B Text concatenation operation Binary
A contains B Text, set or collection contains test Binary
A starts B Text starts with test Binary
A ends B Text ends with test Binary
A matches B Regular expression text match test Binary
A replace B with C Regular expression text replace operation Trinary
A repeat B Text repetition operation Binary
A in versions B Version range test (NuGet style semantic versions) Binary

Built-in functions

Function Description Example
abs(A) Absolute value abs(-12.34)
sin(A) Trigonometric sine operation sin(12.34)
asin(A) Trigonometric arc-sine operation asin(12.34)
cos(A) Trigonometric cosine operation cos(12.34)
acos(A) Trigonometric arc-sine operation acos(12.34)
tan(A) Trigonometric tangent operation tan(12.34)
atan(A) Trigonometric arc-tangent operation atan(-12.34)
atan2(A,B) Trigonometric arc-tangent operation, with sign atan2(-12.34,0)
pow(A,B) Raise to power operation pow(12.34,12.34)
sqrt(A) Square root operation sqrt(12.34)
exp(A) Exponent operation exp(12.34)
log(A) Natural logarithm operation log(12.34)
log10(A) Base 10 logarithm operation log10(12.34)
frac(A) Get the fraction of a number frac(12.34) is .34
round(A) Round a number at the midpoint up round(12.34) is 12
floor(A) Round down a number floor(12.34) is 12
ceil(A) Round up a number ceil(12.34) is 13
trunc(A) Truncate a number, remove the fraction trunc(12.34) is 12
count(A) Text length or collection count function count("length") is 6
isNaN(A) Not a number check isNaN(NaN) is true
isInf(A) Infinity check isInf(+Inf) is true
txt(A) Text conversion txt(32) is "32"
num(A) Number conversion num("32") is 32
bool(A) Bool conversion bool(0) is false
(false, 0, null and blank strings are false) bool(32) is true
type(A) Type description text type(something)
v(A) Version conversion v("1.0.0.0-x+y")
โš ๏ธ **GitHub.com Fallback** โš ๏ธ