Skip to content

Commit

Permalink
parser_bug_fix (#10)
Browse files Browse the repository at this point in the history
* Add tests that fail

* Added default snippet to be retried when stack is empty

* Added docs for implicit snippet

* Added tests for implicit snippet
  • Loading branch information
Ahhhhmed committed Apr 13, 2018
1 parent a535529 commit 37f01e7
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 2 deletions.
13 changes: 13 additions & 0 deletions docs/contributing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,19 @@ This should be translated to:
Character :code:`>` is used to donate the inside of a snippet in snippet definitions.
This is why all occurrences of :code:`<` and :code:`&` are translated to :code:`>`.

Implicit snippet
""""""""""""""""

Consider following example:

.. code-block:: text
for>if<while
The :code:`while` is not a part of :code:`for` snippet.
Parser creates an implicit :code:`block` snippet at the beginning.
:code:`block` snippet is implemented as a list of sub-snippets in new lines.


Syntax tree
^^^^^^^^^^^
Expand Down
2 changes: 1 addition & 1 deletion homotopy/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def merge_stack(stack):
"""
last_tree = stack.pop()
if last_tree != SimpleSnippet(''):
next_tree = stack.pop()
next_tree = stack.pop() if stack else SimpleSnippet("block")
stack.append(CompositeSnippet(next_tree, Parser.in_operator, last_tree))


Expand Down
4 changes: 3 additions & 1 deletion homotopy/stdlib/cpp.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,7 @@
{"name": "prot_extend","language": "C++","snippet": ": protected %%%"},
{"name": "func","language": "C++","snippet": "### $$$({{params}}){\n{{inside_block}}\n}"},
{"name": "params","language": "C++","snippet": "### $$${{opt_params}}"},
{"name": "opt_params","language": "C++","snippet": ", ### $$${{opt_params}}"}
{"name": "opt_params","language": "C++","snippet": ", ### $$${{opt_params}}"},
{"name": "block","language": "C++","snippet": ">>>{{opt_block}}"},
{"name": "opt_block","language": "C++","snippet": "\n>>>{{opt_block}}"}
]
25 changes: 25 additions & 0 deletions test/testParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,28 @@ def test_into(self):
SimpleSnippet('if'))), '>',
SimpleSnippet('if'))
)

self.assertEqual(parser.parse('for&for'),
CompositeSnippet(
CompositeSnippet(
SimpleSnippet('block'),
'>',
SimpleSnippet('for')),
'>',
SimpleSnippet('for')
))

self.assertEqual(parser.parse('for>if<if'),
CompositeSnippet(
CompositeSnippet(
SimpleSnippet('block'),
'>',
CompositeSnippet(
SimpleSnippet('for'),
'>',
SimpleSnippet('if')
)
),
'>',
SimpleSnippet('if')
))

0 comments on commit 37f01e7

Please sign in to comment.