]>
git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/python/lib/gdb/styling.py
1 # Styling related hooks.
2 # Copyright (C) 2010-2024 Free Software Foundation, Inc.
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 3 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17 """Utilities for styling."""
22 from pygments
import formatters
, lexers
, highlight
23 from pygments
.token
import Error
, Comment
, Text
24 from pygments
.filters
import TokenMergeFilter
30 if _formatter
is None:
31 _formatter
= formatters
.TerminalFormatter()
34 def colorize(filename
, contents
):
35 # Don't want any errors.
37 lexer
= lexers
.get_lexer_for_filename(filename
, stripnl
=False)
38 formatter
= get_formatter()
39 return highlight(contents
, lexer
, formatter
).encode(
40 gdb
.host_charset(), "backslashreplace"
45 class HandleNasmComments(TokenMergeFilter
):
47 def fix_comments(lexer
, stream
):
49 for ttype
, value
in stream
:
50 if ttype
is Error
and value
== "#":
53 if ttype
is Text
and value
== "\n":
56 ttype
= Comment
.Single
59 def filter(self
, lexer
, stream
):
60 f
= HandleNasmComments
.fix_comments
61 return super().filter(lexer
, f(lexer
, stream
))
65 def __get_asm_lexer(gdbarch
):
68 # For an i386 based architecture, in 'intel' mode, use the nasm
70 flavor
= gdb
.parameter("disassembly-flavor")
71 if flavor
== "intel" and gdbarch
.name()[:4] == "i386":
74 # If GDB is built without i386 support then attempting to fetch
75 # the 'disassembly-flavor' parameter will throw an error, which we
80 if lexer_type
not in _asm_lexers
:
81 _asm_lexers
[lexer_type
] = lexers
.get_lexer_by_name(lexer_type
)
82 _asm_lexers
[lexer_type
].add_filter(HandleNasmComments())
83 _asm_lexers
[lexer_type
].add_filter("raiseonerror")
84 return _asm_lexers
[lexer_type
]
86 def colorize_disasm(content
, gdbarch
):
87 # Don't want any errors.
89 lexer
= __get_asm_lexer(gdbarch
)
90 formatter
= get_formatter()
91 return highlight(content
, lexer
, formatter
).rstrip().encode()
97 def colorize(filename
, contents
):
100 def colorize_disasm(content
, gdbarch
):