forked from natural/java2python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase.py
More file actions
100 lines (83 loc) · 2.75 KB
/
base.py
File metadata and controls
100 lines (83 loc) · 2.75 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# java2python.lang.base -> lexer and parser support classes.
#
# This module provides the following:
#
# * `Tokens`
#
# This class is used to create the single `token` instance in this
# module. It is used to map between parser tokens and their ids and
# vice-versa.
# ANTLR notes:
#
# recognizers: lexer, parser, treeparser
# streams: string, file name, file handle
#
# Parsers use TokenStreams (CommonTokenStream or TokenRewriteStream)
#
# Tree parsers use TreeNodeStream (CommonTreeNodeStream)
#
# Lexers emit Token objects (buffered in TokenStream objects)
#
# Parsers build trees if their output is AST.
#
# token types: CommonToken and ClassicToken. Our tree adaptor
# creates LocalTree instances instead.
#
# Tree (CommonTree) wraps Token objects. We provide extra functionality via
# the LocalTree class.
#
# TreeAdaptor (CommonTreeAdaptor) is used by the parser to create
# Tree objects. Our adaptor, TreeAdaptor, creates the LocalTree
# instances.
#
from io import StringIO
from antlr4 import CommonTokenStream as TokenStream
from java2python.lib import colors
class Tokens(object):
""" Tokens -> simplifies token id-name and name-id mapping. """
def __init__(self):
self.cache, self.parserModule = {}, None
def __getattr__(self, name):
""" tokenname -> tokenvalue """
return getattr(self.module, name)
@property
def commentTypes(self):
""" Well-known comment types. """
mod = self.module
return (mod.COMMENT, mod.LINE_COMMENT, mod.JAVADOC_COMMENT, )
@property
def map(self):
""" (tokentype, tokenname) mapping as a dictionary """
cache, module = self.cache, self.module
if cache:
return cache
mapping = [(getattr(module, k, None), k) for k in module.tokenNames]
mapping = [(k, v) for k, v in mapping if k is not None]
cache.update(mapping)
return cache
@property
def methodTypes(self):
""" Well-known method types. """
mod = self.module
return (mod.VOID_METHOD_DECL, mod.FUNCTION_METHOD_DECL, )
@property
def primitiveTypeNames(self):
""" Type name of well-known primitive types """
return ('bool', 'str', 'int', 'long', 'float', )
@property
def module(self):
""" Provides lazy import to the parser module. """
module = self.parserModule
if module:
return module
import java2python.lang.JavaParser as module
self.parserModule = module
return module
@staticmethod
def title(name):
""" Returns a nice title given a token type name. """
return ''.join(part.title() for part in name.split('_'))
## sometimes you really do only need one.
tokens = Tokens()