-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathnash.ohm
More file actions
182 lines (130 loc) · 3.61 KB
/
nash.ohm
File metadata and controls
182 lines (130 loc) · 3.61 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Formal specification of nash language using ohm PEG
// For informations about ohm syntax visit the site below:
// https://github.com/harc/ohm
// There's an interactive Nash's grammar visualizer at:
// https://ohmlang.github.io/editor/#fca725147857c09bb442be1ce03048e0
// Note: Nash has automatic semicolon insertion, but this isn't defined here
// to not complicate the grammar spec.
// TODO: rfork, cmd redirections, pipes, import, string-concat
Nash {
Block
= Statement*
Body
= "{" Block "}"
comment
= "#" everythingNoNL*
Comment
= comment
// Syntatic rules named *Internal are only created to code reuse
// They're not nash syntax per-se.
VarStmt (a variable declaration)
= "var" AssignInternal stmtEnd
AssignStmt (an assignment declaration)
= AssignInternal stmtEnd
MultipleAssignStmt
= MultipleAssignInternal stmtEnd
MultipleAssign2Stmt
= MultipleAssign2Internal stmtEnd
AssignInternal
= SingleAssign | MultipleAssignInternal | MultipleAssign2Internal
SingleAssign
= ident (ValueAssign | ExecAssign)
ValueAssign
= "=" VRHS
VRHS (a value right-hand-side)
= Literal | Variable
ExecAssign
= "<=" ExecExpr
// a, b, c = ...
// out, err, status <=
MultipleAssignInternal (a multiple variables assignment)
= MLHS (MultipleValueAssignInternal | ExecAssign)
// a, b, c
MLHS (a multiple left hand side)
= NonemptyListOf<ident, ",">
MultipleAssign2Internal (a list of assignments)
= "(" NonemptyListOf<SingleAssign, ","> ","? ")"
MultipleValueAssignInternal
= "=" MVRHS
// "1", "2", (), ("test")
MVRHS
= NonemptyListOf<VRHS, ","> ","?
// ls $path;
// ls /etc;
CmdStmt (a command statement)
= CmdExpr stmtEnd
CmdExpr
= Program ~"(" (Arg|Variable)*
// An executable expression is a command or function call
// echo "hello"
// deploy()
ExecExpr (an executable expression)
= CmdExpr | FuncallExpr
FuncallStmt (a function call)
= FuncallExpr stmtEnd
FuncallExpr
= ident "(" ListOf<Expr, ","> ")"
// for {}
// for i in $lst {}
ForStmt (a for statement)
= "for" ForClause? Body
ForClause
= ident "in" Variable
IfStmt (an if statement)
= "if" Condition Body ElseStmt?
ElseStmt
= "else" Body
Condition
= Expr logicalOp Expr
SetenvStmt
= "setenv" AssignInternal stmtEnd
Expr (an expression)
= Variable | Literal | FuncallExpr
logicalOp (a logical operator)
= "==" | "!="
Indexing
= "[" (integer|Variable) "]"
// $a
// $a[0]
// $a[$b]
Variable
= "$" ident Indexing?
// A program cannot have a keyword name
Program (a program)
= ~keywords (ident | Arg)
Arg (an argument)
= string | argLiteral
// /usr/home/ken
// C:\Users\Bill
argLiteral
= (alnum|pathSeparator|":"|"-")+
ident (an identifier)
= ( letter | "_" ) (alnum | "_")*
pathSeparator (a path separator)
= "/" | "\\"
lineTerminator (a line terminator)
= "\n" | "\r" | "\u2028" | "\u2029" | "\r\n"
stmtEnd (end of statement)
= end|";"
// TODO: This list is not complete
// TODO: How to use the ~ lookhead to solve this?
string
= "\"" (alnum|blank|"\\"|"-"|":"|"/"|".")* "\""
integer (a number)
= digit+
Literal (a literal)
= string | ListStmt
ListStmt
= "(" Literal* ")"
blank
= " " | "\t" | "\n" | "\r"
// TODO: get rid of this
everythingNoNL
= (alnum|"_"|"!"|"@"|"#"|"$"|"%"|"¨"|"&"|"*"|"("|")"|"_"|"-"|"+"|"="|
"`"|"´"|"["|"{"|"^"|"~"|"]"|"}"|"<"|">"|":"|"?"|"/"|";"|"."|","|"\\"|
"|"|"'"|"\""|" ")
keywords
= "for" | "var" | "in" | "setenv" | "if" | "rfork" | "import"
Statement = (comment | VarStmt | AssignStmt | CmdStmt | FuncallStmt | ForStmt |
IfStmt | SetenvStmt)
}