Skip to content

Commit

Permalink
Merge pull request #10 from superindustries/feature/slang-syntax-pars…
Browse files Browse the repository at this point in the history
…er-profile-v6

Feature/slang syntax parser profile v6
  • Loading branch information
lukas-valenta authored Aug 18, 2020
2 parents 03e1e3f + 14dfae9 commit 61b3281
Show file tree
Hide file tree
Showing 22 changed files with 1,138 additions and 795 deletions.
10 changes: 4 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@
"name": "@superindustries/superface-parser",
"version": "0.0.0",
"description": "Level 5 autonomous, self-driving API client, https://superface.ai",
"main": "dist/superface-parser.js",
"main": "dist/index.js",
"source": "src/index.ts",
"module": "dist/superface-parser.modern.js",
"unpkg": "dist/superface-parser.umd.js",
"browser": "dist/superface-parser.umd.js",
"types": "dist/index.d.ts",
"repository": "https://github.com/superindustries/superface-parser.git",
"author": "Superface Team",
Expand All @@ -19,7 +16,8 @@
"registry": "https://npm.pkg.github.com/"
},
"scripts": {
"build": "microbundle --tsconfig tsconfig.release.json",
"bundle_build": "microbundle --tsconfig tsconfig.release.json",
"build": "tsc -p tsconfig.release.json --outDir dist",
"watch": "yarn build --watch",
"clean": "rimraf dist/",
"prebuild": "yarn clean",
Expand All @@ -46,7 +44,7 @@
"ts-jest": "^26.1.0"
},
"dependencies": {
"@superindustries/language": "^0.0.10",
"@superindustries/language": "^0.0.11",
"@superindustries/superface": "^0.0.1-test",
"typescript": "^3.9.5"
}
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './errors';
export * from './interfaces';
export * from './language';
34 changes: 17 additions & 17 deletions src/language/error.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,17 +130,17 @@ class TestSyntaxRule<R extends RuleResult<T>, T = unknown> extends SyntaxRule<
describe('langauge syntax errors', () => {
describe('lexer', () => {
it('before', () => {
const lexer = new Lexer(new Source('before\n\t@safes'));
const lexer = new Lexer(new Source('before\n\t0xx'));

lexer.advance();
lexer.advance();

expect(() => lexer.advance()).toThrowSyntaxError(
'Expected one of [safe, unsafe, idempotent]',
'Expected a number following integer base prefix',
'[input]:2:2',
'1 | before',
'2 | \t@safes',
' | \t^^ '
'2 | \t0xx',
' | \t^^^'
);
});

Expand All @@ -159,31 +159,31 @@ describe('langauge syntax errors', () => {
});

it('before and after', () => {
const lexer = new Lexer(new Source('before\n\t@safes\nafter'));
const lexer = new Lexer(new Source('before\n\t0xx\nafter'));

lexer.advance();
lexer.advance();

expect(() => lexer.advance()).toThrowSyntaxError(
'Expected one of [safe, unsafe, idempotent]',
'Expected a number following integer base prefix',
'[input]:2:2',
'1 | before',
'2 | \t@safes',
' | \t^^ ',
'2 | \t0xx',
' | \t^^^',
'3 | after'
);
});

it('neither before nor after', () => {
const lexer = new Lexer(new Source('\t@safes'));
const lexer = new Lexer(new Source('\t0xx'));

lexer.advance();

expect(() => lexer.advance()).toThrowSyntaxError(
'Expected one of [safe, unsafe, idempotent]',
'Expected a number following integer base prefix',
'[input]:1:2',
'1 | \t@safes',
' | \t^^ '
'1 | \t0xx',
' | \t^^^'
);
});
});
Expand All @@ -206,25 +206,25 @@ describe('langauge syntax errors', () => {
expect(() =>
parseRule(profile.PRIMITIVE_TYPE_NAME, source, true)
).toThrowSyntaxError(
'Expected `Boolean` or `Number` or `String` but found `!`',
'Expected `boolean` or `number` or `string` but found `!`',
'[input]:1:1',
'1 | !',
' | ^'
);
});

it('should report enum value rule error', () => {
const tokens = new Source(`Enum {
'asdf'
const tokens = new Source(`enum {
asdf = 'asdf'
!
}`);

expect(() =>
parseRule(profile.ENUM_DEFINITION, tokens, true)
).toThrowSyntaxError(
'Expected string or literal or identifier or `}` but found `!`',
'Expected string or identifier or `}` but found `!`',
'[input]:3:1',
"2 | 'asdf'",
"2 | asdf = 'asdf'",
'3 | !',
' | ^',
'4 | }'
Expand Down
19 changes: 17 additions & 2 deletions src/language/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ function computeVisualizeBlockSpan(
* Example: ` 13 | ` with `padSize = 3` and `lineNumber = 13`.
*/
function formatLinePrefix(padSize?: number, lineNumber?: number): string {
const value = lineNumber?.toString() ?? '';
let value = '';
if (lineNumber !== undefined) {
value = lineNumber.toString();
}

return `${value.padEnd(padSize ?? 4, ' ')} | `;
}
Expand Down Expand Up @@ -178,7 +181,19 @@ export class SyntaxError {
const location = result.attempts.token?.location ?? { line: 0, column: 0 };
const span = result.attempts.token?.span ?? { start: 0, end: 0 };

const expected = result.attempts.rules.map(r => r.toString()).join(' or ');
const expectedFilterSet = new Set();
const expected = result.attempts.rules
.map(r => r.toString())
.filter(r => {
if (expectedFilterSet.has(r)) {
return false;
}

expectedFilterSet.add(r);

return true;
})
.join(' or ');

let actual = '<NONE>';
if (result.attempts.token !== undefined) {
Expand Down
5 changes: 5 additions & 0 deletions src/language/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export { Location, Span, Source } from './source';
export { SyntaxError } from './error';

export * from './lexer';
export * from './syntax';
Loading

0 comments on commit 61b3281

Please sign in to comment.