jorts/jortsc/parser/parser.py

44 lines
855 B
Python

from lark import Lark
GRAMMAR = """
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)*
"""
def parse(string: str):
"""Parse"""
parser = Lark(GRAMMAR, parser='lalr', debug=True)
return parser.parse(string)