1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
|
Start
= S* line:ShellLine? { return line ? line : [] }
ShellLine
= command:CommandLine S* type:ShellLineType then:ShellLineThen? { return [ { command, type } ].concat(then || []) }
/ command:CommandLine S* type:ShellLineType? { return [ { command, type: type || ';' } ] }
ShellLineThen
= S* then:ShellLine S* { return then }
ShellLineType
= ';'
/ '&'
CommandLine
= chain:CommandChain then:CommandLineThen? { return then ? { chain, then } : { chain } }
CommandLineThen
= S* type:CommandLineType S* then:CommandLine S* { return { type, line: then } }
CommandLineType
= '&&'
/ '||'
CommandChain
= main:Command then:CommandChainThen? { return then ? { ...main, then } : main }
CommandChainThen
= S* type:CommandChainType S* then:CommandChain S* { return { type, chain: then } }
CommandChainType
= '|&'
/ '|'
VariableAssignment
= name:EnvVariable '=' arg:StrictValueArgument S* { return { name, args: [arg] } }
/ name:EnvVariable '=' S* { return { name, args: [] } }
Command
= S* "(" S* subshell:ShellLine S* ")" S* args:RedirectArgument* S* { return { type: `subshell`, subshell, args } }
/ S* "{" S* group:ShellLine S* "}" S* args:RedirectArgument* S* { return { type: `group`, group, args } }
/ S* envs:VariableAssignment* S* args:Argument+ S* { return { type: `command`, args, envs } }
/ S* envs:VariableAssignment+ S* { return { type: `envs`, envs } }
CommandString
= S* args:ValueArgument+ S* { return args }
Argument
= S* arg:RedirectArgument { return arg }
/ S* arg:ValueArgument { return arg }
RedirectArgument
= S* fd:[0-9]? redirect:RedirectType arg:ValueArgument { return { type: `redirection`, subtype: redirect, fd: fd !== null ? parseInt(fd) : null, args: [arg] } }
RedirectType
= '>>'
/ '>&'
/ '>'
/ '<<<'
/ '<&'
/ '<'
ValueArgument
= S* arg:StrictValueArgument { return arg }
StrictValueArgument
= segments:ArgumentSegment+ { return { type: `argument`, segments: [].concat(... segments) } }
ArgumentSegment
= string:CQuoteString { return string }
/ string:SglQuoteString { return string }
/ string:DblQuoteString { return string }
/ string:PlainString { return string }
CQuoteString
= "$'" text:CQuoteStringText "'" { return [ { type: `text`, text } ] }
SglQuoteString
= "'" text:SglQuoteStringText "'" { return [ { type: `text`, text } ] }
DblQuoteString
= '""' { return { type: `text`, text: `` } } / '"' segments:DblQuoteStringSegment* '"' { return segments }
PlainString
= segments:PlainStringSegment+ { return segments }
DblQuoteStringSegment
= arithmetic:Arithmetic { return { type: `arithmetic`, arithmetic, quoted: true} }
/ shell:Subshell { return { type: `shell`, shell, quoted: true } }
/ variable:Variable { return { type: `variable`, ...variable, quoted: true } }
/ text:DblQuoteStringText { return { type: `text`, text } }
PlainStringSegment
= arithmetic:Arithmetic { return { type: `arithmetic`, arithmetic, quoted: false} }
/ shell:Subshell { return { type: `shell`, shell, quoted: false } }
/ variable:Variable { return { type: `variable`, ...variable, quoted: false } }
/ pattern:Glob { return { type: `glob`, pattern } }
/ text:PlainStringText { return { type: `text`, text } }
SglQuoteStringText
= chars:[^']* { return chars.join(``) }
DblQuoteStringText
= chars:( DblQuoteEscapedChar / [^$"])+ { return chars.join(``) }
DblQuoteEscapedChar
= '\\\n' { return `` }
/ '\\' c:[\\$"`] { return c }
CQuoteStringText
= chars:(CQuoteEscapedChar / [^'])* { return chars.join(``) }
CQuoteEscapedChar
= '\\a' { return '\a' }
/ '\\b' { return '\b' }
/ '\\' [Ee] { return '\x1b' }
/ '\\f' { return '\f' }
/ '\\n' { return '\n' }
/ '\\r' { return '\r' }
/ '\\t' { return '\t' }
/ '\\v' { return '\v' }
/ '\\' c:[\\'"?] { return c }
/ HexCodeString
HexCodeString
= '\\' c:HexCodeChar0 { return String.fromCharCode(parseInt(c, 16)) }
/ char:('\\x' c:$(HexCodeChar0 HexCodeChar / HexCodeChar0) { return String.fromCharCode(parseInt(c, 16)) })
/ utf8:('\\u' c:$(HexCodeChar HexCodeChar HexCodeChar HexCodeChar) { return String.fromCharCode(parseInt(c, 16)) })
/ utf16:('\\U' c:$(HexCodeChar HexCodeChar HexCodeChar HexCodeChar HexCodeChar HexCodeChar HexCodeChar HexCodeChar) { return String.fromCodePoint(parseInt(c, 16)) })
HexCodeChar0
= [0-7]
HexCodeChar
= [0-9a-fA-f]
PlainStringText
= chars:('\\' c:. { return c } / '{}' { return '{}' } / !SpecialShellChars c:. { return c })+ { return chars.join(``) }
ArithmeticPrimary
= sign:('-' / '+')? left:[0-9]+ '.' right:[0-9]+ { return { type: `number`, value: (sign === '-' ? -1 : 1) * parseFloat(left.join(``) + `.` + right.join(``)) } }
/ sign:('-' / '+')? value:[0-9]+ { return { type: `number`, value: (sign === '-' ? -1 : 1) * parseInt(value.join(``)) } }
/ variable:Variable { return { type: `variable`, ...variable } }
/ name:Identifier { return { type: `variable`, name } }
/ '(' S* value:ArithmeticExpression S* ')' { return value }
ArithmeticTimesExpression
= left:ArithmeticPrimary rest:(S* op:('*' / '/') S* right:ArithmeticPrimary { return { type: op === `*` ? `multiplication` : `division`, right } })* {
return rest.reduce((left, right) => ({ left, ...right }), left)
}
ArithmeticExpression
= left:ArithmeticTimesExpression rest:(S* op:('+' / '-') S* right:ArithmeticTimesExpression { return { type: op === `+` ? `addition` : `subtraction`, right } })* {
return rest.reduce((left, right) => ({ left, ...right }), left)
}
Arithmetic
= '$((' S* arithmetic:ArithmeticExpression S* '))' { return arithmetic }
Subshell
= '$(' command:ShellLine ')' { return command }
Variable
= '${' name:Identifier ':-' arg:CommandString '}' { return { name, defaultValue: arg } }
/ '${' name:Identifier ':-}' { return { name, defaultValue: [] } }
/ '${' name:Identifier ':+' arg:CommandString '}' { return { name, alternativeValue: arg } }
/ '${' name:Identifier ':+}' { return { name, alternativeValue: [] } }
/ '${' name:Identifier '}' { return { name } }
/ '$' name:Identifier { return { name } }
Glob
= pattern:GlobText & { return options.isGlobPattern(pattern) } { return pattern }
GlobText
= chars:(!GlobSpecialShellChars c:. { return c })+ { return chars.join(``) }
EnvVariable
= [a-zA-Z0-9_]+ { return text() }
Identifier
= [$@*?#a-zA-Z0-9_-]+ { return text() }
SpecialShellChars
= [()}<>$|&; \t"']
GlobSpecialShellChars
= [<>&; \t"']
S = [ \t]+
|