All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Jan Kończak" <jan.konczak@cs.put.poznan.pl>
To: netfilter-devel@vger.kernel.org
Cc: Florian Westphal <fw@strlen.de>
Subject: Re: [PATCH nft] parser_bison: on syntax errors, output expected tokens
Date: Sat, 13 Dec 2025 13:32:42 +0100	[thread overview]
Message-ID: <6217966.lOV4Wx5bFT@imladris> (raw)
In-Reply-To: <aTINLRJlBUIox3pC@strlen.de>

> Just a note that there might be slight delay with this getting
> applied because we'd like to make a new release soon.

Regarding the patch to dump all expected tokens on syntax errors in
'nft' command: should I modify it somehow so that it gets applied?
Or just wait, or you decided it's not a good idea after all?

In the meantime, I toyed with what parser expects as the next token,
and I see surprises here and there.
For example, is it intended that 'add' and 'rule' keywords are
optional? Right now, to add a rule, it suffices:
   nft F I tcp dport ssh accept

Plus, bogon hints from parser occur on parsing expressions such as
   nft add rule ip F I ct state ?
   nft add rule ip F I tcp dport ?
Because all these are parsed by the same subset of grammar rules
(I guess starting at 'relational_expr'), the possible tokens are
the same for any such expr.
By "bogon hints" I mean that commands get parsed successfully but
raise an error later on. E.g., 'nft F I ct state missing' yields
datatype mismatch claiming that missing is a boolean, not a state.

Interestingly, by "abusing" the junk token which always triggers
a syntax error it is possible to create a bash autocompletion script
that tries to execute nft command typed in so far but appended with
junk, and parses expected tokens into completions. I feel ambivalent
if it makes sense to build autocompletion this way, but it speeds up
checking what the parser expects.

Simple autocompletion (obviously requiring the patch) follows.
----------------------------
_nft(){
    expectedTokens=$(
        "${COMP_WORDS[@]:0:$COMP_CWORD}" $'\025' 2>&1 \
        | grep -A1 "unexpected junk"                   \
        | grep '^expected any of:'                      \
        | cut -d: -f2-                                   \
        | sed 's/, /\n/g'
    )
    [ "$expectedTokens" ] || return 1;
    EXPECTED=()   NONKEYWORD=()
    while read token; do
        [[ $token == '<'*'>'       ]] && { NONKEYWORD+=("$token"); continue; }
        [[ $token == "end of file" ]] && continue
        [[ $token == "newline"     ]] && continue 
        [[ $token == "colon"       ]] && { EXPECTED+=(':');   continue; }
        [[ $token == "semicolon"   ]] && { EXPECTED+=('\\;'); continue; }
        [[ $token == "comma"       ]] && { EXPECTED+=(',');   continue; }
        [[ $token == *[[:space:]]* ]] && { printf "\e[s\nautocompletion problem: space in token \"$token\"\n\e[u" 1>&2; continue; }
        EXPECTED+=("$token")
    done <<< "$expectedTokens"
    COMPREPLY=( $(compgen -W "${EXPECTED[*]}" -- ${COMP_WORDS[$COMP_CWORD]}) )
    if [[ $NONKEYWORD ]]; then
        # TODO: logic for values (non-keyword tokens); either call some
        # 'nft list …' to detect what shall be proposed here and add it to
        # COMPREPLY, or adjust COMPREPLY if the value is a new name / address
        # / port etc. that cannot be completed / guessed.

        if [[ ${COMP_LINE:$COMP_POINT-1:1} == [[:space:]] ]] ; then
            # append non-keyword placeholders after a whitespace character to
            # make them appear among completions, but be non-autocompletable
            for VAL in "${NONKEYWORD[@]}"; do
                COMPREPLY+=(" $VAL")
            done;
            # if there is only one possibility, then add an empty alternative
            # to prevent autocompleting the name of the non-keyword itself
            # (e.g. 'nft delete rule ip F T handle' expects only <number>)
            [[ ${#NONKEYWORD[@]} == 1 && ${#COMPREPLY[@]} == 1 ]] && COMPREPLY+=("")
        fi
    fi
    return 0;
}
complete -F _nft nft
----------------------------




  reply	other threads:[~2025-12-13 12:42 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-12-04 21:54 [PATCH nft] parser_bison: on syntax errors, output expected tokens Jan Kończak
2025-12-04 22:37 ` Florian Westphal
2025-12-13 12:32   ` Jan Kończak [this message]
2026-01-16 13:07 ` Florian Westphal

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=6217966.lOV4Wx5bFT@imladris \
    --to=jan.konczak@cs.put.poznan.pl \
    --cc=fw@strlen.de \
    --cc=netfilter-devel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.