Skip to content

Commit

Permalink
feat!: refactor a lot of stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
denizenging committed Oct 8, 2023
1 parent 163a004 commit 4fc49d1
Show file tree
Hide file tree
Showing 25 changed files with 1,001 additions and 2,127 deletions.
5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,15 @@
"cjs-fixup": "pnpm -F @slangroom/* exec sh -c \"echo '{\\\"type\\\":\\\"commonjs\\\"}' >build/cjs/package.json\""
},
"devDependencies": {
"@types/body-parser": "^1.19.3",
"@types/express": "^4.17.18",
"@types/node": "^20.3.1",
"@typescript-eslint/eslint-plugin": "^5.59.11",
"@typescript-eslint/parser": "^5.59.11",
"ava": "^5.3.1",
"body-parser": "^1.20.2",
"c8": "^8.0.1",
"esbuild": "^0.18.4",
"eslint": "^8.43.0",
"eslint-config-prettier": "^8.8.0",
"express": "^4.18.2",
"nock": "^13.3.3",
"prettier": "^2.8.8",
"ts-node": "^10.9.1",
"tslib": "^2.5.3",
Expand Down
6 changes: 6 additions & 0 deletions pkg/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export * from '@slangroom/core/tokens';
export * from '@slangroom/core/lexer';
export * from '@slangroom/core/parser';
export * from '@slangroom/core/visitor';
export * from '@slangroom/core/plugin';
export * from '@slangroom/core/slangroom';
69 changes: 44 additions & 25 deletions pkg/core/src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,37 @@ import {
Send,
To,
} from '@slangroom/core/tokens';
import { CstParser, type IToken } from '@slangroom/deps/chevrotain';
import { CstParser, type IToken, type CstNode } from '@slangroom/deps/chevrotain';

export type StatementCst = CstNode & {
children: {
connect?: ConnectCst;
sendpass: SendpassCst[];
phrase: PhraseCst;
into?: IntoCst;
};
};

export type ConnectCst = CstNode & {
children: { Identifier: [IToken] };
};

export type SendpassCst = CstNode & {
children: ({ Send: [IToken] } | { Pass: [IToken] }) & {
phrase: PhraseCst;
Identifier: [IToken];
};
};

export type PhraseCst = CstNode & {
children: {
Buzzword: [IToken, ...IToken[]];
};
};

export type IntoCst = CstNode & {
children: { Identifier: [IToken] };
};

const Parser = new (class extends CstParser {
constructor() {
Expand All @@ -20,13 +50,9 @@ const Parser = new (class extends CstParser {

statement = this.RULE('statement', () => {
this.OPTION1(() => this.SUBRULE(this.#connect));
this.MANY(() => {
this.SUBRULE(this.#sendpass);
});
this.SUBRULE(this.#buzzwords);
this.OPTION(() => {
this.SUBRULE(this.#into);
});
this.MANY(() => this.SUBRULE(this.#sendpass));
this.SUBRULE(this.#phrase);
this.OPTION2(() => this.SUBRULE(this.#into));
});

#connect = this.RULE('connect', () => {
Expand All @@ -37,37 +63,30 @@ const Parser = new (class extends CstParser {
});

#sendpass = this.RULE('sendpass', () => {
this.OR([
{
ALT: () => this.CONSUME(Send),
},
{
ALT: () => this.CONSUME(Pass),
},
]);
this.SUBRULE(this.#buzzwords);
this.OR([{ ALT: () => this.CONSUME(Send) }, { ALT: () => this.CONSUME(Pass) }]);
this.SUBRULE(this.#phrase);
this.CONSUME(Identifier);
this.CONSUME(And);
});

#phrase = this.RULE('phrase', () => {
this.AT_LEAST_ONE(() => this.CONSUME(Buzzword));
});

#into = this.RULE('into', () => {
this.CONSUME(And);
this.CONSUME(Output);
this.CONSUME(Into);
this.CONSUME(Identifier);
});

#buzzwords = this.RULE('buzzwords', () => {
this.AT_LEAST_ONE(() => this.CONSUME(Buzzword));
});
})();

export const CstVisitor = Parser.getBaseCstVisitorConstructor();

export const parse = (tokens: IToken[]) => {
Parser.input = tokens;

const res = Parser.statement();
console.log(Parser.errors)
return res
return {
cst: Parser.statement(),
errors: Parser.errors,
};
};
Loading

0 comments on commit 4fc49d1

Please sign in to comment.