You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
Hi, I want to write a grammar like
add value [value ...]
, for example: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:
It seems that it can only recognize the last var1:
Many thanks.
@jonathanslenders
The text was updated successfully, but these errors were encountered: