This repository has been archived by the owner on Apr 27, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
grammar.pest
51 lines (41 loc) · 2.15 KB
/
grammar.pest
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//! This grammar is a Ocypode programming language grammar.
//! It is used to parse Ocypode source code into an AST.
//! For more information about Ocypode, see https://github.com/TheAwiteb/ocypode-lang
WHITESPACE = _{ WHITE_SPACE }
COMMENT = _{ comment_line | comment_block }
IDENT = @{ !reserved ~ (ASCII_ALPHA | "_") ~ (ASCII_ALPHANUMERIC | "_")* }
PUBLIC = { "^" }
PRIVATE = { "" }
visibility = ${ (PUBLIC | PRIVATE) ~ !LETTER }
semicolon = { ";" }
keyword = { ("return") ~ !LETTER }
reserved = { (keyword | boolean | nil) ~ !LETTER }
comment_line = _{ "//" ~ (!NEWLINE ~ ANY)* }
start_comment_block = _{ "/*" }
end_comment_block = _{ "*/" }
comment_block = _{ start_comment_block ~ (!end_comment_block ~ ANY)* ~ end_comment_block }
string_delimiter = _{ "\"" }
string_escape = { "\\" ~ (string_delimiter | "\\" | "n" | "r" | "t") }
string = @{ string_delimiter ~ (string_escape | !(string_delimiter | "\\") ~ ANY)* ~ string_delimiter }
integer = @{ ("0" | ASCII_NONZERO_DIGIT ~ ASCII_DIGIT*) }
float = @{ integer ~ "." ~ ASCII_DIGIT+ }
boolean = @{ ("true" | "false") ~ !LETTER }
nil = @{ "nil" ~ !LETTER }
array = { "[" ~ !"," ~ (expression ~ ("," ~ expression)*)? ~ ","? ~ "]" }
value = { string | float | integer | boolean | array | nil | IDENT }
pack = ${ "*" | "" }
unpack = ${ "..." | "" }
param = ${ "<" ~ pack ~ IDENT ~ ">" }
params = { param* }
anonymous_function_params = { ("<" ~ ">") | param+ }
arg = { "<" ~ unpack ~ expression ~ ">" }
args = { ("<" ~ ">") | arg+ }
block = { "{<" ~ (statement ~ semicolon)* ~ ">}" }
func_def = { visibility ~ "~" ~ IDENT ~ params ~ block }
anonymous_function = { anonymous_function_params ~ block }
func_call = { (IDENT | anonymous_function) ~ args }
return_stmt = { "return" ~ expression }
assignment = { IDENT ~ "=" ~ expression }
expression = { func_call | anonymous_function | value }
statement = { func_def | assignment | return_stmt | expression }
program = { SOI ~ func_def* ~ EOI }