jorts/jortsc/parser/parser.py

44 lines
855 B
Python
Raw Normal View History

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