Don't try to lex tag-macro calls as shebangs

This commit is contained in:
Kodi Arfer 2017-07-23 11:43:19 -07:00
parent 35f7dd36bb
commit 28ce83524b
6 changed files with 18 additions and 21 deletions

2
NEWS
View File

@ -18,6 +18,8 @@ Changes from 0.13.0
and a symbol
* Hy now respects the environment variable PYTHONDONTWRITEBYTECODE
* String literals should no longer be interpreted as special forms or macros
* Tag macros (née sharp macros) whose names begin with `!` are no longer
mistaken for shebang lines
Changes from 0.12.1

View File

@ -39,7 +39,10 @@ def import_file_to_hst(fpath):
"""Import content from fpath and return a Hy AST."""
try:
with open(fpath, 'r', encoding='utf-8') as f:
return import_buffer_to_hst(f.read())
buf = f.read()
# Strip the shebang line, if there is one.
buf = re.sub(r'\A#!.*', '', buf)
return import_buffer_to_hst(buf)
except IOError as e:
raise HyIOError(e.errno, e.strerror, e.filename)

View File

@ -25,7 +25,6 @@ lg.add('QUOTE', r'\'%s' % end_quote)
lg.add('QUASIQUOTE', r'`%s' % end_quote)
lg.add('UNQUOTESPLICE', r'~@%s' % end_quote)
lg.add('UNQUOTE', r'~%s' % end_quote)
lg.add('HASHBANG', r'#!.*[^\r\n]')
lg.add('HASHSTARS', r'#\*+')
lg.add('HASHOTHER', r'#%s' % identifier)

View File

@ -90,23 +90,13 @@ def set_quote_boundaries(fun):
return wrapped
@pg.production("main : HASHBANG real_main")
def main_hashbang(p):
return p[1]
@pg.production("main : real_main")
@pg.production("main : list_contents")
def main(p):
return p[0]
@pg.production("real_main : list_contents")
def real_main(p):
return p[0]
@pg.production("real_main : $end")
def real_main_empty(p):
@pg.production("main : $end")
def main_empty(p):
return []

View File

@ -36,6 +36,15 @@
(assert (= #spam_eggs 42 ['spam 42 'eggs])))
(defn test-bang-tag-macro []
"Test tag macros whose names start with `!`"
; https://github.com/hylang/hy/issues/1334
(deftag !a [x] `["foo" ~x])
(assert (= #!a 3 ["foo" 3]))
(deftag ! [x] `["bar" ~x])
(assert (= #! 4 ["bar" 4])))
(defn test-tag-macro-whitespace []
"Test whitespace after a tag macro"
(deftag foo [expr]

View File

@ -301,12 +301,6 @@ def test_unicode_escapes():
assert [ord(x) for x in entry] == [97, 172, 4660, 8364, 32768]
def test_hashbang():
""" Ensure we can escape things """
entry = tokenize("#!this is a comment\n")
assert entry == []
def test_complex():
"""Ensure we tokenize complex numbers properly"""
# This is a regression test for #143