-
Notifications
You must be signed in to change notification settings - Fork 0
/
_cpp.py
127 lines (101 loc) · 3.81 KB
/
_cpp.py
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
from dragonfly import (Choice, Function, MappingRule, Grammar, CompoundRule, Dictation, Text)
from vim.rules.letter import (snake_case)
from macro_utilities import (replace_in_text, execute_with_dictation)
class BarneyEnabler(CompoundRule):
spec = "enable Barney"
def _process_recognition(self, node, extras):
barney_bootstrap.disable()
barney_grammar.enable()
print("Barney grammar enabled!")
class BarneyDisabler(CompoundRule):
spec = "disable Barney"
def _process_recognition(self, node, extras):
barney_grammar.disable()
barney_bootstrap.enable()
print("Barney grammar disabled")
type_name_choice_map = {
"short": "short",
"integer": "int",
"long": "long",
"unsigned short": "unsigned short",
"unsigned integer": "unsigned int",
"unsigned long": "unsigned long",
"character": "char",
"boolean": "bool",
"void": "void",
"bite": "byte",
"string": "std::string",
"string view": "std::string_view",
"unique_pointer": "std::unique_ptr",
"shared_pointer": "std::shared_ptr",
"standard vector": "std::vector",
"you eight": "uint8_t",
"you sixteen": "uint16_t",
"you thirty two": "uint32_t",
"you sixty four": "uint64_t",
"unsigned eight": "uint8_t",
"unsigned sixteen": "uint16_t",
"unsigned thirty two": "uint32_t",
"unsigned sixty four": "uint64_t",
"i eight": "int8_t",
"i sixteen": "int16_t",
"i thirty two": "int32_t",
"i sixty four": "int64_t",
"signed eight": "int8_t",
"signed sixteen": "int16_t",
"signed thirty two": "int32_t",
"signed sixty four": "int64_t",
"size": "size_t",
}
def type_name_choice(name="type_name"):
return Choice(name, type_name_choice_map)
def output_value(value_name, type_name=None, is_constant=False):
constant_prefix = "const " if is_constant else ""
if type_name is not None:
execute_with_dictation(
value_name,
lambda n: replace_in_text(
"%s%s %s = $;" % (constant_prefix, type_name, format_value_name(n))
),
lambda n: replace_in_text("%s%s $ = _;" % (constant_prefix, type_name))
)
def output_type_annotation(value_name, type_name=None, is_constant=False):
constant_prefix = "const " if is_constant else ""
if type_name is not None:
execute_with_dictation(
value_name,
lambda n: Text(
"%s%s %s" % (constant_prefix, type_name, format_value_name(n))
),
lambda n: replace_in_text("%s%s $" % (constant_prefix, type_name))
)
def format_value_name(name):
return snake_case(str(name))
class BarneyUtilities(MappingRule):
mapping = {
"constant [<value_name>]": Function(output_value, type_name="auto", is_constant=True),
"constant <type_name> [<value_name>]": Function(output_value, is_constant=True),
"variable [<value_name>]": Function(output_value, type_name="auto"),
"variable <type_name> [<value_name>]": Function(output_value),
"<type_name> [<value_name>]": Function(output_type_annotation),
"<type_name> [<value_name>] is constant": Function(output_type_annotation,
is_constant=True),
}
extras = [
Dictation("value_name", default=""),
type_name_choice("type_name")
]
# The main Barney grammar rules are activated here
barney_bootstrap = Grammar("barney bootstrap")
barney_bootstrap.add_rule(BarneyEnabler())
barney_bootstrap.load()
barney_grammar = Grammar("barney grammar")
barney_grammar.add_rule(BarneyUtilities())
barney_grammar.add_rule(BarneyDisabler())
barney_grammar.load()
barney_grammar.disable()
def unload():
global barney_grammar
if barney_grammar:
barney_grammar.unload()
barney_grammar = None