Skip to content

Commit

Permalink
preprocessor_docs (#12)
Browse files Browse the repository at this point in the history
* Added docstrings to Preprocessor class

* Add preprocessor documentation
  • Loading branch information
Ahhhhmed committed Apr 14, 2018
1 parent 4a22288 commit 4b07198
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
42 changes: 42 additions & 0 deletions docs/contributing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,55 @@ Internals

The code is separated in several components:

* `Preprocessor`_
* `Parser`_
* `Syntax tree`_
* `Snippet provider`_
* `Compiler`_
* `Utilities`_
* `Application frontend`_

Preprocessor
^^^^^^^^^^^^

Before parsing and creating syntax tree some prepossessing is done to enable some feathers.

Decorators
""""""""""

Consider flowing example:

.. code-block:: text
1. for#int$i%n
2. for[[sup]]
And flowing snippet definition:

.. code-block:: json
{"name": "sup","language": "C++","snippet": "#int$i%n"}
This pattern occurs often and it is useful to name it and use it by name ('sub' stands for 'standard up').
More realistic examples are design pattern implementation (singleton for example).
Another advantage of this approach is that multiple decorators can be combined to make a more complex construction.

Preprocessor detects decorators that are defined in double square brackets and expands them using snippet provider.

Cursor marker
"""""""""""""

After expanding a snippet user should have the cursor at a convenient location.
The following rule is followed.


*Writing some text after expanding the snippet should have the same effect as
writing that text and expanding the snippet afterwords.*


To enable this preprocessor appends :code:`&[{cursor_marker}]` to the snippet text
so a plugin can put cursor at the marker location.

Parser
^^^^^^

Expand Down
15 changes: 15 additions & 0 deletions homotopy/preprocessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,30 @@


class Preprocessor:
"""
Prepossess snippet text to enable extra feathers.
"""
cursor_marker = "[{cursor_marker}]"

@staticmethod
def expand_decorators(snippet_text):
"""
Expand decorators to enable concise writing of common patterns.
:param snippet_text: Snippet text
:return: Expanded snippet text
"""
return re.sub(
r'\[\[(.*?)\]\]',
lambda match_group: snippetProvider[match_group.group(1)],
snippet_text)

@staticmethod
def put_cursor_marker(snippet_text):
"""
Put cursor marker witch should be used by editor plugins for better experience.
:param snippet_text: Snippet text
:return: Snippet text with marker at the end
"""
return snippet_text + "&" + Preprocessor.cursor_marker

0 comments on commit 4b07198

Please sign in to comment.