Skip to content

Commit d63349c

Browse files
author
whitequark
committedJun 6, 2015
Also return comments from pythonparse.parse_buffer.
1 parent a143c4f commit d63349c

File tree

4 files changed

+28
-9
lines changed

4 files changed

+28
-9
lines changed
 

‎pythonparser/__init__.py

+12-6
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@
44
def parse_buffer(buffer, mode="exec", flags=[], version=None, engine=None):
55
"""
66
Like :meth:`parse`, but accepts a :class:`source.Buffer` instead of
7-
source and filename."""
7+
source and filename, and returns comments as well.
8+
9+
:see: :meth:`parse`
10+
:return: (:class:`ast.AST`, list of :class:`source.Comment`)
11+
Abstract syntax tree and comments
12+
"""
813

914
if version is None:
1015
version = sys.version_info[0:2]
@@ -20,11 +25,11 @@ def parse_buffer(buffer, mode="exec", flags=[], version=None, engine=None):
2025
parser.add_flags(flags)
2126

2227
if mode == "exec":
23-
return parser.file_input()
28+
return parser.file_input(), lexer.comments
2429
elif mode == "single":
25-
return parser.single_input()
30+
return parser.single_input(), lexer.comments
2631
elif mode == "eval":
27-
return parser.eval_input()
32+
return parser.eval_input(), lexer.comments
2833

2934
def parse(source, filename="<unknown>", mode="exec",
3035
flags=[], version=None, engine=None):
@@ -49,6 +54,7 @@ def parse(source, filename="<unknown>", mode="exec",
4954
:raise: :class:`diagnostic.Error`
5055
if the source code is not well-formed
5156
"""
52-
return parse_buffer(pythonparser.source.Buffer(source, filename),
53-
mode, flags, version, engine)
57+
ast, comments = parse_buffer(pythonparser.source.Buffer(source, filename),
58+
mode, flags, version, engine)
59+
return ast
5460

‎pythonparser/lexer.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ def _refill(self, eof_token):
310310
return
311311

312312
if match.group(4) is not None: # comment
313-
self.comments.append((tok_range, match.group(4)))
313+
self.comments.append(source.Comment(tok_range, match.group(4)))
314314
return self._refill(eof_token)
315315

316316
# Lexing non-whitespace now.

‎pythonparser/source.py

+11
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,17 @@ def __ne__(self, other):
170170
"""
171171
return not (self == other)
172172

173+
class Comment:
174+
"""
175+
A comment in the source code.
176+
177+
:ivar loc: (:class:`Range`) source location
178+
:ivar text: (string) comment text
179+
"""
180+
181+
def __init__(self, loc, text):
182+
self.loc, self.text = loc, text
183+
173184
class RewriterConflict(Exception):
174185
"""
175186
An exception that is raised when two ranges supplied to a rewriter overlap.

‎pythonparser/test/test_lexer.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,10 @@ def test_newline(self):
6363

6464
def test_comment(self):
6565
self.assertLexes("# foo")
66-
self.assertEqual([(source.Range(self.buffer, 0, 5), "# foo")],
67-
self.lexer.comments)
66+
self.assertEqual(source.Range(self.buffer, 0, 5),
67+
self.lexer.comments[0].loc)
68+
self.assertEqual("# foo",
69+
self.lexer.comments[0].text)
6870

6971
self.assertLexes("class x:\n # foo\n pass",
7072
"class", None,

0 commit comments

Comments
 (0)
Please sign in to comment.