Skip to content

Commit

Permalink
feat: refactor the file structure of parser
Browse files Browse the repository at this point in the history
  • Loading branch information
Solo-steven committed Oct 27, 2024
1 parent 2611046 commit 3e2cbbf
Show file tree
Hide file tree
Showing 28 changed files with 8,287 additions and 7,649 deletions.
4 changes: 2 additions & 2 deletions web-infras/parser/src/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { parse } from "@/src/index";
import { transformSyntaxKindToLiteral } from "../tests/parserRunner/helpers/transform";
import fs from "fs";
import path from "path";
import { ParserPlugin } from "./parser/config";
// import { ParserPlugin } from "./parser/config";
const code = fs.readFileSync(path.join(__dirname, "test.ts")).toString();
// console.log(code);
// const writePath = path.join(__dirname, "test.json");
Expand Down Expand Up @@ -50,7 +50,7 @@ function printLexer(code: string) {
);
}
function printParser(code: string) {
const ast = parse(code, { sourceType: "module", plugins: [ParserPlugin.TypeScript] });
const ast = parse(code, { sourceType: "module", plugins: [] });
transformSyntaxKindToLiteral(ast);
const astJsonString = JSON.stringify(ast, null, 4);
fs.writeFileSync("./test.json", astJsonString);
Expand Down
6 changes: 3 additions & 3 deletions web-infras/parser/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createParser } from "@/src/parser";
import { Parser } from "@/src/parser";
import { createLexer } from "@/src/lexer";
import { Token } from "@/src/lexer/type";
import { SyntaxKinds } from "web-infra-common";
Expand All @@ -7,8 +7,8 @@ import { createErrorHandler } from "./errorHandler";

export function parse(code: string, config?: ParserUserConfig) {
const errorHandler = createErrorHandler(code);
const parser = createParser(code, errorHandler, config);
const program = parser.parse();
const parser = new Parser(code, errorHandler, config);
const program = parser.parseProgram();
if (errorHandler.haveError()) {
throw new Error(errorHandler.formatErrorString());
}
Expand Down
2 changes: 2 additions & 0 deletions web-infras/parser/src/lexer/lexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ const KeywordLiteralSet = new Set([
...LexicalLiteral.UndefinbedLiteral,
]);

export type Lexer = ReturnType<typeof createLexer>;

export function createLexer(code: string) {
/**
* state and context for lexer,
Expand Down
1 change: 1 addition & 0 deletions web-infras/parser/src/parser/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const defaultConfig: ParserConfig = {
allowUndeclaredExports: false,
plugins: [],
};

export function getConfigFromUserInput(config?: ParserUserConfig): ParserConfig {
return {
...defaultConfig,
Expand Down
Loading

0 comments on commit 3e2cbbf

Please sign in to comment.