Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expansions #196

Merged
merged 5 commits into from
Aug 24, 2023
Merged

Expansions #196

merged 5 commits into from
Aug 24, 2023

Conversation

amaanq
Copy link
Member

@amaanq amaanq commented Aug 23, 2023

@amaanq amaanq mentioned this pull request Aug 23, 2023
@amaanq
Copy link
Member Author

amaanq commented Aug 23, 2023

${!x//c/x} is tough

@ObserverOfTime
Copy link
Member

${!x//c/x} is tough

I don't think that's valid...

@amaanq
Copy link
Member Author

amaanq commented Aug 23, 2023

it's in the examples corpus (bash repo)

Fixing some things breaks others, and vice versa 😁

@ObserverOfTime
Copy link
Member

Fixing some things breaks others, and vice versa 😁

The circle of life.

@ObserverOfTime
Copy link
Member

Maybe you could make expansions start with optional('!') with the exception of ${#foo}, ${!foo*} & ${!foo@}.

@c4rlo c4rlo mentioned this pull request Aug 23, 2023
@amaanq
Copy link
Member Author

amaanq commented Aug 24, 2023

${!##} is doing my head in

@amaanq amaanq force-pushed the expansions branch 2 times, most recently from 42957cb to 29d4030 Compare August 24, 2023 04:16
@amaanq amaanq force-pushed the expansions branch 2 times, most recently from a56888e to 53f03b5 Compare August 24, 2023 04:52
@amaanq
Copy link
Member Author

amaanq commented Aug 24, 2023

after a few painful hours I think I have a complete solution, would appreciate you trying it out @ObserverOfTime

@ObserverOfTime
Copy link
Member

ObserverOfTime commented Aug 24, 2023

Expansions appear to be fine now but there are a few arithmetic issues left.

  • Operators in C-style binary expressions are not operator fields.
  • The unary expression operators "-" "+" "~" cannot be captured.
    • Maybe unary and postfix operators should also be fields.
  • test_operator seems to have higher precedence over negative variables.
echo $((- f))
echo $((-f))
Parse tree
0:0  - 2:0     program
0:0  - 0:13      command
0:0  - 0:4         name: command_name
0:0  - 0:4           word `echo`
0:5  - 0:13        argument: arithmetic_expansion
0:5  - 0:8           "$(("
0:8  - 0:11          unary_expression
0:10 - 0:11            variable_name `f`
0:11 - 0:13          "))"
0:13 - 1:0       "\n"
1:0  - 1:12      command
1:0  - 1:4         name: command_name
1:0  - 1:4           word `echo`
1:5  - 1:12        argument: arithmetic_expansion
1:5  - 1:8           "$(("
1:8  - 1:10          test_operator `-f`
1:10 - 1:12          "))"
1:12 - 2:0       "\n"

@amaanq
Copy link
Member Author

amaanq commented Aug 24, 2023

are test operators allowed in arithmetic expansions? (I'm guessing no)

@ObserverOfTime
Copy link
Member

ObserverOfTime commented Aug 24, 2023

Not as far as I can tell.

echo $((0 -eq 0))
# bash: 0 -eq 0: syntax error in expression (error token is "0")

I assume it parses 0 - eq as a binary expression so the error is on the leftover 0.

@amaanq
Copy link
Member Author

amaanq commented Aug 24, 2023

all operators are fielded & should be exposed (incl. unary ops)

@amaanq
Copy link
Member Author

amaanq commented Aug 24, 2023

Hmm interesting, token(prec(choice(...))) prevents querying the operators, but not choice(...token(prec())) (no need for aliasing)

@ahlinc

@ObserverOfTime
Copy link
Member

I still can't seem to capture unary operators.

@amaanq
Copy link
Member Author

amaanq commented Aug 24, 2023

got a sample query code? this works for me on this branch:

(unary_expression "+" @op)

@ObserverOfTime
Copy link
Member

Does it work with x=$((+y)) ?

@amaanq
Copy link
Member Author

amaanq commented Aug 24, 2023

damn, the query compiles but doesn't seem to execute properly, I'll bring back the aliases

@ObserverOfTime
Copy link
Member

I will once again advocate for a prec.lexical function that is intuitive and works as expected.

@amaanq
Copy link
Member Author

amaanq commented Aug 24, 2023

Nope, this was my fault. I forgot I had _arithmetic_unary_expression too

image

I would say the current mechanism works as expected (after the bug fix yesterday), but it's definitely not intuitive.

@amaanq
Copy link
Member Author

amaanq commented Aug 24, 2023

try now @ObserverOfTime

@ObserverOfTime
Copy link
Member

ObserverOfTime commented Aug 24, 2023

No field. :^)
But it does work.

@amaanq
Copy link
Member Author

amaanq commented Aug 24, 2023

would you consider ternaries as candidates to be fielded as operator?

@ObserverOfTime
Copy link
Member

would you consider ternaries as candidates to be fielded as operator?

It's fine either way.

@amaanq
Copy link
Member Author

amaanq commented Aug 24, 2023

Alright, I don't really care to - so this should be good as is hopefully

@ObserverOfTime
Copy link
Member

Yeah, finally bash is usable.

@amaanq
Copy link
Member Author

amaanq commented Aug 24, 2023

Yeah it was pretty "eh" the few times I used it in the past :^) (and arithmetic expansions broke everything)

But writing the parser this language definitely sucks

@ObserverOfTime
Copy link
Member

But writing the parser this language definitely sucks

What do you mean this language sucks? 💢

@amaanq
Copy link
Member Author

amaanq commented Aug 24, 2023

pfft...it won't suck all that much compared to zsh 😬 🤢

@amaanq amaanq merged commit 9903ac8 into tree-sitter:master Aug 24, 2023
5 checks passed
@ObserverOfTime
Copy link
Member

Or powershell.

@amaanq
Copy link
Member Author

amaanq commented Aug 24, 2023

as if I'd want to touch Windows 😁

@verhovsky
Copy link
Collaborator

@amaanq powershell actually runs on Linux and macOS, it's even open source under the MIT license.

https://formulae.brew.sh/cask/powershell

https://github.com/PowerShell/PowerShell

@amaanq
Copy link
Member Author

amaanq commented Aug 24, 2023

wow, I never knew, that's cool but I wonder if people actually use it on a non-Windows OS (except for cross-platform testing)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants