Skip to content

Commit c690b6f

Browse files
committed
Separate out package to parse repl.txt to AST
1 parent fda8f6e commit c690b6f

File tree

23 files changed

+942
-7
lines changed

23 files changed

+942
-7
lines changed
Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2018 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# Parse
22+
23+
> Parse the contents of a `repl.txt` file and return an abstract syntax tree (AST) representation.
24+
25+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
26+
27+
<section class="intro">
28+
29+
</section>
30+
31+
<!-- /.intro -->
32+
33+
<!-- Package usage documentation. -->
34+
35+
<section class="usage">
36+
37+
## Usage
38+
39+
```javascript
40+
var parse = require( '@stdlib/_tools/repl-txt/parse' );
41+
```
42+
43+
#### parse( repl )
44+
45+
Parses the contents of a `repl.txt` file and returns an abstract syntax tree (AST) representation.
46+
47+
```javascript
48+
var repl = [
49+
'{{alias}}( value )',
50+
' Tests if a value is a number.',
51+
'',
52+
' Parameters',
53+
' ----------',
54+
' value: any',
55+
' Value to test.',
56+
'',
57+
' Returns',
58+
' -------',
59+
' bool: boolean',
60+
' Boolean indicating whether value is a number.',
61+
'',
62+
' Examples',
63+
' --------',
64+
' > var bool = {{alias}}( 3.14 )',
65+
' true',
66+
' > bool = {{alias}}( new Number( 3.14 ) )',
67+
' true',
68+
' > bool = {{alias}}( NaN )',
69+
' true',
70+
' > bool = {{alias}}( null )',
71+
' false',
72+
'',
73+
' See Also',
74+
' --------'
75+
].join( '\n' );
76+
77+
var ast = parse( repl );
78+
/* returns
79+
[
80+
{
81+
'raw': '...',
82+
'idx': 0,
83+
'last': true,
84+
'start': 1,
85+
'signature': {
86+
'name': '{{alias}}',
87+
'parameters': [...],
88+
'raw': '{{alias}}( value )',
89+
'start': 1,
90+
'end': 1
91+
},
92+
'description': {
93+
'raw': ' Tests if a value is a number.\n',
94+
'start': 2,
95+
'end': 2
96+
},
97+
'sections': [...],
98+
'end': 23
99+
}
100+
]
101+
*/
102+
```
103+
104+
</section>
105+
106+
<!-- /.usage -->
107+
108+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
109+
110+
<section class="notes">
111+
112+
</section>
113+
114+
<!-- /.notes -->
115+
116+
<!-- Package usage examples. -->
117+
118+
<section class="examples">
119+
120+
## Examples
121+
122+
<!-- eslint no-undef: "error" -->
123+
124+
```javascript
125+
var resolve = require( 'path' ).resolve;
126+
var readFileSync = require( '@stdlib/fs/read-file' ).sync;
127+
var parse = require( '@stdlib/_tools/repl-txt/parse' );
128+
129+
var repl = readFileSync( resolve( __dirname, './fixtures/repl.txt' ) ).toString();
130+
console.log( repl );
131+
132+
var ast = parse( repl );
133+
console.log( ast );
134+
```
135+
136+
</section>
137+
138+
<!-- /.examples -->
139+
140+
<!-- Section for describing a command-line interface. -->
141+
142+
* * *
143+
144+
<section class="cli">
145+
146+
## CLI
147+
148+
<!-- CLI usage documentation. -->
149+
150+
<section class="usage">
151+
152+
### Usage
153+
154+
```bash
155+
Usage: parse-repl-txt [options] [str]
156+
157+
Options:
158+
159+
-h, --help Print this message.
160+
-V, --version Print the package version.
161+
```
162+
163+
</section>
164+
165+
<!-- /.usage -->
166+
167+
<!-- CLI usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
168+
169+
<section class="notes">
170+
171+
### Notes
172+
173+
- Results are printed to `stdout` as JavaScript object notation ([JSON][json]).
174+
175+
</section>
176+
177+
<!-- /.notes -->
178+
179+
<!-- CLI usage examples. -->
180+
181+
<section class="examples">
182+
183+
### Examples
184+
185+
```bash
186+
$ parse-repl-txt "$(cat ./docs/repl.txt)"
187+
```
188+
189+
</section>
190+
191+
<!-- /.examples -->
192+
193+
</section>
194+
195+
<!-- /.cli -->
196+
197+
<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
198+
199+
<section class="references">
200+
201+
</section>
202+
203+
<!-- /.references -->
204+
205+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
206+
207+
<section class="links">
208+
209+
[json]: http://json.org/
210+
211+
</section>
212+
213+
<!-- /.links -->
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#!/usr/bin/env node
2+
3+
/**
4+
* @license Apache-2.0
5+
*
6+
* Copyright (c) 2018 The Stdlib Authors.
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
*/
20+
21+
'use strict';
22+
23+
// MODULES //
24+
25+
var resolve = require( 'path' ).resolve;
26+
var readFileSync = require( '@stdlib/fs/read-file' ).sync;
27+
var stdin = require( '@stdlib/process/read-stdin' );
28+
var CLI = require( '@stdlib/tools/cli' );
29+
var parse = require( './../lib' );
30+
31+
32+
// MAIN //
33+
34+
/**
35+
* Main execution sequence.
36+
*
37+
* @private
38+
* @returns {void}
39+
*/
40+
function main() {
41+
var args;
42+
var repl;
43+
var cli;
44+
45+
// Create a command-line interface:
46+
cli = new CLI({
47+
'pkg': require( './../package.json' ),
48+
'options': require( './../etc/cli_opts.json' ),
49+
'help': readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), {
50+
'encoding': 'utf8'
51+
})
52+
});
53+
54+
// Get any provided command-line arguments:
55+
args = cli.args();
56+
57+
// Check if we are receiving data from `stdin`...
58+
if ( !process.stdin.isTTY ) {
59+
return stdin( onRead );
60+
}
61+
repl = args[ 0 ];
62+
process.nextTick( next );
63+
64+
/**
65+
* Callback invoked upon reading from `stdin`.
66+
*
67+
* @private
68+
* @param {(Error|null)} error - error object
69+
* @param {Buffer} data - data
70+
* @returns {void}
71+
*/
72+
function onRead( error, data ) {
73+
if ( error ) {
74+
console.error( 'Error: %s', error.message ); // eslint-disable-line no-console
75+
return cli.exit( 1 );
76+
}
77+
repl = data.toString();
78+
next();
79+
}
80+
81+
/**
82+
* Parses the contents of a `repl.txt` file and prints an abstract syntax tree (AST) representation.
83+
*
84+
* @private
85+
* @returns {void}
86+
*/
87+
function next() {
88+
var ast;
89+
90+
ast = parse( repl );
91+
console.log( JSON.stringify( ast ) );
92+
}
93+
}
94+
95+
main();
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
Usage: parse-repl-txt [options] [str]
3+
4+
Options:
5+
6+
-h, --help Print this message.
7+
-V, --version Print the package version.
8+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"boolean": [
3+
"help",
4+
"version"
5+
],
6+
"alias": {
7+
"help": [
8+
"h"
9+
],
10+
"version": [
11+
"V"
12+
]
13+
}
14+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2+
{{alias}}( str, words[, ignoreCase] )
3+
Removes all occurrences of the given words from a `string`.
4+
5+
Parameters
6+
----------
7+
str: string
8+
Input string.
9+
10+
words: Array<string>
11+
Array of words to be removed.
12+
13+
ignoreCase: boolean (optional)
14+
Boolean indicating whether to perform a case-insensitive operation.
15+
Default: `false`.
16+
17+
Returns
18+
-------
19+
out: string
20+
String with words removed.
21+
22+
Examples
23+
--------
24+
> var out = {{alias}}( 'beep boop Foo bar', [ 'boop', 'foo' ] )
25+
'beep Foo bar'
26+
27+
// Case-insensitive:
28+
> out = {{alias}}( 'beep boop Foo bar', [ 'boop', 'foo' ], true )
29+
'beep bar'
30+
31+
See Also
32+
--------
33+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2018 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
var resolve = require( 'path' ).resolve;
22+
var readFileSync = require( '@stdlib/fs/read-file' ).sync;
23+
var parse = require( './../lib' );
24+
25+
var repl = readFileSync( resolve( __dirname, './fixtures/repl.txt' ) ).toString();
26+
console.log( repl );
27+
28+
var ast = parse( repl );
29+
console.log( ast );

lib/node_modules/@stdlib/_tools/repl-txt/validate/lib/extract_signature.js renamed to lib/node_modules/@stdlib/_tools/repl-txt/parse/lib/extract_signature.js

File renamed without changes.

lib/node_modules/@stdlib/_tools/repl-txt/validate/lib/extract_tags.js renamed to lib/node_modules/@stdlib/_tools/repl-txt/parse/lib/extract_tags.js

File renamed without changes.

lib/node_modules/@stdlib/_tools/repl-txt/validate/lib/extract_type.js renamed to lib/node_modules/@stdlib/_tools/repl-txt/parse/lib/extract_type.js

File renamed without changes.

lib/node_modules/@stdlib/_tools/repl-txt/validate/lib/generate_code_array.js renamed to lib/node_modules/@stdlib/_tools/repl-txt/parse/lib/generate_code_array.js

File renamed without changes.

0 commit comments

Comments
 (0)