| 1 |
# Copyright (C) 2016 the V8 project authors. All rights reserved. |
| 2 |
# This code is governed by the BSD license found in the LICENSE file. |
| 3 |
|
| 4 |
def find_comments(source): |
| 5 |
'''Parse input string describing JavaScript source and yield dictionaries |
| 6 |
describing the JavaScript comments in the order they appear in the source. |
| 7 |
This includes comment patterns within string literals. |
| 8 |
|
| 9 |
Each dictionary defines the following attributes: |
| 10 |
|
| 11 |
- source: the source text of the comment |
| 12 |
- firstchar: the zero-indexed position of the token that begins the comment |
| 13 |
- lastchar: the zero-indexed position of the token that closes the comment |
| 14 |
- lineno: the zero-indexed offset of the line on which the comment appears |
| 15 |
- in_string: `False` if the comment is a true JavaScript comment, one of |
| 16 |
'\'' (single quote), '"' (double quote), or '`' (back tick) if |
| 17 |
the comment pattern appears within a string literal. |
| 18 |
''' |
| 19 |
in_string = False |
| 20 |
in_s_comment = False |
| 21 |
in_m_comment = False |
| 22 |
follows_escape = False |
| 23 |
comment = '' |
| 24 |
lineno = 0 |
| 25 |
|
| 26 |
for idx in xrange(len(source)): |
| 27 |
if source[idx] == '\n': |
| 28 |
lineno += 1 |
| 29 |
|
| 30 |
# Within comments and strings, any odd number of back-slashes begins an |
| 31 |
# escape sequence. |
| 32 |
if source[idx - 1] == '\\': |
| 33 |
follows_escape = not follows_escape |
| 34 |
else: |
| 35 |
follows_escape = False |
| 36 |
|
| 37 |
if in_s_comment: |
| 38 |
if source[idx] == '\n': |
| 39 |
in_s_comment = False |
| 40 |
yield dict( |
| 41 |
source=comment[1:], |
| 42 |
firstchar=idx - len(comment) - 1, |
| 43 |
lastchar=idx, |
| 44 |
in_string=in_string, |
| 45 |
lineno=lineno) |
| 46 |
continue |
| 47 |
elif in_m_comment: |
| 48 |
if source[idx - 1] == '*' and source[idx] == '/': |
| 49 |
in_m_comment = False |
| 50 |
yield dict( |
| 51 |
source=comment[1:-1], |
| 52 |
firstchar=idx - len(comment) - 1, |
| 53 |
lastchar=idx + 1, |
| 54 |
in_string=in_string, |
| 55 |
lineno=lineno) |
| 56 |
continue |
| 57 |
elif in_string: |
| 58 |
if source[idx] == in_string and not follows_escape: |
| 59 |
in_string = False |
| 60 |
elif source[idx] == '\n' and in_string != '`' and not follows_escape: |
| 61 |
in_string = False |
| 62 |
|
| 63 |
if in_m_comment or in_s_comment: |
| 64 |
comment += source[idx] |
| 65 |
continue |
| 66 |
|
| 67 |
in_m_comment = source[idx] == '/' and source[idx + 1] == '*' |
| 68 |
in_s_comment = source[idx] == '/' and source[idx + 1] == '/' |
| 69 |
|
| 70 |
if in_m_comment or in_s_comment: |
| 71 |
comment = '' |
| 72 |
elif source[idx] == '\'' or source[idx] == '"' or source[idx] == '`': |
| 73 |
in_string = source[idx] |