This repository has been archived by the owner on Apr 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add readtoken to recognize pipe tokens
- Loading branch information
KernelDeimos
committed
May 10, 2023
1 parent
0151b84
commit 1b2638e
Showing
5 changed files
with
160 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
export class Pipe { | ||
constructor () { | ||
this.readableStream = new ReadableStream({ | ||
start: controller => { | ||
this.readController = controller; | ||
} | ||
}); | ||
this.writableStream = new WritableStream({ | ||
start: controller => { | ||
this.writableController = controller; | ||
}, | ||
write: item => { | ||
this.readController.enqueue(item); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// [reference impl](https://github.com/brgl/busybox/blob/master/shell/ash.c) | ||
|
||
const list_ws = [' ', '\n', '\t']; | ||
const list_recorded_tokens = [ | ||
'|','>','<','&',';','(',')', | ||
]; | ||
const list_stoptoken = [ | ||
'|','>','<','&','\\','#',';','(',')', | ||
...list_ws | ||
]; | ||
|
||
export const TOKENS = {}; | ||
for ( const k of list_recorded_tokens ) { | ||
TOKENS[k] = {}; | ||
} | ||
|
||
export const readtoken = str => { | ||
let state = null; | ||
let buffer = ''; | ||
let quoteType = ''; | ||
const tokens = []; | ||
|
||
const actions = { | ||
endToken: () => { | ||
tokens.push(buffer); | ||
buffer = ''; | ||
} | ||
}; | ||
|
||
const states = { | ||
start: i => { | ||
if ( list_ws.includes(str[i]) ) { | ||
return; | ||
} | ||
if ( str[i] === '#' ) return str.length; | ||
if ( list_recorded_tokens.includes(str[i]) ) { | ||
tokens.push(TOKENS[str[i]]); | ||
return; | ||
} | ||
if ( str[i] === '"' || str[i] === "'" ) { | ||
state = states.quote; | ||
quoteType = str[i]; | ||
return; | ||
} | ||
state = states.text; | ||
return i; // prevent increment | ||
}, | ||
text: i => { | ||
if ( str[i] === '"' || str[i] === "'" ) { | ||
state = states.quote; | ||
quoteType = str[i]; | ||
return; | ||
} | ||
if ( list_stoptoken.includes(str[i]) ) { | ||
state = states.start; | ||
actions.endToken(); | ||
return i; // prevent increment | ||
} | ||
buffer += str[i]; | ||
}, | ||
quote: i => { | ||
if ( str[i] === '\\' ) { | ||
state = states.quote_esc; | ||
return; | ||
} | ||
if ( str[i] === quoteType ) { | ||
state = states.text; | ||
return; | ||
} | ||
buffer += str[i]; | ||
}, | ||
quote_esc: i => { | ||
buffer += str[i]; | ||
state = states.quote; | ||
} | ||
}; | ||
state = states.start; | ||
for ( let i=0 ; i < str.length ; ) { | ||
let newI = state(i); | ||
i = newI !== undefined ? newI : i+1; | ||
} | ||
|
||
if ( buffer !== '' ) actions.endToken(); | ||
|
||
return tokens; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import assert from 'assert'; | ||
import { readtoken, TOKENS } from '../src/ansi-shell/readtoken.js'; | ||
|
||
describe('readtoken', () => { | ||
const tcases = [ | ||
{ | ||
desc: 'should accept unquoted string', | ||
input: 'asdf', | ||
expected: ['asdf'] | ||
}, | ||
{ | ||
desc: 'should accept leading spaces', | ||
input: ' asdf', | ||
expected: ['asdf'] | ||
}, | ||
{ | ||
desc: 'should accept trailing spaces', | ||
input: 'asdf ', | ||
expected: ['asdf'] | ||
}, | ||
{ | ||
desc: 'should expected quoted string', | ||
input: '"asdf"', | ||
expected: ['asdf'] | ||
}, | ||
{ | ||
desc: 'should recognize pipe with no whitespace', | ||
input: 'asdf|zxcv', | ||
expected: ['asdf', TOKENS['|'], 'zxcv'] | ||
}, | ||
{ | ||
desc: 'mixed quoted and unquoted should work', | ||
input: '"asdf" zxcv', | ||
expected: ['asdf', 'zxcv'] | ||
}, | ||
]; | ||
for ( const { desc, input, expected } of tcases ) { | ||
it(desc, () => { | ||
assert.deepEqual(readtoken(input), expected) | ||
}); | ||
} | ||
}) |