GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — pflags/internal_parsing.odin at main · prnvbn/pflags

Expand file tree

/

internal_parsing.odin

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
238 lines (200 loc) · 5.31 KB
/

internal_parsing.odin

Copy path

File metadata and controls

238 lines (200 loc) · 5.31 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#+private
package pflags
import "core:container/bit_array"
@(require) import "core:fmt"
import "core:strconv"
import "core:strings"
// Used to group state together.
Parser :: struct {
// `fields_set` tracks which arguments have been set.
// It uses their struct field index.
fields_set: bit_array.Bit_Array,
// `filled_pos` tracks which arguments have been filled into positional
// spots, much like how `fmt` treats them.
filled_pos: bit_array.Bit_Array,
}
parse_one_odin_arg :: proc(model: ^$T, parser: ^Parser, arg: string) -> (error: Error) {
arg := arg
if strings.has_prefix(arg, "-") {
arg = arg[1:]
flag: string
assignment_rune: rune
find_assignment: for r, i in arg {
switch r {
case ':', '=':
assignment_rune = r
flag = arg[:i]
arg = arg[1 + i:]
break find_assignment
case:
continue find_assignment
}
}
if assignment_rune == 0 {
if len(arg) == 0 {
return Parse_Error{.No_Flag, "No flag was given."}
}
// -flag
set_odin_flag(model, parser, arg) or_return
} else if assignment_rune == ':' {
// -flag:option <OR> -map:key=value
error = set_option(model, parser, flag, arg)
if error != nil {
// -flag:option did not work, so this may be a -map:key=value set.
find_equals: for r, i in arg {
if r == '=' {
key := arg[:i]
arg = arg[1 + i:]
error = set_key_value(model, parser, flag, key, arg)
break find_equals
}
}
}
} else {
// -flag=option, alternative syntax
set_option(model, parser, flag, arg) or_return
}
} else {
// positional
error = push_positional(model, parser, arg)
}
return
}
parse_one_unix_arg :: proc(
model: ^$T,
parser: ^Parser,
arg: string,
) -> (
future_args: int,
current_flag: string,
error: Error,
) {
arg := arg
if strings.has_prefix(arg, "-") {
// -short or --flag
arg = arg[1:]
is_short := true
if strings.has_prefix(arg, "-") {
// --flag
arg = arg[1:]
is_short = false
if len(arg) == 0 {
// `--`, and only `--`.
// Everything from now on will be treated as an argument.
future_args = max(int)
current_flag = INTERNAL_OVERFLOW_FLAG
return
}
}
if is_short {
return parse_unix_short_arg(model, parser, arg)
}
flag: string
find_assignment: for r, i in arg {
if r == '=' {
// --flag=option
flag = arg[:i]
arg = arg[1 + i:]
flag = resolve_unix_flag_name(model, flag, is_short) or_return
error = set_option(model, parser, flag, arg)
return
}
}
// --flag option, potentially
current_flag = resolve_unix_flag_name(model, arg, is_short) or_return
future_args, _ = set_unix_flag(model, parser, current_flag, false) or_return
} else {
// positional
error = push_positional(model, parser, arg)
}
return
}
// Parse one or more declared single-dash short options. Boolean options may be
// bundled. An option that takes a value consumes the rest of the argument, or
// the next argument when there is no attached value.
parse_unix_short_arg :: proc(
model: ^$T,
parser: ^Parser,
arg: string,
) -> (
future_args: int,
current_flag: string,
error: Error,
) {
if len(arg) == 0 {
current_flag = resolve_unix_flag_name(model, arg, true) or_return
return
}
for i in 0 ..< len(arg) {
#no_bounds_check short := arg[i:i + 1]
current_flag = resolve_unix_flag_name(model, short, true) or_return
if i + 1 == len(arg) {
future_args, _ = set_unix_flag(model, parser, current_flag, false) or_return
return
}
if arg[i + 1] == '=' {
#no_bounds_check option := arg[i + 2:]
error = set_option(model, parser, current_flag, option)
return
}
_, is_boolean := set_unix_flag(model, parser, current_flag, true) or_return
if is_boolean {
continue
}
#no_bounds_check option := arg[i + 1:]
error = set_option(model, parser, current_flag, option)
return
}
return
}
// Resolve a declared single-dash short name to the field's canonical long name.
// Double-dash names are already canonical and pass through unchanged.
resolve_unix_flag_name :: proc(model: ^$T, name: string, is_short: bool) -> (resolved_name: string, error: Error) {
if !is_short || name == RESERVED_HELP_FLAG_SHORT {
return name, nil
}
if field, _, ok := get_field_by_short(model, name); ok {
return get_field_name(field), nil
}
return "", Parse_Error{.Missing_Flag, fmt.tprintf("Unable to find any short flag named `%s`.", name)}
}
// Parse a number of requirements specifier.
//
// Examples:
//
// `min`
// `<max`
// `min<max`
parse_requirements :: proc(str: string) -> (minimum, maximum: int, ok: bool) {
if len(str) == 0 {
return 1, max(int), true
}
if less_than := strings.index_byte(str, '<'); less_than != -1 {
if len(str) == 1 {
return 0, 0, false
}
#no_bounds_check left := str[:less_than]
#no_bounds_check right := str[1 + less_than:]
if left_value, parse_ok := strconv.parse_u64_of_base(left, 10); parse_ok {
minimum = cast(int)left_value
} else if len(left) > 0 {
return 0, 0, false
}
if right_value, parse_ok := strconv.parse_u64_of_base(right, 10); parse_ok {
maximum = cast(int)right_value
} else if len(right) > 0 {
return 0, 0, false
} else {
maximum = max(int)
}
} else {
if value, parse_ok := strconv.parse_u64_of_base(str, 10); parse_ok {
minimum = cast(int)value
maximum = max(int)
} else {
return 0, 0, false
}
}
ok = true
return
}
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.