vivjson.tokens (2025-03-30)
index
https://github.com/benesult/vivjson-python/tree/main/vivjson/tokens.py

Token for VivJson
 
For example,
 
              GT  NUMBER  ASSIGN  PLUS  NUMBER
               |  |            |   |    |
    while  ( i >  3     )  { i = i +    1     }
    |      | |          |  | |   |            |
IDENTIFIER | IDENTIFIER |  | IDENTIFIER       |
           |            |  |                  |
       LEFT_PARENTHESIS |  LEFT_CURLY_BRACKET |
                        |                     |
          RIGHT_PARENTHESIS            RIGHT_CURLY_BRACKET
 
 
Refer to:
- "Let's make a Teeny Tiny compiler"
  https://austinhenley.com/blog/teenytinycompiler1.html
- "Crafting Interpreters"
  https://craftinginterpreters.com/
Note that this code is made from scratch. The source code
of the above WEB sites is not used.
 
Note:
This file name is "tokens.py" instead of "token.py". Because `pydoc`
does not work correctly for "token.py". Then `pydoc` outputs error
as below.
> ImportError: cannot import name 'EXACT_TOKEN_TYPES' from 'token'
 
Environment:
- Python 3.9 or later
 
License:
Copyright 2025 benesult
 
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
 
    http://www.apache.org/licenses/LICENSE-2.0
 
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

 
Classes
       
builtins.object
Token

 
class Token(builtins.object)
    Token(token_type, medium=None, line_number=None, column_number=None, lexeme=None)
 
Token class
 
Attributes:
    type (Token.Type): The type of token
    lexeme (str or NoneType): The token's actual text
    medium (str or NoneType): The name of object where the token is
                              existed. For example, "test.viv",
                              "1st argument".
    line_number (int or NoneType): Line number where the token is
                                   existed.
    column_number (int or NoneType): Column number where the token
                                     is existed.
    _keywords (dict): Keywords table that is generated from
                      _KEYWORD_TYPES.
    _KEYWORD_TYPES (tuple): Token types of Keyword.
 
  Methods defined here:
__init__(self, token_type, medium=None, line_number=None, column_number=None, lexeme=None)
Initialize class.
 
Args:
    token_type (Token.Type): The type of token
    medium (str, optional): The name of object where the token
                            is existed. For example,
                            "test.viv", "1st argument".
    line_number (int, optional): Line number where the token
                                 is existed.
    column_number (int, optional): Column number where the
                                   token is existed.
    lexeme (str, optional): The token's actual text. It is so
                            called "lexeme".
                            When this is omitted or this is
                            None, the assigned value of Token
                            type is used.
__repr__(self)
Return repr(self).
fix_keyword_type(self)
Fix type if this is Keyword.
 
Returns:
    bool: True if it is fixed,
          False otherwise.
to_string(self, omit_type_name=False)
Get as string.
 
Args:
    omit_type_name (bool, optional): Type name is omitted when
                                     it is True.
                                     The default is False.
 
Returns:
    str: Type name and lexeme

Data descriptors defined here:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)

Data and other attributes defined here:
Type = <enum 'Type'>
Token types

 
Author
        Fumiaki Motegi <motegi@benesult.com>