-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtest-helpers.ts
165 lines (153 loc) · 4.71 KB
/
test-helpers.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import { execSync } from 'child_process';
import { GrammarError } from 'peggy';
import util from 'util';
import generate from './generator.js';
import { AstNode, FunctionNode, Program } from '../ast/index.js';
import { Parse, ParserOptions } from './parser.js';
import { FunctionScopeIndex, Scope, ScopeIndex } from './scope.js';
export const inspect = (arg: any) =>
console.log(util.inspect(arg, false, null, true));
export const nextWarn = () => {
console.warn = jest.fn();
let i = 0;
// @ts-ignore
const mock = console.warn.mock;
return () => mock.calls[i++][0];
};
type Context = {
parse: Parse;
parseSrc: ParseSrc;
};
export const buildParser = () => {
execSync(
'npx peggy --cache --format es -o src/parser/parser.js src/parser/glsl-grammar.pegjs'
);
const parser = require('./parser');
const parse = parser.parse as Parse;
const ps = parseSrc(parse);
const ctx: Context = {
parse,
parseSrc: ps,
};
return {
parse,
parser,
parseSrc: ps,
debugSrc: debugSrc(ctx),
debugStatement: debugStatement(ctx),
expectParsedStatement: expectParsedStatement(ctx),
parseStatement: parseStatement(ctx),
expectParsedProgram: expectParsedProgram(ctx),
};
};
// Keeping this around in case I need to figure out how to do tracing again
// Most of this ceremony around building a parser is dealing with Peggy's error
// format() function, where the grammarSource has to line up in generate() and
// format() to get nicely formatted errors if there's a syntax error in the
// grammar
// const buildParser = (file: string) => {
// const grammar = fileContents(file);
// try {
// return peggy.generate(grammar, {
// grammarSource: file,
// cache: true,
// trace: false,
// });
// } catch (e) {
// const err = e as SyntaxError;
// if ('format' in err && typeof err.format === 'function') {
// console.error(err.format([{ source: file, text: grammar }]));
// }
// throw e;
// }
// };
const middle = /\/\* start \*\/((.|[\r\n])+)(\/\* end \*\/)?/m;
type ParseSrc = (src: string, options?: ParserOptions) => Program;
const parseSrc = (parse: Parse): ParseSrc => (src, options = {}) => {
const grammarSource = '<anonymous glsl>';
try {
return parse(src, {
...options,
grammarSource,
tracer: {
trace: (type) => {
if (
type.type === 'rule.match' &&
type.rule !== 'whitespace' &&
type.rule !== 'single_comment' &&
type.rule !== 'comment' &&
type.rule !== 'digit_sequence' &&
type.rule !== 'digit' &&
type.rule !== 'fractional_constant' &&
type.rule !== 'floating_constant' &&
type.rule !== 'translation_unit' &&
type.rule !== 'start' &&
type.rule !== 'external_declaration' &&
type.rule !== 'SEMICOLON' &&
type.rule !== 'terminal' &&
type.rule !== '_'
) {
if (type.rule === 'IDENTIFIER' || type.rule === 'TYPE_NAME') {
console.log(
'\x1b[35mMatch literal\x1b[0m',
type.rule,
type.result
);
} else {
console.log('\x1b[35mMatch\x1b[0m', type.rule);
}
}
// if (type.type === 'rule.fail') {
// console.log('fail', type.rule);
// }
},
},
});
} catch (e) {
const err = e as GrammarError;
if ('format' in err) {
console.error(err.format([{ source: grammarSource, text: src }]));
}
console.error(`Error parsing lexeme!\n"${src}"`);
throw err;
}
};
const debugSrc = ({ parseSrc }: Context) => (src: string) => {
inspect(parseSrc(src).program);
};
const debugStatement = ({ parseSrc }: Context) => (stmt: AstNode) => {
const program = `void main() {/* start */${stmt}/* end */}`;
const ast = parseSrc(program);
inspect((ast.program[0] as FunctionNode).body.statements[0]);
};
const expectParsedStatement = ({ parseSrc }: Context) => (
src: string,
options = {}
) => {
const program = `void main() {/* start */${src}/* end */}`;
const ast = parseSrc(program, options);
const glsl = generate(ast);
if (glsl !== program) {
inspect(ast.program[0]);
// @ts-ignore
expect(glsl.match(middle)[1]).toBe(src);
}
};
const parseStatement = ({ parseSrc }: Context) => (
src: string,
options: ParserOptions = {}
) => {
const program = `void main() {${src}}`;
return parseSrc(program, options);
};
const expectParsedProgram = ({ parseSrc }: Context) => (
src: string,
options?: ParserOptions
) => {
const ast = parseSrc(src, options);
const glsl = generate(ast);
if (glsl !== src) {
inspect(ast);
expect(glsl).toBe(src);
}
};