Skip to content

Commit

Permalink
Renamed stuff for clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
james-pre committed Jan 5, 2025
1 parent 450c2ce commit 7f787f7
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 19 deletions.
2 changes: 1 addition & 1 deletion scripts/bnf.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
4 changes: 2 additions & 2 deletions scripts/dump.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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);
Expand Down
14 changes: 7 additions & 7 deletions src/bnf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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)!;

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 });
Expand Down Expand Up @@ -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: [],
Expand Down
10 changes: 5 additions & 5 deletions src/issue.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export interface SourceIssue {
export interface Issue {
id?: string;
line: number;
column: number;
Expand All @@ -16,7 +16,7 @@ export enum IssueLevel {
}

/**
* Placed into \x1b[<here>m
* Placed into ANSI escape code
*/
const colors = {
[IssueLevel.Error]: 31,
Expand All @@ -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;
}

Expand All @@ -43,7 +43,7 @@ export interface IssueFormatting {
trace: boolean;
}

export function stringify_issue(i: SourceIssue, options: Partial<IssueFormatting>): string {
export function stringify_issue(i: Issue, options: Partial<IssueFormatting>): 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) : '';
Expand All @@ -61,5 +61,5 @@ export function stringify_issue(i: SourceIssue, options: Partial<IssueFormatting
column -= offset;
}

return `${i.id}:${i.line}:${column}\n\t${excerpt}\n\t${' '.repeat(column)}^\n${level}: ${i.message}${trace}`;
return `${i.id ? i.id + ':' : ''}${i.line}:${column}\n\t${excerpt}\n\t${' '.repeat(column)}^\n${level}: ${i.message}${trace}`;
}
4 changes: 2 additions & 2 deletions src/parser.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Common as CommonConfig } from './config.js';
import type { IssueLevel, SourceIssue } from './issue.js';
import type { IssueLevel, Issue } from './issue.js';
import type { Token, TokenDefinition } from './tokens.js';
import { tokenize } from './tokens.js';

Expand Down Expand Up @@ -140,7 +140,7 @@ export function parse(options: ParseOptions): Node[] {

const attempts = new Map<string, Node | null>();

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 };
Expand Down
4 changes: 2 additions & 2 deletions src/tokens.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { SourceIssue } from './issue.js';
import type { Issue } from './issue.js';

export interface Token {
kind: string;
Expand Down Expand Up @@ -32,7 +32,7 @@ export function tokenize(source: string, definitions: Iterable<TokenDefinition>)
}

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);
Expand Down

0 comments on commit 7f787f7

Please sign in to comment.