Skip to content

Expansions - #196

Merged
amaanq merged 5 commits into
tree-sitter:masterfrom
amaanq:expansions
Aug 24, 2023
Merged

amaanq merged 5 commits into
tree-sitter:masterfrom
amaanq:expansions

Conversation

@amaanq

@amaanq amaanq commented Aug 23, 2023

Copy link
Copy Markdown
Member

@amaanq amaanq mentioned this pull request Aug 23, 2023
@amaanq

amaanq commented Aug 23, 2023

Copy link
Copy Markdown
Member Author

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

@ObserverOfTime

Copy link
Copy Markdown
Member

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

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

@amaanq

amaanq commented Aug 23, 2023 •

Copy link
Copy Markdown
Member Author

it's in the examples corpus (bash repo)

Fixing some things breaks others, and vice versa 😁

@ObserverOfTime

Copy link
Copy Markdown
Member

Fixing some things breaks others, and vice versa 😁

The circle of life.

@ObserverOfTime

Copy link
Copy Markdown
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

amaanq commented Aug 24, 2023

Copy link
Copy Markdown
Member Author

${!##} 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

amaanq commented Aug 24, 2023

Copy link
Copy Markdown
Member Author

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

Comment thread test/corpus/literals.txt Resolved
@ObserverOfTime

ObserverOfTime commented Aug 24, 2023 •

Copy link
Copy Markdown
Member

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

amaanq commented Aug 24, 2023 •

Copy link
Copy Markdown
Member Author

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

@ObserverOfTime

ObserverOfTime commented Aug 24, 2023 •

Copy link
Copy Markdown
Member

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

amaanq commented Aug 24, 2023

Copy link
Copy Markdown
Member Author

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

@amaanq

amaanq commented Aug 24, 2023 •

Copy link
Copy Markdown
Member Author

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

@ahlinc

@ObserverOfTime

Copy link
Copy Markdown
Member

I still can't seem to capture unary operators.

@amaanq

amaanq commented Aug 24, 2023 •

Copy link
Copy Markdown
Member Author

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

(unary_expression "+" @op)

@ObserverOfTime

Copy link
Copy Markdown
Member

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

@amaanq

amaanq commented Aug 24, 2023

Copy link
Copy Markdown
Member Author

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

@ObserverOfTime

Copy link
Copy Markdown
Member

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

@amaanq

amaanq commented Aug 24, 2023 •

Copy link
Copy Markdown
Member Author

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

amaanq commented Aug 24, 2023

Copy link
Copy Markdown
Member Author

try now @ObserverOfTime

@ObserverOfTime

ObserverOfTime commented Aug 24, 2023 •

Copy link
Copy Markdown
Member

No field. :^)
But it does work.

@amaanq

amaanq commented Aug 24, 2023 •

Copy link
Copy Markdown
Member Author

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

@ObserverOfTime

Copy link
Copy Markdown
Member

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

It's fine either way.

@amaanq

amaanq commented Aug 24, 2023

Copy link
Copy Markdown
Member Author

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

@ObserverOfTime

Copy link
Copy Markdown
Member

Yeah, finally bash is usable.

@amaanq

amaanq commented Aug 24, 2023

Copy link
Copy Markdown
Member Author

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
Copy Markdown
Member

But writing the parser this language definitely sucks

What do you mean this language sucks? 💢

@amaanq

amaanq commented Aug 24, 2023

Copy link
Copy Markdown
Member Author

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

@amaanq
amaanq merged commit 9903ac8 into tree-sitter:master Aug 24, 2023
@ObserverOfTime

Copy link
Copy Markdown
Member

Or powershell.

@amaanq

amaanq commented Aug 24, 2023

Copy link
Copy Markdown
Member Author

as if I'd want to touch Windows 😁

@verhovsky

Copy link
Copy Markdown
Collaborator

@amaanq

amaanq commented Aug 24, 2023 •

Copy link
Copy Markdown
Member Author

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