Skip to content

Commit

Permalink
Allow applying attributes to multiple prereqs at once with backquotes
Browse files Browse the repository at this point in the history
  • Loading branch information
zyedidia committed Nov 25, 2022
1 parent a870c81 commit 3143bf6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 25 deletions.
29 changes: 4 additions & 25 deletions rules/lex.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,6 @@ func lexTopLevel(l *lexer) lexerStateFun {
return lexSingleQuotedWord
case '`':
return lexBackQuotedWord
// case '[':
// return lexBracketedWord
}

return lexBareWord
Expand All @@ -317,25 +315,6 @@ func lexComment(l *lexer) lexerStateFun {
return lexTopLevel
}

// func lexBracketedWord(l *lexer) lexerStateFun {
// l.skip() // '['
// for l.peek() != ']' && l.peek() != eof {
// l.acceptUntil("\\]")
// if l.accept("\\") {
// l.accept("]")
// }
// }
//
// if l.peek() == eof {
// l.lexError("end of file encountered while parsing a bracketed string.")
// }
//
// l.pushtmp()
//
// l.skip() // ']'
// return lexBareWord
// }

func lexDoubleQuotedWord(l *lexer) lexerStateFun {
l.skip() // '"'
for l.peek() != '"' && l.peek() != eof {
Expand All @@ -356,11 +335,11 @@ func lexDoubleQuotedWord(l *lexer) lexerStateFun {
}

func lexBackQuotedWord(l *lexer) lexerStateFun {
l.skip() // '`'
l.next() // '`'
l.acceptUntil("`")
l.pushtmp()
l.skip() // '`'
return lexBareWord
l.next() // '`'
l.emit(tokenWord)
return lexTopLevel
}

func lexSingleQuotedWord(l *lexer) lexerStateFun {
Expand Down
16 changes: 16 additions & 0 deletions rules/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,22 @@ func parseRecipe(p *parser, t token) parserStateFun {
// prereqs
base.prereqs = make([]string, 0)
for k := j + 1; k < len(p.tokenbuf); k++ {
// pretty hacky method to handle applying attributes to multiple prereqs at once with `prereqs`[attrs] syntax
if strings.HasPrefix(p.tokenbuf[k].val, "`") && strings.HasSuffix(p.tokenbuf[k].val, "`") {
length := len(p.tokenbuf[k].val)
inner := lex(p.tokenbuf[k].val[1:length-1], p.tokenbuf[k].line)
attrs := ""
if len(p.tokenbuf) > k+1 && strings.HasPrefix(p.tokenbuf[k+1].val, "[") {
attrs = p.tokenbuf[k+1].val
}
for t := inner.nextToken(); t.typ != tokenEnd; t = inner.nextToken() {
if t.typ == tokenError {
break
}
base.prereqs = append(base.prereqs, t.val+attrs)
}
continue
}
base.prereqs = append(base.prereqs, p.tokenbuf[k].val)
}

Expand Down

0 comments on commit 3143bf6

Please sign in to comment.