Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion docs/syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ The parser accepts a pretty basic grammar. It's similar to normal JavaScript exp
| ^ | Right | Exponentiation |
| +, -, not, sqrt, etc. | Right | Unary prefix operators (see below for the full list) |
| \*, /, % | Left | Multiplication, division, remainder |
| +, -, \|\| | Left | Addition, subtraction, array/list concatenation |
| +, -, \| | Left | Addition, subtraction, array/string concatenation |
| ==, !=, >=, <=, >, <, in | Left | Equals, not equals, etc. "in" means "is the left operand included in the right array operand?" |
| and | Left | Logical AND |
| or | Left | Logical OR |
Expand All @@ -30,6 +30,42 @@ const parser = new Parser({
// Now parser supports 'x in array' and 'y = 2*x' expressions
```

## Concatenation Operator

The `|` (pipe) operator concatenates arrays or strings:
- If both operands are arrays, they are concatenated as arrays
- If both operands are strings, they are concatenated as strings
- If operands are of different types, the result is `undefined`

| Operator | Description |
|:-------- |:----------- |
| a \| b | Concatenates `a` and `b` if both are arrays or both are strings; otherwise returns `undefined`. |

### Array Concatenation

When both operands are arrays, the `|` operator returns a new array containing all elements from both arrays:

```js
const parser = new Parser();

parser.evaluate('[1, 2] | [3, 4]'); // [1, 2, 3, 4]
parser.evaluate('[1] | [2] | [3]'); // [1, 2, 3]
parser.evaluate('["a", "b"] | ["c", "d"]'); // ["a", "b", "c", "d"]
```

### String Concatenation

When both operands are strings, the `|` operator returns a new string combining both:

```js
const parser = new Parser();

parser.evaluate('"hello" | " " | "world"'); // "hello world"
parser.evaluate('"a" | "b" | "c"'); // "abc"
```

> **Note:** Mixing types (e.g., an array with a string) will return `undefined`.

## Unary Operators

The parser has several built-in "functions" that are actually unary operators. The primary difference between these and functions are that they can only accept exactly one argument, and parentheses are optional. With parentheses, they have the same precedence as function calls, but without parentheses, they keep their normal precedence (just below `^`). For example, `sin(x)^2` is equivalent to `(sin x)^2`, and `sin x^2` is equivalent to `sin(x^2)`.
Expand Down