def test_pygments_tokens(self):
     pph = PPHighlighter(parser_factory_pygments, uses_pygments_tokens=True)
     fragments = pph.highlight('(1)')
     expected = [('class:pygments.text', '('),
                 ('class:pygments.literal.number.integer', '1'),
                 ('class:pygments.text', ')')]
     self.assertEqual(fragments, expected)
Ejemplo n.º 2
0
def main():
    """The main function."""
    expr = parser_factory(dummy_styler)
    pph = PPHighlighter(parser_factory)
    lexer = Python3Lexer()
    formatter = Terminal256Formatter()

    # Generate the test data
    random.seed(0)
    data = [[random.randrange(1000) for _ in range(100)] for _ in range(100)]
    s = str(data)

    # Perform the benchmarks
    t1 = time.perf_counter()
    for _ in range(N_RUNS):
        result = expr.parseString(s, parseAll=True)[0]
    t2 = time.perf_counter()
    for _ in range(N_RUNS):
        fragments = pph.highlight(s)
    t3 = time.perf_counter()
    for _ in range(N_RUNS):
        highlight(s, lexer, formatter)
    t4 = time.perf_counter()

    # Verify the results
    assert data == result
    assert s == ''.join(text for _, text in fragments)

    # Display the results
    print('Input string size: {} chars'.format(len(s)))
    print('Parsing completed in {:.3f}ms'.format((t2 - t1) / N_RUNS * 1000))
    print('Highlighting completed in {:.3f}ms'.format(
        (t3 - t2) / N_RUNS * 1000))
    print('Pygments highlighting completed in {:.3f}ms'.format(
        (t4 - t3) / N_RUNS * 1000))
 def test_document_lexer_multiline(self):
     pph = PPHighlighter(parser_factory)
     lines = pph.lex_document(Document('( \n 1)'))
     self.assertEqual(lines(0), [('', '( ')])
     self.assertEqual(lines(1), [('', ' '), ('class:int', '1'), ('', ')')])
     with self.assertRaises(IndexError):
         lines(2)
 def test_adjacent_styled_fragments(self):
     pph = PPHighlighter(parser_factory_abc)
     s = 'aabca'
     pph.expr.parseString(s, parseAll=True)
     fragments = pph.highlight(s)
     expected = [('#f00', 'a'), ('#f00', 'a'), ('#00f', 'b'), ('', 'c'),
                 ('#f00', 'a')]
     self.assertEqual(fragments, expected)
 def test_complex(self):
     pph = PPHighlighter(parser_factory)
     s = '(1 (2 3.00 () 4) 5)'
     pph.expr.parseString(s, parseAll=True)
     fragments = pph.highlight(s)
     expected = [('', '('), ('class:int', '1'), ('', ' ('),
                 ('class:int', '2'), ('', ' '), ('class:float', '3.00'),
                 ('', ' () '), ('class:int', '4'), ('', ') '),
                 ('class:int', '5'), ('', ')')]
     self.assertEqual(fragments, expected)
Ejemplo n.º 6
0
def repl(parser_factory, *, prompt='> ', multiline=False, style=None,
         validate_while_typing=True, validate=True, prompt_continuation=': ',
         uses_pygments_tokens=False, prints_result=True,
         prints_exceptions=True):
    """A read-eval-print loop for pyparsing-highlighting examples."""

    def prompt_continuation_fn(*args, **kwargs):
        return prompt_continuation

    parser = parser_factory(dummy_styler)
    pph = PPHighlighter(parser_factory,
                        uses_pygments_tokens=uses_pygments_tokens)
    ppv = PPValidator(parser, multiline=multiline) if validate else None
    history = InMemoryHistory()

    session = PromptSession(prompt, multiline=multiline, lexer=pph,
                            validate_while_typing=validate_while_typing,
                            validator=ppv, style=style, history=history,
                            prompt_continuation=prompt_continuation_fn)

    while True:
        try:
            with patch_stdout():
                s = session.prompt()
            result = parser.parseString(s, parseAll=True)
            if prints_result:
                for item in result:
                    print(repr(item))
        except ParseBaseException as err:
            if prints_exceptions:
                print('{}: {}'.format(type(err).__name__, err), file=sys.stderr)
        except KeyboardInterrupt:
            pass
        except EOFError:
            break
 def test_nonstring_fail(self):
     pph = PPHighlighter(parser_factory)
     with self.assertRaises(TypeError):
         pph.highlight(0)
 def test_warning(self):
     pph = PPHighlighter(parser_factory_exception)
     with self.assertWarns(RuntimeWarning):
         pph.highlight('(1)')
 def test_backout(self):
     pph = PPHighlighter(parser_factory_backout)
     fragments = pph.highlight('(1)')
     expected = [('', '(1)')]
     self.assertEqual(fragments, expected)
 def test_restart(self):
     pph = PPHighlighter(parser_factory)
     fragments = pph.highlight('(1 (a 2))')
     expected = [('', '('), ('class:int', '1'), ('', ' (a '),
                 ('class:int', '2'), ('', '))')]
     self.assertEqual(fragments, expected)
 def test_document_lexer(self):
     pph = PPHighlighter(parser_factory)
     lines = pph.lex_document(Document('(1)'))
     self.assertEqual(lines(0), [('', '('), ('class:int', '1'), ('', ')')])
 def test_newlines(self):
     pph = PPHighlighter(parser_factory)
     fragments = pph.highlight('( \n 1)')
     self.assertEqual(fragments, [('', '( \n '), ('class:int', '1'),
                                  ('', ')')])
 def test_pygments_class(self):
     pph = PPHighlighter(parser_factory_pygments, uses_pygments_tokens=True)
     fragments = pph.highlight('(1)')
     self.assertTrue(isinstance(fragments, FormattedText))
 def test_html_dotted_classes(self):
     pph = PPHighlighter(parser_factory_dotted_classes)
     html = pph.highlight_html('(1)')
     expected = '<pre class="highlight">(<span class="number-int">1</span>)</pre>'
     self.assertEqual(html, expected)
 def test_html_escape(self):
     pph = PPHighlighter(parser_factory_htmlescape)
     html = pph.highlight_html('<1>')
     expected = '<pre class="highlight">&lt;<span class="int">1</span>&gt;</pre>'
     self.assertEqual(html, expected)
 def test_html_wrapping_class(self):
     pph = PPHighlighter(parser_factory)
     html = pph.highlight_html('(1)', css_class='thing')
     expected = '<pre class="thing">(<span class="int">1</span>)</pre>'
     self.assertEqual(html, expected)
 def test_overlap(self):
     pph = PPHighlighter(parser_factory_overlap)
     fragments = pph.highlight('(1 2) ')
     self.assertEqual(fragments, [('bold', '(1 2)'), ('', ' ')])
 def test_single_int(self):
     pph = PPHighlighter(parser_factory)
     fragments = pph.highlight('(1)')
     self.assertEqual(fragments, [('', '('), ('class:int', '1'), ('', ')')])
 def test_set_parse_action(self):
     pph = PPHighlighter(parser_factory_set_parse_action)
     fragments = pph.highlight('(1)')
     self.assertEqual(fragments, [('', '('), ('class:int', '1'), ('', ')')])
     result = pph.expr.parseString('(1)', parseAll=True)
     self.assertEqual(result[0], -1)
 def test_preserve_original(self):
     pph = PPHighlighter(parser_factory)
     fragments = pph.highlight('(1.000)')
     self.assertEqual(fragments, [('', '('), ('class:float', '1.000'),
                                  ('', ')')])
 def test_html_pygments_subtype(self):
     pph = PPHighlighter(parser_factory_pygments_subtype,
                         uses_pygments_tokens=True)
     html = pph.highlight_html('(1)')
     expected = '<pre class="highlight">(<span class="mi">1</span>)</pre>'
     self.assertEqual(html, expected)
 def test_class(self):
     pph = PPHighlighter(parser_factory)
     fragments = pph.highlight('(1)')
     self.assertTrue(isinstance(fragments, FormattedText))