Esempio n. 1
0
 def canon_repr(value):
     if isinstance(value, _py2to3.text_type):
         return _py2to3.text_repr(value)
     elif isinstance(value, list):
         return '[{}]'.format(', '.join(
             canon_repr(v) for v in value))
     else:
         return repr(value)
Esempio n. 2
0
def unirepr(value, native_dict_keys=False):
    if isinstance(value, _py2to3.bytes_type):
        return _py2to3.bytes_repr(value)
    elif isinstance(value, _py2to3.text_type):
        return _py2to3.text_repr(value)
    elif isinstance(value, dict):
        return '{{{}}}'.format(', '.join(
            '{}: {}'.format(key if native_dict_keys else unirepr(key),
                            unirepr(item))
            for key, item in sorted(value.items())))
    elif isinstance(value, tuple):
        if len(value) == 1:
            return '({},)'.format(unirepr(value[0]))
        return '({})'.format(', '.join(unirepr(item) for item in value))
    else:
        return repr(value)
Esempio n. 3
0
def process(filename, with_trivia):
    ctx = lal.AnalysisContext(with_trivia=with_trivia)
    unit = ctx.get_from_file(filename)
    token = unit.first_token
    prev_token = None

    print('Tokens for {} ({} trivia):'.format(filename,
                                              'with' if with_trivia else 'no'))
    while token:
        pt = token.previous
        assert pt == prev_token

        print('  [{typ} {index}] {kind} {image}'.format(
            typ='trivia' if token.is_trivia else 'token ',
            index=token.index,
            kind=token.kind,
            image=_py2to3.text_repr(token.text),
        ))
        prev_token = token
        token = token.next

    print('')
Esempio n. 4
0
    def process_unit(self, unit):

        def previous_not_trivia(token):
            ret = token
            while ret.is_trivia:
                ret = ret.previous
            return ret

        for tok in unit.iter_tokens():
            if tok.kind == 'Comment' and tok.text.startswith('--%'):
                expr_text = tok.text[3:].strip()
                if expr_text.startswith('$'):
                    expr_text = expr_text[1:].strip()
                current_node = unit.root.lookup(
                    previous_not_trivia(tok).sloc_range.start
                )
                print("Eval '{}' on node {}".format(
                    col(expr_text, YELLOW),
                    col(current_node.entity_repr, YELLOW)
                ))
                try:
                    value = eval(
                        expr_text, None,
                        {'lal': lal, 'node': current_node}
                    )
                except lal.PropertyError as pe:
                    print('Exception: {}'.format(
                        ' '.join(str(a) for a in pe.args)
                    ))
                else:

                    # Hide discrepancies between Python2 and Python3
                    value_repr = (_py2to3.text_repr(value)
                                  if isinstance(value, _py2to3.text_type) else
                                  repr(value))
                    print('Result: {}'.format(col(value_repr, YELLOW)))
                print('')

        print('')
Esempio n. 5
0
import libadalang as lal
from libadalang import _py2to3

ctx = lal.AnalysisContext()
unit = ctx.get_from_file('test.adb')
unit2 = ctx.get_from_file('test2.adb')

print('First and last tokens for test.adb:')
print('  * {}'.format(unit.first_token))
print('  * {}'.format(unit.last_token))
print('')

print('Whole source buffer for test.adb:')
print(
    _py2to3.text_repr(lal.Token.text_range(unit.first_token, unit.last_token)))
print('')

last = unit.first_token.next
first = unit.last_token.previous
print('Empty range for the following bounds:')
print('  * {}'.format(first))
print('  * {}'.format(last))
print(_py2to3.text_repr(lal.Token.text_range(first, last)))
print('')

print('Source excerpts for all Basic_Decl in test.adb:')
for n in unit.root.findall(lal.BasicDecl):
    print('  * {}'.format(n))
    print('    {}'.format(_py2to3.text_repr(n.text)))
    print('')
Esempio n. 6
0
"""
Test that CharLiteral.p_denoted_value properly decodes all valid Ada character
literals.
"""

from __future__ import absolute_import, division, print_function

import libadalang as lal
from libadalang import _py2to3

c = lal.AnalysisContext('utf-8')
u = c.get_from_file('foo.ads')

for decl in u.root.findall(lal.ObjectDecl):
    name = decl.f_ids.text
    expr = decl.f_default_expr
    assert isinstance(expr, lal.CharLiteral)
    try:
        v = expr.p_denoted_value
    except lal.PropertyError:
        v = u'<PropertyError>'
    print('{} ({}) -> {}'.format(name, _py2to3.text_repr(expr.text),
                                 _py2to3.text_repr(v)))
Esempio n. 7
0
from __future__ import absolute_import, division, print_function

import libadalang
from libadalang import _py2to3

ctx = libadalang.AnalysisContext(with_trivia=True)
u = ctx.get_from_file('foo.adb')

prev_token = None
for token in u.iter_tokens():
    assert prev_token == token.previous, 'Inconsistent previous token'
    print('{}{}'.format(
        token.kind,
        ' {}'.format(_py2to3.text_repr(token.text)) if token.text else ''))
    prev_token = token
print('Done.')
Esempio n. 8
0
]:
    pflush('Trying to load {}:'.format(label))
    try:
        ufp = libadalang.UnitProvider.for_project(project_file, **kwargs)
    except libadalang.InvalidProject as exc:
        pflush('   ... got an exception: ' + format_exc(exc))
    else:
        pflush('   ... got no exception')

# Then do something that is supposed to work
ctx = libadalang.AnalysisContext(
    unit_provider=libadalang.UnitProvider.for_project('p.gpr'))

# And try to load units with various invalid names
for filename in (u'\xe9', u' '):
    pflush('Trying to get unit: {}'.format(_py2to3.text_repr(filename)))
    try:
        unit = ctx.get_from_provider(filename,
                                     libadalang.AnalysisUnitKind.unit_body)
    except libadalang.InvalidUnitNameError as exc:
        pflush('   ... got an exception: {}'.format(exc))
    else:
        pflush('   ... got no exception')
        if unit.diagnostics:
            pflush('   ... but we got diagnostics:')
            for d in unit.diagnostics:
                print('{}: {}'.format(d.sloc_range,
                                      _py2to3.text_repr(d.message)))

print('Done.')