Skip to content

Commit 1526dbc

Browse files
committed
moar changes
1 parent d48ab83 commit 1526dbc

File tree

8 files changed

+970
-879
lines changed

8 files changed

+970
-879
lines changed

grammar.pegjs

Lines changed: 42 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
script "one or more statements separated by control operators"
1+
script
2+
= sections:(spaceNL* statementList spaceNL*)+
3+
4+
statementList "a list of statements"
25
= first:statement
3-
rest:(controlOperator statement)*
4-
last:controlOperator?
5-
spaceNL*
6+
rest:(space* controlOperator spaceNL* statement)*
7+
last:(space* controlOperator)?
68

79
statement
810
= statement:( command
911
/ conditionalLoop
1012
/ ifBlock
1113
)
12-
space* next:chainedStatement?
14+
next:(space* chainedStatement)?
1315

1416
chainedStatement
1517
= operator:('&&' / '||') spaceNL* statement:statement
@@ -18,36 +20,39 @@ controlOperator
1820
= op:('&' / ';' / '\n')
1921

2022
command "a single command"
21-
= spaceNL*
22-
pre:((variableAssignment / redirect) space+)*
23-
name:commandName
23+
= pre:((variableAssignment / redirect) space+)*
24+
name:(commandName / builtinCommandName)
2425
post:(space+ (redirect / argument))*
2526

2627
conditionalLoop
27-
= kind:("while" / "until") spaceNL+ test:script spaceNL*
28-
"do" spaceNL*
29-
body:script spaceNL*
30-
"done" spaceNL*
28+
= kind:("while" / "until") spaceNL+ test:condition spaceNL*
29+
"do" spaceNL
30+
body:script
31+
"done"
3132

3233
ifBlock
33-
= "if" spaceNL+ test:script spaceNL*
34-
"then" spaceNL* body:script spaceNL*
34+
= "if" spaceNL+ test:script
35+
"then" spaceNL+ body:script
3536
elifBlocks:elifBlock*
3637
elseBody:("else" script)?
37-
"fi" spaceNL*
38+
"fi"
3839

3940
elifBlock
40-
= "elif" spaceNL+ test:script "then" spaceNL+ body:script
41+
= "elif" spaceNL+ test:condition "then" spaceNL+ body:script
4142

4243
condition
43-
= '[' test:script ']'
44+
= test:script
4445

4546
variableAssignment
4647
= writableVariableName '=' argument
4748

4849
commandName "command name"
4950
= !redirect !keyword name:(concatenation / '[')
5051

52+
builtinCommandName
53+
= '['
54+
/ '[['
55+
5156
argument "command argument"
5257
= commandName
5358
/ commandSubstitution
@@ -63,32 +68,37 @@ concatenation
6368
/ doubleQuote
6469
)+
6570

66-
bareword = cs:barewordChar+
71+
bareword
72+
= cs:barewordChar+
6773

6874
barewordChar
6975
= '\\' chr:barewordMeta { return chr }
7076
/ !barewordMeta chr:. { return chr }
7177

72-
barewordMeta = [$"';&<>\n()\[\]*?|` ]
78+
barewordMeta = [$"';&<>\n()\[*?|` ]
7379

74-
glob = (barewordChar* ('*' / '?' / characterRange / braceExpansion)+ barewordChar*)+
80+
glob
81+
= barewordChar* ('*' / '?' / characterRange / braceExpansion)+ barewordChar*
7582

76-
characterRange =
77-
$('[' !'-' . '-' !'-' . ']')
83+
characterRange
84+
= $('[' !'-' . '-' !'-' . ']')
7885

79-
braceExpansion =
80-
(.? !'$') '{' barewordChar+ '}'
86+
braceExpansion
87+
= (.? !'$') '{' barewordChar+ '}'
8188

82-
singleQuote = "'" inner:$([^']*) "'"
89+
singleQuote
90+
= "'" inner:$([^']*) "'"
8391

84-
doubleQuote = '"' contents:(expandsInQuotes / doubleQuoteChar+)* '"'
92+
doubleQuote
93+
= '"' contents:(expandsInQuotes / doubleQuoteChar+)* '"'
8594

8695
doubleQuoteChar
8796
= '\\' chr:doubleQuoteMeta { return chr }
8897
/ '\\\\' { return '\\' }
8998
/ !doubleQuoteMeta chr:. { return chr }
9099

91-
doubleQuoteMeta = '"' / '$' / '`'
100+
doubleQuoteMeta
101+
= '"' / '$' / '`'
92102

93103
expandsInQuotes
94104
= backticks
@@ -107,10 +117,10 @@ backticks
107117
= '`' commands:(!backticks command)+ '`'
108118

109119
subshell
110-
= '$(' commands:script ')'
120+
= '$(' commands:statementList ')'
111121

112122
commandSubstitution
113-
= rw:[<>] '(' commands:script ')'
123+
= rw:[<>] '(' commands:statementList ')'
114124

115125
redirect
116126
= moveFd / duplicateFd / redirectFd / pipe
@@ -151,7 +161,9 @@ keyword
151161
/ "then"
152162
/ "else"
153163
/ "elif"
154-
/ "fi" )
164+
/ "fi"
165+
/ "[["
166+
)
155167
( spaceNL+ / EOF )
156168

157169
continuationStart

overrides.js

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,14 @@ exports.initializer = [
4949
// it's still a concatenation, append it's pieces to ours
5050
result = result.concat(current.pieces)
5151
}
52-
else if (current.type == 'literal' && prev.type == 'literal') {
53-
// merge two literals
52+
else if ((current.type == 'literal' || current.type == 'glob')
53+
&& (prev.type == 'literal' || prev.type == 'glob')) {
54+
// globs & literals can be merged
5455
prev.value += current.value
56+
if (prev.type != 'glob' && current.type == 'glob') {
57+
// globs are infectious
58+
prev.type = 'glob'
59+
}
5560
}
5661
else {
5762
result.push(current)
@@ -65,12 +70,20 @@ exports.initializer = [
6570
}
6671
].join('\n')
6772

68-
rules.script = function (first, rest, last) {
73+
rules.script = function (sections) {
74+
var statements = []
75+
map(sections, function (section) {
76+
statements = statements.concat(section[1])
77+
})
78+
return statements
79+
}
80+
81+
rules.statementList = function (first, rest, last) {
6982
var statements = [first]
7083
var prev = first
71-
map(rest, function (oc, i, cmds) {
72-
setOperator(oc[0], prev)
73-
statements.push(prev = oc[1])
84+
map(rest, function (spaceOpSpaceCmd, i, cmds) {
85+
setOperator(spaceOpSpaceCmd[1], prev)
86+
statements.push(prev = spaceOpSpaceCmd[3])
7487
})
7588
return statements
7689

@@ -108,12 +121,13 @@ rules.elifBlock = function (test, body) {
108121
}
109122
}
110123

111-
rules.condition = function (test) {
124+
rules.condition = function bare (test) {
112125
return test
113126
}
114127

115128
rules.statement = function (statement, next) {
116-
if (next) {
129+
if (typeof next !== 'undefined' && next) {
130+
next = next[1]
117131
statement.control = next[0]
118132
statement.next = next[1]
119133
} else {
@@ -188,7 +202,7 @@ rules.bareword = function (cs) { return literal(cs) }
188202
rules.glob = function (cs) {
189203
return {
190204
type: 'glob',
191-
pattern: text()
205+
value: text()
192206
}
193207
}
194208

0 commit comments

Comments
 (0)