vivjson.parser (2025-04-04)
index
https://github.com/benesult/vivjson-python/tree/main/vivjson/parser.py

Parser for VivJson
 
Parser: Constructor. Its argument is source code as string.
Parser#parse: Parse source code and get statements.
 
<program> ::= [ "{" [ <nl>+ ] ]
    [ <statement> { <end> <statement> } [ <end> ] ]
    [ [ <nl>+ ] "}" ]
<block> ::= "{" [ <nl>+ ]
    [ <statement> { <end> <statement> } [ <end> ] ]
    [ <nl>+ ] "}"
<statement> ::= "break" | "continue" | <return> | <remove>
    | <expression> | <call_if> | <call_for>
    | <call_primitive> | <call_extended> | <declaration>
<return> ::= "return" [ <nl>+ ] [ "(" [ <nl>+ ] <or> [ <nl>+ ] ")" ]
<remove> ::= "remove" [ <nl>+ ] "(" [ <nl>+ ] <element> [ <nl>+ ] ")"
<declaration> ::= <callee>
<callee> ::= <modifier> [ <nl>+ ] <identifier> [ <nl>+ ]
    [ "(" [ <nl> ] [ <parameter> { <end> <parameter> } ] [ <nl> ] ")" ]
    [ <nl>+ ] <block>
<call_extended> ::= <element> [ <nl>+ ]
    [ "(" [ <nl> ] [ <argument> { <end> <argument> } ] [ <nl> ]
    [ <block> [ <nl> ] ] ")" ] [ <nl>+ ] [ <block> ]
<call_primitive> ::= <element> [ <nl>+ ]
    "(" [ <nl> ] [ <argument> { <end> <argument> } ] [ <nl> ] ")"
<call_for> ::= "for" [ <nl>+ ] "(" [ <nl> ]
    ( [ <argument_for> ] [ <end> ] [ <or> ] [ <end> ]
      [ <argument_for> ] [ <end> ] [ <block> ] )
    [ <nl> ] ")" [ <nl>+ ] [ <block> ]
<call_if> ::= "if" [ <nl>+ ] "(" [ <nl>+ ] <argument>
      [ <end> [ <nl>+ ] <block> ] [ <nl> ] ")" [ <nl>+ ] [ <block> ]
    { [ <nl>+ ] "elseif" [ <nl>+ ] "(" [ <nl>+ ] <argument>
      [ <end> [ <nl>+ ] <block> ] [ <nl> ] ")" [ <nl>+ ] [ <block> ] }
    [ [ <nl>+ ] "else" [ <nl>+ ] [ "("
      [ [ <nl>+ ] <block> ] [ <nl> ] ")" ] [ <nl>+ ] [ <block> ] ]
<parameter> ::= [ <modifier> [ <nl>+ ] ] <identifier>
<argument> ::= <or>
<argument_for> ::= <assignment> | <call_primitive> | <call_extended>
<expression> ::= <assignment> | <result>
<result> ::= ":=" [ <nl>+ ] <or>
<assignment> ::= <element> [ <nl>+ ]
    ( "=" | ":" | "+=" | "-=" | "*=" | "/=" | "%=" ) [ <nl>+ ] <or>
<group> :: = "(" [ <nl>+ ] <or> [ <nl>+ ] ")"
<or> ::= <and> { [ <nl>+ ] "or" [ <nl>+ ] <and> }
<and> ::= <equality> { [ <nl>+ ] "and" [ <nl>+ ] <equality> }
<equality> ::= <comparison>
    { [ <nl>+ ] ( "==" | "!=" | "in" ) [ <nl>+ ] <comparison> }
<comparison> ::= <term>
    { [ <nl>+ ] ( "<" | "<=" | ">" | ">=" ) [ <nl>+ ] <term> }
<term> ::= <factor> { [ <nl>+ ] ( "+" | "-" ) [ <nl>+ ] <factor> }
<factor> ::= <thing>
    { [ <nl>+ ] ( "*" | "/" | "%" ) [ <nl>+ ] <thing> }
<thing> ::= <unary> | <array> | <block>
<unary> ::= [ "+" | "-" | "not" ] ( <primary> | <element>
    | <call_extended> | <call_if> | <call_for> )
<element> ::=
    ( ( <identifier> | <call_primitive> )
      {
        ( [ <nl>+ ] "." [ <nl>+ ]
            ( <identifier> | <call_primitive> |
              ( [ "+" | "-" ] <digit>+ ) ) )
        | ( "[" [ <nl>+ ] <term> [ <nl>+ ] "]" )
      }
    )
<array> :: = "[" [ <nl> ] 
      [ <argument> { <end> <argument> } [ <end> ] ] [ <nl> ]
    "]"
<primary> ::= <number> | <string> | <boolean> | "null" | <group>
<modifier> ::= "function" | "reference"
<identifier> ::= ( "_" | <alphabet> ) { "_" | <alphabet> | <digit> }
<boolean> ::= "true" | "false"
<number> ::= <digit>+ [ "." <digit>+ ]
    [ ( "e" | "E" ) ( "+" | "-" ) <digit>+ ]
<digit> ::= "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
<alphabet> ::= "A" | "B" | "C" | "D" | "E" | "F" | "G" | "H"
    | "I" | "J" | "K" | "L" | "M" | "N" | "O" | "P" | "Q"
    | "R" | "S" | "T" | "U" | "V" | "W" | "X" | "Y" | "Z"
    | "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h"
    | "i" | "j" | "k" | "l" | "m" | "n" | "o" | "p" | "q"
    | "r" | "s" | "t" | "u" | "v" | "w" | "x" | "y" | "z"
<end> ::= <nl> | ";" | ","
<nl> ::= "
"
 
<string> is any characters as UTF-8. It is surrounded with "" or ''.
 
[]: 0 or 1 time (? is not used here. But it has same meaning.)
{}: 0 or more times (* is not used here. But it has same meaning.)
+: 1 or more times
(): group
|: or
 
"." is allowed as the right-hand sided operand of "in".
 
Refer to:
- "Let's make a Teeny Tiny compiler"
  https://austinhenley.com/blog/teenytinycompiler2.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.

 
Classes
       
builtins.object
Parser

 
class Parser(builtins.object)
    Parser(source_code, medium=None, config=None)
 
Parser class
 
Attributes:
    _config (Config or NoneType): Configuration
    _is_enable_only_json (bool): When true is given, the given data
                                 is parsed as JSON.
                                 In other words, script is
                                 disabled.
    _lexer (Lexer): An instance of Lexer
    _make_statement_methods (tuple): Methods of making statement.
    _make_unary_methods (tuple): Methods of making unary.
    _make_for_argument_methods (tuple): Methods of making for-loop
                                        argument.
    _tokens (list): Tokens of the current statement.
    _index (int): The current index of the above list "_tokens".
    _is_implicit_assign (bool): When the given source code is not
                                JSON object (a.k.a. map, hash,
                                dictionary), this is True.
                                The default is False.
 
  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 object that has source code.
                            It is used to report error.
    config (Config, optional): Configuration if needed.
parse(self)
Parse source code and get statements that construct it.
 
- Parse VivJson and JSON object (with/without bracket).
- Parse directly represented value of JSON.
 
Returns:
    list[Statement]: <program>
 
Raises:
    LexError: It is happen when unknown token is found.
    ParseError: It is happen when parsing is failed.

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>