Skip to main content

A tokenizer with file position tracking and backtracking suitable for making parsers.

Project description

tokenbuffer

TokenBuffer provides a TokenBuffer class and tokenizer provides a Token data class.

Use

After creating a new TokenBuffer, You will have to initialize the regex patterns with a dictionary of token names and patterns.

from TokenBuffer import TokenBuffer, Token

patterns = {
    'INTEGER': r'\d+',
    'MATH_OPERATOR': r'[\+\-\*\/]',
    'BINARY_OPERATOR': r'[\&\|\^]',
    'ASSIGNMENT': r'=',
    'IDENTIFIER': r'\w+'
}

buffer = TokenBuffer()
buffer.init_patterns(patterns)

Now you will have to provide content to be tokenized. This can be done by adding lines or loading files or both. load_files accepts a list of strings, which are the files to load. add_lines takes a "file name", for tracking purposes, and a list of strings.

buffer.load_file(sys.argv[1:])
buffer.add_lines('cleanup', ['push(result)', 'exit()'])

Once the source text is ready, you will have to tokenize it.

buffer.tokenize()

The tokenizer provides white space, end of line, and end of file tokens. You can configure how they are handled when reading the buffer. The defaults are:

  • 'skip_white_space': False
  • 'skip_EOF': True
  • 'skip_EOL': True
buffer.config(
        skip_white_space = True,
        skip_EOF = True,
        skip_EOL = True
    )

You can read through the buffer with these methods:

  • peek(): Returns the current token, skipping the tokens specified in the configuration.
  • consume(): Advances to the next token.
  • consume_line(): Advances to the start of the next source line.
  • expect_value(expected_value, lower = False): Returns the result of an equality test. Set lower to True if you want the test to be case insenstive.
  • expect_type(expected_type): expected_type is the token name you provided with the regex patterns.
  • backtrack(): Backtracks to the previous unskipped token.
  • backtrack_line(): Backtracks to the end of the previous line, skipping unwanted tokens.
  • at_start(): Returns True if the buffer is at the first token of the first file.
  • out_of_tokens(): Returns True if there are no more tokens available.
  • get_position(): Returns a tuple of current file name, current file line, token column. Token column is not the column of the source file text! See Token below. This column is the token index of the current line. IE: The third token on the line is 2, while the source text column could be anything.
  • reset(): Reset the position of the buffer to the first token of the first line.

Token

Token is a dataclass with three data members:

  • type: The regex group name you provided.
  • value: The value of the regex match. This is always a string. You will have to convert to the appropriate data type while processing.
  • column: This the the position on the source text line where the value was found.
while not buffer.out_of_tokens():
    peek: Token = buffer.peek()
    file, line, column = buffer.get_position()
    print(f"file: {file} line {line} column {peek.column} type: {peek.type} value: {peek.value}")
    buffer.consume()

print('\nBacktracking')
while not buffer.at_start():
    buffer.backtrack()
    peek: Token = buffer.peek()
    file, line, column = buffer.get_position()
    print(f"file: {file} line {line} column {peek.column} type: {peek.type} value: {peek.value}")

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tokenbuffer-0.1.4.tar.gz (4.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tokenbuffer-0.1.4-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

File details

Details for the file tokenbuffer-0.1.4.tar.gz.

File metadata

  • Download URL: tokenbuffer-0.1.4.tar.gz
  • Upload date:
  • Size: 4.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.3

File hashes

Hashes for tokenbuffer-0.1.4.tar.gz
Algorithm Hash digest
SHA256 9f8795b4ac3624db86d514f6ef08ae54fd1bf04c3dd21836b1374879a44c1af6
MD5 515f3e27437e462ce048dd0461c440bf
BLAKE2b-256 cbe9652831997db089a6cd3c7f84f55cb05bf2e28a6183ca6122212df644cccc

See more details on using hashes here.

File details

Details for the file tokenbuffer-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: tokenbuffer-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 4.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.3

File hashes

Hashes for tokenbuffer-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 407992022efb38a65aff91e773ed6a75bee22b0ab342015847681349389ee65f
MD5 d55f991ee1b13de8447dc89ce5addbd8
BLAKE2b-256 73358661acf04a9bb70b01d9d9381a890776802ccd120f1b0d3c43cfd77c7837

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page