Skip to content

Commit b7f3d3e

Browse files
committed
refactor: only expose subset of parser api
1 parent c1b8bcf commit b7f3d3e

File tree

4 files changed

+44
-19
lines changed

4 files changed

+44
-19
lines changed

src/__tests__/bench.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,14 @@ const COMPARE = process.env.COMPARE;
3939
const { createParser } = API;
4040
suite.add(entry, () => {
4141
const parser = createParser({});
42-
parser.parse(src, filename);
42+
parser.parse(src);
4343
});
4444

4545
if (compareModule) {
4646
const { createParser } = compareModule;
4747
suite.add(`${entry} #${COMPARE}`, () => {
4848
const parser = createParser({});
49-
parser.parse(src, filename);
49+
parser.parse(src);
5050
});
5151
}
5252

src/__tests__/main.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ for (const entry of fs.readdirSync(FIXTURES)) {
117117
addValueRange(range.block ? `scriptlet:block` : `scriptlet`, range);
118118
},
119119
});
120-
parser.parse(src, filename);
120+
parser.parse(src);
121121

122122
let result = "";
123123
for (let line = 0; line < partsByLine.length; line++) {

src/core/Parser.ts

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ export class Parser {
3232
public pos!: number;
3333
public maxPos!: number;
3434
public data!: string;
35-
public filename!: string;
3635
public activeState!: StateDefinition;
3736
public activeRange!: Meta;
3837
public forward!: boolean;
@@ -47,16 +46,6 @@ export class Parser {
4746

4847
constructor(public handlers: Handlers) {
4948
this.handlers = handlers;
50-
this.reset();
51-
}
52-
53-
reset() {
54-
this.forward = this.isConcise = true;
55-
this.pos = this.maxPos = this.textPos = -1;
56-
this.data = this.filename = this.indent = "";
57-
this.beginMixedMode = this.endingMixedModeAtEOL = false;
58-
this.lines = this.activeTag = this.activeAttr = undefined;
59-
this.enterState(STATE.CONCISE_HTML_CONTENT);
6049
}
6150

6251
read(range: Range) {
@@ -291,16 +280,21 @@ export class Parser {
291280
this.pos += ahead;
292281
}
293282

294-
parse(data: string, filename: string) {
295-
this.data = data;
296-
this.filename = filename;
283+
parse(data: string) {
297284
const maxPos = (this.maxPos = data.length);
285+
this.data = data;
286+
this.indent = "";
287+
this.textPos = -1;
288+
this.forward = this.isConcise = true;
289+
this.beginMixedMode = this.endingMixedModeAtEOL = false;
290+
this.lines = this.activeTag = this.activeAttr = undefined;
298291

299292
// Skip the byte order mark (BOM) sequence
300293
// at the beginning of the file if there is one:
301294
// - https://en.wikipedia.org/wiki/Byte_order_mark
302295
// > The Unicode Standard permits the BOM in UTF-8, but does not require or recommend its use.
303296
this.pos = data.charCodeAt(0) === 0xfeff ? 1 : 0;
297+
this.enterState(STATE.CONCISE_HTML_CONTENT);
304298

305299
while (this.pos < maxPos) {
306300
const code = data.charCodeAt(this.pos);

src/index.ts

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { type Handlers, Parser } from "./internal";
1+
import { type Handlers, type Range, Parser } from "./internal";
22
export {
33
OpenTagEnding,
44
type Handlers,
@@ -8,6 +8,37 @@ export {
88
type Range,
99
} from "./internal";
1010

11+
/**
12+
* Creates a new Marko parser.
13+
*/
1114
export function createParser(handlers: Handlers) {
12-
return new Parser(handlers);
15+
// Expose a subset of the parser api.
16+
const parser = new Parser(handlers);
17+
18+
return {
19+
/**
20+
* Parses code and calls the provided handlers.
21+
*/
22+
parse(code: string) {
23+
return parser.parse(code);
24+
},
25+
/**
26+
* Given an offset range in the current source code, reads and returns the substring in the input code.
27+
*/
28+
read(range: Range) {
29+
return parser.read(range);
30+
},
31+
/**
32+
* Given a offset in the current source code, returns a Position object with line & character information.
33+
*/
34+
positionAt(index: number) {
35+
return parser.positionAt(index);
36+
},
37+
/**
38+
* Given a offset range in the current source code, returns a Location object with a start & end position information.
39+
*/
40+
locationAt(range: Range) {
41+
return parser.locationAt(range);
42+
},
43+
};
1344
}

0 commit comments

Comments
 (0)