aboutsummaryrefslogtreecommitdiff
path: root/py/lexer.c
diff options
context:
space:
mode:
authorTom Collins <tom.collins@digi.com>2017-05-09 13:19:46 -0700
committerDamien George <damien.p.george@gmail.com>2017-05-12 15:14:24 +1000
commit6f56412ec3b93bb93b109f0a01512ecaeebf4f79 (patch)
tree95beb09e0c85b8e98f46fa9c2da8bdd37cf66db2 /py/lexer.c
parent5feeba889799834af0d504dcb5993be5905a9d63 (diff)
py/lexer: Process CR earlier to allow newlines checks on chr1.
Resolves an issue where lexer failed to accept CR after line continuation character. It also simplifies the code.
Diffstat (limited to 'py/lexer.c')
-rw-r--r--py/lexer.c19
1 files changed, 7 insertions, 12 deletions
diff --git a/py/lexer.c b/py/lexer.c
index 4cbf31d90..abc1f3ebb 100644
--- a/py/lexer.c
+++ b/py/lexer.c
@@ -137,23 +137,18 @@ STATIC void next_char(mp_lexer_t *lex) {
lex->chr1 = lex->chr2;
lex->chr2 = lex->reader.readbyte(lex->reader.data);
- if (lex->chr0 == '\r') {
+ if (lex->chr1 == '\r') {
// CR is a new line, converted to LF
- lex->chr0 = '\n';
- if (lex->chr1 == '\n') {
- // CR LF is a single new line
- lex->chr1 = lex->chr2;
+ lex->chr1 = '\n';
+ if (lex->chr2 == '\n') {
+ // CR LF is a single new line, throw out the extra LF
lex->chr2 = lex->reader.readbyte(lex->reader.data);
}
}
- if (lex->chr2 == MP_LEXER_EOF) {
- // EOF, check if we need to insert a newline at end of file
- if (lex->chr1 != MP_LEXER_EOF && lex->chr1 != '\n') {
- // if lex->chr1 == '\r' then this makes a CR LF which will be converted to LF above
- // otherwise it just inserts a LF
- lex->chr2 = '\n';
- }
+ // check if we need to insert a newline at end of file
+ if (lex->chr2 == MP_LEXER_EOF && lex->chr1 != MP_LEXER_EOF && lex->chr1 != '\n') {
+ lex->chr2 = '\n';
}
}