Skip to content

Commit

Permalink
Shasta extension for libbash (#5)
Browse files Browse the repository at this point in the history
* Updates to shasta for bash

* Implemented all types of bash redirects

* update redirection structure

* fix time node implementation

* all bash tests pass shasta

* Shasta passing all bash scripts including those with SOH

* bug fixes to make sure we're still compatible with dash

* minor bug fix

* bug fix for arith nodes

* bug fix eof edge case

* Integrate bash and dash tests

* Add global BASH_MODE

Somewhat of a bad practice but, otherwise, every class that calls
string_of_arg would have to have a bash_mode field to change behavior
for it

* Don't escape dollar signs for bash mode

Currently, bash mode interprets all characters as CArgChar, relying
on the user to manually expand as necessary. This breaks string_of_arg,
since it escapes all '$' CArgChars, assuming that it shouldn't be
interprted as variable interpolation when it's pretty printed. This
disables escaping in bash mode.

* Backport bash changes to python 3.8

Only requires adding a few "from __future__ import annotations"
to allow type hints like list[int] to be used instead of List[int]

* Add BashNode superclass marker

* Fix type hinting as possible

Type hinting is only being used for documentation at this time,
but it's good to use specific and syntactically correct type hints.
However, to use the type checker in a useful way, you'd have to
use isinstance instead of string tags for the AstNodes
(or an equivalent "is_type(*AstNode)" function).

* Remove unused bash code

* Add bash documentation

* Add libbash as dependency

* Remove libbash as dependency

See #5 (comment)
For tests, libbash (and now libdash) are dev dependencies.

* Fix typo

* Format new code

* Fix linting errors

* Refactor utf-8 check

* Refactor BASH_MODE for global variable

Caused very annoying bug in that bash_node was being set in the bash
parsing code, but then dash parsing code is called internally, then
bash parsed objects print out in dash mode.

Side note: this is why they teach that globals are bad in intro to CS!

* Fix code typo

* Update shasta version

Signed-off-by: Bolun Thompson <[email protected]>

* Add case fall through support

Signed-off-by: Bolun Thompson <[email protected]>

* Fix case bug

Signed-off-by: Bolun Thompson <[email protected]>

---------

Signed-off-by: Bolun Thompson <[email protected]>
Co-authored-by: sethsabar <[email protected]>
  • Loading branch information
BolunThompson and sethsabar authored Dec 17, 2024
1 parent a85f998 commit be560cf
Show file tree
Hide file tree
Showing 685 changed files with 47,146 additions and 86 deletions.
36 changes: 36 additions & 0 deletions dash-bash-diffs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Differences between Dash and Bash frontends

Shasta was designed for libdash, with libbash support added later.
Both `json_to_ast` (for libdash) and `bash_to_shasta_ast` (for libbash) contain `to_ast_node(s)` functions,
which each take a parsed, untyped AST and convert it to a shasta AST as defined in `ast_node`.
Thr transformation is direct; bash is assumed to be a superset of dash. This is not strictly true, since
both have some minor divergences from the POSIX spec, but is good enough.

### The following fields of `AstNodes` are only used for Bash scripts:

- `RedirectionNode` and `*RedirNode`: The `fd` field is either `('var', filename)` or `('fixed', fd)`.
Dash only uses the latter form; the first exists to support bashisms like `exec {fd} > log.txt`,
which open the file and assigns `fd` the new file descriptor.
- `FileRedirNode`: 'ReadingString' subtype. Handles here-strings.
- `BackgroundNode`: The `after_ampersand` field. Handles an edge case with heredocs.

The pretty printer uses the various `nobraces` and `semicolon` fields to determine
whether braces and semicolons are printed. In dash, this doesn't matter, but in bash
they can lead to a syntax error. For example:

```bash
( { echo hi; echo bye } )
```
is invalid.
### The following AstNodes are only used for Bash scripts:
- `SelectNode`
- `ArithNode`
- `CondNode`
- `ArithForNode`
- `CoprocNode`
- `TimeNode`
- `SingleArgRedirNode`
- `GroupNode`
5 changes: 4 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "shasta"
version = "0.1.0"
version = "0.2.0"
authors = [
{ name="Konstantinos Kallas", email="[email protected]" },
]
Expand All @@ -15,6 +15,9 @@ classifiers = [
"Operating System :: POSIX",
]

[project.optional-dependencies]
dev = ["libbash", "libdash"]

[project.urls]
"Homepage" = "https://github.com/binpash/shasta"
"Bug Tracker" = "https://github.com/binpash/shasta/issues"
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
long_description = (Path(__file__).parent / "README.md").read_text()

setup(name='shasta',
version='0.1.0',
version='0.2.0',
packages=['shasta'],
## Necessary for the markdown to be properly rendered
long_description=long_description,
Expand Down
Loading

0 comments on commit be560cf

Please sign in to comment.