4 ms·
Do you have any thoughts, or writings that you could point me to, regarding shell's parseability? For example, is it possible to parse a shell script without ru
by drothlis 13y ago
Do you have any thoughts, or writings that you could point me to,
regarding shell's parseability? For example, is it possible to parse a
shell script without running it? Would it be possible to extract Bash's
lexer+parser into a stand-alone library to be used by IDEs?
- idank 13y agoYes, you can parse without executing. I think shell is easier to parse than it seems, there are definitely some nuances that might be tricky to emulate, but I guess it's a matter of effort and how far you're willing to go. For syntax highlighting in IDEs, you probably don't need much, and maybe even a few regexs can get you close enough. I actually spent a considerable amount of time going over bash's parser [0], and also zsh's [1]. Bash uses lex/yacc with some custom code. zsh seems to have a custom written lexer/parser. Unfortunately none of them were written to be used as a library, as evident by global state, and a lot of hooks for prompts while parsing, etc. It might be possible to somehow make it work, but it seems like a lot of work to me. You might have an easier time looking at the parser I wrote, and extending that, or rewriting it in another language (as it's fairly small compared to the ones you find in shells). There's also libbash [2], haven't looked closely though. [0]: http://git.savannah.gnu.org/cgit/bash.git/tree/parse.y http://git.savannah.gnu.org/cgit/bash.git/tree/parse.y [1]: https://github.com/zsh-users/zsh/blob/master/Src/parse.c https://github.com/zsh-users/zsh/blob/master/Src/parse.c [2]: http://www.gentoo.org/proj/en/libbash/ http://www.gentoo.org/proj/en/libbash/