~sircmpwn/rc

rc/doc/grammar.txt -rw-r--r-- 2.9 KiB
f408a956Drew DeVault parse: remove unreachable abort 11 days ago
                                                                                
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
# (value) indicates an optional terminal or non-terminal
#
# Not specified in the grammer is comments, which are
#
# '#' /[^\n]*/
#
# This may appear anywhere outside of a quoted string and should be removed by
# the lexer.
#
# Continuations are also not specified, i.e. '\' LF
#
# TODO:
# - foo=bar command
# - rework numeric tokens

script		commands (ALLWS)

commands	(ALLWS) command
		commands (ALLWS) separator command

separator	LF
		';'

command 	compound-command
		assign
		if-command
		for-command
		while-command
		switch-command
		func
		'{' commands '}'
		'@' '{' commands '}'
		WS command (WS)

simple-command	value-list (redirections)

compound-command
		simple-command
		builtin-command
		'!' (WS) compound-command
		compound-command (WS) pipe (WS) compound-command
		compound-command (WS) '&&' (WS) compound-command
		compound-command (WS) '||' (WS) compound-command

builtin-command
		builtin value-list (redirections)

builtin		'cd'
		'break'
		'continue'
		'eval'
		'exit'
		'echo'
		'shift'
		'unset'

assign		word (WS) '=' (WS) value

for-command	'for' (WS) '(' (WS) word (WS) ')' command
		'for' (WS) '(' word (WS) 'in' (WS) value-list ')' command

if-command	'if' (WS) condition command
		if-command 'else' command

while-command	'while' (WS) condition command

switch-command	'switch' (WS) '(' (WS) value (WS) ')' (WS) '{' switch-cases '}'

switch-cases	switch-case
		switch-cases switch-case

switch-case	(WS) 'case' WS value ALLWS command
		(WS) 'default' WS value ALLWS command

condition	'(' command ')'

func		'fn' WS word (params) WS command

params		'(' param-list ')'

param-list	word
		param-list WS word

pipe		'|' (file)

redirections	redirection
		redirections redirection

redirection	'<' (file) (WS) value
		'>' (file) (WS) value
		'>' '>' (file) (WS) value
		'<' '>' (file) (WS) value
		'>' '>' heredoc

heredoc
		(-) word [...] word
		(-) string-literal [...] string-literal

file		'[' (WS) integer (WS) ']'
		'[' (WS) integer (WS) '=' (WS) ']'
		'[' (WS) integer (WS) '=' (WS) integer (WS) ']'

value		literal
		access
		subscript
		pipescript
		'(' (WS) value-list (WS) ')'

value-list	value
		value WS value-list

literal		argument
		string-literal
		literal argument
		literal string-literal

string-literal
		'"' nondquote '"'
		"'" nonsquote "'"

access		'$' varname
		'$' '#' varname
		'$' '"' varname
		'$' varname '(' (WS) access-list (WS) ')'

varname		word
		numeric
		'*'

access-list	index
		'-' (WS) index
		index (WS) '-'
		index (WS) '-' (WS) index

index		numeric
		access

subscript	'`' '{' script '}'
		'`' string-literal '{' script '}'

pipescript	'<' '{' script '}'
		'>' '{' script '}'

word		/[A-Za-z_][A-Za-z0-9_.]*/
argument	/[^\[\](){}<>'"&|!;=$#@`\\ \t\n]+/
nondquote	/[^"\n]+/
nonsquote	/[^'\n]+/
numeric		integer
integer		/[0-9]+/

linefeeds	LF (WS)
		LF (WS) linefeeds

WS	SP
	TS
	WS SP
	WS TS

ALLWS
	SP
	TS
	LF
	WS SP
	WS TS
	WS LF

SP	' '
TS	'\t'
LF	'\n'