diff --git a/scripts/bnf.js b/scripts/bnf.js index cafd83c..29d8ff6 100755 --- a/scripts/bnf.js +++ b/scripts/bnf.js @@ -112,7 +112,7 @@ function include(path) { } } -let config = bnf.ast_to_config(ast, logger(verbose), include); +let config = bnf.create_config(ast, { log: logger(verbose), include }); if (options.compress) config = compress_config(config); diff --git a/scripts/dump.js b/scripts/dump.js index fd5fbd9..e12672f 100755 --- a/scripts/dump.js +++ b/scripts/dump.js @@ -2,7 +2,7 @@ import { readFileSync } from 'node:fs'; import { parseArgs } from 'node:util'; import { parse, parse_info, stringify_node } from '../dist/parser.js'; -import { parse_json as parseJSONConfig } from '../dist/config.js'; +import { parse_json as parse_json_config } from '../dist/config.js'; import { tokenize } from '../dist/tokens.js'; import { is_issue, stringify_issue } from '../dist/issue.js'; @@ -69,7 +69,7 @@ for (const rawOption of options.debug) { let config; try { - config = parseJSONConfig(JSON.parse(readFileSync(options.config, 'utf-8'))); + config = parse_json_config(JSON.parse(readFileSync(options.config, 'utf-8'))); } catch (e) { if ('errno' in e) console.error(e); else console.error('Failed to resolve config:', e); diff --git a/src/bnf.ts b/src/bnf.ts index 8c60265..1bd1231 100644 --- a/src/bnf.ts +++ b/src/bnf.ts @@ -35,12 +35,12 @@ const typeForGroup = { left_paren: 'required', } as const; -export interface ASTConfigOptions { +export interface CreateConfigOptions { log?: Logger; include?: (name: string) => Node[]; } -interface ASTConfigContext extends ASTConfigOptions { +interface CreateConfigContext extends CreateConfigOptions { /** * The current depth */ @@ -68,14 +68,14 @@ interface ASTConfigContext extends ASTConfigOptions { * Creates a copy of a context for use with children. * Right now this just increments the depth */ -function child_context(context: ASTConfigContext): ASTConfigContext { +function child_context(context: CreateConfigContext): CreateConfigContext { return { ...context, depth: context.depth + 1, }; } -function config_process_directive(text: string, $: ASTConfigContext) { +function config_process_directive(text: string, $: CreateConfigContext) { const log = logger($.log, { kind: 'directive', depth: $.depth }); const [, directive, contents] = text.match(/##(\w+) (.*)/i)!; @@ -147,7 +147,7 @@ function config_process_directive(text: string, $: ASTConfigContext) { } } -function config_process_expression(expression: Node, $: ASTConfigContext): [DefinitionPart[], boolean] { +function config_process_expression(expression: Node, $: CreateConfigContext): [DefinitionPart[], boolean] { const _sub = child_context($); let isAlternation = false; @@ -253,7 +253,7 @@ function config_process_expression(expression: Node, $: ASTConfigContext): [Defi return [pattern, isAlternation]; } -function config_process_node(node: Node, $: ASTConfigContext) { +function config_process_node(node: Node, $: CreateConfigContext) { const _sub = child_context($); const log = logger($.log, { kind: node.kind, depth: $.depth }); @@ -324,7 +324,7 @@ function config_process_node(node: Node, $: ASTConfigContext) { }); } -export function create_config(ast: Node[], options: ASTConfigOptions): Config { +export function create_config(ast: Node[], options: CreateConfigOptions): Config { const config: PureConfig = { definitions: [], literals: [], diff --git a/src/issue.ts b/src/issue.ts index d62ebcd..ede8997 100644 --- a/src/issue.ts +++ b/src/issue.ts @@ -1,4 +1,4 @@ -export interface SourceIssue { +export interface Issue { id?: string; line: number; column: number; @@ -16,7 +16,7 @@ export enum IssueLevel { } /** - * Placed into \x1b[m + * Placed into ANSI escape code */ const colors = { [IssueLevel.Error]: 31, @@ -34,7 +34,7 @@ function extract_location(stack: string = ''): string { return '(unknown origin)'; } -export function is_issue(i: unknown): i is SourceIssue { +export function is_issue(i: unknown): i is Issue { return typeof i == 'object' && i != null && 'line' in i && 'column' in i && 'position' in i && 'source' in i && 'level' in i; } @@ -43,7 +43,7 @@ export interface IssueFormatting { trace: boolean; } -export function stringify_issue(i: SourceIssue, options: Partial): string { +export function stringify_issue(i: Issue, options: Partial): string { const level = options.colors ? `\x1b[1;${colors[i.level]}m${IssueLevel[i.level]}\x1b[0m` : IssueLevel[i.level]; const trace = options.trace ? ' ' + extract_location(i.stack) : ''; @@ -61,5 +61,5 @@ export function stringify_issue(i: SourceIssue, options: Partial(); - function _issue(level: IssueLevel, message?: string): SourceIssue { + function _issue(level: IssueLevel, message?: string): Issue { const token = tokens[position]; const { stack } = new Error(); return { id, line: token.line, column: token.column, position: token.position, source, level, message, stack }; diff --git a/src/tokens.ts b/src/tokens.ts index ce500f5..427e3dd 100644 --- a/src/tokens.ts +++ b/src/tokens.ts @@ -1,4 +1,4 @@ -import type { SourceIssue } from './issue.js'; +import type { Issue } from './issue.js'; export interface Token { kind: string; @@ -32,7 +32,7 @@ export function tokenize(source: string, definitions: Iterable) } if (!token) { - throw { line, column, position, source, message: 'Unexpected token: ' + source[position], level: 0, stack: new Error().stack } satisfies SourceIssue; + throw { line, column, position, source, message: 'Unexpected token: ' + source[position], level: 0, stack: new Error().stack } satisfies Issue; } tokens.push(token);