aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ast.go7
-rw-r--r--parser.go45
-rw-r--r--testcase/comment_in_command.mk5
3 files changed, 47 insertions, 10 deletions
diff --git a/ast.go b/ast.go
index 8689b70..9cc566e 100644
--- a/ast.go
+++ b/ast.go
@@ -1,6 +1,9 @@
package main
-import "fmt"
+import (
+ "fmt"
+ "strings"
+)
type AST interface {
eval(*Evaluator)
@@ -66,7 +69,7 @@ func (ast *MaybeRuleAST) eval(ev *Evaluator) {
func (ast *MaybeRuleAST) show() {
Log("%s", ast.expr)
for _, cmd := range ast.cmds {
- Log("\t%s", cmd)
+ Log("\t%s", strings.Replace(cmd, "\n", `\n`, -1))
}
}
diff --git a/parser.go b/parser.go
index 5571397..b9083b8 100644
--- a/parser.go
+++ b/parser.go
@@ -73,6 +73,31 @@ func (p *parser) readLine() []byte {
line = bytes.TrimRight(line, "\n")
+ return line
+}
+
+func removeComment(line []byte) []byte {
+ var parenStack []byte
+ for i, ch := range line {
+ switch ch {
+ case '(', '{':
+ parenStack = append(parenStack, ch)
+ case ')', '}':
+ if len(parenStack) > 0 {
+ parenStack = parenStack[:len(parenStack)-1]
+ }
+ case '#':
+ if len(parenStack) == 0 {
+ return line[:i]
+ }
+ }
+ }
+ return line
+}
+
+func (p *parser) readMakefileLine() []byte {
+ line := p.readLine()
+
// TODO: Handle \\ at the end of the line?
for len(line) > 0 && line[len(line)-1] == '\\' {
line = line[:len(line)-1]
@@ -81,12 +106,20 @@ func (p *parser) readLine() []byte {
p.lineno = lineno
line = append(line, nline...)
}
+ return removeComment(line)
+}
- index := bytes.IndexByte(line, '#')
- if index >= 0 {
- line = line[:index]
- }
+func (p *parser) readRecipeLine() []byte {
+ line := p.readLine()
+ // TODO: Handle \\ at the end of the line?
+ for len(line) > 0 && line[len(line)-1] == '\\' {
+ line = append(line, '\n')
+ lineno := p.lineno
+ nline := p.readLine()
+ p.lineno = lineno
+ line = append(line, nline...)
+ }
return line
}
@@ -129,7 +162,7 @@ func (p *parser) parseMaybeRule(line string) AST {
ast.lineno = p.lineno
ast.cmdLineno = p.elineno + 1
for {
- line := p.readLine()
+ line := p.readRecipeLine()
if len(line) == 0 {
break
} else if line[0] == '\t' {
@@ -373,7 +406,7 @@ func (p *parser) parse() (mk Makefile, err error) {
}
}()
for !p.done {
- line := p.readLine()
+ line := p.readMakefileLine()
if len(p.inDef) > 0 {
if strings.TrimLeft(string(line), " ") == "endef" {
diff --git a/testcase/comment_in_command.mk b/testcase/comment_in_command.mk
index 7ca6eba..e139198 100644
--- a/testcase/comment_in_command.mk
+++ b/testcase/comment_in_command.mk
@@ -1,5 +1,3 @@
-# TODO: Maybe unecessary
-
test1:
# foo
echo PASS
@@ -12,3 +10,6 @@ test3: $(shell echo foo #)
test4:
echo $(shell echo OK #)
+
+foo:
+ echo OK