GithubHelp home page GithubHelp logo

purplex's Issues

Using OrderedDict for token list is slow

Switching to a regular list improves performance.

diff --git a/purplex/lex.py b/purplex/lex.py
index e70af02..93a0e94 100644
--- a/purplex/lex.py
+++ b/purplex/lex.py
@@ -15,11 +15,10 @@ class LexerBase(type):
         for name, attr in dct.items():
             if isinstance(attr, TokenDef):
                 tokens[name] = attr
-                del dct[name]
         ret = super(LexerBase, cls).__new__(cls, name, bases, dct)
         if hasattr(ret, 'tokens'):
             tokens.update(ret.tokens)
-        ret.tokens = tokens
+        ret.tokens = list(tokens.items())
         return ret


@@ -49,7 +48,7 @@ class Lexer(metaclass=LexerBase):
     def next_token(self):
         matches = []

-        for name, token_defn in self.tokens.items():
+        for name, token_defn in self.tokens:
             match = token_defn.regexp.match(self.input_text, self.input_pos)
             if match:
                 token = Token(name, match.group(0), token_defn,
diff --git a/purplex/parse.py b/purplex/parse.py
index b42dd99..bdf4bc6 100644
--- a/purplex/parse.py
+++ b/purplex/parse.py
@@ -94,7 +94,7 @@ class Parser(metaclass=ParserBase):

     def _build(self, start, debug):
         magic = MagicParser()
-        magic.tokens = [name for name, tokendef in self.LEXER.tokens.items()
+        magic.tokens = [name for name, tokendef in self.LEXER.tokens
                         if not tokendef.ignore]
         if self.PRECEDENCE:
             magic.precedence = self.PRECEDENCE
diff --git a/purplex/test/test_lexer.py b/purplex/test/test_lexer.py
index 5665db9..a0b7c51 100644
--- a/purplex/test/test_lexer.py
+++ b/purplex/test/test_lexer.py
@@ -26,13 +26,13 @@ def test_lex_foobar():

     assert tokens[0].name == 'FOO'
     assert tokens[0].value == 'foo'
-    assert tokens[0].defn == FooBarLexer.tokens['FOO']
+    assert tokens[0].defn == FooBarLexer.FOO
     assert tokens[0].line_num == 1
     assert tokens[0].line_pos == 1

     assert tokens[1].name == 'BAR'
     assert tokens[1].value == 'bar'
-    assert tokens[1].defn == FooBarLexer.tokens['BAR']
+    assert tokens[1].defn == FooBarLexer.BAR
     assert tokens[1].line_num == 1
     assert tokens[1].line_pos == 4

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.