Skip to content

Instantly share code, notes, and snippets.

@cking
Created May 3, 2016 21:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cking/4823847da30aa24bef6487c3169c7945 to your computer and use it in GitHub Desktop.
Save cking/4823847da30aa24bef6487c3169c7945 to your computer and use it in GitHub Desktop.
Line
= _* cmd:Command pipes:(_* "|" _* Command)* redirect:(_* ">" _* Command)? _*
{ return { cmd, pipes: pipes? pipes.map(pipe => pipe[3]): null, redirect: redirect? redirect[3]: null } }
Command
= exe:Executable args:Arguments
{ return { exe, args } }
Executable
= ExecutableName ("/" ExecutableName)*
{ return text() }
ExecutableName
= [^/ \t]+
Arguments
= args:(_+ Argument)*
{ return args.map(arg => arg[1]) }
Argument
= LiteralArgument
/ CommandArgument
/ SingleQuoteArgument
/ DoubleQuoteArgument
LiteralArgument
= LiteralArgumentChar+
{ return { type: "literal", content: text() } }
LiteralArgumentChar "literal argument char"
= "\\" .
/ [^\\\n\r'"|> ]
CommandArgument
= "$(" line:Line ")"
{ return { type: "parse", content: [ { type: "cmd", cmd } ] } }
SingleQuoteArgument
= "'" SingleQuoteArgumentChar+ "'"
{ return { type: "literal", content: text().substr(1, text().length - 2) } }
SingleQuoteArgumentChar "single quote char"
= "\\" .
/ [^\\\n\r'|>]
DoubleQuoteArgument
= '"' part:DoubleQuoteArgumentPart* '"'
{ return { type: "parse", content: part } }
DoubleQuoteArgumentPart
= DoubleQuoteArgumentChar+ { return { type: "literal", content: text() } }
/ cmd:DoubleQuoteCommand { return { type: "cmd", cmd } }
DoubleQuoteCommand
= "$(" line:Line ")"
{ return line }
DoubleQuoteArgumentChar "double quote char"
= "\\" .
/ [^\\\n\r"|>$]
_ "whitespace"
= [ \t]+
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment