Skip to content

Commit

Permalink
refactor(bazel-module): Reorganize parser into the own directory (#30945
Browse files Browse the repository at this point in the history
)
  • Loading branch information
zharinov authored Aug 22, 2024
1 parent 65b4212 commit bb1acb8
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { codeBlock } from 'common-tags';
import * as fragments from './fragments';
import { parse } from './parser';
import * as fragments from '../fragments';
import { parse } from '.';

describe('modules/manager/bazel-module/parser', () => {
describe('modules/manager/bazel-module/parser/index', () => {
describe('parse', () => {
it('returns empty string if invalid content', () => {
const input = codeBlock`
Expand Down
19 changes: 19 additions & 0 deletions lib/modules/manager/bazel-module/parser/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { lang, query as q } from 'good-enough-parser';
import { Ctx } from '../context';
import type { RecordFragment } from '../fragments';
import { moduleRules } from './module';

const rule = q.alt<Ctx>(moduleRules);

const query = q.tree<Ctx>({
type: 'root-tree',
maxDepth: 16,
search: rule,
});

const starlarkLang = lang.createLang('starlark');

export function parse(input: string): RecordFragment[] {
const parsedResult = starlarkLang.query(input, query, new Ctx());
return parsedResult?.results ?? [];
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { lang, query as q } from 'good-enough-parser';
import { regEx } from '../../../util/regex';
import { Ctx } from './context';
import type { RecordFragment } from './fragments';
import * as starlark from './starlark';
import { query as q } from 'good-enough-parser';
import { regEx } from '../../../../util/regex';
import type { Ctx } from '../context';
import * as starlark from '../starlark';

const booleanValuesRegex = regEx(`^${starlark.booleanStringValues.join('|')}$`);
const supportedRules = [
Expand All @@ -26,7 +25,7 @@ const kvParams = q
q.sym<Ctx>(booleanValuesRegex, (ctx, token) => ctx.addBoolean(token.value)),
);

const moduleRules = q
export const moduleRules = q
.sym<Ctx>(supportedRulesRegex, (ctx, token) => ctx.startRule(token.value))
.join(
q.tree({
Expand All @@ -36,18 +35,3 @@ const moduleRules = q
postHandler: (ctx, tree) => ctx.endRule(),
}),
);

const rule = q.alt<Ctx>(moduleRules);

const query = q.tree<Ctx>({
type: 'root-tree',
maxDepth: 16,
search: rule,
});

const starlarkLang = lang.createLang('starlark');

export function parse(input: string): RecordFragment[] {
const parsedResult = starlarkLang.query(input, query, new Ctx());
return parsedResult?.results ?? [];
}

0 comments on commit bb1acb8

Please sign in to comment.