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
kind
attribute is notAssignment
:- Evaluation error.
- Otherwise, evaluate an assignment with node
- If the value of the node's
- Return the local scope.
- Let
name
be the value of the node'sname
attribute - If the override flag is
false
, and an environment variable namedname
is defined:- Let
value
be the value of the environment variable namedname
- Let
- Otherwise:
- Let
node-list
be the value of the node'svalue
attribute - Let
value
be the result of evaluating an expression withnode-list
- Let
- Update the local scope with (
name
,value
).
- Let
result
be the empty string. - For each node in
node-list
:- Switch on the node's
kind
attribute:Characters
:- append the value of the node's
value
attribute toresult
- append the value of the node's
Expansion
:- let
value
be the result of evaluate an expansion with node. - append
value
toresult
- let
- anything else:
- Evaluation error.
- Switch on the node's
- Return
result
- Let
name
be the value of the node'sname
attribute - Let
operator
be the value of the node'soperator
attribute - Let
node-list
be the value of the node'svalue
attribute - Let
value
be the result of resolving a name withname
- Switch on
operator
:-
(U+002D HYPHEN-MINUS):- If
value
is undefined:- Set
value
to the result of evaluating an expression withnode-list
.
- Set
- If
:-
(U+003A COLON, U+002D HYPHEN-MINUS):- If
value
is either undefined or the empty string:- Set
value
to the result of evaluating an expression withnode-list
.
- Set
- If
+
(U+002B PLUS SIGN):- If
value
is undefined:- Set
value
to the empty string.
- Set
- Otherwise:
- Set
value
to the result of evaluating an expression withnode-list
.
- Set
- If
:+
(U+003A COLON, U+002B PLUS SIGN):- If
value
is either undefined or the empty string:- Set
value
to the empty string.
- Set
- Otherwise:
- Set
value
to the result of evaluating an expression withnode-list
.
- Set
- If
=
(U+003D EQUALS SIGN):- If
value
is undefined:- Set
value
to 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
value
is either undefined or the empty string:- Set
value
to the result of evaluating an expression withnode-list
. - Update the local scope with (
name
,value
)
- Set
- If
?
(U+003F QUESTION MARK):- If
value
is undefined:- Let
message
be the result of evaluating an expression withnode-list
. - If
message
is 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
value
is either undefined or the empty string:- Let
message
be the result of evaluating an expression withnode-list
. - If
message
is 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
name
in the local scope
- return the value associated with the key for
- Otherwise, if an environment variable named
name
is 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
name
is 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
name
in the local scope
- return the value associated with the key for
- Otherwise:
- return undefined
- If an environment variable named