-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
110 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,18 @@ | ||
import re | ||
import ast | ||
|
||
from . import python_to_ts, pseudocode | ||
|
||
RE_COMMENT = re.compile(r"(\s*)# ?(.*)") | ||
RE_NOLINE = re.compile(r"^(\s*)$", flags=re.MULTILINE) | ||
|
||
|
||
def indent_at(string, pos): | ||
if pos <= 0: | ||
return "" | ||
|
||
newline_before = string.rfind('\n', 0, pos - 1) | ||
if newline_before == -1: | ||
return "" | ||
|
||
line_before = string[newline_before + 1:pos] | ||
|
||
additional = 0 | ||
if line_before.endswith(":\n"): | ||
additional = 4 | ||
|
||
return " " * (len(line_before) - len(line_before.lstrip()) + additional) | ||
|
||
|
||
def gather_indent(m): | ||
spaces = m.group(1) | ||
if spaces: | ||
return spaces | ||
|
||
return indent_at(m.string, m.start()) | ||
|
||
|
||
def process_code(source): | ||
return RE_NOLINE.sub( | ||
lambda m: gather_indent(m) + "''", | ||
RE_COMMENT.sub(r"\1'''\2'''", source.strip()) | ||
) | ||
|
||
|
||
def code_to_ast(source): | ||
return ast.parse(process_code(source), type_comments=True) | ||
return ast.parse(source, type_comments=True) | ||
|
||
|
||
def code_to_samples(source): | ||
tree = code_to_ast(source) | ||
comments = python_to_ts.CommentData(source) | ||
return { | ||
"ast": tree, | ||
"pseudo": pseudocode.PseudoCode().convert(tree), | ||
"pseudo": pseudocode.PseudoCode().convert(tree, comments), | ||
"py": source.strip("\n"), | ||
"ts": python_to_ts.Py2Ts().convert(tree), | ||
"ts": python_to_ts.Py2Ts().convert(tree, comments), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters