Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add chevrotain parser with Unicode support! #3432

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
"@typescript-eslint/parser": "^5.36.1",
"babel-jest": "^29.0.2",
"babel-loader": "^8.2.2",
"chevrotain": "^10.3.0",
"concurrently": "^7.0.0",
"coveralls": "^3.0.2",
"css-to-string-loader": "^0.1.3",
Expand Down
6 changes: 2 additions & 4 deletions src/diagram-api/diagramAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,9 @@ import ganttStyles from '../diagrams/gantt/styles';

import infoDb from '../diagrams/info/infoDb';
import infoRenderer from '../diagrams/info/infoRenderer';
// @ts-ignore
import infoParser from '../diagrams/info/parser/info';
import infoParser from '../diagrams/info/infoParser';
import infoStyles from '../diagrams/info/styles';
// @ts-ignore
import pieParser from '../diagrams/pie/parser/pie';
import pieParser from '../diagrams/pie/pieParser';
import pieDb from '../diagrams/pie/pieDb';
import pieRenderer from '../diagrams/pie/pieRenderer';
import pieStyles from '../diagrams/pie/styles';
Expand Down
14 changes: 0 additions & 14 deletions src/diagrams/info/info.spec.js

This file was deleted.

53 changes: 53 additions & 0 deletions src/diagrams/info/info.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import infoDb from './infoDb';
import infoParser from './infoParser';

describe('when parsing an info graph it', function () {
beforeEach(() => {
infoDb.clear();
});

it.each([
// Without newlines
`info
showInfo`,

// With newlines at beginning
`
info
showInfo`,
// Extra newlines
`

info

showInfo

`,
])('should handle valid info definitions', function (str: string = '') {
expect(infoDb.getInfo()).toEqual(false);
infoParser.parse(str);
expect(infoDb.getInfo()).toEqual(true);
});

it('should throw an error when the info is not defined', function () {
expect(() => {
infoParser.parse(``);
}).toThrow();
});

it('should not throw an error when showInfo is not defined', function () {
infoParser.parse('info');
expect(infoDb.getInfo()).toEqual(true);
});

it.each([
`InFo`,
`Info
showinfo`,
`info
shOWINFO`,
])('should be case insensitive', function (str) {
infoParser.parse(str);
expect(infoDb.getInfo()).toEqual(true);
});
});
15 changes: 8 additions & 7 deletions src/diagrams/info/infoDb.js → src/diagrams/info/infoDb.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/** Created by knut on 15-01-14. */
import { log } from '../../logger';
import { clear } from '../../commonDb';
import { clear as commonClear } from '../../commonDb';

var message = '';
var info = false;

export const setMessage = (txt) => {
export const setMessage = (txt: string) => {
log.debug('Setting message to: ' + txt);
message = txt;
};
Expand All @@ -14,23 +14,24 @@ export const getMessage = () => {
return message;
};

export const setInfo = (inf) => {
export const setInfo = (inf: boolean) => {
info = inf;
};

export const getInfo = () => {
return info;
};

// export const parseError = (err, hash) => {
// global.mermaidAPI.parseError(err, hash)
// }
export const clear = () => {
commonClear();
message = '';
info = false;
};

export default {
setMessage,
getMessage,
setInfo,
getInfo,
clear,
// parseError
};
79 changes: 79 additions & 0 deletions src/diagrams/info/infoParser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { createToken, EmbeddedActionsParser, Lexer } from 'chevrotain';
import { log } from '../../logger';
import infoDb from './infoDb';

const Info = createToken({ name: 'Info', pattern: /info/i });
const ShowInfo = createToken({ name: 'ShowInfo', pattern: /showInfo/i });
const NewLine = createToken({
name: 'NewLine',
pattern: /\r?\n/,
});
const WhiteSpace = createToken({
name: 'WhiteSpace',
pattern: /\s+/,
group: Lexer.SKIPPED,
});

const allTokens = [NewLine, WhiteSpace, ShowInfo, Info];
const InfoLexer = new Lexer(allTokens);

class InfoParser extends EmbeddedActionsParser {
constructor() {
super(allTokens);
this.performSelfAnalysis();
}

public reset(): void {
super.reset();
infoDb.clear();
}

public diagram = this.RULE('diagram', () => {
this.MANY(() => {
this.CONSUME(NewLine);
});
this.SUBRULE(this.hdr);
this.MANY2(() => {
this.SUBRULE2(this.row);
});
this.ACTION(() => infoDb.setInfo(true));
this.MANY3(() => {
this.CONSUME2(NewLine);
});
});

public hdr = this.RULE('hdr', () => {
this.CONSUME(Info);
this.OPTION(() => this.CONSUME(NewLine));
});

public row = this.RULE('row', () => {
this.SUBRULE(this.field);
this.MANY(() => {
this.CONSUME(NewLine);
});
});

public field = this.RULE('field', () => {
this.CONSUME(ShowInfo);
this.ACTION(() => infoDb.setInfo(true));
});
}

const parser = new InfoParser();

const parse = (text: string): void => {
const lexResult = InfoLexer.tokenize(text);
parser.input = lexResult.tokens;
parser.diagram();

if (parser.errors.length > 0 || lexResult.errors.length > 0) {
log.error(
{ parserErrors: parser.errors, lexerErrors: lexResult.errors },
'Error parsing info diagram'
);
throw new Error(`Parser errors: ${parser.errors} Lex errors: ${lexResult.errors}`);
}
};

export default { parser: {}, parse };
48 changes: 0 additions & 48 deletions src/diagrams/info/parser/info.jison

This file was deleted.

106 changes: 0 additions & 106 deletions src/diagrams/pie/parser/pie.jison

This file was deleted.

Loading