summaryrefslogtreecommitdiff
path: root/py/lexer.c
AgeCommit message (Collapse)Author
2017-08-13all: Raise exceptions via mp_raise_XXXJavier Candeira
- Changed: ValueError, TypeError, NotImplementedError - OSError invocations unchanged, because the corresponding utility function takes ints, not strings like the long form invocation. - OverflowError, IndexError and RuntimeError etc. not changed for now until we decide whether to add new utility functions.
2017-07-31all: Use the name MicroPython consistently in commentsAlexander Steffen
There were several different spellings of MicroPython present in comments, when there should be only one.
2017-07-07py,extmod: Some casts and minor refactors to quiet compiler warnings.Tom Collins
2017-05-12py/lexer: Process CR earlier to allow newlines checks on chr1.Tom Collins
Resolves an issue where lexer failed to accept CR after line continuation character. It also simplifies the code.
2017-05-09py/lexer: Simplify lexer startup by using dummy bytes and next_char().Tom Collins
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.
2017-03-29py/lexer: Simplify and reduce code size for operator tokenising.Damien George
By removing the 'E' code from the operator token encoding mini-language the tokenising can be simplified. The 'E' code was only used for the != operator which is now handled as a special case; the optimisations for the general case more than make up for the addition of this single, special case. Furthermore, the . and ... operators can be handled in the same way as != which reduces the code size a little further. This simplification also removes a "goto". Changes in code size for this patch are (measured in bytes): bare-arm: -48 minimal x86: -64 unix x86-64: -112 unix nanbox: -64 stmhal: -48 cc3200: -48 esp8266: -76
2017-03-23py/lexer: Remove obsolete comment, since lexer can now raise exceptions.Damien George
2017-03-14py: Allow lexer to raise exceptions during construction.Damien George
This patch refactors the error handling in the lexer, to simplify it (ie reduce code size). A long time ago, when the lexer/parser/compiler were first written, the lexer and parser were designed so they didn't use exceptions (ie nlr) to report errors but rather returned an error code. Over time that has gradually changed, the parser in particular has more and more ways of raising exceptions. Also, the lexer never really handled all errors without raising, eg there were some memory errors which could raise an exception (and in these rare cases one would get a fatal nlr-not-handled fault). This patch accepts the fact that the lexer can raise exceptions in some cases and allows it to raise exceptions to handle all its errors, which are for the most part just out-of-memory errors during construction of the lexer. This makes the lexer a bit simpler, and also the persistent code stuff is simplified. What this means for users of the lexer is that calls to it must be wrapped in a nlr handler. But all uses of the lexer already have such an nlr handler for the parser (and compiler) so that doesn't put any extra burden on the callers.
2017-02-17py/lexer: Convert mp_uint_t to size_t where appropriate.Damien George
2017-02-17py: Do adjacent str/bytes literal concatenation in lexer, not compiler.Damien George
It's much more efficient in RAM and code size to do implicit literal string concatenation in the lexer, as opposed to the compiler. RAM usage is reduced because the concatenation can be done right away in the tokeniser by just accumulating the string/bytes literals into the lexer's vstr. Prior to this patch adjacent strings/bytes would create a parse tree (one node per string/bytes) and then in the compiler a whole new chunk of memory was allocated to store the concatenated string, which used more than double the memory compared to just accumulating in the lexer. This patch also significantly reduces code size: bare-arm: -204 minimal: -204 unix x64: -328 stmhal: -208 esp8266: -284 cc3200: -224
2017-02-17py/lexer: Simplify handling of line-continuation error.Damien George
Previous to this patch there was an explicit check for errors with line continuation (where backslash was not immediately followed by a newline). But this check is not necessary: if there is an error then the remaining logic of the tokeniser will reject the backslash and correctly produce a syntax error.
2017-02-17py/lexer: Use strcmp to make keyword searching more efficient.Damien George
Since the table of keywords is sorted, we can use strcmp to do the search and stop part way through the search if the comparison is less-than. Because all tokens that are names are subject to this search, this optimisation will improve the overall speed of the lexer when processing a script. The change also decreases code size by a little bit because we now use strcmp instead of the custom str_strn_equal function.
2017-02-17py/lexer: Move check for keyword to name-tokenising block.Damien George
Keywords only needs to be searched for if the token is a MP_TOKEN_NAME, so we can move the seach to the part of the code that does the tokenising for MP_TOKEN_NAME.
2017-02-17py/lexer: Simplify handling of indenting of very first token.Damien George
2017-02-16py/lexer: Don't generate string representation for period or ellipsis.Damien George
It's not needed.
2017-01-30extmod/vfs_fat: Remove MICROPY_READER_FATFS component.Damien George
2017-01-27extmod: Add generic VFS sub-system.Damien George
This provides mp_vfs_XXX functions (eg mount, open, listdir) which are agnostic to the underlying filesystem type, and just require an object with the relevant filesystem-like methods (eg .mount, .open, .listidr) which can then be mounted. These mp_vfs_XXX functions would typically be used by a port to implement the "uos" module, and mp_vfs_open would be the builtin open function. This feature is controlled by MICROPY_VFS, disabled by default.
2016-12-22py/lexer: Permanently disable the mp_lexer_show_token function.Damien George
The lexer is very mature and this debug function is no longer used. If it's really needed one can uncomment it and recompile.
2016-12-22py/lexer: Remove unnecessary check for EOF in lexer's next_char func.Damien George
This check always fails (ie chr0 is never EOF) because the callers of this function never call it past the end of the input stream. And even if they did it would be harmless because 1) reader.readbyte must continue to return an EOF char if the stream is exhausted; 2) next_char would just count the subsequent EOF's as characters worth 1 column.
2016-12-22py/lexer: Remove unreachable code in string tokeniser.Damien George
2016-12-22tests/basics/lexer: Add a test for newline-escaping within a string.Damien George
2016-11-16py/lexer: Make lexer use an mp_reader as its source.Damien George
2016-11-16py/lexer: Rewrite mp_lexer_new_from_fd in terms of mp_reader.Damien George
2016-11-16py/lexer: Provide generic mp_lexer_new_from_file based on mp_reader.Damien George
If a port defines MICROPY_READER_POSIX or MICROPY_READER_FATFS then lexer.c now provides an implementation of mp_lexer_new_from_file using the mp_reader_new_file function.
2016-11-16py/lexer: Rewrite mp_lexer_new_from_str_len in terms of mp_reader_mem.Damien George
2016-10-12py/lexer: Remove unnecessary code, and unreachable code.Damien George
Setting emit_dent=0 is unnecessary because arriving in that part of the if-logic will guarantee that emit_dent is already zero. The block to check indent_top(lex)>0 is unreachable because a newline is always inserted an the end of the input stream, and hence dedents are always processed before EOF.
2016-09-19py/vstr: Remove vstr.had_error flag and inline basic vstr functions.Damien George
The vstr.had_error flag was a relic from the very early days which assumed that the malloc functions (eg m_new, m_renew) returned NULL if they failed to allocate. But that's no longer the case: these functions will raise an exception if they fail. Since it was impossible for had_error to be set, this patch introduces no change in behaviour. An alternative option would be to change the malloc calls to the _maybe variants, which return NULL instead of raising, but then a lot of code will need to explicitly check if the vstr had an error and raise if it did. The code-size savings for this patch are, in bytes: bare-arm:188, minimal:456, unix(NDEBUG,x86-64):368, stmhal:228, esp8266:360.
2016-05-20py: Declare constant data as properly constant.Damien George
Otherwise some compilers (eg without optimisation) will put this read-only data in RAM instead of ROM.
2016-04-13py: add async/await/async for/async with syntaxpohmelie
They are sugar for marking function as generator, "yield from" and pep492 python "semantically equivalents" respectively. @dpgeorge was the original author of this patch, but @pohmelie made changes to implement `async for` and `async with`.
2016-02-25py: Add MICROPY_DYNAMIC_COMPILER option to config compiler at runtime.Damien George
This new compile-time option allows to make the bytecode compiler configurable at runtime by setting the fields in the mp_dynamic_compiler structure. By using this feature, the compiler can generate bytecode that targets any MicroPython runtime/VM, regardless of the host and target compile-time settings. Options so far that fall under this dynamic setting are: - maximum number of bits that a small int can hold; - whether caching of lookups is used in the bytecode; - whether to use unicode strings or not (lexer behaviour differs, and therefore generated string constants differ).
2015-12-18py: Add MICROPY_ENABLE_COMPILER and MICROPY_PY_BUILTINS_EVAL_EXEC opts.Damien George
MICROPY_ENABLE_COMPILER can be used to enable/disable the entire compiler, which is useful when only loading of pre-compiled bytecode is supported. It is enabled by default. MICROPY_PY_BUILTINS_EVAL_EXEC controls support of eval and exec builtin functions. By default they are only included if MICROPY_ENABLE_COMPILER is enabled. Disabling both options saves about 40k of code size on 32-bit x86.
2015-09-07py/lexer: Properly classify floats that look like hex numbers.Damien George
Eg 0e0 almost looks like a hex number but in fact is a float.
2015-09-07py/lexer: Raise SyntaxError when unicode char point out of range.Damien George
2015-09-07py/lexer: Raise NotImplError for unicode name escape, instead of assert.Damien George
2015-07-23py/lexer: Raise SyntaxError when str hex escape sequence is malformed.Damien George
Addresses issue #1390.
2015-06-22py: Cast argument for printf to int, to be compatible with more ports.Damien George
This allows stmhal to be compiled with MICROPY_DEBUG_PRINTERS.
2015-06-09py: Support unicode (utf-8 encoded) identifiers in Python source.Damien George
Enabled simply by making the identifier lexing code 8-bit clean.
2015-05-20extmod: Add ubinascii.unhexlifyDave Hylands
This also pulls out hex_digit from py/lexer.c and makes unichar_hex_digit
2015-03-19py: Allow to compile with extra warnings (sign-compare, unused-param).Damien George
2015-02-08py: Parse big-int/float/imag constants directly in parser.Damien George
Previous to this patch, a big-int, float or imag constant was interned (made into a qstr) and then parsed at runtime to create an object each time it was needed. This is wasteful in RAM and not efficient. Now, these constants are parsed straight away in the parser and turned into objects. This allows constants with large numbers of digits (so addresses issue #1103) and takes us a step closer to #722.
2015-01-30py: Convert CR to LF and CR LF to LF in lexer.Damien George
Only noticeable difference is how newlines are encoded in triple-quoted strings. The behaviour now matches CPython3.
2015-01-28py: Be more precise about unicode type and disabled unicode behaviour.Damien George
2015-01-16py, unix: Allow to compile with -Wsign-compare.Damien George
See issue #699.
2015-01-07py: Put all global state together in state structures.Damien George
This patch consolidates all global variables in py/ core into one place, in a global structure. Root pointers are all located together to make GC tracing easier and more efficient.
2015-01-01py: Move to guarded includes, everywhere in py/ core.Damien George
Addresses issue #1022.
2014-12-05py: Fix printing of size_t entity; fix qemu-arm for changes to lexer.Damien George
2014-12-05py: Optimise lexer by exposing lexer type.Damien George
mp_lexer_t type is exposed, mp_token_t type is removed, and simple lexer functions (like checking current token kind) are now inlined. This saves 784 bytes ROM on 32-bit unix, 348 bytes on stmhal, and 460 bytes on bare-arm. It also saves a tiny bit of RAM since mp_lexer_t is a bit smaller. Also will run a bit more efficiently.
2014-10-09py: Add further checks for failed malloc in lexer init functions.Damien George
2014-07-30py: Change lexer stream API to return bytes not chars.Damien George
Lexer is now 8-bit clean inside strings.
2014-07-03lexer: Convert type (u)int to mp_(u)int_t.Damien George