Skip to content

Commit

Permalink
Add docstrings to compiler and parser
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahhhhmed committed Apr 12, 2018
1 parent 2f66947 commit a535529
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
21 changes: 21 additions & 0 deletions homotopy/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,16 @@


class Compiler(SnippetVisitor):
"""
Compiler for snippets. Turns syntax tree into text.
"""
def visit_composite_snippet(self, composite_snippet):
"""
Replace right site in the appropriate place in the left side.
:param composite_snippet: Composite snippet
:return: Text of left side replaced with right side
"""
left_side = snippetProvider[self.visit(composite_snippet.left)]
right_side = snippetProvider[self.compile(composite_snippet.right)]

Expand Down Expand Up @@ -39,9 +48,21 @@ def expansion_function(match_object):
return left_side

def visit_simple_snippet(self, simple_snippet):
"""
Compile simple snippet.
:param simple_snippet: Simple snippet
:return: Text of compile snippet
"""
return snippetProvider[simple_snippet.value]

def compile(self, snippet):
"""
Compile a snippet.
:param snippet: Snippet
:return: Text of compiled snippet
"""
compiled_snippet = self.visit(snippet)

return re.sub(r'({{.*?}})', "", compiled_snippet)
Expand Down
15 changes: 15 additions & 0 deletions homotopy/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,22 @@


class Parser:
"""
Class for parsing a string to produce a syntax tree.
"""
parameter_chars = "!@#$%:~"
in_operator = '>'
out_operator = '<'
and_operator = '&'

@staticmethod
def parse(snippet_text):
"""
Parsing a string to produce syntax tree.
:param snippet_text: Text to parse
:return: syntax tree instance corresponding to given text
"""
stack = []
current_match = []
last_operator = Parser.in_operator
Expand Down Expand Up @@ -44,6 +53,12 @@ def parse(snippet_text):

@staticmethod
def merge_stack(stack):
"""
Helper method to combine two snippets on top of the stack.
:param stack: Stack
:return: None
"""
last_tree = stack.pop()
if last_tree != SimpleSnippet(''):
next_tree = stack.pop()
Expand Down

0 comments on commit a535529

Please sign in to comment.