2019-03-09 19:52:34 +00:00
|
|
|
|
|
|
|
from lark import Lark
|
|
|
|
|
|
|
|
GRAMMAR = """
|
2019-03-09 21:58:28 +00:00
|
|
|
FN: "fn"
|
|
|
|
IMPORT: "import"
|
|
|
|
COMMA: ","
|
|
|
|
DOT: "."
|
|
|
|
SINGLE_COMMENT: "//"
|
|
|
|
NEWLINE: /[ \\n\\t]+/
|
|
|
|
ANY: /.+/
|
|
|
|
WHITESPACE: " "
|
|
|
|
INTEGER: /[0-9]+/
|
|
|
|
ARROW: "->"
|
|
|
|
COM_START: "/*"
|
|
|
|
COM_END: "*/"
|
|
|
|
QUOTE: "\\""
|
|
|
|
|
|
|
|
identifier: WHITESPACE* ANY WHITESPACE*
|
|
|
|
|
|
|
|
single_comment: "//" ANY* NEWLINE
|
|
|
|
multi_comment: COM_START ANY* COM_END
|
|
|
|
|
|
|
|
import_stmt: IMPORT identifier NEWLINE
|
|
|
|
|
|
|
|
fn_arg: identifier identifier
|
|
|
|
fn_stmt: FN identifier? "(" [fn_arg COMMA] ")" [ARROW identifier] "{" [stmt NEWLINE]* "}"
|
|
|
|
|
|
|
|
sign_int: "+" | "-"
|
|
|
|
string: QUOTE ANY* QUOTE
|
|
|
|
value: (sign_int* INTEGER) | string
|
|
|
|
|
|
|
|
call_stmt: [identifier DOT] identifier "(" [value COMMA]* ")"
|
|
|
|
|
|
|
|
stmt: (NEWLINE*) | import_stmt | fn_stmt | call_stmt
|
|
|
|
|
|
|
|
start: (stmt NEWLINE)*
|
2019-03-09 19:52:34 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
def parse(string: str):
|
|
|
|
"""Parse"""
|
|
|
|
parser = Lark(GRAMMAR, parser='lalr', debug=True)
|
|
|
|
return parser.parse(string)
|