Skip to content

Commit 03b1986

Browse files
alexwforsythemaaslalani
authored andcommitted
parser: catch out of order modifiers
1 parent 637e426 commit 03b1986

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

parser.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,17 +147,27 @@ func (p *Parser) parseTime() string {
147147
func (p *Parser) parseCtrl() Command {
148148
var args []string
149149

150+
inModifierChain := true
150151
for p.peek.Type == PLUS {
151152
p.nextToken()
152153
peek := p.peek
153154

154155
// Get key from keywords and check if it's a valid modifier
155156
if k := keywords[peek.Literal]; IsModifier(k) {
157+
if !inModifierChain {
158+
p.errors = append(p.errors, NewError(p.cur, "Modifiers must come before other characters"))
159+
// Clear args so the error is returned
160+
args = nil
161+
continue
162+
}
163+
156164
args = append(args, peek.Literal)
157165
p.nextToken()
158166
continue
159167
}
160168

169+
inModifierChain = false
170+
161171
// Add key argument.
162172
switch {
163173
case peek.Type == ENTER,

parser_test.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -220,10 +220,14 @@ func TestParseCtrl(t *testing.T) {
220220
wantErr: false,
221221
},
222222
{
223-
name: "should parse with out of order modifiers",
224-
tape: "Ctrl+Shift+C+Alt",
225-
wantArgs: []string{"Shift", "C", "Alt"},
226-
wantErr: false,
223+
name: "should not parse with out of order modifiers",
224+
tape: "Ctrl+Shift+C+Alt",
225+
wantErr: true,
226+
},
227+
{
228+
name: "should not parse with out of order modifiers",
229+
tape: "Ctrl+Shift+C+Alt+C",
230+
wantErr: true,
227231
},
228232
{
229233
tape: "Ctrl+Alt+Right",

0 commit comments

Comments
 (0)