mirror of
https://github.com/xonsh/xonsh.git
synced 2025-03-09 02:41:00 +01:00
Add support for path literals
This commit is contained in:
parent
5fedb677ca
commit
c685de9803
3 changed files with 15 additions and 4 deletions
|
@ -14,6 +14,7 @@ import shlex
|
||||||
import signal
|
import signal
|
||||||
import atexit
|
import atexit
|
||||||
import inspect
|
import inspect
|
||||||
|
import pathlib
|
||||||
import builtins
|
import builtins
|
||||||
import itertools
|
import itertools
|
||||||
import subprocess
|
import subprocess
|
||||||
|
@ -138,6 +139,9 @@ def reglob(path, parts=None, i=None):
|
||||||
return paths
|
return paths
|
||||||
|
|
||||||
|
|
||||||
|
def path_literal(s):
|
||||||
|
return pathlib.Path(s)
|
||||||
|
|
||||||
def regexsearch(s):
|
def regexsearch(s):
|
||||||
s = expand_path(s)
|
s = expand_path(s)
|
||||||
return reglob(s)
|
return reglob(s)
|
||||||
|
@ -1124,6 +1128,7 @@ def load_builtins(execer=None, config=None, login=False, ctx=None):
|
||||||
builtins.__xonsh_completers__ = xonsh.completers.init.default_completers()
|
builtins.__xonsh_completers__ = xonsh.completers.init.default_completers()
|
||||||
builtins.__xonsh_call_macro__ = call_macro
|
builtins.__xonsh_call_macro__ = call_macro
|
||||||
builtins.__xonsh_enter_macro__ = enter_macro
|
builtins.__xonsh_enter_macro__ = enter_macro
|
||||||
|
builtins.__xonsh_path_literal__ = path_literal
|
||||||
# public built-ins
|
# public built-ins
|
||||||
builtins.XonshError = XonshError
|
builtins.XonshError = XonshError
|
||||||
builtins.XonshBlockError = XonshBlockError
|
builtins.XonshBlockError = XonshBlockError
|
||||||
|
@ -1192,6 +1197,7 @@ def unload_builtins():
|
||||||
'__xonsh_completers__',
|
'__xonsh_completers__',
|
||||||
'__xonsh_call_macro__',
|
'__xonsh_call_macro__',
|
||||||
'__xonsh_enter_macro__',
|
'__xonsh_enter_macro__',
|
||||||
|
'__xonsh_path_literal__',
|
||||||
'XonshError',
|
'XonshError',
|
||||||
'XonshBlockError',
|
'XonshBlockError',
|
||||||
'XonshCalledProcessError',
|
'XonshCalledProcessError',
|
||||||
|
|
|
@ -1973,6 +1973,11 @@ class BaseParser(object):
|
||||||
def p_string_literal(self, p):
|
def p_string_literal(self, p):
|
||||||
"""string_literal : string_tok"""
|
"""string_literal : string_tok"""
|
||||||
p1 = p[1]
|
p1 = p[1]
|
||||||
|
if p1.value.startswith('p'):
|
||||||
|
s = ast.literal_eval(p1.value[1:])
|
||||||
|
p[0] = xonsh_call('__xonsh_path_literal__', [s],
|
||||||
|
lineno=p1.lineno, col=p1.lexpos)
|
||||||
|
else:
|
||||||
s = ast.literal_eval(p1.value)
|
s = ast.literal_eval(p1.value)
|
||||||
cls = ast.Bytes if p1.value.startswith('b') else ast.Str
|
cls = ast.Bytes if p1.value.startswith('b') else ast.Str
|
||||||
p[0] = cls(s=s, lineno=p1.lineno, col_offset=p1.lexpos)
|
p[0] = cls(s=s, lineno=p1.lineno, col_offset=p1.lexpos)
|
||||||
|
|
|
@ -205,7 +205,7 @@ Floatnumber = group(Pointfloat, Expfloat)
|
||||||
Imagnumber = group(r'[0-9]+[jJ]', Floatnumber + r'[jJ]')
|
Imagnumber = group(r'[0-9]+[jJ]', Floatnumber + r'[jJ]')
|
||||||
Number = group(Imagnumber, Floatnumber, Intnumber)
|
Number = group(Imagnumber, Floatnumber, Intnumber)
|
||||||
|
|
||||||
StringPrefix = r'(?:[bB][rR]?|[rR][bB]?|[uU])?'
|
StringPrefix = r'(?:[bB][rR]?|[rR][bB]?|[uU]|[p])?'
|
||||||
|
|
||||||
# Tail end of ' string.
|
# Tail end of ' string.
|
||||||
Single = r"[^'\\]*(?:\\.[^'\\]*)*'"
|
Single = r"[^'\\]*(?:\\.[^'\\]*)*'"
|
||||||
|
|
Loading…
Add table
Reference in a new issue