Skip to content

Commit

Permalink
Merge pull request #4 from 1Kinoti/main
Browse files Browse the repository at this point in the history
update grammar
  • Loading branch information
fdncred authored Apr 26, 2023
2 parents ff69aa6 + 589e18c commit 83ac764
Show file tree
Hide file tree
Showing 11 changed files with 267,059 additions and 6,217 deletions.
88 changes: 41 additions & 47 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,47 +1,41 @@
# tree-sitter-nu

This is an experiment to see if we can create a good parser for nu-lang.

## Goals (brainstorming)

- Ability to serve as a parser for a repl
- Ability to work in editors that support tree-sitter (`hx`, `nvim`, others)
- Ability to work in rust
- Fast
- Ability to be utilized for syntax highlighting
- ?

## How-To - taken from [here](https://tree-sitter.github.io/tree-sitter/creating-parsers)

1. `cargo install tree-sitter-cli`
2. make sure nodejs and npm is installed (you may have to do npm install - see docs above)
3. `tree-sitter generate`
4. `tree-sitter parse example-file.nu`

## Examples with rust

1. main.rs
2. `cargo run --example main`

## Testing

1. Add a file to test/corpus/descriptive_test_name.txt
2. Use the `custom_command_1.txt` as a guide
3. run `tree-sitter test -f 'custom command 1'` and evaluate the results
4. More information can be found [here](https://tree-sitter.github.io/tree-sitter/creating-parsers#command-test)

## Tips

- A line in the grammar.js that begins with `$._blah` means that `blah` is anonymous and won't show up in the final syntax tree.
- Most of the grammar how-to is found [here](https://tree-sitter.github.io/tree-sitter/creating-parsers#the-grammar-dsl)

## Further reading

- https://siraben.dev/2022/03/22/tree-sitter-linter.html
- https://derek.stride.host/posts/comprehensive-introduction-to-tree-sitter

- https://tree-sitter.github.io/tree-sitter/
- https://rfdonnelly.github.io/posts/using-tree-sitter-parsers-in-rust/
- https://deepsource.io/blog/lightweight-linting/
- https://github.com/tree-sitter/tree-sitter/issues/418
- https://github.com/DerekStride/tree-sitter-math
# tree-sitter nu

[**WIP**] [nushell](https://github.com/nushell/nushell) grammar for [tree-sitter](https://tree-sitter.github.io/tree-sitter/)

# issues
1. unquoted strings
```nu
# this should parse as a pipeline with two elements
'nushell' | str contains n
# but it is parsed as two pipelines, with one ending
# at `contains`
'nushell' | str contains
n
# quoting the string solves it though
'nushell' | str contains 'n' # <- okay
```

2. blocks vs records
since we cannot backtrack, it is difficult to tell apart blocks and
records. so currently, blocks are not parsed as values
```nu
# this will not parse
ls | each { $in + 4 }
# however this parses
ls | each {|it| $it + 4 }
```

3. cellpaths with numbers
```nu
ls | $in.4 # does not parse
```

4. command arguments are parsed as strings
```nu
some-cmd 2 -f arg
```

here `2`, `-f`, `arg` will all be parsed as strings
Loading

0 comments on commit 83ac764

Please sign in to comment.