21 lines
731 B
TypeScript
21 lines
731 B
TypeScript
import { readFileSync } from 'fs';
|
|
|
|
import { Parser, Tokenizer } from '@/compiler';
|
|
import * as grammars from '@/grammars';
|
|
|
|
const grammar = grammars.v1;
|
|
|
|
const tokenizer = new Tokenizer(grammar);
|
|
const parser = new Parser(tokenizer, grammar);
|
|
|
|
const source = readFileSync(__dirname + '/example.jn', 'utf-8');
|
|
const maxLength = Math.max(...source.split('\n').map((line) => line.length));
|
|
const padding = Math.floor((maxLength - ' Parsing: '.length) / 2);
|
|
try {
|
|
console.log('='.repeat(padding) + ' Parsing: ' + '='.repeat(padding));
|
|
console.log(source);
|
|
console.log('='.repeat(maxLength));
|
|
console.log(JSON.stringify(parser.parse(source), null, 2));
|
|
} catch (error) {
|
|
console.error(`Error parsing "${source}":`, error);
|
|
}
|