jlex 1.0.1 → 1.1.1

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/README.md CHANGED
@@ -102,7 +102,9 @@ When you execute the former program, you get:
102
102
  ]
103
103
  ```
104
104
 
105
- Here is a description of the lexer object:
105
+ ## The Lexical Analyzer Object
106
+
107
+ Here is a description of the attributes of the lexer object:
106
108
 
107
109
  ```js
108
110
  {
@@ -1,6 +1,7 @@
1
+ comment [/][*](.|[\r\n])*?[*][/]
1
2
  %%
2
- \s+ /* skip whitespace */
3
+ \s+|{comment} /* skip whitespace */
3
4
  [0-9]+ return 'NUMBER';
4
- "-" return '-';
5
+ [-+*/] return 'OPERATOR';
5
6
  <<EOF>> return 'EOF';
6
7
  . return 'INVALID';
package/examples/main.js CHANGED
@@ -1,5 +1,6 @@
1
1
  const lex = require("./example");
2
- lex.setInput("2\n-\n3")
2
+ const input = process.argv[2] || "2\n-/* a comment*/\n3";
3
+ lex.setInput(input);
3
4
 
4
5
  const results = [];
5
6
 
@@ -9,28 +10,3 @@ results.push({ type: lex.lex(), lexeme: lex.yytext, loc: lex.yylloc });
9
10
  results.push({ type: lex.lex(), lexeme: lex.yytext, loc: lex.yylloc });
10
11
 
11
12
  console.log(results);
12
- /*
13
- ➜ examples git:(main) ✗ node main.js
14
- [
15
- {
16
- type: 'NUMBER',
17
- lexeme: '2',
18
- loc: { first_line: 1, last_line: 1, first_column: 0, last_column: 1 }
19
- },
20
- {
21
- type: '-',
22
- lexeme: '-',
23
- loc: { first_line: 2, last_line: 2, first_column: 0, last_column: 1 }
24
- },
25
- {
26
- type: 'NUMBER',
27
- lexeme: '3',
28
- loc: { first_line: 3, last_line: 3, first_column: 0, last_column: 1 }
29
- },
30
- {
31
- type: 'EOF',
32
- lexeme: '',
33
- loc: { first_line: 3, last_line: 3, first_column: 1, last_column: 1 }
34
- }
35
- ]
36
- */
package/jlex.js CHANGED
@@ -2,11 +2,33 @@
2
2
  // replace example by the name of the generated module
3
3
  const fs = require("fs");
4
4
  const { execSync } = require('child_process');
5
- const fileName = process.argv[2];
6
- console.log("Processing file:", fileName);
7
- execSync(`npx jison-lex ${fileName}.l`, { encoding: 'utf-8' });
8
- let lexerStr = fs.readFileSync(`${fileName}.js`, "utf8").toString();
9
- //console.log("Processing file:", lexerStr);
10
- let lexerModule = lexerStr.replace(new RegExp(`var ${fileName} =`, 'g'), `\nmodule.exports =`);
11
- console.log("Writing file:", `${fileName}.js`);
12
- fs.writeFileSync(`${fileName}.js`, lexerModule);
5
+ const { Command } = require('commander')
6
+ const packageJson = require('./package.json')
7
+ const path = require('path');
8
+ const program = new Command();
9
+
10
+ program
11
+ .version(packageJson.version)
12
+ .description('A tiny wrapper around jison-lex that allows you to use jison-lex as a standalone (flex like) processor.')
13
+ .option("-o <fileName>", "Output file name")
14
+ .usage("[options] <filename>");
15
+
16
+ program.parse(process.argv);
17
+ const options = program.opts();
18
+
19
+ const fileName = program.args[0];
20
+
21
+ const {dir,name } = path.parse(fileName); // { dir, base, ext, name }
22
+ const outputFileName = options.o || path.join(dir, `${name}.js`);
23
+ const outputParse = path.parse(outputFileName);
24
+
25
+ const shellCommand = `npx jison-lex ${fileName} -o ${outputFileName}`;
26
+ try {
27
+ execSync(shellCommand, { encoding: 'utf-8' });
28
+ let lexerStr = fs.readFileSync(outputFileName, "utf8").toString();
29
+ let lexerModule = lexerStr.replace(new RegExp(`var ${outputParse.name} =`), `\nmodule.exports =`);
30
+ console.log("Writing file:", outputFileName);
31
+ fs.writeFileSync(outputFileName, lexerModule);
32
+ } catch (error) {
33
+ console.error("Error:", error.message);
34
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jlex",
3
- "version": "1.0.1",
3
+ "version": "1.1.1",
4
4
  "description": "A wrapper around jison-lex to make it work as a standalone program like Flex",
5
5
  "keywords": [
6
6
  "jison-lex",
@@ -27,10 +27,10 @@
27
27
  },
28
28
  "scripts": {
29
29
  "test": "echo \"Error: no test specified\" && exit 1",
30
- "example": "cd examples && ../jlex.js example"
30
+ "example": "./jlex.js examples/example.l && node examples/main.js"
31
31
  },
32
32
  "dependencies": {
33
+ "commander": "^11.0.3",
33
34
  "jison": "^0.4.18"
34
- },
35
- "devDependencies": {}
35
+ }
36
36
  }