Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
F
firmware
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
External wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
wink
firmware
Commits
b38163f2
Verified
Commit
b38163f2
authored
5 years ago
by
rahix
Browse files
Options
Downloads
Patches
Plain Diff
feat(genapi): Bailout on errors instead of silently ignoring them
Signed-off-by:
Rahix
<
rahix@rahix.de
>
parent
976c63a8
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
epicardium/api/genapi.py
+42
-15
42 additions, 15 deletions
epicardium/api/genapi.py
with
42 additions
and
15 deletions
epicardium/api/genapi.py
+
42
−
15
View file @
b38163f2
...
...
@@ -2,13 +2,22 @@ import argparse
import
os
import
re
import
subprocess
import
sys
MATCH_DECLARATION
=
re
.
compile
(
r
"
__GENERATE_API \$ __GEN_ID_(?P<id>\w+) \$ (?P<type>\w+(?:\*+|\s+))(?P<name>.+?)\((?P<args>.*?)\) \$
"
,
MATCH_EXPANSION
=
re
.
compile
(
r
"
__GENERATE_API \$ __GEN_ID_(?P<id>\w+) \$ (?P<decl>.*?) \$
"
,
re
.
DOTALL
|
re
.
MULTILINE
,
)
MATCH_ARGS
=
re
.
compile
(
r
"
(?P<type>\w+(?:\*+|\s+))(?P<name>\w+),
"
)
MATCH_DECLARATION
=
re
.
compile
(
r
"
^(?P<typename>.*?)\s*\((?P<args>.*)\)$
"
,
re
.
DOTALL
,
)
MATCH_TYPENAME
=
re
.
compile
(
r
"
^(?P<type>(?:const )?(?:struct )?\w+(?:\s+|\*+))(?P<name>\w+)$
"
,
)
def
sizeof
(
args
):
...
...
@@ -16,32 +25,50 @@ def sizeof(args):
return
"
+
"
.
join
(
a
[
"
sizeof
"
]
for
a
in
args
)
if
args
!=
[]
else
"
0
"
def
bailout
(
message
,
*
args
,
**
kwargs
):
fmt
=
"
\x1B
[31;1mError
\x1B
[0m: {}
"
print
(
fmt
.
format
(
message
.
format
(
*
args
,
**
kwargs
)),
file
=
sys
.
stderr
)
sys
.
exit
(
1
)
def
parse_declarations
(
source
):
"""
Parse all declarations in the given source.
"""
declarations
=
[]
for
decl
in
MATCH_DECLARATION
.
finditer
(
source
):
id
=
decl
.
group
(
"
id
"
)
return_type
=
decl
.
group
(
"
type
"
).
strip
()
name
=
decl
.
group
(
"
name
"
)
for
exp
in
MATCH_EXPANSION
.
finditer
(
source
):
id
=
exp
.
group
(
"
id
"
)
decl
=
MATCH_DECLARATION
.
match
(
exp
.
group
(
"
decl
"
))
if
decl
is
None
:
bailout
(
"
Error in declaration
'
{}
'"
,
exp
.
group
(
"
decl
"
))
typename
=
MATCH_TYPENAME
.
match
(
decl
.
group
(
"
typename
"
))
args
=
[]
args_str
=
decl
.
group
(
"
args
"
)
if
typename
is
None
:
bailout
(
"
Error in declaration
'
{}
'"
,
exp
.
group
(
"
decl
"
))
# Parse arguments
for
arg
in
MATCH_ARGS
.
finditer
(
args_str
+
"
,
"
):
arg_type
=
arg
.
group
(
"
type
"
).
strip
()
arg_name
=
arg
.
group
(
"
name
"
)
for
arg_str
in
map
(
str
.
strip
,
args_str
.
split
(
"
,
"
)):
if
arg_str
in
[
"
void
"
,
""
]:
continue
arg
=
MATCH_TYPENAME
.
match
(
arg_str
.
strip
())
if
arg
is
None
:
bailout
(
"
Failed to parse argument
'
{}
'"
,
arg_str
.
strip
())
args
.
append
({
"
type
"
:
arg
_type
,
"
name
"
:
arg
_
name
,
"
sizeof
"
:
"
sizeof({})
"
.
format
(
arg
_type
),
"
type
"
:
arg
.
group
(
"
type
"
).
strip
()
,
"
name
"
:
arg
.
group
(
"
name
"
)
,
"
sizeof
"
:
"
sizeof({})
"
.
format
(
arg
.
group
(
"
type
"
).
strip
()
),
"
offset
"
:
sizeof
(
args
),
})
declarations
.
append
({
"
id
"
:
id
,
"
return_type
"
:
return_type
,
"
name
"
:
name
,
"
return_type
"
:
typename
.
group
(
"
type
"
).
strip
()
,
"
name
"
:
typename
.
group
(
"
name
"
)
,
"
args
"
:
args
,
"
args_str
"
:
args_str
,
})
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment