@yyds-lang/lexer 0.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.
@@ -0,0 +1,2 @@
1
+ import { t as tokenize } from "./tokenize-DUjh2gNM.mjs";
2
+ export { tokenize };
package/dist/index.mjs ADDED
@@ -0,0 +1,2 @@
1
+ import { t as tokenize } from "./tokenize-CmVwIkkB.mjs";
2
+ export { tokenize };
@@ -0,0 +1,145 @@
1
+ //#region src/lexer/tokenize.ts
2
+ const SYMBOLS = new Set([
3
+ "{",
4
+ "}",
5
+ "[",
6
+ "]",
7
+ "|",
8
+ "/",
9
+ ".",
10
+ "@",
11
+ "%",
12
+ "=",
13
+ "-",
14
+ ">",
15
+ "+"
16
+ ]);
17
+ function createPosition(line, column, offset) {
18
+ return {
19
+ line,
20
+ column,
21
+ offset
22
+ };
23
+ }
24
+ function createRange(start, end) {
25
+ return {
26
+ start,
27
+ end
28
+ };
29
+ }
30
+ function isAlphaNumeric(char) {
31
+ return /[A-Za-z0-9_]/.test(char);
32
+ }
33
+ function tokenize(source) {
34
+ const tokens = [];
35
+ let index = 0;
36
+ let line = 1;
37
+ let column = 1;
38
+ while (index < source.length) {
39
+ const char = source[index];
40
+ if (char === "\n") {
41
+ index += 1;
42
+ line += 1;
43
+ column = 1;
44
+ continue;
45
+ }
46
+ if (char === " " || char === " " || char === "\r") {
47
+ index += 1;
48
+ column += 1;
49
+ continue;
50
+ }
51
+ if (char === "/" && source[index + 1] === "/") {
52
+ while (index < source.length && source[index] !== "\n") {
53
+ index += 1;
54
+ column += 1;
55
+ }
56
+ continue;
57
+ }
58
+ const start = createPosition(line, column, index);
59
+ if (char === "\"") {
60
+ let value = "\"";
61
+ index += 1;
62
+ column += 1;
63
+ while (index < source.length && source[index] !== "\"") {
64
+ value += source[index];
65
+ index += 1;
66
+ column += 1;
67
+ }
68
+ if (source[index] === "\"") {
69
+ value += "\"";
70
+ index += 1;
71
+ column += 1;
72
+ }
73
+ tokens.push({
74
+ type: "string",
75
+ value,
76
+ range: createRange(start, createPosition(line, column, index))
77
+ });
78
+ continue;
79
+ }
80
+ if (/[0-9]/.test(char)) {
81
+ let value = "";
82
+ while (index < source.length && /[0-9.]/.test(source[index])) {
83
+ value += source[index];
84
+ index += 1;
85
+ column += 1;
86
+ }
87
+ tokens.push({
88
+ type: "number",
89
+ value,
90
+ range: createRange(start, createPosition(line, column, index))
91
+ });
92
+ continue;
93
+ }
94
+ if (/[A-Za-z_]/.test(char)) {
95
+ let value = "";
96
+ while (index < source.length && isAlphaNumeric(source[index])) {
97
+ value += source[index];
98
+ index += 1;
99
+ column += 1;
100
+ }
101
+ tokens.push({
102
+ type: "ident",
103
+ value,
104
+ range: createRange(start, createPosition(line, column, index))
105
+ });
106
+ continue;
107
+ }
108
+ if (char === "-" && source[index + 1] === ">") {
109
+ index += 2;
110
+ column += 2;
111
+ tokens.push({
112
+ type: "symbol",
113
+ value: "->",
114
+ range: createRange(start, createPosition(line, column, index))
115
+ });
116
+ continue;
117
+ }
118
+ if (SYMBOLS.has(char)) {
119
+ index += 1;
120
+ column += 1;
121
+ tokens.push({
122
+ type: "symbol",
123
+ value: char,
124
+ range: createRange(start, createPosition(line, column, index))
125
+ });
126
+ continue;
127
+ }
128
+ index += 1;
129
+ column += 1;
130
+ tokens.push({
131
+ type: "symbol",
132
+ value: char,
133
+ range: createRange(start, createPosition(line, column, index))
134
+ });
135
+ }
136
+ const eof = createPosition(line, column, index);
137
+ tokens.push({
138
+ type: "eof",
139
+ value: "",
140
+ range: createRange(eof, eof)
141
+ });
142
+ return tokens;
143
+ }
144
+ //#endregion
145
+ export { tokenize as t };
@@ -0,0 +1,6 @@
1
+ import { Token } from "@yyds-lang/ast/types";
2
+
3
+ //#region src/lexer/tokenize.d.ts
4
+ declare function tokenize(source: string): Token[];
5
+ //#endregion
6
+ export { tokenize as t };
@@ -0,0 +1,2 @@
1
+ import { t as tokenize } from "./tokenize-DUjh2gNM.mjs";
2
+ export { tokenize };
package/dist/types.mjs ADDED
@@ -0,0 +1,2 @@
1
+ import { t as tokenize } from "./tokenize-CmVwIkkB.mjs";
2
+ export { tokenize };
package/package.json ADDED
@@ -0,0 +1,32 @@
1
+ {
2
+ "name": "@yyds-lang/lexer",
3
+ "version": "0.0.0",
4
+ "description": "YYDS lexer package.",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "files": [
8
+ "dist"
9
+ ],
10
+ "publishConfig": {
11
+ "access": "public"
12
+ },
13
+ "dependencies": {
14
+ "@yyds-lang/ast": "0.0.0"
15
+ },
16
+ "devDependencies": {
17
+ "@types/node": "^24",
18
+ "typescript": "^5",
19
+ "vite-plus": "latest"
20
+ },
21
+ "exports": {
22
+ ".": "./dist/index.mjs",
23
+ "./types": "./dist/types.mjs",
24
+ "./package.json": "./package.json"
25
+ },
26
+ "scripts": {
27
+ "build": "vp pack",
28
+ "dev": "vp pack --watch",
29
+ "test": "vp test",
30
+ "check": "vp check"
31
+ }
32
+ }