Commit Graph

36 Commits

Author SHA1 Message Date
Johannes Altmanninger
3e3f507012 Fix regression expanding \$()
When expanding command substitutions, we use a naïve way of detecting whether
the cmdsub has the optional leading dollar. We check if the last character was
a dollar, which breaks if it's an escaped dollar.  We wrongly expand
\$(echo "") to the empty string. Fix this by checking if the dollar was escaped.

The parse_util_* functions have a bunch of output parameters. We should
return a parameter bag instead (I think I tried once and failed).
2022-04-03 15:54:08 +02:00
Johannes Altmanninger
4a575b26f5 Fix error check for repeated quoted command substitution
Commit e40eba358 (Treat text following quoted command substitution
as quoted) made parse_util_locate_cmdsubst_range() aware of quoted
command substitutions, by skipping surrounding text via quote_end().

However, it was not quite right. We fail to properly parse
two consecutive command substitutions in the same string,
because we don't maintain the quoting context across calls to
parse_util_locate_cmdsubst_range().  Let's track that bit in a
parameter. This allows us to get rid of the quote_end() hack.

Also apply this to the other place where we call
parse_util_locate_cmdsubst_range() in a loop (highlighting).

Fixes #8500
2021-12-04 16:56:07 +01:00
ridiculousfish
4a6d622733 Continue to refactor functions
Now that we have immutable props, we can remove a bunch of 'helper'
functions.
2021-10-23 10:12:52 -07:00
Fabian Homborg
35ca42413d Simplify some parse_util functions
Don't just reflexively drop down to wchar_t.
2021-07-27 18:39:56 +02:00
ridiculousfish
52c354a60f Simplify slice parsing in highlighting
Factor out parsing of slices, which is only used for highlighting.
2021-07-14 13:59:48 -07:00
ridiculousfish
a2dfd87928 Simplify parse_util_get_parameter_info
We no longer use any part of the "parameter info" except its quote type.
Just return the quote type directly.
2021-07-14 13:59:48 -07:00
ridiculousfish
083d8c5d23 Minor cleanup of certain parsing and quote finding functions
This makes more variables const and removes some suspicious casts.
2021-07-14 13:59:46 -07:00
Johannes Altmanninger
ec3d3a481b Support "$(cmd)" command substitution without line splitting
This adds a hack to the parser. Given a command

	echo "x$()y z"

we virtually insert double quotes before and after the command
substitution, so the command internally looks like

	echo "x"$()"y z"

This hack allows to reuse the existing logic for handling (recursive)
command substitutions.

This makes the quoting syntax more complex; external highlighters
should consider adding this if possible.

The upside (more Bash compatibility) seems worth it.

Closes #159
2021-07-13 21:33:42 +02:00
ridiculousfish
fb92ad946b Rework null terminated arrays
Several functions including wgetopt and execve operate on null-terminated
arrays of nul-terminated pointers: a list of pointers to C strings where
the last pointer is null. Prior to this change, each process_t stored its
argv in such an array. This had two problems:

1. It was awkward to work with this type, instead of using std::vector,
etc.
2. The process's arguments would be rearranged by builtins which is
surprising

Our null terminated arrays were built around a fancy type that would copy
input strings and also generate an array of pointers to them, in one big
allocation.

Switch to a new model where we construct an array of pointers over
existing strings. So you can supply a `vector<string>` and now
`null_terminated_array_t` will just make a list of pointers to them. Now
processes can just store their argv in a familiar wcstring_list_t.
2021-03-28 15:31:25 -07:00
Fabian Homborg
c1fe5be0ce parse_util: Remove locate_cmdsubst
We have *3* functions to find command substitutions, this is the most awkward.
2021-03-26 19:32:14 +01:00
ridiculousfish
0f2d73e4a3 Remove a stale comment 2020-12-22 12:38:51 -08:00
ridiculousfish
487de1e6c3 Reduce copying in parse_util_detect_errors
Allow parse_util_detect_errors to accept an already-parsed ast. This
eliminates a copy of the source, which is helpful when executing large
scripts.
2020-07-12 16:57:30 -07:00
ridiculousfish
0c22f67bde Remove the old parser bits
Now that everything has been migrated to the new AST, remove as much of
the parse_tree bits as possible
2020-07-04 14:58:05 -07:00
ridiculousfish
202fdfa54a Adopt the new AST in parse_util_detect_errors
This switches parse_util_detect_errors from parsing with parse_tree to
the new ast.
2020-07-04 14:58:05 -07:00
Rosen Penev
1055ff321c [clang-tidy] Replace NULL with nullptr
Found with modernize-use-nullptr

Signed-off-by: Rosen Penev <rosenp@gmail.com>
2019-11-25 14:23:03 -08:00
Rosen Penev
5ca80a61e3 [clang-tidy] Fix inconsistent declarations
Found with readability-inconsistent-declaration-parameter-name

Signed-off-by: Rosen Penev <rosenp@gmail.com>
2019-11-25 14:13:33 -08:00
Jason
3cf6ebc0e1 Amend typos and grammar errors 2019-11-25 13:07:15 +01:00
Johannes Altmanninger
6fb7f9b6b8 Fix completion for builtins (with subcommands)
Presently the completion engine ignores builtins that are part of the
fish syntax. This can be a problem when completing a string that was
based on the output of `commandline -p`.  This changes completions to
treat these builtins like any other command.

This also disables generic (filename) completion inside comments and
after strings that do not tokenize.

Additionally, comments are stripped off the output of `commandline -p`.

Fixes #5415
Fixes #2705
2019-11-04 16:44:51 +01:00
ridiculousfish
1a65e18ba8 Clean up some tokenization
Remove TOK_NONE
Turn token_type into an enum class
Make next() turn a maybe_t<tok_t> instead of a bool
2019-10-13 16:06:16 -07:00
ridiculousfish
29dede8139 Migrate parse_util_detect_errors to a free function 2019-08-04 14:49:56 -07:00
ridiculousfish
01d87455e1 Teach parse_util_escape_string_with_quote about tildes
Properly escape literal tildes in tab completion results. Currently we
always escape tildes in unquoted arguments; in the future we may escape
only leading tildes.

Fixes #2274
2018-02-17 15:18:43 -08:00
ridiculousfish
55fc10ea6e Migrate parse_util_detect_errors_in_argument to tnode_t 2018-01-20 11:31:40 -08:00
ridiculousfish
a99eecfad8 Wrap up source code and a parse tree into a new type parsed_source_ref_t
This will make it unnecessary to carry around the parse tree and source
separately, and enable some simplifications.
2017-12-22 14:40:15 -08:00
Kurtis Rader
82f5fb507d fix echo -h
In addition to fixing `echo -h` this includes some debugging related
cleanups I made while investigating the issue.

Fixes #4120
2017-06-18 22:10:19 -07:00
Kurtis Rader
509ee64fc9 implement our own assert() function
I recently upgraded the software on my macOS server and was dismayed to
see that cppcheck reported a huge number of format string errors due to
mismatches between the format string and its arguments from calls to
`assert()`. It turns out they are due to the macOS header using `%lu`
for the line number which is obviously wrong since it is using the C
preprocessor `__LINE__` symbol which evaluates to a signed int.

I also noticed that the macOS implementation writes to stdout, rather
than stderr. It also uses `printf()` which can be a problem on some
platforms if the stream is already in wide mode which is the normal case
for fish.

So implement our own `assert()` implementation. This also eliminates
double-negative warnings that we get from some of our calls to
`assert()` on some platforms by oclint.

Also reimplement the `DIE()` macro in terms of our internal
implementation.

Rewrite `assert(0 && msg)` statements to `DIE(msg)` for clarity and to
eliminate oclint warnings about constant expressions.

Fixes #3276, albeit not in the fashion I originally envisioned.
2017-02-14 18:48:27 -08:00
Aaron Gyes
fa78a7101c Make IWYU output in lint.cpp less messy
And re-run IWYU, adjust #includes.
2016-06-23 17:26:08 -07:00
Aaron Gyes
1357f5a364 Repair various invalid HeaderDoc comments.
Enable build setting to allow Xcode to complain about invalid
comments.
2016-06-05 18:57:45 -07:00
Kurtis Rader
ca912f157e restyle parse_util module to match project style
Reduces lint errors from 187 to 91 (-51%). Line count from 1754 to 1477 (-16%).

Another step in resolving issue #2902.
2016-05-02 17:11:02 -07:00
Kurtis Rader
1f06e5f0b9 add better support for IWYU and fix things
Remove the "make iwyu" build target. Move the functionality into the
recently introduced lint.fish script. Fix a lot, but not all, of the
include-what-you-use errors. Specifically, it fixes all of the IWYU errors
on my OS X server but only removes some of them on my Ubuntu 14.04 server.

Fixes #2957
2016-04-26 15:02:22 -07:00
ridiculousfish
3633c51ad8 Re-use the parse tree generated during error detection for execution
Prior to this fix, read_ni would use parse_util_detect_errors
to lint the script to run, and then parser_t::eval() to execute it.
Both functions would parse the script into a parse tree. This allows
us to re-use the parse tree, improving perfomance.
2016-02-28 00:44:20 -08:00
ridiculousfish
b59904632d Rewrite parse_util_unescape_wildcards
Make it simpler, and use wcstring instead of wcsdup
2015-08-19 11:35:24 -07:00
ridiculousfish
93d57bd73a Factor function environment preparation into its own function 2015-08-15 13:37:17 -07:00
David Adam
a6a16808e3 Merge branch 'iwyu'
http://include-what-you-use.org/
2015-07-29 09:30:19 +08:00
ridiculousfish
4ebaa7b6bd Continue migration to the new tokenizer interface 2015-07-26 00:12:36 -07:00
David Adam
3929e9de0e Merge branch 'master' into iwyu 2015-07-26 10:20:13 +08:00
ridiculousfish
b4f53143b0 Migrate source files into src/ directory
This change moves source files into a src/ directory,
and puts object files into an obj/ directory. The Makefile
and xcode project are updated accordingly.

Fixes #1866
2015-07-24 00:59:27 -07:00