Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
M
micropython
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
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
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
card10
micropython
Commits
0b7184dc
Commit
0b7184dc
authored
11 years ago
by
Paul Sokolovsky
Browse files
Options
Downloads
Patches
Plain Diff
Implement octal and hex escapes in strings.
parent
0914371f
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
py/lexer.c
+58
-6
58 additions, 6 deletions
py/lexer.c
py/misc.h
+1
-0
1 addition, 0 deletions
py/misc.h
py/unicode.c
+4
-0
4 additions, 0 deletions
py/unicode.c
tests/basics/string-escape.py
+11
-0
11 additions, 0 deletions
tests/basics/string-escape.py
with
74 additions
and
6 deletions
py/lexer.c
+
58
−
6
View file @
0b7184dc
...
...
@@ -126,6 +126,10 @@ static bool is_following_digit(mp_lexer_t *lex) {
return
unichar_isdigit
(
lex
->
chr1
);
}
static
bool
is_following_odigit
(
mp_lexer_t
*
lex
)
{
return
lex
->
chr1
>=
'0'
&&
lex
->
chr1
<=
'7'
;
}
// TODO UNICODE include unicode characters in definition of identifiers
static
bool
is_head_of_identifier
(
mp_lexer_t
*
lex
)
{
return
is_letter
(
lex
)
||
lex
->
chr0
==
'_'
;
...
...
@@ -275,6 +279,32 @@ static const char *tok_kw[] = {
NULL
,
};
static
int
hex_digit
(
unichar
c
)
{
// c is assumed to be hex digit
int
n
=
c
-
'0'
;
if
(
n
>
9
)
{
n
&=
~
(
'a'
-
'A'
);
n
-=
(
'A'
-
(
'9'
+
1
));
}
return
n
;
}
// This is called with CUR_CHAR() before first hex digit, and should return with
// it pointing to last hex digit
static
bool
get_hex
(
mp_lexer_t
*
lex
,
int
num_digits
,
uint
*
result
)
{
uint
num
=
0
;
while
(
num_digits
--
!=
0
)
{
next_char
(
lex
);
unichar
c
=
CUR_CHAR
(
lex
);
if
(
!
unichar_isxdigit
(
c
))
{
return
false
;
}
num
=
(
num
<<
4
)
+
hex_digit
(
c
);
}
*
result
=
num
;
return
true
;
}
static
void
mp_lexer_next_token_into
(
mp_lexer_t
*
lex
,
mp_token_t
*
tok
,
bool
first_token
)
{
// skip white space and comments
bool
had_physical_newline
=
false
;
...
...
@@ -439,12 +469,34 @@ static void mp_lexer_next_token_into(mp_lexer_t *lex, mp_token_t *tok, bool firs
case
'v'
:
c
=
0x0b
;
break
;
case
'f'
:
c
=
0x0c
;
break
;
case
'r'
:
c
=
0x0d
;
break
;
// TODO \ooo octal
case
'x'
:
// TODO \xhh
case
'N'
:
// TODO \N{name} only in strings
case
'u'
:
// TODO \uxxxx only in strings
case
'U'
:
// TODO \Uxxxxxxxx only in strings
default:
break
;
// TODO error message
case
'x'
:
{
uint
num
;
if
(
!
get_hex
(
lex
,
2
,
&
num
))
{
// TODO error message
assert
(
0
);
}
c
=
num
;
break
;
}
case
'N'
:
break
;
// TODO \N{name} only in strings
case
'u'
:
break
;
// TODO \uxxxx only in strings
case
'U'
:
break
;
// TODO \Uxxxxxxxx only in strings
default:
if
(
c
>=
'0'
&&
c
<=
'7'
)
{
// Octal sequence, 1-3 chars
int
digits
=
3
;
int
num
=
c
-
'0'
;
while
(
is_following_odigit
(
lex
)
&&
--
digits
!=
0
)
{
next_char
(
lex
);
num
=
num
*
8
+
(
CUR_CHAR
(
lex
)
-
'0'
);
}
c
=
num
;
}
else
{
// TODO error message
assert
(
0
);
}
break
;
}
if
(
c
!=
MP_LEXER_CHAR_EOF
)
{
vstr_add_char
(
&
lex
->
vstr
,
c
);
...
...
This diff is collapsed.
Click to expand it.
py/misc.h
+
1
−
0
View file @
0b7184dc
...
...
@@ -43,6 +43,7 @@ bool unichar_isspace(unichar c);
bool
unichar_isalpha
(
unichar
c
);
bool
unichar_isprint
(
unichar
c
);
bool
unichar_isdigit
(
unichar
c
);
bool
unichar_isxdigit
(
unichar
c
);
/** string ******************************************************/
...
...
This diff is collapsed.
Click to expand it.
py/unicode.c
+
4
−
0
View file @
0b7184dc
...
...
@@ -62,6 +62,10 @@ bool unichar_isdigit(unichar c) {
return
c
<
128
&&
(
attr
[
c
]
&
FL_DIGIT
)
!=
0
;
}
bool
unichar_isxdigit
(
unichar
c
)
{
return
unichar_isdigit
(
c
)
||
(
c
>=
'a'
&&
c
<=
'f'
)
||
(
c
>=
'A'
&&
c
<=
'F'
);
}
/*
bool char_is_alpha_or_digit(unichar c) {
return c < 128 && (attr[c] & (FL_ALPHA | FL_DIGIT)) != 0;
...
...
This diff is collapsed.
Click to expand it.
tests/basics/string-escape.py
0 → 100644
+
11
−
0
View file @
0b7184dc
a
=
"
a
\1
b
"
print
(
len
(
a
))
print
(
ord
(
a
[
1
]))
print
(
len
(
"
a
\123
b
"
))
a
=
"
a
\123
45b
"
print
(
len
(
a
))
print
(
ord
(
a
[
1
]))
a
=
"
a
\xff
b
"
print
(
len
(
a
))
print
(
ord
(
a
[
1
]))
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