def _create_path_completer(self): def unwrapper(text): return re.sub(r'\\(.)', r'\1', text) def single_quoted_wrapper(text): return text.replace('\\', '\\\\').replace("'", "\\'") def double_quoted_wrapper(text): return text.replace('\\', '\\\\').replace('"', '\\"') grammar = r""" # Text before the current string. ( [^'"#] | # Not quoted characters. ''' ([^']|'[^']|''[^']|\\.)* ''' | # Inside single quoted triple strings "" " ([^"]|"[^"]|""[^"]|\\.)* "" " | # Inside double quoted triple strings \#[^\n]*(\n|$) | # Comment. "(?!"") ([^"\\]|\\.)*" | # Inside double quoted strings. '(?!'') ([^'\\]|\\.)*' # Inside single quoted strings. # Warning: The negative lookahead in the above two # statements is important. If we drop that, # then the regex will try to interpret every # triple quoted string also as a single quoted # string, making this exponentially expensive to # execute! )* # The current string that we're completing. ( ' (?P<var1>([^\n'\\]|\\.)*) | # Inside a single quoted string. " (?P<var2>([^\n"\\]|\\.)*) # Inside a double quoted string. ) """ g = compile_grammar( grammar, escape_funcs={ 'var1': single_quoted_wrapper, 'var2': double_quoted_wrapper, }, unescape_funcs={ 'var1': unwrapper, 'var2': unwrapper, }) return g, GrammarCompleter(g, { 'var1': PathCompleter(expanduser=True), 'var2': PathCompleter(expanduser=True), })
def _create_path_completer(self): def unwrapper(text): return re.sub(r'\\(.)', r'\1', text) def single_quoted_wrapper(text): return text.replace('\\', '\\\\').replace("'", "\\'") def double_quoted_wrapper(text): return text.replace('\\', '\\\\').replace('"', '\\"') grammar = r""" # Text before the current string. ( [^'"#] | # Not quoted characters. ''' ([^']|'[^']|''[^']|\\.)* ''' | # Inside single quoted triple strings "" " ([^"]|"[^"]|""[^"]|\\.)* "" " | # Inside double quoted triple strings \#[^\n]*(\n|$) | # Comment. "(?!"") ([^"\\]|\\.)*" | # Inside double quoted strings. '(?!'') ([^'\\]|\\.)*' # Inside single quoted strings. # Warning: The negative lookahead in the above two # statements is important. If we drop that, # then the regex will try to interpret every # triple quoted string also as a single quoted # string, making this exponentially expensive to # execute! )* # The current string that we're completing. ( ' (?P<var1>([^\n'\\]|\\.)*) | # Inside a single quoted string. " (?P<var2>([^\n"\\]|\\.)*) # Inside a double quoted string. ) """ g = compile_grammar(grammar, escape_funcs={ 'var1': single_quoted_wrapper, 'var2': double_quoted_wrapper, }, unescape_funcs={ 'var1': unwrapper, 'var2': unwrapper, }) return g, GrammarCompleter( g, { 'var1': PathCompleter(expanduser=True), 'var2': PathCompleter(expanduser=True), })
def _create_path_completer_grammar(self) -> "_CompiledGrammar": def unwrapper(text: str) -> str: return re.sub(r"\\(.)", r"\1", text) def single_quoted_wrapper(text: str) -> str: return text.replace("\\", "\\\\").replace("'", "\\'") def double_quoted_wrapper(text: str) -> str: return text.replace("\\", "\\\\").replace('"', '\\"') grammar = r""" # Text before the current string. ( [^'"#] | # Not quoted characters. ''' ([^'\\]|'(?!')|''(?!')|\\.])* ''' | # Inside single quoted triple strings "" " ([^"\\]|"(?!")|""(?!^)|\\.])* "" " | # Inside double quoted triple strings \#[^\n]*(\n|$) | # Comment. "(?!"") ([^"\\]|\\.)*" | # Inside double quoted strings. '(?!'') ([^'\\]|\\.)*' # Inside single quoted strings. # Warning: The negative lookahead in the above two # statements is important. If we drop that, # then the regex will try to interpret every # triple quoted string also as a single quoted # string, making this exponentially expensive to # execute! )* # The current string that we're completing. ( ' (?P<var1>([^\n'\\]|\\.)*) | # Inside a single quoted string. " (?P<var2>([^\n"\\]|\\.)*) # Inside a double quoted string. ) """ return compile_grammar( grammar, escape_funcs={ "var1": single_quoted_wrapper, "var2": double_quoted_wrapper }, unescape_funcs={ "var1": unwrapper, "var2": unwrapper }, )
def _create_path_completer(self): def unwrapper(text): return re.sub(r'\\(.)', r'\1', text) def single_quoted_wrapper(text): return text.replace('\\', '\\\\').replace("'", "\\'") def double_quoted_wrapper(text): return text.replace('\\', '\\\\').replace('"', '\\"') grammar = r""" # Text before the current string. ( [^'"#] | # Not quoted characters. '''.*?''' | # Inside single quoted triple strings "" ".*?"" " | # Inside double quoted triple strings \#[^\n]*(\n|$) | # Comment. "([^"\\]|\\.)*" | # Inside double quoted strings. '([^'\\]|\\.)*' # Inside single quoted strings. )* # The current string that we're completing. ( ' (?P<var1>([^\n'\\]|\\.)*) | # Inside a single quoted string. " (?P<var2>([^\n"\\]|\\.)*) # Inside a double quoted string. ) """ g = compile_grammar( grammar, escape_funcs={ 'var1': single_quoted_wrapper, 'var2': double_quoted_wrapper, }, unescape_funcs={ 'var1': unwrapper, 'var2': unwrapper, }) return g, GrammarCompleter(g, { 'var1': PathCompleter(), 'var2': PathCompleter(), })
def _create_path_completer(self): def unwrapper(text): return re.sub(r'\\(.)', r'\1', text) def single_quoted_wrapper(text): return text.replace('\\', '\\\\').replace("'", "\\'") def double_quoted_wrapper(text): return text.replace('\\', '\\\\').replace('"', '\\"') grammar = r""" # Text before the current string. ( [^'"#] | # Not quoted characters. '''.*''' | # Inside single quoted triple strings "" ".*"" " | # Inside double quoted triple strings \#[^\n]* | # Comment. "([^"\\]|\\.)*" | # Inside double quoted strings. '([^'\\]|\\.)*' # Inside single quoted strings. )* # The current string that we're completing. ( ' (?P<var1>([^\n'\\]|\\.)*) | # Inside a single quoted string. " (?P<var2>([^\n"\\]|\\.)*) # Inside a double quoted string. ) """ g = compile_grammar(grammar, escape_funcs={ 'var1': single_quoted_wrapper, 'var2': double_quoted_wrapper, }, unescape_funcs={ 'var1': unwrapper, 'var2': unwrapper, }) return g, GrammarCompleter(g, { 'var1': PathCompleter(), 'var2': PathCompleter(), })