summaryrefslogtreecommitdiffstats
path: root/examples/pdb.py
blob: 6302bca4c50af0e9c86e62566a71a9ec471fcfdd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#!/usr/bin/env python
"""
Work in progress version of a PDB prompt.

This is an example of "prompt_toolkit.contrib.shell". It's still very
incomplete, non-functional and experimental. But it shows the direction we're
heading.
"""
from pygments.token import Token, Keyword, Operator, Number, Name, Error, Comment
from pygments.style import Style

from prompt_toolkit import CommandLineInterface, AbortAction
from prompt_toolkit.contrib.shell.completion import ShellCompleter
from prompt_toolkit.contrib.shell.rules import Any, Sequence, Literal, Variable, Repeat
from prompt_toolkit.contrib.shell.layout import CompletionHint
from prompt_toolkit.contrib.shell.parse_info import get_parse_info, InvalidCommandException
from prompt_toolkit import Exit
from prompt_toolkit.line import Line
from prompt_toolkit.layout.menus import CompletionMenu

from prompt_toolkit.layout import Layout
from prompt_toolkit.layout.prompt import DefaultPrompt


grammar = Any([
    Sequence([
        Any([
            Literal('b', dest='cmd_break'),
            Literal('break', dest='cmd_break'),
            Literal('tbreak', dest='tbreak'),
        ]),
        Variable(placeholder='<file:lineno/function>', dest='break_line'),
        Variable(placeholder='<condition>', dest='break_condition'),
    ]),
    Sequence([
        Literal('condition', dest='cmd_condition'),
        Variable(placeholder='<bpnumber>', dest='condition_bpnumber'),
        Variable(placeholder='<str_condition>', dest='condition_str')
    ]),
    Sequence([
        Any([
            Literal('l', dest='cmd_list'),
            Literal('list', dest='cmd_list'),
        ]),
        Variable(placeholder='<first>', dest='list_first'),
        Variable(placeholder='<last>', dest='list_last')
    ]),
    Sequence([
        Literal('debug'),
        Variable(placeholder='<code>'),
    ]),
    Sequence([
        Literal('disable'),
        Variable(placeholder='<bpnumber>'),
    ]),
    Sequence([
        Literal('enable'),
        Repeat(Variable(placeholder='<bpnumber>')),
    ]),
    Sequence([
        Literal('ignore'),
        Variable(placeholder='<bpnumber>'),
        Variable(placeholder='<count>'),
    ]),
    Sequence([
        Literal('run'),
        Repeat(Variable(placeholder='<args>')),
    ]),
    Sequence([
        Literal('alias'),
        Variable(placeholder='<name>'),
        Variable(placeholder='<command>'),
        Repeat(Variable(placeholder='<parameter>')),
    ]),
    Sequence([
        Any([
            Literal('h'),
            Literal('help'),
        ]),
        Variable(placeholder='<command>'),
    ]),
    Sequence([
        Literal('unalias'),
        Variable(placeholder='<name>'),
    ]),
    Sequence([
        Literal('jump'),
        Variable(placeholder='<lineno>'),
    ]),
    Sequence([
        Literal('whatis'),
        Variable(placeholder='<arg>'),
    ]),
    Sequence([
        Literal('pp'),
        Variable(placeholder='<expression>'),
    ]),
    Sequence([
        Literal('p'),
        Variable(placeholder='<expression>'),
    ]),
    Sequence([
        Any([
            Literal('cl'),
            Literal('clear'),
        ]),
        Repeat(Variable(placeholder='<bpnumber>')),
    ]),
    Any([
        Literal('a'),
        Literal('args'),
    ]),
    Any([
        Literal('cont'),
        Literal('continue'),
    ]),
    Any([
        Literal('d'),
        Literal('down'),
    ]),
    Any([
        Literal('exit'),
        Literal('q'),
        Literal('quit'),
    ]),
    Any([
        Literal('n'),
        Literal('next'),
    ]),
    Any([
        Literal('run'),
        Literal('restart'),
    ]),
    Any([
        Literal('unt'),
        Literal('until'),
    ]),
    Any([
        Literal('u'),
        Literal('up'),
    ]),
    Any([
        Literal('r'),
        Literal('return'),
    ]),
    Any([
        Literal('w', dest='where'),
        Literal('where', dest='where'),
        Literal('bt'),
    ]),
    Any([
        Literal('s'),
        Literal('step'),
    ]),
    Literal('commands'),
])


class PdbStyle(Style):
    background_color = None
    styles = {
        # Pdb commands highlighting.
        Token.Placeholder:           "#aa8888",
        Token.Placeholder.Variable:  "#aa8888",
        Token.Placeholder.Bracket:   "bold #ff7777",
        Token.Placeholder.Separator: "#ee7777",
        Token.Aborted:               "#aaaaaa",
        Token.Prompt:                "bold",

        # Python code highlighting.
        Keyword:                      '#ee00ee',
        Operator:                     '#aa6666',
        Number:                       '#ff0000',
        Name:                         '#008800',
        Token.Literal.String:         '#440000',
        Comment:                      '#0000dd',
        Error:                        '#000000 bg:#ff8888',

        # Completion Menu
        Token.Menu.Completer.Completion.Current: 'bg:#00aaaa #000000',
        Token.Menu.Completer.Completion:         'bg:#008888 #ffffff',
        Token.Menu.Completer.ProgressButton:     'bg:#003333',
        Token.Menu.Completer.ProgressBar:        'bg:#00aaaa',
    }


if __name__ == '__main__':
    cli = CommandLineInterface(
        layout=Layout(before_input=DefaultPrompt('(pdb) '),
                      after_input=CompletionHint(grammar),
                      menus=[CompletionMenu()]),
        line=Line(completer=ShellCompleter(grammar)),
        style=PdbStyle)

    try:
        while True:
            document = cli.read_input(on_exit=AbortAction.RAISE_EXCEPTION)

            try:
                parse_info = get_parse_info(grammar, document)
            except InvalidCommandException:
                print('Invalid command\n')
                continue
            else:
                print(parse_info.get_variables())

    except Exit:
        pass