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 | ||||||||||
|
Author | ||
Fumiaki Motegi <motegi@benesult.com> |