Commit Graph

127 Commits

Author SHA1 Message Date
Brandon T. Willard
fb6feaf082 Improve correspondence with Python errors and console behavior
Compiler and command-line error messages now reflect their Python counterparts.
E.g. where Python emits a `SyntaxError`, so does Hy; same for `TypeError`s.
Multiple tests have been added that check the format and type of raised
exceptions over varying command-line invocations (e.g. interactive and not).

A new exception type for `require` errors was added so that they can be treated
like normal run-time errors and not compiler errors.

The Hy REPL has been further refactored to better match the class-structured
API.  Now, different error types are handled separately and leverage more base
class-provided functionality.

Closes hylang/hy#1486.
2019-02-07 13:45:41 -05:00
Brandon T. Willard
51c7efe6e8 Retain compiled source and file information for exceptions
This commit refactors the exception/error classes and their handling.
It also retains Hy source strings and their originating file information, when
available, all throughout the core parser and compiler functions.

As well, with these changes, calling code is no longer responsible for providing
source and file details to exceptions,

Closes hylang/hy#657.
2019-02-07 13:43:58 -05:00
Kodi Arfer
62638b44a3 Update copyright years 2019-02-07 08:57:35 -05:00
Kodi Arfer
b777972a0e Fix mangling of characters below 0xFF 2018-12-14 15:54:23 -05:00
Brandon T. Willard
8b6646d5c9 Remove hy.core compilation requirement from hy package
Previously, when importing `hy` (and any of its sub-packages/modules), Hy source
compilation for `hy.core.language` was necessarily triggered.  This, in turn,
would trigger compilation of the other standard library source files.

This commit removes that chain of events and allows the `hy` package to be
imported without any Hy compilation.

Furthermore, `read` and `read_str` are now implemented in Python and the Hy
standard library files now handle their own dependencies explicitly (i.e. they
`import` and/or `require` the other standard library files upon which they
depend).

The latter changes were necessary, because the automatically triggered
compilation of `hy.core.language` (and associated standard library files) was
serving--implicitly--as a means of producing bytecode in an order that just
happened to work for any compilation occurring afterward.  This chain of
events/dependencies was extremely cryptic, brittle, and difficult to debug, and
these changes should help to remedy that.

Closes hylang/hy#1697.
2018-11-28 14:12:33 -05:00
Brandon T. Willard
86fda31ab1 Move compilation and parsing functions out of importer.py
Functions and variables relating to compilation and parsing have been moved to
`compiler.py` and `lex/__init__.py`, respectively.  Those functions are
  - `hy_parse` from `hy.importer` to `hy.lex`
  - `hy_eval`, `ast_compile`, and `calling_module` from `hy.importer` to
  `hy.compiler`

Closes hylang/hy#1695.
2018-11-28 14:12:33 -05:00
Kodi Arfer
734cdcd2fd Delay importing the lexer and parser
This speeds up runs of Hy that never need to parse or compile Hy code (e.g., running a Hy program that's already byte-compiled).
2018-08-18 18:05:40 -04:00
Kodi Arfer
9c6714c176 Remove unused imports 2018-06-30 11:04:11 -07:00
Kodi Arfer
1732ddec4d Disable the parser cache
I wasn't able to observe a meaningful performance improvement from it, and it requires some file I/O. Here are the times, in seconds, I observed on my ThinkPad T450s with Python 3.6 and Ubuntu 18.04:

                test 1  test 2
    master       8.96    2.59
    this change  8.88    2.62

Test 1 is the total time from:

$ time dash -c 'git clean -dfx && pip install -e . && pytest --ignore tests/test_bin.py'

Test 2 is pytest's reported total time from a subsequent:

$ pytest --ignore tests/test_bin.py
2018-05-21 09:22:49 -07:00
Kodi Arfer
c93a60ede0 Remove support for cons cells 2018-04-12 16:45:17 -07:00
Simon Gomizelj
291847af15 Make HyKeyword constructor consistent
The colon is now only part of its string representation and no longer
accepted as part of the constructor:

  :foobar == HyKeyword("foobar")
2018-04-07 22:29:20 -07:00
Yigong Wang
564c030950 Raise LexException when codec can't decode some bytes 2018-04-07 11:47:06 -07:00
Kodi Arfer
f17a21be63 Use X as the mangle delimiter on Python 3 2018-04-04 10:50:33 -07:00
Yigong Wang
5ae6875e88 Fix mangle for Pythons compiled with UCS-2 2018-03-30 07:23:32 -07:00
Kodi Arfer
85968e70dd Add mangle and unmangle as core functions 2018-03-13 14:55:55 -04:00
Kodi Arfer
52edad28e2 Overhaul mangling rules 2018-03-13 14:55:53 -04:00
Kodi Arfer
d501d4d806 Mangle symbols at compile-time instead of parse-time
This means that a HySymbol remembers its original name. That is, `a-b` and `a_b` are different symbols although `(setv a-b 1)` and `(setv a_b 1)` set the same variable (namely, `a_b`).

Most of the edits in this commit are to switch underscores to hyphens in places where mangling hasn't happened yet.

I removed some lexer tests since the lexer no longer does any mangling.
2018-03-13 14:55:15 -04:00
Kodi Arfer
6de7ddfee5 Update copyright years 2018-01-01 10:38:33 -05:00
Kodi Arfer
deb801edab Preserve .brackets in quoted HyStrings 2017-09-08 11:27:34 -07:00
Kodi Arfer
eb23ddc1e2 Add #[DELIM[ … ]DELIM] syntax for string literals 2017-09-08 11:27:34 -07:00
Kodi Arfer
98fb33d0a3 Simplify string parsing with unicode_literals
I switched from `ast.literal_eval` back to `eval` because the former doesn't respect `unicode_literals`.
2017-08-28 10:28:19 -07:00
gilch
203965d0fa add #_ discard syntax 2017-08-02 16:55:14 -06:00
Kodi Arfer
28ce83524b Don't try to lex tag-macro calls as shebangs 2017-07-23 11:43:19 -07:00
Kodi Arfer
2d863abc85 Implement #* and #** unpacking 2017-07-17 13:34:39 -07:00
Kodi Arfer
26d1b3f72e Don't parse 5.attr or :foo.attr as symbols 2017-06-23 08:30:37 -07:00
gilch
6cd3201421 rename sharp macros to tag macros 2017-06-22 22:48:46 -06:00
gilch
20c26a52e4 make sharp macros take arbitrary identifiers
Previously, only a single character was allowed.
2017-06-22 22:46:24 -06:00
Kodi Arfer
2eb81864df Make all files comply with license-header policy 2017-04-27 14:16:57 -07:00
Kodi Arfer
4c38e2c9dd Rename reader macros to "sharp macros" (#1282)
They're not actually reader macros, since their arguments are parsed s-expressions, like a regular macro, not pre-parsed source text.
2017-04-21 10:07:48 -05:00
Kodi Arfer
286d568959 Fix a crash when tokenizing a single quote 2017-04-07 21:12:17 +03:00
Kodi Arfer
45b7a4ac9d Add bytestring literals 2017-02-19 09:04:45 +02:00
Kodi Arfer
ace125ee9b Consolidate hy.models.* and tests/models/* into one file apiece
They were a lot of similar, tiny files.
2017-02-16 19:43:00 -08:00
Kodi Arfer
5b85990d87 Add a module for getting reserved words (#1171)
This is helpful for writing syntax highlighters (e.g., https://github.com/hylang/hy/pull/1170).
2016-12-12 10:11:42 +02:00
Kodi Arfer
ed8e37da62 Burninate the synonyms true, false, and nil
Per the straw poll in #908, as an alternative to #1147.

Now you must use `True`, `False`, and `None`, as in Python. Or just assign `true` to `True`, etc.; the old synonyms aren't reserved words anymore.
2016-11-23 18:35:17 -08:00
Gergely Nagy
26f342d580 Teach apply about symbol mangling
apply now mangles strings and keywords according to the Hy mangling
rules (by using the same function, now imported from
hy.lex.parser). With this change, if the dict passed to apply has
keywords, strings or quoted symbols, they'll get mangled, to turn them
into proper keys.

This only works for the cases where the keys are directly in the apply
params. A previously deffed dict, or key through a variable will not be
mangled.

This closes #219.

Signed-off-by: Gergely Nagy <algernon@madhouse-project.org>
2015-09-24 09:47:12 +02:00
Ryan Gonzalez
73ace37678 Fix #831
Blame dot not matching newlines
2015-08-22 15:13:46 -05:00
gilch
a22e2ca4cc purged null from Hy
we already have `nil`, and `null` was barely ever used.
2015-08-11 16:22:13 -06:00
Gergely Nagy
73f8a47f65
Mangle trailing bangs on symbols
Postfixing functions with a bang, like set!, get!, etc are relatively
common. However, those names are not valid in python, so in the name of
python interoperability, lets mangle them to set_bang and get_bang,
respectively.

Closes #536.

Signed-off-by: Gergely Nagy <algernon@madhouse-project.org>
2015-08-10 14:37:17 +02:00
Ryan Gonzalez
016d25d104 Add one-argument division and rationals (closes #825 and #826) 2015-07-29 13:30:24 -05:00
Gregor Best
0ba620fc73 Explicitly set missing source on LexExceptions
This fixes the following code:

    (import [hy.lex [tokenize]])
    (tokenize "(foo))")
2015-07-28 16:46:26 +02:00
Ryan Gonzalez
c94c0e8b50 Add set literals (closes #827) 2015-07-14 23:59:29 +03:00
Adam Schwalm
f1df108b31 Add support for multi-line strings in interpreter 2014-12-28 23:38:38 -06:00
James King
277028cdd5 Remove HyLambdaListKeyword from the parser
It's not a syntactic element and doesn't belong in the parser. Parsing
lambda lists is now handled by the compiler alone.
2014-06-03 21:36:49 -04:00
Nicolas Dandrimont
52144820ca Add a cons object and related mechanisms
Closes: #183
2014-01-23 23:08:52 +01:00
Nicolas Dandrimont
eedebd12b3 Merge branch 'master' into pr/443 2014-01-17 20:25:09 +01:00
Foxboron
a35ecc41bd Fix reader macros to actually be macros 2014-01-17 01:07:47 +01:00
Gergely Nagy
8ef02a54b5 hy/lex/parser.py: Add support for sub-object mangling
With this patch, every identifier is split up along dots, each part gets
separately mangled, and then it is all joined back together. This allows
for fun stuff like (.foo? (Foo)), and even more contrived examples.

Signed-off-by: Gergely Nagy <algernon@madhouse-project.org>
2014-01-16 16:23:35 +01:00
Gergely Nagy
ceb615d010 hy/lex/parser.py: Move most of the identifier mangling into a def
As a refactoring step, move the identifier mangling done in t_identifier
into a separate def.

Signed-off-by: Gergely Nagy <algernon@madhouse-project.org>
2014-01-16 16:20:51 +01:00
Nicolas Dandrimont
a44e53f4de Comments end when the input ends or a newline occurs
This fixes #382, which occured because the REPL doesn't use trailing newlines.
2014-01-02 03:09:18 +01:00
Bob Tolbert
f6160c755a Much better version of new error messages.
This version is much simpler.

At the point that the exception is raised, we don't have access to
the actual source, just the current expression. but as the
exception percolates up, we can intercept it, add the source and
the re-raise it.

Then at the final point, in the cmdline handler, we can choose to
let the entire traceback print, or just the simpler, direct error
message.

And even with the full traceback, the last bit is nicely formatted
just like the shorter, simpler message.

The error message is colored if clint is installed, but to avoid
yet another dependency, you get monochrome without clint.

I'm sure there is a better way to do the markup, the current method
is kludgy but works.

I wish there was more shared code between HyTypeError and LexException
but they are kind of different in some fundamental ways.

This doesn't work (yet) with runtime errors generated from Python,
like NameError, but I have a method that can catch NameError and turn it
into a more pleasing output.

Finally, there is no obvious way to raise HyTypeError from pure Hy code,
so methods in core/language.hy throw ugly TypeError/ValueError.
2013-12-31 09:56:03 -07:00