Pythonic way to implement a tokenizer

前端 未结 12 729
青春惊慌失措
青春惊慌失措 2020-12-30 07:11

I\'m going to implement a tokenizer in Python and I was wondering if you could offer some style advice?

I\'ve implemented a tokenizer before in C and in Java so I\'m

12条回答
  •  不知归路
    2020-12-30 07:41

    Thanks for your help, I've started to bring these ideas together, and I've come up with the following. Is there anything terribly wrong with this implementation (particularly I'm concerned about passing a file object to the tokenizer):

    class Tokenizer(object):
    
      def __init__(self,file):
         self.file = file
    
      def __get_next_character(self):
          return self.file.read(1)
    
      def __peek_next_character(self):
          character = self.file.read(1)
          self.file.seek(self.file.tell()-1,0)
          return character
    
      def __read_number(self):
          value = ""
          while self.__peek_next_character().isdigit():
              value += self.__get_next_character()
          return value
    
      def next_token(self):
          character = self.__peek_next_character()
    
          if character.isdigit():
              return self.__read_number()
    

提交回复
热议问题