Repository navigation
Expand file tree
/
Copy path__init__.py
More file actions
48 lines (38 loc) · 1.71 KB
/
Copy path__init__.py
File metadata and controls
48 lines (38 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env python
# java2python.compiler package marker.
#
# This module provides a simpler facade over the rest of the compiler
# subpackage. Client code should use the values in this module
# instead of using directly referencing items within the subpackage.
from java2python.compiler.block import Module
from java2python.lang import Lexer, Parser, StringStream, TokenStream, TreeAdaptor
class JavaSyntaxError(Exception):
""" Raised when the Java source cannot be parsed. """
def buildAST(source):
""" Returns an AST for the given source.
The lexer and parser report their own errors to stderr and then
recover, which leaves a tree that later stages cannot walk, so the
error count is checked here instead of failing further downstream.
"""
lexer = Lexer(StringStream(source))
parser = Parser(TokenStream(lexer))
adapter = TreeAdaptor(lexer, parser)
parser.setTreeAdaptor(adapter)
scope = parser.javaSource()
errors = lexer.getNumberOfSyntaxErrors() + parser.getNumberOfSyntaxErrors()
if errors:
raise JavaSyntaxError(f'{errors} syntax error(s) in Java source')
return scope.tree
def buildJavaDocAST(source):
""" Returns an AST for the given javadoc source. """
from java2python.lang.JavaDocLexer import JavaDocLexer
from java2python.lang.JavaDocParser import JavaDocParser
lexer = JavaDocLexer(StringStream(source))
parser = JavaDocParser(TokenStream(lexer))
scope = parser.commentBody()
return scope.tree
def transformAST(tree, config):
""" Walk the tree and apply the transforms in the config. """
for selector, call in config.last('astTransforms', ()):
for node in selector.walk(tree):
call(node, config)