@futpib/parser 1.0.3 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/bash.d.ts +84 -0
- package/build/bash.js +1 -0
- package/build/bashParser.d.ts +6 -0
- package/build/bashParser.js +294 -0
- package/build/bashParser.test.d.ts +1 -0
- package/build/bashParser.test.js +181 -0
- package/build/index.d.ts +24 -2
- package/build/index.js +22 -1
- package/build/regexpParser.d.ts +2 -0
- package/build/regexpParser.js +71 -0
- package/build/regexpParser.test.d.ts +1 -0
- package/build/regexpParser.test.js +83 -0
- package/build/regularExpression.d.ts +63 -0
- package/build/regularExpression.js +1 -0
- package/build/regularExpressionParser.d.ts +3 -0
- package/build/regularExpressionParser.js +580 -0
- package/build/regularExpressionParser.test.d.ts +1 -0
- package/build/regularExpressionParser.test.js +89 -0
- package/package.json +2 -1
- package/src/bash.ts +120 -0
- package/src/bashParser.test.ts +332 -0
- package/src/bashParser.ts +461 -0
- package/src/index.ts +113 -2
- package/src/regexpParser.test.ts +186 -0
- package/src/regexpParser.ts +94 -0
- package/src/regularExpression.ts +24 -0
- package/src/regularExpressionParser.test.ts +102 -0
- package/src/regularExpressionParser.ts +921 -0
package/build/bash.d.ts
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
export type BashWord = {
|
|
2
|
+
parts: BashWordPart[];
|
|
3
|
+
};
|
|
4
|
+
export type BashWordPart = BashWordPartLiteral | BashWordPartSingleQuoted | BashWordPartDoubleQuoted | BashWordPartVariable | BashWordPartVariableBraced | BashWordPartCommandSubstitution | BashWordPartBacktickSubstitution | BashWordPartArithmeticExpansion;
|
|
5
|
+
export type BashWordPartLiteral = {
|
|
6
|
+
type: 'literal';
|
|
7
|
+
value: string;
|
|
8
|
+
};
|
|
9
|
+
export type BashWordPartSingleQuoted = {
|
|
10
|
+
type: 'singleQuoted';
|
|
11
|
+
value: string;
|
|
12
|
+
};
|
|
13
|
+
export type BashWordPartDoubleQuoted = {
|
|
14
|
+
type: 'doubleQuoted';
|
|
15
|
+
parts: BashWordPart[];
|
|
16
|
+
};
|
|
17
|
+
export type BashWordPartVariable = {
|
|
18
|
+
type: 'variable';
|
|
19
|
+
name: string;
|
|
20
|
+
};
|
|
21
|
+
export type BashWordPartVariableBraced = {
|
|
22
|
+
type: 'variableBraced';
|
|
23
|
+
name: string;
|
|
24
|
+
operator?: string;
|
|
25
|
+
operand?: BashWord;
|
|
26
|
+
};
|
|
27
|
+
export type BashWordPartCommandSubstitution = {
|
|
28
|
+
type: 'commandSubstitution';
|
|
29
|
+
command: BashCommand;
|
|
30
|
+
};
|
|
31
|
+
export type BashWordPartBacktickSubstitution = {
|
|
32
|
+
type: 'backtickSubstitution';
|
|
33
|
+
command: BashCommand;
|
|
34
|
+
};
|
|
35
|
+
export type BashWordPartArithmeticExpansion = {
|
|
36
|
+
type: 'arithmeticExpansion';
|
|
37
|
+
expression: string;
|
|
38
|
+
};
|
|
39
|
+
export type BashRedirect = {
|
|
40
|
+
fd?: number;
|
|
41
|
+
operator: '>' | '>>' | '<' | '<<' | '<<<' | '>&' | '<&' | '>|';
|
|
42
|
+
target: BashWord | BashHereDoc;
|
|
43
|
+
};
|
|
44
|
+
export type BashHereDoc = {
|
|
45
|
+
type: 'hereDoc';
|
|
46
|
+
delimiter: string;
|
|
47
|
+
content: string;
|
|
48
|
+
quoted: boolean;
|
|
49
|
+
};
|
|
50
|
+
export type BashAssignment = {
|
|
51
|
+
name: string;
|
|
52
|
+
value?: BashWord;
|
|
53
|
+
};
|
|
54
|
+
export type BashSimpleCommand = {
|
|
55
|
+
type: 'simple';
|
|
56
|
+
name?: BashWord;
|
|
57
|
+
args: BashWord[];
|
|
58
|
+
redirects: BashRedirect[];
|
|
59
|
+
assignments: BashAssignment[];
|
|
60
|
+
};
|
|
61
|
+
export type BashSubshell = {
|
|
62
|
+
type: 'subshell';
|
|
63
|
+
body: BashCommand;
|
|
64
|
+
};
|
|
65
|
+
export type BashBraceGroup = {
|
|
66
|
+
type: 'braceGroup';
|
|
67
|
+
body: BashCommand;
|
|
68
|
+
};
|
|
69
|
+
export type BashCommandUnit = BashSimpleCommand | BashSubshell | BashBraceGroup;
|
|
70
|
+
export type BashPipeline = {
|
|
71
|
+
type: 'pipeline';
|
|
72
|
+
negated: boolean;
|
|
73
|
+
commands: BashCommandUnit[];
|
|
74
|
+
};
|
|
75
|
+
export type BashCommandListSeparator = '&&' | '||' | ';' | '&' | '\n';
|
|
76
|
+
export type BashCommandList = {
|
|
77
|
+
type: 'list';
|
|
78
|
+
entries: {
|
|
79
|
+
pipeline: BashPipeline;
|
|
80
|
+
separator?: BashCommandListSeparator;
|
|
81
|
+
}[];
|
|
82
|
+
};
|
|
83
|
+
export type BashCommand = BashCommandList;
|
|
84
|
+
export type BashScript = BashCommand;
|
package/build/bash.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { type Parser } from './parser.js';
|
|
2
|
+
import { type BashWord, type BashSimpleCommand, type BashCommand } from './bash.js';
|
|
3
|
+
export declare const bashWordParser: Parser<BashWord, string>;
|
|
4
|
+
export declare const bashSimpleCommandParser: Parser<BashSimpleCommand, string>;
|
|
5
|
+
export declare const bashCommandParser: Parser<BashCommand, string>;
|
|
6
|
+
export declare const bashScriptParser: Parser<BashCommand, string>;
|
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
import { setParserName } from './parser.js';
|
|
2
|
+
import { createExactSequenceParser } from './exactSequenceParser.js';
|
|
3
|
+
import { promiseCompose } from './promiseCompose.js';
|
|
4
|
+
import { createTupleParser } from './tupleParser.js';
|
|
5
|
+
import { createDisjunctionParser } from './disjunctionParser.js';
|
|
6
|
+
import { createArrayParser } from './arrayParser.js';
|
|
7
|
+
import { createParserAccessorParser } from './parserAccessorParser.js';
|
|
8
|
+
import { createOptionalParser } from './optionalParser.js';
|
|
9
|
+
import { createRegExpParser } from './regexpParser.js';
|
|
10
|
+
import { createNonEmptyArrayParser } from './nonEmptyArrayParser.js';
|
|
11
|
+
import { createSeparatedNonEmptyArrayParser } from './separatedNonEmptyArrayParser.js';
|
|
12
|
+
// Whitespace (spaces and tabs, not newlines - those are significant)
|
|
13
|
+
const bashInlineWhitespaceParser = promiseCompose(createRegExpParser(/[ \t]+/), match => match[0]);
|
|
14
|
+
const bashOptionalInlineWhitespaceParser = promiseCompose(createRegExpParser(/[ \t]*/), match => match[0]);
|
|
15
|
+
// Newline
|
|
16
|
+
const bashNewlineParser = promiseCompose(createRegExpParser(/\n/), match => match[0]);
|
|
17
|
+
// Word characters (unquoted, no special chars)
|
|
18
|
+
// Note: {} are excluded so brace groups are parsed correctly
|
|
19
|
+
const bashUnquotedWordCharsParser = promiseCompose(createRegExpParser(/[^\s\n|&;<>(){}$`"'\\#]+/), match => match[0]);
|
|
20
|
+
// Single quoted string: '...'
|
|
21
|
+
const bashSingleQuotedParser = promiseCompose(createTupleParser([
|
|
22
|
+
createExactSequenceParser("'"),
|
|
23
|
+
promiseCompose(createRegExpParser(/[^']*/), match => match[0]),
|
|
24
|
+
createExactSequenceParser("'"),
|
|
25
|
+
]), ([, value]) => ({
|
|
26
|
+
type: 'singleQuoted',
|
|
27
|
+
value,
|
|
28
|
+
}));
|
|
29
|
+
// Variable name
|
|
30
|
+
const bashVariableNameParser = promiseCompose(createRegExpParser(/[a-zA-Z_][a-zA-Z0-9_]*|[0-9]+|[@*#?$!-]/), match => match[0]);
|
|
31
|
+
// Simple variable: $var
|
|
32
|
+
const bashSimpleVariableParser = promiseCompose(createTupleParser([
|
|
33
|
+
createExactSequenceParser('$'),
|
|
34
|
+
bashVariableNameParser,
|
|
35
|
+
]), ([, name]) => ({
|
|
36
|
+
type: 'variable',
|
|
37
|
+
name,
|
|
38
|
+
}));
|
|
39
|
+
// Command substitution: $(...)
|
|
40
|
+
const bashCommandSubstitutionParser = promiseCompose(createTupleParser([
|
|
41
|
+
createExactSequenceParser('$('),
|
|
42
|
+
bashOptionalInlineWhitespaceParser,
|
|
43
|
+
createParserAccessorParser(() => bashCommandParser),
|
|
44
|
+
bashOptionalInlineWhitespaceParser,
|
|
45
|
+
createExactSequenceParser(')'),
|
|
46
|
+
]), ([, , command]) => ({
|
|
47
|
+
type: 'commandSubstitution',
|
|
48
|
+
command,
|
|
49
|
+
}));
|
|
50
|
+
// Backtick substitution: `...`
|
|
51
|
+
const bashBacktickSubstitutionParser = promiseCompose(createTupleParser([
|
|
52
|
+
createExactSequenceParser('`'),
|
|
53
|
+
createParserAccessorParser(() => bashCommandParser),
|
|
54
|
+
createExactSequenceParser('`'),
|
|
55
|
+
]), ([, command]) => ({
|
|
56
|
+
type: 'backtickSubstitution',
|
|
57
|
+
command,
|
|
58
|
+
}));
|
|
59
|
+
// Double quoted string parts (inside "...")
|
|
60
|
+
const bashDoubleQuotedPartParser = createDisjunctionParser([
|
|
61
|
+
bashSimpleVariableParser,
|
|
62
|
+
bashCommandSubstitutionParser,
|
|
63
|
+
bashBacktickSubstitutionParser,
|
|
64
|
+
// Escape sequences in double quotes
|
|
65
|
+
promiseCompose(createRegExpParser(/\\[\\$`"!\n]/), match => ({
|
|
66
|
+
type: 'literal',
|
|
67
|
+
value: match[0].slice(1),
|
|
68
|
+
})),
|
|
69
|
+
// Literal text (no special chars)
|
|
70
|
+
promiseCompose(createRegExpParser(/[^$`"\\]+/), match => ({
|
|
71
|
+
type: 'literal',
|
|
72
|
+
value: match[0],
|
|
73
|
+
})),
|
|
74
|
+
]);
|
|
75
|
+
// Double quoted string: "..."
|
|
76
|
+
const bashDoubleQuotedParser = promiseCompose(createTupleParser([
|
|
77
|
+
createExactSequenceParser('"'),
|
|
78
|
+
createArrayParser(bashDoubleQuotedPartParser),
|
|
79
|
+
createExactSequenceParser('"'),
|
|
80
|
+
]), ([, parts]) => ({
|
|
81
|
+
type: 'doubleQuoted',
|
|
82
|
+
parts,
|
|
83
|
+
}));
|
|
84
|
+
// Literal word part (unquoted)
|
|
85
|
+
const bashLiteralWordPartParser = promiseCompose(bashUnquotedWordCharsParser, value => ({
|
|
86
|
+
type: 'literal',
|
|
87
|
+
value,
|
|
88
|
+
}));
|
|
89
|
+
// Escape sequence outside quotes
|
|
90
|
+
const bashEscapeParser = promiseCompose(createRegExpParser(/\\./), match => ({
|
|
91
|
+
type: 'literal',
|
|
92
|
+
value: match[0].slice(1),
|
|
93
|
+
}));
|
|
94
|
+
// Word part (any part of a word)
|
|
95
|
+
const bashWordPartParser = createDisjunctionParser([
|
|
96
|
+
bashSingleQuotedParser,
|
|
97
|
+
bashDoubleQuotedParser,
|
|
98
|
+
bashCommandSubstitutionParser,
|
|
99
|
+
bashBacktickSubstitutionParser,
|
|
100
|
+
bashSimpleVariableParser,
|
|
101
|
+
bashEscapeParser,
|
|
102
|
+
bashLiteralWordPartParser,
|
|
103
|
+
]);
|
|
104
|
+
// Word (sequence of word parts)
|
|
105
|
+
export const bashWordParser = promiseCompose(createNonEmptyArrayParser(bashWordPartParser), parts => ({ parts }));
|
|
106
|
+
setParserName(bashWordParser, 'bashWordParser');
|
|
107
|
+
// Assignment: NAME=value or NAME=
|
|
108
|
+
const bashAssignmentParser = promiseCompose(createTupleParser([
|
|
109
|
+
promiseCompose(createRegExpParser(/[a-zA-Z_][a-zA-Z0-9_]*=/), match => match[0].slice(0, -1)),
|
|
110
|
+
createOptionalParser(bashWordParser),
|
|
111
|
+
]), ([name, value]) => ({
|
|
112
|
+
name,
|
|
113
|
+
value: value ?? undefined,
|
|
114
|
+
}));
|
|
115
|
+
// Redirect operators
|
|
116
|
+
const bashRedirectOperatorParser = createDisjunctionParser([
|
|
117
|
+
promiseCompose(createExactSequenceParser('>>'), () => '>>'),
|
|
118
|
+
promiseCompose(createExactSequenceParser('>&'), () => '>&'),
|
|
119
|
+
promiseCompose(createExactSequenceParser('>|'), () => '>|'),
|
|
120
|
+
promiseCompose(createExactSequenceParser('>'), () => '>'),
|
|
121
|
+
promiseCompose(createExactSequenceParser('<<<'), () => '<<<'),
|
|
122
|
+
promiseCompose(createExactSequenceParser('<<'), () => '<<'),
|
|
123
|
+
promiseCompose(createExactSequenceParser('<&'), () => '<&'),
|
|
124
|
+
promiseCompose(createExactSequenceParser('<'), () => '<'),
|
|
125
|
+
]);
|
|
126
|
+
// Redirect: [n]op word
|
|
127
|
+
const bashRedirectParser = promiseCompose(createTupleParser([
|
|
128
|
+
createOptionalParser(promiseCompose(createRegExpParser(/[0-9]+/), match => Number.parseInt(match[0], 10))),
|
|
129
|
+
bashRedirectOperatorParser,
|
|
130
|
+
bashOptionalInlineWhitespaceParser,
|
|
131
|
+
bashWordParser,
|
|
132
|
+
]), ([fd, operator, , target]) => ({
|
|
133
|
+
fd: fd ?? undefined,
|
|
134
|
+
operator,
|
|
135
|
+
target,
|
|
136
|
+
}));
|
|
137
|
+
// Word with optional trailing whitespace - for use in arrays
|
|
138
|
+
const bashWordWithWhitespaceParser = promiseCompose(createTupleParser([
|
|
139
|
+
bashWordParser,
|
|
140
|
+
bashOptionalInlineWhitespaceParser,
|
|
141
|
+
]), ([word]) => word);
|
|
142
|
+
// Redirect with optional trailing whitespace
|
|
143
|
+
const bashRedirectWithWhitespaceParser = promiseCompose(createTupleParser([
|
|
144
|
+
bashRedirectParser,
|
|
145
|
+
bashOptionalInlineWhitespaceParser,
|
|
146
|
+
]), ([redirect]) => redirect);
|
|
147
|
+
// Word or redirect - for interleaved parsing in simple commands
|
|
148
|
+
const bashWordOrRedirectParser = createDisjunctionParser([
|
|
149
|
+
promiseCompose(bashRedirectWithWhitespaceParser, redirect => ({ type: 'redirect', redirect })),
|
|
150
|
+
promiseCompose(bashWordWithWhitespaceParser, word => ({ type: 'word', word })),
|
|
151
|
+
]);
|
|
152
|
+
// Simple command: [assignments] [name] [args] [redirects]
|
|
153
|
+
export const bashSimpleCommandParser = promiseCompose(createTupleParser([
|
|
154
|
+
// Assignments at the start
|
|
155
|
+
createArrayParser(promiseCompose(createTupleParser([
|
|
156
|
+
bashAssignmentParser,
|
|
157
|
+
bashOptionalInlineWhitespaceParser,
|
|
158
|
+
]), ([assignment]) => assignment)),
|
|
159
|
+
// Command name, args, and redirects (interleaved)
|
|
160
|
+
createArrayParser(bashWordOrRedirectParser),
|
|
161
|
+
]), ([assignments, items]) => {
|
|
162
|
+
const words = [];
|
|
163
|
+
const redirects = [];
|
|
164
|
+
for (const item of items) {
|
|
165
|
+
if (item.type === 'word') {
|
|
166
|
+
words.push(item.word);
|
|
167
|
+
}
|
|
168
|
+
else {
|
|
169
|
+
redirects.push(item.redirect);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
const [name, ...args] = words;
|
|
173
|
+
return {
|
|
174
|
+
type: 'simple',
|
|
175
|
+
name,
|
|
176
|
+
args,
|
|
177
|
+
redirects,
|
|
178
|
+
assignments,
|
|
179
|
+
};
|
|
180
|
+
});
|
|
181
|
+
setParserName(bashSimpleCommandParser, 'bashSimpleCommandParser');
|
|
182
|
+
// Subshell: ( command )
|
|
183
|
+
const bashSubshellParser = promiseCompose(createTupleParser([
|
|
184
|
+
createExactSequenceParser('('),
|
|
185
|
+
bashOptionalInlineWhitespaceParser,
|
|
186
|
+
createParserAccessorParser(() => bashCommandParser),
|
|
187
|
+
bashOptionalInlineWhitespaceParser,
|
|
188
|
+
createExactSequenceParser(')'),
|
|
189
|
+
]), ([, , body]) => ({
|
|
190
|
+
type: 'subshell',
|
|
191
|
+
body,
|
|
192
|
+
}));
|
|
193
|
+
setParserName(bashSubshellParser, 'bashSubshellParser');
|
|
194
|
+
// Brace group: { command; }
|
|
195
|
+
const bashBraceGroupParser = promiseCompose(createTupleParser([
|
|
196
|
+
createExactSequenceParser('{'),
|
|
197
|
+
bashInlineWhitespaceParser,
|
|
198
|
+
createParserAccessorParser(() => bashCommandParser),
|
|
199
|
+
bashOptionalInlineWhitespaceParser,
|
|
200
|
+
createOptionalParser(createExactSequenceParser(';')),
|
|
201
|
+
bashOptionalInlineWhitespaceParser,
|
|
202
|
+
createExactSequenceParser('}'),
|
|
203
|
+
]), ([, , body]) => ({
|
|
204
|
+
type: 'braceGroup',
|
|
205
|
+
body,
|
|
206
|
+
}));
|
|
207
|
+
setParserName(bashBraceGroupParser, 'bashBraceGroupParser');
|
|
208
|
+
// Command unit: simple command, subshell, or brace group
|
|
209
|
+
const bashCommandUnitParser = createDisjunctionParser([
|
|
210
|
+
bashSubshellParser,
|
|
211
|
+
bashBraceGroupParser,
|
|
212
|
+
bashSimpleCommandParser,
|
|
213
|
+
]);
|
|
214
|
+
setParserName(bashCommandUnitParser, 'bashCommandUnitParser');
|
|
215
|
+
// Single pipe (not ||) - matches | only when not followed by another |
|
|
216
|
+
const bashSinglePipeParser = promiseCompose(createRegExpParser(/\|(?!\|)/), match => match[0]);
|
|
217
|
+
// Pipeline: [!] cmd [| cmd]...
|
|
218
|
+
const bashPipelineParser = promiseCompose(createTupleParser([
|
|
219
|
+
createOptionalParser(promiseCompose(createTupleParser([
|
|
220
|
+
createExactSequenceParser('!'),
|
|
221
|
+
bashInlineWhitespaceParser,
|
|
222
|
+
]), () => true)),
|
|
223
|
+
createSeparatedNonEmptyArrayParser(bashCommandUnitParser, createTupleParser([
|
|
224
|
+
bashOptionalInlineWhitespaceParser,
|
|
225
|
+
bashSinglePipeParser,
|
|
226
|
+
bashOptionalInlineWhitespaceParser,
|
|
227
|
+
])),
|
|
228
|
+
]), ([negated, commands]) => ({
|
|
229
|
+
type: 'pipeline',
|
|
230
|
+
negated: negated ?? false,
|
|
231
|
+
commands,
|
|
232
|
+
}));
|
|
233
|
+
setParserName(bashPipelineParser, 'bashPipelineParser');
|
|
234
|
+
// Command list separator
|
|
235
|
+
const bashListSeparatorParser = createDisjunctionParser([
|
|
236
|
+
promiseCompose(createExactSequenceParser('&&'), () => '&&'),
|
|
237
|
+
promiseCompose(createExactSequenceParser('||'), () => '||'),
|
|
238
|
+
promiseCompose(createExactSequenceParser(';'), () => ';'),
|
|
239
|
+
promiseCompose(createExactSequenceParser('&'), () => '&'),
|
|
240
|
+
promiseCompose(bashNewlineParser, () => '\n'),
|
|
241
|
+
]);
|
|
242
|
+
// Command list: pipeline [sep pipeline]...
|
|
243
|
+
const bashCommandListParser = promiseCompose(createTupleParser([
|
|
244
|
+
bashPipelineParser,
|
|
245
|
+
createArrayParser(promiseCompose(createTupleParser([
|
|
246
|
+
bashOptionalInlineWhitespaceParser,
|
|
247
|
+
bashListSeparatorParser,
|
|
248
|
+
bashOptionalInlineWhitespaceParser,
|
|
249
|
+
bashPipelineParser,
|
|
250
|
+
]), ([, separator, , pipeline]) => ({ separator, pipeline }))),
|
|
251
|
+
createOptionalParser(promiseCompose(createTupleParser([
|
|
252
|
+
bashOptionalInlineWhitespaceParser,
|
|
253
|
+
bashListSeparatorParser,
|
|
254
|
+
]), ([, separator]) => separator)),
|
|
255
|
+
]), ([firstPipeline, rest, trailingSeparator]) => {
|
|
256
|
+
const entries = [];
|
|
257
|
+
if (rest.length === 0) {
|
|
258
|
+
entries.push({
|
|
259
|
+
pipeline: firstPipeline,
|
|
260
|
+
separator: trailingSeparator ?? undefined,
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
else {
|
|
264
|
+
entries.push({
|
|
265
|
+
pipeline: firstPipeline,
|
|
266
|
+
separator: rest[0].separator,
|
|
267
|
+
});
|
|
268
|
+
for (let i = 0; i < rest.length - 1; i++) {
|
|
269
|
+
entries.push({
|
|
270
|
+
pipeline: rest[i].pipeline,
|
|
271
|
+
separator: rest[i + 1].separator,
|
|
272
|
+
});
|
|
273
|
+
}
|
|
274
|
+
entries.push({
|
|
275
|
+
pipeline: rest[rest.length - 1].pipeline,
|
|
276
|
+
separator: trailingSeparator ?? undefined,
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
return {
|
|
280
|
+
type: 'list',
|
|
281
|
+
entries,
|
|
282
|
+
};
|
|
283
|
+
});
|
|
284
|
+
setParserName(bashCommandListParser, 'bashCommandListParser');
|
|
285
|
+
// Top-level command parser
|
|
286
|
+
export const bashCommandParser = bashCommandListParser;
|
|
287
|
+
setParserName(bashCommandParser, 'bashCommandParser');
|
|
288
|
+
// Script parser (handles leading/trailing whitespace)
|
|
289
|
+
export const bashScriptParser = promiseCompose(createTupleParser([
|
|
290
|
+
bashOptionalInlineWhitespaceParser,
|
|
291
|
+
bashCommandParser,
|
|
292
|
+
bashOptionalInlineWhitespaceParser,
|
|
293
|
+
]), ([, command]) => command);
|
|
294
|
+
setParserName(bashScriptParser, 'bashScriptParser');
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
import test from 'ava';
|
|
2
|
+
import { runParser } from './parser.js';
|
|
3
|
+
import { stringParserInputCompanion } from './parserInputCompanion.js';
|
|
4
|
+
import { bashScriptParser, bashWordParser, bashSimpleCommandParser } from './bashParser.js';
|
|
5
|
+
test('simple command parser - single word', async (t) => {
|
|
6
|
+
const result = await runParser(bashSimpleCommandParser, 'cmd', stringParserInputCompanion, { errorStack: true });
|
|
7
|
+
t.is(result.type, 'simple');
|
|
8
|
+
t.deepEqual(result.name, { parts: [{ type: 'literal', value: 'cmd' }] });
|
|
9
|
+
});
|
|
10
|
+
test('simple command parser - two words', async (t) => {
|
|
11
|
+
const result = await runParser(bashSimpleCommandParser, 'echo hello', stringParserInputCompanion);
|
|
12
|
+
t.is(result.type, 'simple');
|
|
13
|
+
t.deepEqual(result.name, { parts: [{ type: 'literal', value: 'echo' }] });
|
|
14
|
+
t.is(result.args.length, 1);
|
|
15
|
+
});
|
|
16
|
+
test('word parser - simple literal', async (t) => {
|
|
17
|
+
const result = await runParser(bashWordParser, 'hello', stringParserInputCompanion);
|
|
18
|
+
t.deepEqual(result, {
|
|
19
|
+
parts: [{ type: 'literal', value: 'hello' }],
|
|
20
|
+
});
|
|
21
|
+
});
|
|
22
|
+
test('word parser - variable', async (t) => {
|
|
23
|
+
const result = await runParser(bashWordParser, '$HOME', stringParserInputCompanion);
|
|
24
|
+
t.deepEqual(result, {
|
|
25
|
+
parts: [{ type: 'variable', name: 'HOME' }],
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
test('simple command', async (t) => {
|
|
29
|
+
const result = await runParser(bashScriptParser, 'echo hello', stringParserInputCompanion);
|
|
30
|
+
t.deepEqual(result, {
|
|
31
|
+
type: 'list',
|
|
32
|
+
entries: [{
|
|
33
|
+
pipeline: {
|
|
34
|
+
type: 'pipeline',
|
|
35
|
+
negated: false,
|
|
36
|
+
commands: [{
|
|
37
|
+
type: 'simple',
|
|
38
|
+
name: { parts: [{ type: 'literal', value: 'echo' }] },
|
|
39
|
+
args: [{ parts: [{ type: 'literal', value: 'hello' }] }],
|
|
40
|
+
redirects: [],
|
|
41
|
+
assignments: [],
|
|
42
|
+
}],
|
|
43
|
+
},
|
|
44
|
+
separator: undefined,
|
|
45
|
+
}],
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
test('simple command with multiple args', async (t) => {
|
|
49
|
+
const result = await runParser(bashScriptParser, 'echo hello world', stringParserInputCompanion);
|
|
50
|
+
t.is(result.entries[0].pipeline.commands[0].type, 'simple');
|
|
51
|
+
const cmd = result.entries[0].pipeline.commands[0];
|
|
52
|
+
if (cmd.type === 'simple') {
|
|
53
|
+
t.is(cmd.args.length, 2);
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
test('pipeline', async (t) => {
|
|
57
|
+
const result = await runParser(bashScriptParser, 'cat file | grep pattern', stringParserInputCompanion);
|
|
58
|
+
t.is(result.entries[0].pipeline.commands.length, 2);
|
|
59
|
+
});
|
|
60
|
+
test('redirect output', async (t) => {
|
|
61
|
+
const result = await runParser(bashScriptParser, 'echo foo > file', stringParserInputCompanion);
|
|
62
|
+
const cmd = result.entries[0].pipeline.commands[0];
|
|
63
|
+
if (cmd.type === 'simple') {
|
|
64
|
+
t.is(cmd.redirects.length, 1);
|
|
65
|
+
t.is(cmd.redirects[0].operator, '>');
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
test('redirect with fd', async (t) => {
|
|
69
|
+
const result = await runParser(bashScriptParser, 'cmd 2>&1', stringParserInputCompanion);
|
|
70
|
+
const cmd = result.entries[0].pipeline.commands[0];
|
|
71
|
+
if (cmd.type === 'simple') {
|
|
72
|
+
t.is(cmd.redirects.length, 1);
|
|
73
|
+
t.is(cmd.redirects[0].fd, 2);
|
|
74
|
+
t.is(cmd.redirects[0].operator, '>&');
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
test('single quoted string', async (t) => {
|
|
78
|
+
const result = await runParser(bashScriptParser, "echo 'hello world'", stringParserInputCompanion);
|
|
79
|
+
const cmd = result.entries[0].pipeline.commands[0];
|
|
80
|
+
if (cmd.type === 'simple') {
|
|
81
|
+
t.deepEqual(cmd.args[0], {
|
|
82
|
+
parts: [{ type: 'singleQuoted', value: 'hello world' }],
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
test('double quoted string with variable', async (t) => {
|
|
87
|
+
const result = await runParser(bashScriptParser, 'echo "hello $name"', stringParserInputCompanion);
|
|
88
|
+
const cmd = result.entries[0].pipeline.commands[0];
|
|
89
|
+
if (cmd.type === 'simple') {
|
|
90
|
+
t.deepEqual(cmd.args[0], {
|
|
91
|
+
parts: [{
|
|
92
|
+
type: 'doubleQuoted',
|
|
93
|
+
parts: [
|
|
94
|
+
{ type: 'literal', value: 'hello ' },
|
|
95
|
+
{ type: 'variable', name: 'name' },
|
|
96
|
+
],
|
|
97
|
+
}],
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
test('simple variable', async (t) => {
|
|
102
|
+
const result = await runParser(bashScriptParser, 'echo $HOME', stringParserInputCompanion);
|
|
103
|
+
const cmd = result.entries[0].pipeline.commands[0];
|
|
104
|
+
if (cmd.type === 'simple') {
|
|
105
|
+
t.deepEqual(cmd.args[0], {
|
|
106
|
+
parts: [{ type: 'variable', name: 'HOME' }],
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
test('command substitution', async (t) => {
|
|
111
|
+
const result = await runParser(bashScriptParser, 'echo $(pwd)', stringParserInputCompanion);
|
|
112
|
+
const cmd = result.entries[0].pipeline.commands[0];
|
|
113
|
+
if (cmd.type === 'simple') {
|
|
114
|
+
t.is(cmd.args[0].parts[0].type, 'commandSubstitution');
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
test('backtick substitution', async (t) => {
|
|
118
|
+
const result = await runParser(bashScriptParser, 'echo `pwd`', stringParserInputCompanion);
|
|
119
|
+
const cmd = result.entries[0].pipeline.commands[0];
|
|
120
|
+
if (cmd.type === 'simple') {
|
|
121
|
+
t.is(cmd.args[0].parts[0].type, 'backtickSubstitution');
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
test('subshell', async (t) => {
|
|
125
|
+
const result = await runParser(bashScriptParser, '(cd dir; pwd)', stringParserInputCompanion);
|
|
126
|
+
t.is(result.entries[0].pipeline.commands[0].type, 'subshell');
|
|
127
|
+
});
|
|
128
|
+
test('brace group', async (t) => {
|
|
129
|
+
const result = await runParser(bashScriptParser, '{ echo hello; }', stringParserInputCompanion);
|
|
130
|
+
t.is(result.entries[0].pipeline.commands[0].type, 'braceGroup');
|
|
131
|
+
});
|
|
132
|
+
test('command list with &&', async (t) => {
|
|
133
|
+
const result = await runParser(bashScriptParser, 'cmd1 && cmd2', stringParserInputCompanion);
|
|
134
|
+
t.is(result.entries.length, 2);
|
|
135
|
+
t.is(result.entries[0].separator, '&&');
|
|
136
|
+
});
|
|
137
|
+
test('command list with ||', async (t) => {
|
|
138
|
+
const result = await runParser(bashScriptParser, 'cmd1 || cmd2', stringParserInputCompanion);
|
|
139
|
+
t.is(result.entries.length, 2);
|
|
140
|
+
t.is(result.entries[0].separator, '||');
|
|
141
|
+
});
|
|
142
|
+
test('command list with ;', async (t) => {
|
|
143
|
+
const result = await runParser(bashScriptParser, 'cmd1; cmd2', stringParserInputCompanion);
|
|
144
|
+
t.is(result.entries.length, 2);
|
|
145
|
+
t.is(result.entries[0].separator, ';');
|
|
146
|
+
});
|
|
147
|
+
test('background command', async (t) => {
|
|
148
|
+
const result = await runParser(bashScriptParser, 'cmd &', stringParserInputCompanion);
|
|
149
|
+
t.is(result.entries[0].separator, '&');
|
|
150
|
+
});
|
|
151
|
+
test('assignment', async (t) => {
|
|
152
|
+
const result = await runParser(bashScriptParser, 'VAR=value cmd', stringParserInputCompanion);
|
|
153
|
+
const cmd = result.entries[0].pipeline.commands[0];
|
|
154
|
+
if (cmd.type === 'simple') {
|
|
155
|
+
t.is(cmd.assignments.length, 1);
|
|
156
|
+
t.is(cmd.assignments[0].name, 'VAR');
|
|
157
|
+
}
|
|
158
|
+
});
|
|
159
|
+
test('negated pipeline', async (t) => {
|
|
160
|
+
const result = await runParser(bashScriptParser, '! cmd', stringParserInputCompanion);
|
|
161
|
+
t.is(result.entries[0].pipeline.negated, true);
|
|
162
|
+
});
|
|
163
|
+
test('complex pipeline with redirects', async (t) => {
|
|
164
|
+
const result = await runParser(bashScriptParser, 'cat file 2>/dev/null | grep pattern | sort > output', stringParserInputCompanion);
|
|
165
|
+
t.is(result.entries[0].pipeline.commands.length, 3);
|
|
166
|
+
});
|
|
167
|
+
test('[[ treated as command name', async (t) => {
|
|
168
|
+
const result = await runParser(bashScriptParser, '[[ -f file ]]', stringParserInputCompanion);
|
|
169
|
+
const cmd = result.entries[0].pipeline.commands[0];
|
|
170
|
+
if (cmd.type === 'simple') {
|
|
171
|
+
t.deepEqual(cmd.name, { parts: [{ type: 'literal', value: '[[' }] });
|
|
172
|
+
t.is(cmd.args.length, 3); // -f, file, ]]
|
|
173
|
+
}
|
|
174
|
+
});
|
|
175
|
+
test('if treated as command name', async (t) => {
|
|
176
|
+
const result = await runParser(bashScriptParser, 'if true', stringParserInputCompanion);
|
|
177
|
+
const cmd = result.entries[0].pipeline.commands[0];
|
|
178
|
+
if (cmd.type === 'simple') {
|
|
179
|
+
t.deepEqual(cmd.name, { parts: [{ type: 'literal', value: 'if' }] });
|
|
180
|
+
}
|
|
181
|
+
});
|
package/build/index.d.ts
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
|
-
export { type Parser, runParser, setParserName, getParserName, } from './parser.js';
|
|
1
|
+
export { type Parser, runParser, runParserWithRemainingInput, setParserName, getParserName, cloneParser, type RunParserOptions, type RunParserWithRemainingInputResult, } from './parser.js';
|
|
2
2
|
export type { ParserContext, } from './parserContext.js';
|
|
3
|
+
export { type ParserInputCompanion, StringParserInputCompanion, stringParserInputCompanion, Uint8ArrayParserInputCompanion, uint8ArrayParserInputCompanion, } from './parserInputCompanion.js';
|
|
4
|
+
export { type UnparserOutputCompanion, StringUnparserOutputCompanion, stringUnparserOutputCompanion, Uint8ArrayUnparserOutputCompanion, uint8ArrayUnparserOutputCompanion, } from './unparserOutputCompanion.js';
|
|
5
|
+
export { type ParserError, type ParserParsingFailedError, type ParserParsingJoinError, type ParserErrorModule, isParserError, isParserParsingFailedError, isParserParsingJoinError, normalParserErrorModule, noStackCaptureOverheadParserErrorModule, } from './parserError.js';
|
|
6
|
+
export { parserCreatorCompose, parserCreatorComposeMem, } from './parserCreatorCompose.js';
|
|
7
|
+
export { promiseCompose, } from './promiseCompose.js';
|
|
8
|
+
export type { DeriveSequenceElement, } from './sequence.js';
|
|
3
9
|
export { createTupleParser, } from './tupleParser.js';
|
|
4
10
|
export { createExactSequenceParser, } from './exactSequenceParser.js';
|
|
5
11
|
export { createFixedLengthSequenceParser, } from './fixedLengthSequenceParser.js';
|
|
@@ -16,6 +22,22 @@ export { createSkipParser, } from './skipParser.js';
|
|
|
16
22
|
export { createEndOfInputParser, } from './endOfInputParser.js';
|
|
17
23
|
export { createListParser, } from './listParser.js';
|
|
18
24
|
export { createDebugLogParser, } from './debugLogParser.js';
|
|
19
|
-
export {
|
|
25
|
+
export { createNonEmptyArrayParser, } from './nonEmptyArrayParser.js';
|
|
26
|
+
export { createSeparatedArrayParser, } from './separatedArrayParser.js';
|
|
27
|
+
export { createSeparatedNonEmptyArrayParser, } from './separatedNonEmptyArrayParser.js';
|
|
28
|
+
export { createLookaheadParser, } from './lookaheadParser.js';
|
|
29
|
+
export { createNegativeLookaheadParser, } from './negativeLookaheadParser.js';
|
|
30
|
+
export { createElementTerminatedSequenceParser, } from './elementTerminatedSequenceParser.js';
|
|
31
|
+
export { createElementTerminatedSequenceArrayParser, } from './elementTerminatedSequenceArrayParser.js';
|
|
32
|
+
export { createElementTerminatedArrayParserUnsafe, } from './elementTerminatedArrayParser.js';
|
|
33
|
+
export { createSequenceTerminatedSequenceParser, } from './sequenceTerminatedSequenceParser.js';
|
|
34
|
+
export { createQuantifierParser, } from './quantifierParser.js';
|
|
35
|
+
export { createSkipToParser, } from './skipToParser.js';
|
|
36
|
+
export { createDebugLogInputParser, } from './debugLogInputParser.js';
|
|
37
|
+
export { createElementSwitchParser, } from './exactElementSwitchParser.js';
|
|
38
|
+
export { createParserConsumedSequenceParser, } from './parserConsumedSequenceParser.js';
|
|
39
|
+
export { type Unparser, type UnparserResult, runUnparser, } from './unparser.js';
|
|
40
|
+
export { type UnparserContext, WriteLater, WriteEarlier, UnparserContextImplementation, } from './unparserContext.js';
|
|
20
41
|
export { createArrayUnparser, } from './arrayUnparser.js';
|
|
21
42
|
export { createSequenceUnparser, } from './sequenceUnparser.js';
|
|
43
|
+
export { createRegExpParser, } from './regexpParser.js';
|
package/build/index.js
CHANGED
|
@@ -1,4 +1,9 @@
|
|
|
1
|
-
export { runParser, setParserName, getParserName, } from './parser.js';
|
|
1
|
+
export { runParser, runParserWithRemainingInput, setParserName, getParserName, cloneParser, } from './parser.js';
|
|
2
|
+
export { StringParserInputCompanion, stringParserInputCompanion, Uint8ArrayParserInputCompanion, uint8ArrayParserInputCompanion, } from './parserInputCompanion.js';
|
|
3
|
+
export { StringUnparserOutputCompanion, stringUnparserOutputCompanion, Uint8ArrayUnparserOutputCompanion, uint8ArrayUnparserOutputCompanion, } from './unparserOutputCompanion.js';
|
|
4
|
+
export { isParserError, isParserParsingFailedError, isParserParsingJoinError, normalParserErrorModule, noStackCaptureOverheadParserErrorModule, } from './parserError.js';
|
|
5
|
+
export { parserCreatorCompose, parserCreatorComposeMem, } from './parserCreatorCompose.js';
|
|
6
|
+
export { promiseCompose, } from './promiseCompose.js';
|
|
2
7
|
export { createTupleParser, } from './tupleParser.js';
|
|
3
8
|
export { createExactSequenceParser, } from './exactSequenceParser.js';
|
|
4
9
|
export { createFixedLengthSequenceParser, } from './fixedLengthSequenceParser.js';
|
|
@@ -15,6 +20,22 @@ export { createSkipParser, } from './skipParser.js';
|
|
|
15
20
|
export { createEndOfInputParser, } from './endOfInputParser.js';
|
|
16
21
|
export { createListParser, } from './listParser.js';
|
|
17
22
|
export { createDebugLogParser, } from './debugLogParser.js';
|
|
23
|
+
export { createNonEmptyArrayParser, } from './nonEmptyArrayParser.js';
|
|
24
|
+
export { createSeparatedArrayParser, } from './separatedArrayParser.js';
|
|
25
|
+
export { createSeparatedNonEmptyArrayParser, } from './separatedNonEmptyArrayParser.js';
|
|
26
|
+
export { createLookaheadParser, } from './lookaheadParser.js';
|
|
27
|
+
export { createNegativeLookaheadParser, } from './negativeLookaheadParser.js';
|
|
28
|
+
export { createElementTerminatedSequenceParser, } from './elementTerminatedSequenceParser.js';
|
|
29
|
+
export { createElementTerminatedSequenceArrayParser, } from './elementTerminatedSequenceArrayParser.js';
|
|
30
|
+
export { createElementTerminatedArrayParserUnsafe, } from './elementTerminatedArrayParser.js';
|
|
31
|
+
export { createSequenceTerminatedSequenceParser, } from './sequenceTerminatedSequenceParser.js';
|
|
32
|
+
export { createQuantifierParser, } from './quantifierParser.js';
|
|
33
|
+
export { createSkipToParser, } from './skipToParser.js';
|
|
34
|
+
export { createDebugLogInputParser, } from './debugLogInputParser.js';
|
|
35
|
+
export { createElementSwitchParser, } from './exactElementSwitchParser.js';
|
|
36
|
+
export { createParserConsumedSequenceParser, } from './parserConsumedSequenceParser.js';
|
|
18
37
|
export { runUnparser, } from './unparser.js';
|
|
38
|
+
export { WriteLater, WriteEarlier, UnparserContextImplementation, } from './unparserContext.js';
|
|
19
39
|
export { createArrayUnparser, } from './arrayUnparser.js';
|
|
20
40
|
export { createSequenceUnparser, } from './sequenceUnparser.js';
|
|
41
|
+
export { createRegExpParser, } from './regexpParser.js';
|