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

no support for herestrings (<<<) #49

Open
spikegrobstein opened this issue Mar 14, 2018 · 8 comments
Open

no support for herestrings (<<<) #49

spikegrobstein opened this issue Mar 14, 2018 · 8 comments

Comments

@spikegrobstein
Copy link

The herestring (<<<) operator in bash is used to pass literal strings/variables into the STDIN of another command. For example. the following two lines are functionally equivalent:

echo "$foo" | grep "x"
grep "x" <<< "$foo"

Trying to parse this with bash-parser results in a parse error:

const ast = parse('grep "x" <<< "$foo"')
Error: Parse error on line 1: Unexpected 'LESS'
    at parse (…/js_bash_ast/node_modules/bash-parser/src/index.js:52:9)
    at fs.readFile (…/js_bash_ast/index.js:14:14)
    at …/js_bash_ast/node_modules/graceful-fs/graceful-fs.js:78:16
    at FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:511:3)

documentation on the herestrings: http://tldp.org/LDP/abs/html/x17837.html

@piranna
Copy link
Collaborator

piranna commented Mar 14, 2018

Thanks for the example and the equivalence, this will help to do a test case :-)

@parro-it
Copy link
Collaborator

Thank you, this is already known, there should be an issue or doc that mentions the lack of herestring. It should be the last POSIX statement missing...

Would you mind doing a PR to implement it? I could provide some suggestion on the sources you have to change to implement it.

@spikegrobstein
Copy link
Author

if you can point me in the right direction for getting it fixed, I'll give it a shot. thanks!

I poked around but didn't see this issue documented anywhere. it took me a while to figure out what LESS it was talking about when it failed to parse my script.

@parro-it
Copy link
Collaborator

if you can point me in the right direction for getting it fixed, I'll give it a shot. thanks!

Awesome!

I poked around but didn't see this issue documented anywhere. it took me a while to figure out what LESS it was talking about when it failed to parse my script.

Ah sorry, I was speaking about POSIX heredocs (that are also missing); I misunderstood you...
So , depending on what you want to accomplish, be aware that there are other bash part missing, you can find a list here: #37.

The operator you're talking about is the last one in the list...

The error you are seeing is because the tokenizer is not aware of the <<< operator, and it is interpreting them (I guess) as tree < (LESS) operators in sequence. and that is not a valid syntax...

@parro-it
Copy link
Collaborator

First, you'll have to change how the bash mode is inherited from POSIX mode. You can start look here:
https://github.com/vorpaljs/bash-parser/blob/master/src/modes/bash/index.js#L124

That module export an object that define the bash mode.
the object contains the property 'inherits', that specify the mode is based on POSIX:

	inherits: 'posix',

and an init function, that the system will call passing the base POSIX mode and an utils object as argument.
The init function then create a copy of the POSIX mode and patch any property or function as needed

@parro-it
Copy link
Collaborator

The POSIX object you receive in init is composed by these properties:

{
			enums,                                              // various enums used by the mode
			tokenizer,                                          // the tokenizer: String -> Iterable<Object>
			lexerPhases                                     // The lexer "enhance" the tokens in a way that they can be understood by the grammar. Is implemented as a list of iterators, composed together. Each of these iterators are keep in the phaseCatalog 
			
                         phaseCatalog, 
			grammarSource,                 // the grammar source code 
			grammar,                             // the grammar built using jison
			astBuilder                            // build the AST nodes for each of the grammar production
		};

You probably could implement the <<< operator by adding a phase to the lexer that check for threee consecutive 'LESS' tokens, and, if the pattern is found, it emit a 'CALL-IT-SOMEHOW' tokens.

Then, you'll have to extend the grammar to take into account the new token.
You probably will have to extend the io_file here:
https://github.com/vorpaljs/bash-parser/blob/master/src/modes/posix/grammar.js#L387

remember that the grammar has to be recompiled using https://github.com/vorpaljs/mode-grammar-builder

@parro-it
Copy link
Collaborator

I suggest you to open a PR early, we can continue the discussion there if you have any doubt...

@spikegrobstein
Copy link
Author

hey, sorry for the late reply. Things have gotten hectic and I don't think I'm going to have time to put together a pull-request. I don't wanna leave you guys hanging.

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

No branches or pull requests

3 participants