def unirepr(value): if isinstance(value, _py2to3.text_type): return _py2to3.text_repr(value) elif isinstance(value, _py2to3.bytes_type): return _py2to3.bytes_repr(value) elif isinstance(value, list): return '[{}]'.format(', '.join(unirepr(item) for item in value)) else: return repr(value)
u2 = parse('bar.txt', b'()') tokens = [] t = u.first_token while t is not None: tokens.append(t) t = t.next # Print all tokens, for output clarity print('Tokens:') for t in tokens: print(' ', t) print('') # Print the whole text buffer print('Input source buffer:\n {}'.format(_py2to3.text_repr(u.text))) print('') # Test Token's comparison operations assert tokens[0] != tokens[1] assert not (tokens[0] == tokens[1]) assert tokens[0] == tokens[0] assert tokens[0] < tokens[1] assert not (tokens[0] < tokens[0]) assert not (tokens[1] < tokens[0]) assert tokens[0] <= tokens[1] assert tokens[0] <= tokens[0] assert not (tokens[1] <= tokens[0])
import sys import libfoolang from libfoolang import _py2to3 print('main.py: Running...') ctx = libfoolang.AnalysisContext() u = ctx.get_from_buffer('foo.txt', b'my_ident') if u.diagnostics: for d in u.diagnostics: print(d) sys.exit(1) for s in (None, 42, 'my_ident', 'MY_IDENT', 'no_such_symbol', 'invalid_symbol0'): try: result = '= {}'.format(_py2to3.text_repr(u.root.p_sym(s))) except TypeError as exc: result = 'raised <TypeError: {}>'.format(exc) except libfoolang.InvalidSymbolError as exc: result = 'raised <InvalidSymbolError: {}>'.format(exc) print('u.root.p_sym({}) {}'.format(repr(s), result)) print('main.py: Done.')
import libfoolang from libfoolang import _py2to3 print('main.py: Running...') ctx = libfoolang.AnalysisContext() u = ctx.get_from_buffer('main.txt', b'example') if u.diagnostics: for d in u.diagnostics: print(d) sys.exit(1) print('root.is_synthetic = {}'.format(u.root.is_synthetic)) n = u.root.p_get print(n) for prop in ['is_synthetic', 'text', 'sloc_range']: try: value = getattr(n, prop) except Exception as exc: result = '<{}: {}>'.format(type(exc).__name__, exc) else: result = (_py2to3.text_repr(value) if isinstance(value, _py2to3.text_type) else repr(value)) print('{} = {}'.format(prop, result)) print('main.py: Done.')
def unirepr(value): return (_py2to3.text_repr(value) if isinstance(value, _py2to3.text_type) else _py2to3.bytes_repr(value))
Testcase(b'example # H\xecllo', 'unknown-charset'), # Check that successfully parsing a unit with one encoding (UTF-8) has no # influence on the default encoding used later. Testcase(b'example # H\xc3\xa9llo', 'utf-8'), Testcase(b'example # H\xc3\xa9llo', None), ] for method in (get_from_buffer, reparse): print('== {} =='.format(method.__name__)) print('') for tc in testcases: try: method(tc.buffer, tc.charset) except Exception as exc: result = '{}: {}'.format(type(exc).__name__, exc) else: if u.diagnostics: result = '\n'.join(['diagnostics:'] + [' {}'.format(d) for d in u.diagnostics]) else: result = _py2to3.text_repr(u.text) print(' buffer={}, charset={}: {}'.format( unirepr(tc.buffer), repr(tc.charset), result, )) print('main.py: Done.')
import sys import libfoolang from libfoolang import _py2to3 print('main.py: Running...') ctx = libfoolang.AnalysisContext() u = ctx.get_from_buffer('main.txt', b'example') if u.diagnostics: for d in u.diagnostics: print(d) sys.exit(1) print('root.is_synthetic = {}'.format(u.root.is_synthetic)) n = u.root.p_get print(n) for prop in ['is_synthetic', 'text', 'sloc_range']: try: value = getattr(n, prop) except Exception as exc: result = '<{}: {}>'.format(type(exc).__name__, exc) else: result = (_py2to3.text_repr(value) if isinstance( value, _py2to3.text_type) else repr(value)) print('{} = {}'.format(prop, result)) print('main.py: Done.')
from __future__ import absolute_import, division, print_function import libfoolang from libfoolang import _py2to3 ctx = libfoolang.AnalysisContext() for filename in ('foo1.txt', 'foo2.txt'): print('== {} =='.format(filename)) u = ctx.get_from_file(filename) for token in u.iter_tokens(): print('>>> {: <12} {: <10} {}'.format( token.kind, _py2to3.text_repr(token.text), token.sloc_range.start )) print('')