jlex 1.0.0
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 +117 -0
- package/examples/example.l +6 -0
- package/examples/main.js +36 -0
- package/jlex.js +12 -0
- package/package.json +19 -0
package/README.md
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
## Using jison-lex
|
|
2
|
+
|
|
3
|
+
I have written a small wrapper around `jison-lex`that allows you to use the script
|
|
4
|
+
`jlex` as standalone (`flex`like) processor.
|
|
5
|
+
Here is an example:
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
➜ only-lexer git:(main) ✗ ./jlex.js example
|
|
9
|
+
Processing file: example
|
|
10
|
+
Writing file: example.js
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
```
|
|
14
|
+
➜ only-lexer git:(main) ✗ npx jison-lex example.l -o example.js
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
Modify `example.js` to export the generated value `example`:
|
|
18
|
+
|
|
19
|
+
```js
|
|
20
|
+
module.exports = example;
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
Then we can use it:
|
|
24
|
+
|
|
25
|
+
```
|
|
26
|
+
➜ only-lexer git:(main) ✗ node
|
|
27
|
+
Welcome to Node.js v25.6.0.
|
|
28
|
+
Type ".help" for more information.
|
|
29
|
+
> lex = require("./example.js")
|
|
30
|
+
{
|
|
31
|
+
EOF: 1,
|
|
32
|
+
parseError: [Function: parseError],
|
|
33
|
+
setInput: [Function: setInput],
|
|
34
|
+
input: [Function: input],
|
|
35
|
+
unput: [Function: unput],
|
|
36
|
+
more: [Function: more],
|
|
37
|
+
reject: [Function: reject],
|
|
38
|
+
less: [Function: less],
|
|
39
|
+
pastInput: [Function: pastInput],
|
|
40
|
+
upcomingInput: [Function: upcomingInput],
|
|
41
|
+
showPosition: [Function: showPosition],
|
|
42
|
+
test_match: [Function: test_match],
|
|
43
|
+
next: [Function: next],
|
|
44
|
+
lex: [Function: lex],
|
|
45
|
+
begin: [Function: begin],
|
|
46
|
+
popState: [Function: popState],
|
|
47
|
+
_currentRules: [Function: _currentRules],
|
|
48
|
+
topState: [Function: topState],
|
|
49
|
+
pushState: [Function: pushState],
|
|
50
|
+
stateStackSize: [Function: stateStackSize],
|
|
51
|
+
options: { moduleName: 'example' },
|
|
52
|
+
performAction: [Function: anonymous],
|
|
53
|
+
rules: [ /^(?:\s+)/, /^(?:[0-9]+)/, /^(?:-)/, /^(?:$)/, /^(?:.)/ ],
|
|
54
|
+
conditions: { INITIAL: { rules: [Array], inclusive: true } }
|
|
55
|
+
}
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
Now:
|
|
59
|
+
|
|
60
|
+
```js
|
|
61
|
+
> lex.setInput("2 - 3")
|
|
62
|
+
{
|
|
63
|
+
...
|
|
64
|
+
offset: 0
|
|
65
|
+
}
|
|
66
|
+
> lex.lex()
|
|
67
|
+
'NUMBER'
|
|
68
|
+
> lex.lex()
|
|
69
|
+
'-'
|
|
70
|
+
> lex.lex()
|
|
71
|
+
'NUMBER'
|
|
72
|
+
> lex.yytext
|
|
73
|
+
'3'
|
|
74
|
+
> lex.yylloc
|
|
75
|
+
{ first_line: 1, last_line: 1, first_column: 4, last_column: 5 }
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
## The program `jlex.js`
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
```
|
|
82
|
+
> lex = require("./example")
|
|
83
|
+
{
|
|
84
|
+
EOF: 1,
|
|
85
|
+
parseError: [Function: parseError],
|
|
86
|
+
setInput: [Function: setInput],
|
|
87
|
+
input: [Function: input],
|
|
88
|
+
unput: [Function: unput],
|
|
89
|
+
more: [Function: more],
|
|
90
|
+
reject: [Function: reject],
|
|
91
|
+
less: [Function: less],
|
|
92
|
+
pastInput: [Function: pastInput],
|
|
93
|
+
upcomingInput: [Function: upcomingInput],
|
|
94
|
+
showPosition: [Function: showPosition],
|
|
95
|
+
test_match: [Function: test_match],
|
|
96
|
+
next: [Function: next],
|
|
97
|
+
lex: [Function: lex],
|
|
98
|
+
begin: [Function: begin],
|
|
99
|
+
popState: [Function: popState],
|
|
100
|
+
_currentRules: [Function: _currentRules],
|
|
101
|
+
topState: [Function: topState],
|
|
102
|
+
pushState: [Function: pushState],
|
|
103
|
+
stateStackSize: [Function: stateStackSize],
|
|
104
|
+
options: { moduleName: 'example' },
|
|
105
|
+
performAction: [Function: anonymous],
|
|
106
|
+
rules: [ /^(?:\s+)/, /^(?:[0-9]+)/, /^(?:-)/, /^(?:$)/, /^(?:.)/ ],
|
|
107
|
+
conditions: { INITIAL: { rules: [Array], inclusive: true } }
|
|
108
|
+
}
|
|
109
|
+
> lex.setInput("2 - 3")
|
|
110
|
+
> lex.lex()
|
|
111
|
+
'NUMBER'
|
|
112
|
+
> lex.yytext
|
|
113
|
+
'2'
|
|
114
|
+
> lex.yylloc
|
|
115
|
+
{ first_line: 1, last_line: 1, first_column: 0, last_column: 1 }
|
|
116
|
+
>
|
|
117
|
+
```
|
package/examples/main.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
const lex = require("./example");
|
|
2
|
+
lex.setInput("2\n-\n3")
|
|
3
|
+
|
|
4
|
+
const results = [];
|
|
5
|
+
|
|
6
|
+
results.push({ type: lex.lex(), lexeme: lex.yytext, loc: lex.yylloc });
|
|
7
|
+
results.push({ type: lex.lex(), lexeme: lex.yytext, loc: lex.yylloc });
|
|
8
|
+
results.push({ type: lex.lex(), lexeme: lex.yytext, loc: lex.yylloc });
|
|
9
|
+
results.push({ type: lex.lex(), lexeme: lex.yytext, loc: lex.yylloc });
|
|
10
|
+
|
|
11
|
+
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
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// replace example by the name of the generated module
|
|
3
|
+
const fs = require("fs");
|
|
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);
|
package/package.json
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "jlex",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"main": "jlex.js",
|
|
5
|
+
"description": "A wrapper around jison-lex to make it work as a standalone program like Flex",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "echo \"Error: no test specified\" && exit 1",
|
|
8
|
+
"example": "cd examples && ../jlex.js example"
|
|
9
|
+
},
|
|
10
|
+
"bin": {
|
|
11
|
+
"jlex": "jlex.js"
|
|
12
|
+
},
|
|
13
|
+
"keywords": ["jison-lex", "jison", "flex"],
|
|
14
|
+
"author": "Casiano Rodriguez Leon <crguezl@ull.edu.es> (https://crguezl.github.io/)",
|
|
15
|
+
"license": "ISC",
|
|
16
|
+
"dependencies": {
|
|
17
|
+
"jison": "^0.4.18"
|
|
18
|
+
}
|
|
19
|
+
}
|