I try to parse arguments by a recursive descent schema.
The while statements calls an eat! function after every execution to get the next $current_token to process.
The problem now is that this way it goes into an infinite loop.
To be exact, there are two while loops in the function.
Either one of them or both are not working because the compound is syntactically (or logically?) wrong.
Could you please check what might be wrong with the following code on the many levels possible?
1)
while $(isWord? $current_token) || ! $(isVersionNumber? $current_token) && ! $(endOfInput?); do
while isVersionNumber? $current_token && ! endOfInput?; do
Also what is correct? To put the checking-functions in a command substitution or not?
Could it be that short-circuiting also prevents the endOfInput?-test ?
Because that should absolutely stop the loop.
On request of ilkkachu
The test functions' code:
isWord? () {
local pattern="^[a-zA-Z0-9_-]+$"
if [[ $1 =~ $pattern ]]; then
echo true
else
echo false
fi
}
isVersionNumber? () {
local pattern="^[0-9]{1,2}(\.[0-9]{,2})*$"
if [[ $1 =~ $pattern ]]; then
echo true
else
echo false
fi
}
EO_ARGS=1
function endOfInput? {
if [ $current_token_index -ge $ARGC ]; then
EO_ARGS=0
fi
echo $EO_ARGS
}
I assume it outputs another command, since you're using it in a command substitution?
No it was just a try because I don't know whether or not I can just call the functions in the conditional without command substitution.
For completeness' sake, I also add the eat! function.
eat! () {
if [[ ! $(($current_char_index + 1)) -gt $ARGC ]]; then
current_token=${ARGV[$current_token_index]}
((current_token_index += 1))
current_char=${current_token:0:1}
}
And may the conditions maybe be formulated better with test / [ ([[) ?
Could the exclamation mark be the point of failure?
The following seems to be syntactically wrong:
while [ endOfInput -eq 1 ] && isWord? "$current_token" || ! isVersionNumber? "$current_token"; do
From the bracket [ I get the error message
[: endOfInput: integral expression expected (translation)