Skip to content

Instantly share code, notes, and snippets.

@virresh
Last active June 22, 2018 16:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save virresh/643cd1a2d9149668980814d970f98fbc to your computer and use it in GitHub Desktop.
Save virresh/643cd1a2d9149668980814d970f98fbc to your computer and use it in GitHub Desktop.
antlr Roundtrip with a source file
from coalib.bears.Bear import Bear
from coalib.bears.BEAR_KIND import BEAR_KIND
from coalib.settings.FunctionMetadata import FunctionMetadata
class LocalBear(Bear):
"""
A LocalBear is a Bear that analyzes only one file at once. It therefore can
not analyze semantical facts over multiple files.
This has the advantage that it can be highly parallelized. In addition,
the results from multiple bears for one file can be shown together for that
file, which is better to grasp for the user. coala takes care of all that.
Examples for LocalBear's could be:
- A SpaceConsistencyBear that checks every line for trailing whitespaces,
tabs, etc.
- A VariableNameBear that checks variable names and constant names for
certain conditions
"""
@staticmethod
def kind():
return BEAR_KIND.LOCAL
def run(self,
filename,
file,
*args,
dependency_results=None,
**kwargs):
"""
Handles the given file.
:param filename: The filename of the file
:param file: The file contents as string array
:return: A list of Result
"""
raise NotImplementedError('This function has to be implemented for a '
'runnable bear.')
@classmethod
def get_metadata(cls):
return FunctionMetadata.from_function(
cls.run,
omit={'self', 'filename', 'file', 'dependency_results'})
fromcoalib.bears.BearimportBear
fromcoalib.bears.BEAR_KINDimportBEAR_KIND
fromcoalib.settings.FunctionMetadataimportFunctionMetadata
classLocalBear(Bear):
"""
A LocalBear is a Bear that analyzes only one file at once. It therefore can
not analyze semantical facts over multiple files.
This has the advantage that it can be highly parallelized. In addition,
the results from multiple bears for one file can be shown together for that
file, which is better to grasp for the user. coala takes care of all that.
Examples for LocalBear's could be:
- A SpaceConsistencyBear that checks every line for trailing whitespaces,
tabs, etc.
- A VariableNameBear that checks variable names and constant names for
certain conditions
"""
@staticmethod
defkind():
returnBEAR_KIND.LOCAL
defrun(self,filename,file,*args,dependency_results=None,**kwargs):
"""
Handles the given file.
:param filename: The filename of the file
:param file: The file contents as string array
:return: A list of Result
"""
raiseNotImplementedError('This function has to be implemented for a ''runnable bear.')
@classmethod
defget_metadata(cls):
returnFunctionMetadata.from_function(cls.run,omit={'self','filename','file','dependency_results'})
#!/usr/bin/env python
from antlr4 import *
from Python3Lexer import Python3Lexer
from Python3Parser import Python3Parser
from Python3Visitor import Python3Visitor
import sys
class Visitor(Python3Visitor):
def visitChildren(self, node):
# print(node.getText(), type(node))
if isinstance(node, Python3Parser.StmtContext):
print(self.stream.getText(node.getSourceInterval()), end='')
return self.defaultResult()
result = self.defaultResult()
n = node.getChildCount()
for i in range(n):
if not self.shouldVisitNextChild(node, result):
return result
c = node.getChild(i)
childResult = c.accept(self)
result = self.aggregateResult(result, childResult)
return result
def main(args):
file = FileStream(args[1])
lexer = Python3Lexer(file)
stream = CommonTokenStream(lexer)
parser = Python3Parser(stream)
tre = parser.file_input()
if parser.getNumberOfSyntaxErrors()!=0:
print("File contains {} "
"syntax errors".format(parser.getNumberOfSyntaxErrors()))
return
# print("\n\nDefault Tree: ")
# print(tree.Trees.Trees.toStringTree(tre,None, parser))
# print("Using Visitor: ")
visitor = Visitor()
visitor.stream = stream
visitor.visit(tre)
if __name__ == '__main__':
main(sys.argv)
python roundTripper.py coalib/bears/LocalBear.py > roundTripped_LocalBear.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment