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

Lexer for VivJson
 
Lexer: Constructor. Its argument is source code as string.
Lexer#get_token: Extract a token from the current position of source
                   code.
 
For example, the given source code is extracted as below.
 
              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.
 
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.

 
Modules
       
re

 
Classes
       
builtins.object
Lexer

 
class Lexer(builtins.object)
    Lexer(source_code, medium=None, config=None)
 
Lexer class
 
Attributes:
    _medium (str or NoneType): The name of object that has source
                               code. It is used to report error.
                               For example, "test.viv",
                               "1st argument".
    _config (Config or NoneType): Configuration
    _maybe_get_token_methods (tuple): Methods of getting
                                      the particular token.
    _source_code (str): Source code as text. It is given from
                        caller.
    _current_letter (str): A current letter in the text.
    _current_position (int): The current position in the text.
    _token_head_position (int): The position of the current
                                token's head in the text.
    _line_number (int): Line number in the text. It is
                        saved into Token and is used for
                        error information.
    _line_head_position (int): The position of the current
                               line's head in the text.
                               It is used to calculate column
                               number that is saved into Token.
                               The column number is used for
                               error information.
    _previous_token (Token or NoneType): Previous token.
                                         None if first parsing.
    _hex_pattern (re.Pattern): Compiled pattern of Hex-digit.
    _EOS (str): A marker for end of string.
                It indicates that all letters are consumed.
    _ONE_LETTER_OPERATORS (tuple): Token types of
                                   1 letter's operations
    _TWO_LETTERS_OPERATORS (tuple): 2 dimensional tuple of
                                    Token type.
                                    1st value is Token type of
                                    2 letter's operation.
                                    2nd value is Token type of
                                    first one letter.
    _TERMINATORS (dict): Token types of terminator.
    _WHITE_SPACES (str): White spaces that should be skipped.
    _REPLACED_LETTERS (dict): The escaped letter and real letter.
 
  Methods defined here:
__init__(self, source_code, medium=None, config=None)
Initialize class.
 
Args:
    source_code (str): Source code as text
    medium (str, optional): The name of object that has source
                            code. It is used to report error.
                            For example, "test.viv",
                            "1st argument".
    config (Config or NoneType): Configuration if needed.
get_token(self)
Get the current token.
 
Returns:
    Token: The current token.
 
Raises:
    LexError: When unknown token is found, it is happen.

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

 
Author
        Fumiaki Motegi <motegi@benesult.com>