Skip to content

Commit

Permalink
Refine heredoc state func.
Browse files Browse the repository at this point in the history
  • Loading branch information
harukasan committed Sep 30, 2016
1 parent 794cfc8 commit 5638d49
Showing 1 changed file with 25 additions and 18 deletions.
43 changes: 25 additions & 18 deletions scanner/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,28 @@ func stateHeredocFirstLine(term []byte, indent bool) stateScanFunc {
}
}

func isHeredocEndTerm(s *Scanner, term []byte, indent bool) (bool, int) {
if s.src[s.offset-1] != '\n' {
return false, 0
}
tOff := s.offset
if indent {
for token.IsWhiteSpace(s.char) {
s.next()
}
}
if bytes.HasPrefix(s.src[s.offset:], term) {
s.skip(len(term))
if s.char == '\n' || s.err == io.EOF {
if s.char == '\n' {
s.next()
}
return true, tOff
}
}
return false, 0
}

func stateInHeredoc(term []byte, indent bool) stateScanFunc {
return func(s *Scanner) (int, token.Token, []byte) {
if s.char == '#' {
Expand All @@ -278,25 +300,10 @@ func stateInHeredoc(term []byte, indent bool) stateScanFunc {

s.begin = s.offset
for s.err == nil {
if s.src[s.offset-1] == '\n' {
tOff := s.offset
if indent {
for token.IsWhiteSpace(s.char) {
s.next()
}
}
if bytes.HasPrefix(s.src[s.offset:], term) {
s.skip(len(term))
if s.char == '\n' || s.err == io.EOF {
if s.char == '\n' {
s.next()
}
s.popCtx()
return s.begin, token.HeredocEnd, s.src[s.begin:tOff]
}
}
if isEnd, off := isHeredocEndTerm(s, term, indent); isEnd {
s.popCtx()
return s.begin, token.HeredocEnd, s.src[s.begin:off]
}

next, skip := decodeEscapes(s, '\n')
if next == '@' || next == '$' || next == '{' {
return s.begin, token.HeredocPart, s.src[s.begin : s.offset-skip]
Expand Down

0 comments on commit 5638d49

Please sign in to comment.