diff --git a/pico8/lua/lua.py b/pico8/lua/lua.py index cd10be5..9947d2c 100644 --- a/pico8/lua/lua.py +++ b/pico8/lua/lua.py @@ -344,24 +344,49 @@ def get_char_count(self): def get_token_count(self): c = 0 - for t in self._lexer._tokens: + prev_op=False #whether previous non whitespace token was an operator (for unary -) + unary_minus_ops=parser.BINOP_PATS + parser.UNOP_PATS+tuple(lexer.TokSymbol(x) for x in + (b'&=', b'|=', b'^^=', b'<<=', b'>>=', b'>>>=', b'<<>=', b'>><=', b'\\=', + b'..=', b'+=', b'-=', b'*=', b'/=', b'%=', b'^=', + b'(', b',', b'{', b'[' ,b'=') ) # these ops are not 100% accurate to how pico8 does it (pico8's list is slightly different) + #but all the edge cases left are pretty niche + for i,t in enumerate(self._lexer._tokens): # TODO: As of 0.1.8, "1 .. 5" is three tokens, "1..5" is one token + if (isinstance(t, lexer.TokSpace) or + isinstance(t, lexer.TokNewline) or + isinstance(t, lexer.TokComment)): + continue + if (t.matches(lexer.TokSymbol(b':')) or t.matches(lexer.TokSymbol(b'.')) or t.matches(lexer.TokSymbol(b')')) or t.matches(lexer.TokSymbol(b']')) or t.matches(lexer.TokSymbol(b'}')) or + t.matches(lexer.TokSymbol(b',')) or + t.matches(lexer.TokSymbol(b';')) or t.matches(lexer.TokKeyword(b'local')) or t.matches(lexer.TokKeyword(b'end'))): # PICO-8 generously does not count these as tokens. pass + elif ((t.matches(lexer.TokSymbol(b'-')) or t.matches(lexer.TokSymbol(b'~'))) and + i+1