py/lexer: Simplify lexer startup by using dummy bytes and next_char().

Now consistently uses the EOL processing ("\r" and "\r\n" convert to "\n")
and EOF processing (ensure "\n" before EOF) provided by next_char().

In particular the lexer can now correctly handle input that starts with CR.
This commit is contained in:
Tom Collins 2017-05-04 16:31:08 -07:00 committed by Damien George
parent e711e2d44a
commit 2998647c4e

View File

@ -677,7 +677,7 @@ mp_lexer_t *mp_lexer_new(qstr src_name, mp_reader_t reader) {
lex->source_name = src_name; lex->source_name = src_name;
lex->reader = reader; lex->reader = reader;
lex->line = 1; lex->line = 1;
lex->column = 1; lex->column = -2; // account for 3 dummy bytes
lex->emit_dent = 0; lex->emit_dent = 0;
lex->nested_bracket_level = 0; lex->nested_bracket_level = 0;
lex->alloc_indent_level = MICROPY_ALLOC_LEXER_INDENT_INIT; lex->alloc_indent_level = MICROPY_ALLOC_LEXER_INDENT_INIT;
@ -688,27 +688,12 @@ mp_lexer_t *mp_lexer_new(qstr src_name, mp_reader_t reader) {
// store sentinel for first indentation level // store sentinel for first indentation level
lex->indent_level[0] = 0; lex->indent_level[0] = 0;
// preload characters // load lexer with start of file, advancing lex->column to 1
lex->chr0 = reader.readbyte(reader.data); // start with dummy bytes and use next_char() for proper EOL/EOF handling
lex->chr1 = reader.readbyte(reader.data); lex->chr0 = lex->chr1 = lex->chr2 = 0;
lex->chr2 = reader.readbyte(reader.data); next_char(lex);
next_char(lex);
// if input stream is 0, 1 or 2 characters long and doesn't end in a newline, then insert a newline at the end next_char(lex);
if (lex->chr0 == MP_LEXER_EOF) {
lex->chr0 = '\n';
} else if (lex->chr1 == MP_LEXER_EOF) {
if (lex->chr0 == '\r') {
lex->chr0 = '\n';
} else if (lex->chr0 != '\n') {
lex->chr1 = '\n';
}
} else if (lex->chr2 == MP_LEXER_EOF) {
if (lex->chr1 == '\r') {
lex->chr1 = '\n';
} else if (lex->chr1 != '\n') {
lex->chr2 = '\n';
}
}
// preload first token // preload first token
mp_lexer_to_next(lex); mp_lexer_to_next(lex);