Implementations must act as if they used the following algorithm to evaluate POSIX-compliant dotenv files.
The inputs to the evaluation stage are:
- a sequence of nodes from the parsing stage
- an optional override flag
The output of the evaluation stage is the result of evaluating an assignment list.
The override flag is a boolean flag that defaults to false
The local scope is an associative array where both the keys and values are strings. It is initially empty.
When a step says to update the local scope with a given (key, value) pair:
- If the given key exists in the local scope:
- set the value associated with the given key to the given value
- Otherwise:
- insert the given (key, value) pair in the local scope.
A special marker value used to indicate the absence of a value.
Please refer to:
- The POSIX specification.
- The article on wikipedia.
An environment variable named name is defined if calling
getenv with name
does not return a null pointer.
- Create a new empty local scope.
- For each node in the sequence of input nodes:
- If the value of the node's
kindattribute is notAssignment:- Evaluation error.
- Otherwise, evaluate an assignment with node
- If the value of the node's
- Return the local scope.
- Let
namebe the value of the node'snameattribute - If the override flag is
false, and an environment variable namednameis defined:- Let
valuebe the value of the environment variable namedname
- Let
- Otherwise:
- Let
node-listbe the value of the node'svalueattribute - Let
valuebe the result of evaluating an expression withnode-list
- Let
- Update the local scope with (
name,value).
- Let
resultbe the empty string. - For each node in
node-list:- Switch on the node's
kindattribute:Characters:- append the value of the node's
valueattribute toresult
- append the value of the node's
Expansion:- let
valuebe the result of evaluate an expansion with node. - append
valuetoresult
- let
- anything else:
- Evaluation error.
- Switch on the node's
- Return
result
- Let
namebe the value of the node'snameattribute - Let
operatorbe the value of the node'soperatorattribute - Let
node-listbe the value of the node'svalueattribute - Let
valuebe the result of resolving a name withname - Switch on
operator:-(U+002D HYPHEN-MINUS):- If
valueis undefined:- Set
valueto the result of evaluating an expression withnode-list.
- Set
- If
:-(U+003A COLON, U+002D HYPHEN-MINUS):- If
valueis either undefined or the empty string:- Set
valueto the result of evaluating an expression withnode-list.
- Set
- If
+(U+002B PLUS SIGN):- If
valueis undefined:- Set
valueto the empty string.
- Set
- Otherwise:
- Set
valueto the result of evaluating an expression withnode-list.
- Set
- If
:+(U+003A COLON, U+002B PLUS SIGN):- If
valueis either undefined or the empty string:- Set
valueto the empty string.
- Set
- Otherwise:
- Set
valueto the result of evaluating an expression withnode-list.
- Set
- If
=(U+003D EQUALS SIGN):- If
valueis undefined:- Set
valueto the result of evaluating an expression withnode-list. - Update the local scope with (
name,value)
- Set
- If
:=(U+003A COLON, U+003D EQUALS SIGN):- If
valueis either undefined or the empty string:- Set
valueto the result of evaluating an expression withnode-list. - Update the local scope with (
name,value)
- Set
- If
?(U+003F QUESTION MARK):- If
valueis undefined:- Let
messagebe the result of evaluating an expression withnode-list. - If
messageis not the empty string:- Missing required value error with message:
message
- Missing required value error with message:
- Otherwise:
- Missing required value error: missing required value for
name
- Missing required value error: missing required value for
- Let
- If
:?(U+003A COLON, U+003F QUESTION MARK):- If
valueis either undefined or the empty string:- Let
messagebe the result of evaluating an expression withnode-list. - If
messageis not the empty string:- Missing required value error with message:
message
- Missing required value error with message:
- Otherwise:
- Missing required value error: missing required value for
name
- Missing required value error: missing required value for
- Let
- If
- anything else:
- Evaluation error: unknown expansion operator.
- Return
value
- If the override flag is
true:- If the local scope contains a key for
name:- return the value associated with the key for
namein the local scope
- return the value associated with the key for
- Otherwise, if an environment variable named
nameis defined:- return the value of this environment variable.
- Otherwise:
- return undefined
- If the local scope contains a key for
- Otherwise:
- If an environment variable named
nameis defined:- return the value of this environment variable.
- Otherwise, if the local scope contains a key for
name:- return the value associated with the key for
namein the local scope
- return the value associated with the key for
- Otherwise:
- return undefined
- If an environment variable named