Skip to content

Commit

Permalink
refactor: only expose subset of parser api
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanPiercey committed Mar 18, 2022
1 parent c1b8bcf commit b7f3d3e
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 19 deletions.
4 changes: 2 additions & 2 deletions src/__tests__/bench.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ const COMPARE = process.env.COMPARE;
const { createParser } = API;
suite.add(entry, () => {
const parser = createParser({});
parser.parse(src, filename);
parser.parse(src);
});

if (compareModule) {
const { createParser } = compareModule;
suite.add(`${entry} #${COMPARE}`, () => {
const parser = createParser({});
parser.parse(src, filename);
parser.parse(src);
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ for (const entry of fs.readdirSync(FIXTURES)) {
addValueRange(range.block ? `scriptlet:block` : `scriptlet`, range);
},
});
parser.parse(src, filename);
parser.parse(src);

let result = "";
for (let line = 0; line < partsByLine.length; line++) {
Expand Down
22 changes: 8 additions & 14 deletions src/core/Parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ export class Parser {
public pos!: number;
public maxPos!: number;
public data!: string;
public filename!: string;
public activeState!: StateDefinition;
public activeRange!: Meta;
public forward!: boolean;
Expand All @@ -47,16 +46,6 @@ export class Parser {

constructor(public handlers: Handlers) {
this.handlers = handlers;
this.reset();
}

reset() {
this.forward = this.isConcise = true;
this.pos = this.maxPos = this.textPos = -1;
this.data = this.filename = this.indent = "";
this.beginMixedMode = this.endingMixedModeAtEOL = false;
this.lines = this.activeTag = this.activeAttr = undefined;
this.enterState(STATE.CONCISE_HTML_CONTENT);
}

read(range: Range) {
Expand Down Expand Up @@ -291,16 +280,21 @@ export class Parser {
this.pos += ahead;
}

parse(data: string, filename: string) {
this.data = data;
this.filename = filename;
parse(data: string) {
const maxPos = (this.maxPos = data.length);
this.data = data;
this.indent = "";
this.textPos = -1;
this.forward = this.isConcise = true;
this.beginMixedMode = this.endingMixedModeAtEOL = false;
this.lines = this.activeTag = this.activeAttr = undefined;

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

while (this.pos < maxPos) {
const code = data.charCodeAt(this.pos);
Expand Down
35 changes: 33 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type Handlers, Parser } from "./internal";
import { type Handlers, type Range, Parser } from "./internal";
export {
OpenTagEnding,
type Handlers,
Expand All @@ -8,6 +8,37 @@ export {
type Range,
} from "./internal";

/**
* Creates a new Marko parser.
*/
export function createParser(handlers: Handlers) {
return new Parser(handlers);
// Expose a subset of the parser api.
const parser = new Parser(handlers);

return {
/**
* Parses code and calls the provided handlers.
*/
parse(code: string) {
return parser.parse(code);
},
/**
* Given an offset range in the current source code, reads and returns the substring in the input code.
*/
read(range: Range) {
return parser.read(range);
},
/**
* Given a offset in the current source code, returns a Position object with line & character information.
*/
positionAt(index: number) {
return parser.positionAt(index);
},
/**
* Given a offset range in the current source code, returns a Location object with a start & end position information.
*/
locationAt(range: Range) {
return parser.locationAt(range);
},
};
}

0 comments on commit b7f3d3e

Please sign in to comment.