-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Removed provider instance from provider file * Added use of context manager for open files * Add facade Homotopy class * Add docstrings to Homotopy class * Add docs for Homotopy class * Rework __main__.py * Add console app info in docs * Change mock snippet provider to avoid using assert_called_once
- Loading branch information
Showing
12 changed files
with
362 additions
and
55 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
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
from homotopy.homotopy import Homotopy |
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,21 +1,41 @@ | ||
import argparse | ||
import homotopy.parser | ||
from homotopy import preprocessor | ||
|
||
from homotopy.compiler import Compiler | ||
from homotopy.homotopy import Homotopy | ||
|
||
|
||
def main(): | ||
parser = argparse.ArgumentParser(description="Compile a snippet.") | ||
parser.add_argument('snippet', nargs=1, type=str, help='a snippet to be compiled') | ||
parser.add_argument('language', type=str, | ||
help='Language for the snippet to be compiled to') | ||
parser.add_argument('snippet', type=str, | ||
help='A snippet to be compiled') | ||
parser.add_argument('-t', '--tabsize', type=int, metavar="N", | ||
help='Number of spaces in one tab. Tabs remain tabs if absent') | ||
parser.add_argument('-c', '--cursor', action='store_true', | ||
help='Indicate cursor marker in compiled snippet') | ||
parser.add_argument('-p', '--path', type=str, metavar="PATH", | ||
help='Path to snippet library folders separated by :') | ||
|
||
snippet = parser.parse_args().snippet[0] | ||
args = parser.parse_args() | ||
|
||
print(Compiler().compile(homotopy.parser.parser.parse( | ||
preprocessor.Preprocessor.put_cursor_marker( | ||
preprocessor.Preprocessor.expand_decorators(snippet)))), | ||
end='') | ||
homotopy = Homotopy(args.language) | ||
|
||
if args.tabsize: | ||
homotopy.set_indent(" "*args.tabsize) | ||
|
||
if __name__ == "__main__": | ||
main() | ||
if args.cursor: | ||
homotopy.enable_cursor_marker() | ||
|
||
if args.path: | ||
for item in args.path.split(':'): | ||
homotopy.add_lib_folder(item) | ||
|
||
print(homotopy.compile(args.snippet), end='') | ||
|
||
|
||
def init(): | ||
if __name__ == "__main__": | ||
main() | ||
|
||
|
||
init() |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import os | ||
|
||
from homotopy import preprocessor, parser, compiler, snippet_provider | ||
|
||
|
||
class Homotopy: | ||
""" | ||
Facade class for accessing homotopy tool. | ||
""" | ||
stdlib_path = os.path.join(os.path.split(__file__)[0], 'stdlib') | ||
|
||
def __init__(self, language): | ||
""" | ||
Initialize instance. | ||
:param language: Language | ||
""" | ||
self.user_path = [] | ||
self.indent = '\t' | ||
self.put_cursor_marker = False | ||
self.language = language | ||
|
||
def add_lib_folder(self, path): | ||
""" | ||
Add path to a snippet library | ||
:param path: Path | ||
""" | ||
self.user_path.append(path) | ||
|
||
def clear_user_lib(self): | ||
""" | ||
Clear user lib paths. | ||
""" | ||
self.user_path.clear() | ||
|
||
def set_indent(self, indent): | ||
""" | ||
Set indent sequence used for indentation instead of tab. | ||
:param indent: Indent sequence | ||
""" | ||
self.indent = indent | ||
|
||
def enable_cursor_marker(self): | ||
""" | ||
Enable cursor marker. | ||
""" | ||
self.put_cursor_marker = True | ||
|
||
def disable_cursor_marker(self): | ||
""" | ||
Disable cursor marker. | ||
""" | ||
self.put_cursor_marker = False | ||
|
||
def set_language(self, language): | ||
""" | ||
Set language. | ||
:param language: Language | ||
""" | ||
self.language = language | ||
|
||
def compile(self, snippet_text): | ||
""" | ||
Compile a snippet. | ||
:param snippet_text: Snippet text | ||
:return: Compiled snippet text | ||
""" | ||
snippet_provider_instance = snippet_provider.SnippetProvider( | ||
self.language, self.user_path + [Homotopy.stdlib_path] | ||
) | ||
|
||
preprocessor_instance = preprocessor.Preprocessor(snippet_provider_instance) | ||
compiler_instance = compiler.Compiler(snippet_provider_instance) | ||
parser_instance = parser.Parser() | ||
|
||
preprocessed_text = preprocessor_instance.expand_decorators(snippet_text) | ||
if self.put_cursor_marker: | ||
preprocessed_text = preprocessor_instance.put_cursor_marker(preprocessed_text) | ||
|
||
syntax_tree = parser_instance.parse(preprocessed_text) | ||
|
||
return compiler_instance.compile(syntax_tree) | ||
|
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
Oops, something went wrong.