prompt-toolkit / prompt-toolkit/python-prompt-toolkit
How can I express `add value [value ...]` in a grammar compile?
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 10.6k
- Forks
- 815
- PR merge metrics
- No merged PRs in 30d
Description
Hi, I want to write a grammar like add value [value ...], for example:
add 1 2 3
add 1 2
add 10 11 12 13
All should be valid grammar.
Is it possible to do this in prompt_toolkit.contrib.regular_languages.compiler.compile? I tried everything but I can't figure it out. I tried with the grammar (\s* (?P<operator1>add) (\s+(?P<var1>[0-9.]+))+ \s*)
Here is the example I tried:
from prompt_toolkit import prompt
from prompt_toolkit.completion import WordCompleter
from prompt_toolkit.contrib.regular_languages.compiler import compile
from prompt_toolkit.contrib.regular_languages.completion import (
GrammarCompleter,
)
from prompt_toolkit.contrib.regular_languages.lexer import GrammarLexer
from prompt_toolkit.lexers import SimpleLexer
from prompt_toolkit.styles import Style
operators1 = ['add']
def create_grammar():
return compile(r"""
(\s* (?P<operator1>add) (\s+(?P<var1>[0-9.]+))+ \s*)
""")
example_style = Style.from_dict({
'operator': '#33aa33 bold',
'number': '#ff0000 bold',
'trailing-input': 'bg:#662222 #ffffff',
})
if __name__ == '__main__':
g = create_grammar()
lexer = GrammarLexer(g, lexers={
'operator1': SimpleLexer('class:operator'),
'var1': SimpleLexer('class:number'),
})
completer = GrammarCompleter(g, {
'operator1': WordCompleter(operators1),
})
# REPL loop.
while True:
# Read input and parse the result.
text = prompt('Calculate: ', lexer=lexer, completer=completer,
style=example_style)
m = g.match(text)
if m:
vars = m.variables()
else:
print('Invalid command\n')
continue
print(vars)
It seems that it can only recognize the last var1:

Many thanks.
@jonathanslenders
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with prompt_toolkit.contrib.regular_languages.compiler.compile and trace how repeated named groups are represented by the match result. Then inspect GrammarLexer and GrammarCompleter to determine whether repeated values are supported consistently; done means commands with two or more numeric values are accepted and all values remain available.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- cli
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100