Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
TASTE
uPython-mirror
Commits
60aca481
Commit
60aca481
authored
Jan 24, 2014
by
Damien George
Browse files
Merge pull request #215 from pfalcon/qstr-special-chars
Allow qstr's with non-ident chars, construct good identifier for them.
parents
2b2cb7b7
ab5d0828
Changes
3
Hide whitespace changes
Inline
Side-by-side
py/makeqstrdata.py
View file @
60aca481
import
argparse
import
re
from
htmlentitydefs
import
codepoint2name
# this must match the equivalent function in qstr.c
def
compute_hash
(
qstr
):
...
...
@@ -10,7 +11,7 @@ def compute_hash(qstr):
def
do_work
(
infiles
):
# read the qstrs in from the input files
qstrs
=
[]
qstrs
=
{}
for
infile
in
infiles
:
with
open
(
infile
,
'rt'
)
as
f
:
line_number
=
0
...
...
@@ -23,28 +24,29 @@ def do_work(infiles):
continue
# verify line is of the correct form
match
=
re
.
match
(
r
'Q\((
[0-9A-Za-z_]
+)\)$'
,
line
)
match
=
re
.
match
(
r
'Q\((
.
+)\)$'
,
line
)
if
not
match
:
print
(
'({}:{}) bad qstr format, got {}'
.
format
(
infile
,
line_number
,
line
))
return
False
# get the qstr value
qstr
=
match
.
group
(
1
)
ident
=
re
.
sub
(
r
'[^A-Za-z0-9_]'
,
lambda
s
:
"_"
+
codepoint2name
[
ord
(
s
.
group
(
0
))]
+
"_"
,
qstr
)
# don't add duplicates
if
qstr
in
qstrs
:
if
ident
in
qstrs
:
continue
# add the qstr to the list
qstrs
.
append
(
qstr
)
qstrs
[
ident
]
=
qstr
# process the qstrs, printing out the generated C header file
print
(
'// This file was automatically generated by makeqstrdata.py'
)
print
(
''
)
for
qstr
in
qstrs
:
for
ident
,
qstr
in
qstrs
.
items
()
:
qhash
=
compute_hash
(
qstr
)
qlen
=
len
(
qstr
)
print
(
'Q({}, (const byte*)"
\\
x{:02x}
\\
x{:02x}
\\
x{:02x}
\\
x{:02x}" "{}")'
.
format
(
qstr
,
qhash
&
0xff
,
(
qhash
>>
8
)
&
0xff
,
qlen
&
0xff
,
(
qlen
>>
8
)
&
0xff
,
qstr
))
print
(
'Q({}, (const byte*)"
\\
x{:02x}
\\
x{:02x}
\\
x{:02x}
\\
x{:02x}" "{}")'
.
format
(
ident
,
qhash
&
0xff
,
(
qhash
>>
8
)
&
0xff
,
qlen
&
0xff
,
(
qlen
>>
8
)
&
0xff
,
qstr
))
return
True
...
...
py/qstrdefs.h
View file @
60aca481
...
...
@@ -80,3 +80,11 @@ Q(sort)
Q
(
join
)
Q
(
strip
)
Q
(
format
)
Q
(
<
module
>
)
Q
(
<
lambda
>
)
Q
(
<
listcomp
>
)
Q
(
<
dictcomp
>
)
Q
(
<
setcomp
>
)
Q
(
<
genexpr
>
)
Q
(
<
stdin
>
)
py/scope.c
View file @
60aca481
...
...
@@ -18,7 +18,7 @@ scope_t *scope_new(scope_kind_t kind, mp_parse_node_t pn, qstr source_file, uint
scope
->
source_file
=
source_file
;
switch
(
kind
)
{
case
SCOPE_MODULE
:
scope
->
simple_name
=
QSTR_
FROM_STR_STATIC
(
"<
module
>"
)
;
scope
->
simple_name
=
MP_
QSTR_
_lt_
module
_gt_
;
break
;
case
SCOPE_FUNCTION
:
case
SCOPE_CLASS
:
...
...
@@ -26,19 +26,19 @@ scope_t *scope_new(scope_kind_t kind, mp_parse_node_t pn, qstr source_file, uint
scope
->
simple_name
=
MP_PARSE_NODE_LEAF_ARG
(((
mp_parse_node_struct_t
*
)
pn
)
->
nodes
[
0
]);
break
;
case
SCOPE_LAMBDA
:
scope
->
simple_name
=
QSTR_
FROM_STR_STATIC
(
"<
lambda
>"
)
;
scope
->
simple_name
=
MP_
QSTR_
_lt_
lambda
_gt_
;
break
;
case
SCOPE_LIST_COMP
:
scope
->
simple_name
=
QSTR_
FROM_STR_STATIC
(
"<
listcomp
>"
)
;
scope
->
simple_name
=
MP_
QSTR_
_lt_
listcomp
_gt_
;
break
;
case
SCOPE_DICT_COMP
:
scope
->
simple_name
=
QSTR_
FROM_STR_STATIC
(
"<
dictcomp
>"
)
;
scope
->
simple_name
=
MP_
QSTR_
_lt_
dictcomp
_gt_
;
break
;
case
SCOPE_SET_COMP
:
scope
->
simple_name
=
QSTR_
FROM_STR_STATIC
(
"<
setcomp
>"
)
;
scope
->
simple_name
=
MP_
QSTR_
_lt_
setcomp
_gt_
;
break
;
case
SCOPE_GEN_EXPR
:
scope
->
simple_name
=
QSTR_
FROM_STR_STATIC
(
"<
genexpr
>"
)
;
scope
->
simple_name
=
MP_
QSTR_
_lt_
genexpr
_gt_
;
break
;
default:
assert
(
0
);
...
...
Write
Preview
Supports
Markdown
0%
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!
Cancel
Please
register
or
sign in
to comment