-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathroot.peg
More file actions
132 lines (95 loc) · 2.3 KB
/
root.peg
File metadata and controls
132 lines (95 loc) · 2.3 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// uwu peg me flushed emoji
// common definitions, courtesy of lark-parser
DIGIT: "0".."9"
HEXDIGIT: "a".."f"|"A".."F"|DIGIT
INT: DIGIT+
SIGNED_INT: ["+"|"-"] INT
DECIMAL: INT "." INT? | "." INT
// float = /-?\d+(\.\d+)?([eE][+-]?\d+)?/
_EXP: ("e"|"E") SIGNED_INT
FLOAT: INT _EXP | DECIMAL _EXP?
SIGNED_FLOAT: ["+"|"-"] FLOAT
NUMBER: FLOAT | INT
SIGNED_NUMBER: ["+"|"-"] NUMBER
//
// Strings
//
_STRING_INNER: /.*?/
_STRING_ESC_INNER: _STRING_INNER /(?<!\\)(\\\\)*?/
ESCAPED_STRING : "\"" _STRING_ESC_INNER "\""
//
// Names (Variables)
//
LCASE_LETTER: "a".."z"
UCASE_LETTER: "A".."Z"
LETTER: UCASE_LETTER | LCASE_LETTER
WORD: LETTER+
CNAME: ("_"|LETTER) ("_"|LETTER|DIGIT)*
//
// Whitespace
//
WS_INLINE: (" "|/\t/)+
WS: /[ \t\f\r\n]/+
CR : /\r/
LF : /\n/
NEWLINE: (CR? LF)+
// Comments
SH_COMMENT: /#[^\n]*/
CPP_COMMENT: /\/\/[^\n]*/
C_COMMENT: "/*" /(.|\n)*?/ "*/"
SQL_COMMENT: /--[^\n]*/
// pseudocode, but tell the parser to ignore whitespace
%ignore WS
// the grammar
start: (expr | block)*
term: "end"
name: CNAME
?value: dict
| list
| tuple
| string
| atom
| SIGNED_NUMBER -> number
| "true" -> true
| "false" -> false
// IDK if we have null but uncomment below if so
// | "null" -> null
list: "[" [value ("," value)*] "]"
tuple: "(" [(value) ("," value)*] ")"
dict: "{" [kv ("," kv)*] "}"
kv: string ":" value
string: ESCAPED_STRING
atom: ":" name
type: name
| name "[]" -> array
| name "?" -> optional
expr: require
| assign
| create
| invoke
constant: "constant"
require: "require" name ("from" string)?
assign: constant? name "=" value
create: name "with" create_args term
create_args: name ":" [(create | value) ("," create | value)*]
invoke: (name ".")? name args
args: "(" [(value) ("," value)*] ")"
| [(value) ("," value)*]
block: enum
| function
| class
| trait
enum: "type" name [name*] term
function: static? function_type body return? term
static: "static"
return: "return" value
body: expr*
function_type: "function" name params kind
params: "(" [("self" | property) ("," property)*] ")"
kind: "->" type
class: name ("implements" name)? class_body term
class_cons: function
class_body: [class_cons] [class_prop*] [function*]
class_prop: name ":" type
trait: name trait_body term
trait_body: [function_type*]