static
| 563 | |
| 564 | //static |
| 565 | void CodeEntry::OnPythonInit() |
| 566 | { |
| 567 | const std::string syntaxHighlightCode = R"(def syntax_highlight_basic(): |
| 568 | #this uses the built in lexer/tokenizer in python to identify part of code |
| 569 | #will return a meaningful lookuptable for index colours per character |
| 570 | import tokenize |
| 571 | import io |
| 572 | import token |
| 573 | import sys |
| 574 | |
| 575 | isPython3 = False |
| 576 | if sys.version_info[0] >= 3: |
| 577 | isPython3 = True |
| 578 | |
| 579 | if isPython3: |
| 580 | text = str(syntax_highlight_code) |
| 581 | else: |
| 582 | text = unicode(syntax_highlight_code) |
| 583 | |
| 584 | tok_name = token.tok_name |
| 585 | #print(tok_name) # <--- dict of token-kinds. |
| 586 | |
| 587 | output = [] |
| 588 | defined = [] |
| 589 | lastCharEnd = 0 |
| 590 | lastRowEnd = 0 |
| 591 | |
| 592 | with io.StringIO(text) as f: |
| 593 | try: |
| 594 | tokens = tokenize.generate_tokens(f.readline) |
| 595 | |
| 596 | for token in tokens: |
| 597 | #print(token) |
| 598 | if token.type in (0, 5, 56, 256): |
| 599 | continue |
| 600 | if not token.string or (token.start == token.end): |
| 601 | continue |
| 602 | |
| 603 | token_type = token.type |
| 604 | |
| 605 | if token.type == 1: |
| 606 | if token.string in {'print', 'def', 'class', 'break', 'continue', 'return', 'while', 'or', 'and', 'dir', 'if', 'elif', 'else', 'is', 'in', 'as', 'out', 'with', 'from', 'import', 'with', 'for'}: |
| 607 | token_type = 90 |
| 608 | elif token.string in {'False', 'True', 'yield', 'repr', 'range', 'enumerate', 'len', 'type', 'list', 'tuple', 'int', 'str', 'float'}: |
| 609 | token_type = 91 |
| 610 | elif token.string in globals() or token.string in defined: |
| 611 | token_type = 92 |
| 612 | #else: |
| 613 | # defined.append(token.string) don't do this one, it makes for confusing highlighting |
| 614 | elif isPython3 and token.type == 54: |
| 615 | # OPS |
| 616 | # 7: 'LPAR', 8: 'RPAR' |
| 617 | # 9: 'LSQB', 10: 'RSQB' |
| 618 | # 25: 'LBRACE', 26: 'RBRACE' |
| 619 | if token.exact_type in {7, 8, 9, 10, 25, 26}: |
| 620 | token_type = token.exact_type |
| 621 | else: |
| 622 | token_type = 51 |
nothing calls this directly
no test coverage detected