Skip to main content

Parser Reference

Laurence MorganAbout 5 minparser

Parser Reference

Syntax

Murex supports both expressions and statements. You can use the interchangeably with your code and the Murex parser will decide whether to run that code as an expression or statement.

Expressions

Expressions are patterns formed like equations (eg $val = 1 + 2).

Strings must be quoted in expressions.

Statements

Statements are traditional shell command calls (eg command parameters...).

Quoting strings is optional in statements.

Not all operators are supported in statements.

Order Of Operations

Expressions and statements are split by pipes and terminators. Each statement and expression is executed from left to right, with the statement or expression parsed by the following rules of operation

Order of operations:

  1. expression or statement discovery
  2. sub-shells / sub-expressions
  3. multiplication / division (expressions only)
  4. addition / subtraction (expressions only)
  5. comparisons, eg greater than
  6. logical and (sub-expressions only)
  7. logical or (sub-expressions only)
  8. elvis (expressions only)
  9. assign (expressions only)
  10. left to right

Expression Or Statement Discovery

First a command is read as an expression. Because the rules of parsing expressions are stricter than statements, everything is assumed to be an expression unless the expression parser fails, which then it is assumed to be a statement.

Operators And Tokens

Terminology

  • left: this is the value to the left hand side of the operator
  • right: this is the value to the right hand side of the operator

Example: left operator right

Modifiers

All modifiers replace the left, operator and right with the returned value of the modifier.

All returns will be num data type (or their original type if strict types is enabled).

Modifiers are only supported in expressions.

OperatorNameOperation
*MultiplicationMultiply left by right
/DivideDivide left by right
+AdditionAdd left with right
-SubtractionSubtract left by right

Read more:

Comparators

All comparators replace the left, operator and right with the returned value of the comparator.

All returns will be bool data type, either true or false.

Comparators are only supported in expressions.

OperatorNameOperation
>Greater Thantrue if left is greater than right
>=Greater Or Equal Totrue if left is greater or equal to right
<Less Thantrue if left is less than right
<=Less Or Equal Totrue if left is less or equal to right
==Equal Totrue if left is equal to right
!=Not Equal Tofalse if left is equal to right
~~Liketrue if left string is like right string
!!Not Likefalse if left string is like right string
=~Matches Regexptrue if left matches regexp pattern on right
!~Does Not Match Regexpfalse if left matches regexp pattern on right

Read more:

Assignment

Assignment returns true if successful.

Assignment is only supported in expressions.

OperatorNameOperation
=Assign (overwrite)Assign right to left
:=Assign (retain)EXPERIMENTAL
<~Assign Or MergeMerge right (array / object) into left
+=Assign And AddAdd right to left and assign to left
-=Assign And SubtractSubtract right from left and assign to left
*=Assign And MultiplyMultiply right with left and assign to left
/=Assign And DivideDivide right with left and assign to left

Read more:

Conditionals

Conditionals replace left, operator and right with the value defined in operation.

These conditionals are only supported in expressions.

OperatorNameOperation
??Null CoalescenceReturns left if not null, otherwise right
?:ElvisReturns left if truthy, otherwise right

Read more:

Sigils

Sigils are special prefixes that provide hints to the parser.

Sigils are supported in both expressions and statements.

TokenNameOperation
$ScalarExpand value as a string
@ArrayExpand value as an array
~HomeExpand value as the persons home directory
%BuilderCreate an array, map or nestable string

Constants

Constants are supported in both expressions and statements. However null, true, false and number will all be interpreted as strings in statements.

TokenNameOperation
nullNullnull (null / nil / void) type
trueTruebool (boolean) true
falseFalsebool (boolean) false
numberNumbernum (numeric) value
'string'String Literalstr (string) literal value
"string"Infix Stringstr (string) value, supports escaping & infixing
%(string)String BuilderCreates a nestable str (string)
%[array]Array BuilderCreates a json (JSON) array (list)
%{map}Object BuilderCreates a json (JSON) object (map / dictionary)

Read more:

Sub-shells

Sub-shells are a way of inlining expressions or statements into an existing expression or statement. Because of this they are supported in both.

SyntaxNameOperation
command( parameters... )C-Style FunctionsInline a command as a function
${command parameters...}Sub-shell (scalar)Inline a command line as a string
@{command parameters...}Sub-shell (array)expand a command line as an array
(expression)Sub-expressionInline an expression (statement)
(expression)Sub-expressionOrder of evaluation (expression)

Read more:

Boolean Operations

Boolean operators behave like pipes.

They are supported in both expressions and statements.

OperatorNameOperation
&&AndEvaluates right if left is truthy
||OrEvaluates right if left is falsy

Pipes

Pipes always flow from left to right.

They are supported in both expressions and statements.

OperatorNameOperation
|POSIX PipePOSIX compatibility
->Arrow PipeContext aware pipe
=>Generic PipeConvert stdout to * (generic) then pipe
|>Truncate FileWrite stdout to file, overwriting contents
>>Append FileWrite stdout to file, appending contents

Terminators

"LF" refers to the life feed character, which is a new line.

TokenNameOperation
;Semi-ColonEnd of statement or expression (optional)
LFLine FeedEnd of statement or expression (new line)

Escape Codes

Any character can be escaped via \ to signal it isn't a token. However some characters have special meanings when escaped.

"LF" refers to the life feed character, which is a new line.

TokenNameOperation
\sSpaceSame as a space character
\tTabSame as a tab character
\rCarriage ReturnCarriage Return (CR) sometimes precedes LF
\nLine FeedLine Feed (LF), typically a new line
\LFEscaped Line FeedStatement continues on next line

Other Reference Material

Language Guides

  1. Language Tour, which is an introduction into the Murex language.

  2. Rosetta Stone, which is a reference table comparing Bash syntax to Murex's.

  3. Builtins, for docs on the core builtins.

Murex's Source Code

The parser is located Murex's source under the lang/ path of the project files.

Pages