Skip to content

Commit

Permalink
preprocesor (#11)
Browse files Browse the repository at this point in the history
* Implemented preprocessor

* Add tests for preprocessor and fix bugs uncovered by testing

* Add test for put_cursor_marker

* Update stdlib tests to include preprocessor and changed cursor marker logic a bit

* Add postprocessing to main
  • Loading branch information
Ahhhhmed committed Apr 14, 2018
1 parent 37f01e7 commit 4a22288
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 6 deletions.
6 changes: 5 additions & 1 deletion homotopy/__main__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import argparse
import homotopy.parser
from homotopy import preprocessor

from homotopy.compiler import Compiler

Expand All @@ -10,7 +11,10 @@ def main():

snippet = parser.parse_args().snippet[0]

print(Compiler().compile(homotopy.parser.parser.parse(snippet)), end='')
print(Compiler().compile(homotopy.parser.parser.parse(
preprocessor.Preprocessor.put_cursor_marker(
preprocessor.Preprocessor.expand_decorators(snippet)))),
end='')


if __name__ == "__main__":
Expand Down
18 changes: 18 additions & 0 deletions homotopy/preprocessor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from homotopy.snippet_provider import snippetProvider

import re


class Preprocessor:
cursor_marker = "[{cursor_marker}]"

@staticmethod
def expand_decorators(snippet_text):
return re.sub(
r'\[\[(.*?)\]\]',
lambda match_group: snippetProvider[match_group.group(1)],
snippet_text)

@staticmethod
def put_cursor_marker(snippet_text):
return snippet_text + "&" + Preprocessor.cursor_marker
28 changes: 28 additions & 0 deletions test/testPreprocessor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from unittest import TestCase
from unittest.mock import patch

from homotopy.preprocessor import Preprocessor


class TestPreprocessor(TestCase):
@patch('homotopy.snippet_provider.SnippetProvider.__getitem__')
def test_expand_decorators(self, mock_provider):
data = {
"def": "expansion",
"def1": "expansion1",
"def2": "expansion2"
}

mock_provider.side_effect = lambda x: x if x not in data else data[x]

self.assertEqual("noExpansion", Preprocessor.expand_decorators("noExpansion"))

self.assertEqual("test_expansion", Preprocessor.expand_decorators("test_[[def]]"))

self.assertEqual("multiple_expansion1_expansions_expansion2",
Preprocessor.expand_decorators("multiple_[[def1]]_expansions_[[def2]]"))

def test_put_cursor_marker(self):
cursor_marker = "[{cursor_marker}]"

self.assertEqual("snippet&" + cursor_marker, Preprocessor.put_cursor_marker("snippet"))
46 changes: 41 additions & 5 deletions test/testStdlib.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
from unittest import TestCase

from homotopy import parser, compiler
from homotopy import parser, compiler, preprocessor


class TestStdlib(TestCase):
def singleSnippet(self, snippet, expected_output):
compiled_snippet = compiler.Compiler().compile(parser.parser.parse(snippet))
compiled_snippet = compiler.Compiler().compile(
parser.parser.parse(
preprocessor.Preprocessor.put_cursor_marker(
preprocessor.Preprocessor.expand_decorators(snippet)))
)

self.assertEqual(expected_output.strip(), compiled_snippet)

Expand All @@ -14,28 +18,46 @@ def testCpp(self):
for(int i; i<n; i++){
}
""")
[{cursor_marker}]
""")

self.singleSnippet("for#int$i%n>", """
for(int i; i<n; i++){
\t[{cursor_marker}]
}
""")

self.singleSnippet('for#int$i%n>printf("hello");', """
for(int i; i<n; i++){
\tprintf("hello");
\t[{cursor_marker}]
}
""")

self.singleSnippet('if#i==3>return 5;', """
if(i==3){
\treturn 5;
\t[{cursor_marker}]
}
""")
self.singleSnippet('forr#int$i%n>printf("hello");', """
for(int i=n; i>=0; i--){
\tprintf("hello");
\t[{cursor_marker}]
}
""")
self.singleSnippet('forr#int$i%n>printf("hello");<', """
for(int i=n; i>=0; i--){
\tprintf("hello");
}
[{cursor_marker}]
""")

self.singleSnippet('forin$item%collection>printf("hello");', """
for(auto&& item: collection){
\tprintf("hello");
\t[{cursor_marker}]
}
""")
self.singleSnippet('switch$i>case$1>asd<case$2>dsa', """
Expand All @@ -45,24 +67,38 @@ def testCpp(self):
\tbreak;
\tcase 2:
\tdsa
\t[{cursor_marker}]
\tbreak;
}
""")
self.singleSnippet('struct#A:B', """
self.singleSnippet('switch$i>case$1>asd<case$2>dsa<<', """
switch(i){
\tcase 1:
\tasd
\tbreak;
\tcase 2:
\tdsa
\tbreak;
}
[{cursor_marker}]
""")
self.singleSnippet('struct#A:B>', """
struct A: public B {
\t[{cursor_marker}]
};
""")
self.singleSnippet('class#A%B>blah', """
class A: protected B {
\tblah
\t[{cursor_marker}]
};
""")
self.singleSnippet('class#A~B>func#void$foo#int$a#int$b', """
class A: private B {
\tvoid foo(int a, int b){
}
\t[{cursor_marker}]
};
""")

0 comments on commit 4a22288

Please sign in to comment.