Skip to content

Command script parsing

Martin Helmut Fieber edited this page Mar 21, 2021 · 8 revisions

Example

Command script example from the configuration file:

# litr.toml
build = "echo %{param}"

Parsing

Simplified Backus–Naur form (BNF [reference]):

# Entry point:
expression = literal
  | or_statement
  | if_statement
  | and_statement
  | function
  | variable

literal = string;
string = "'" .* "'";

or_statement = if_statement "or" expression;

if_statement = expression expression;

and_statement = expression "and" expression;

function = function_name "(" (expression ("," expression)*)? ")";
function_name = alpha (alpha | digit)*;

variable = short_variable | long_variable;
short_variable = strict_alpha;
long_variable = strict_alpha (alpha | digit)+;

digit = 0-9;
alpha = A-Za-z_;
strict_alpha = A-Za-z;

Legend:

  • | Or
  • () Grouping
  • ? Optional
  • * Repeat 0 to n times
  • + Repeat 1 to n times

Examples

# String literal
script = "%{'String'}"
# Variable
script = "%{parameter}"
# If-statement
script = "%{parameter 'String'}"
# Or-statement
script = "%{parameter 'Hello' or 'Bye'}"
# And-statement
script = "%{param1 and param2 'Hello'}"
# Function call
script = "%{fn()}"
# Function with parameters and statements
script = "%{fn1(var1, fn2()) 'First option' or fn3('Second option')}"

Clone this wiki locally