Skip to content

Commit

Permalink
add hasChildren
Browse files Browse the repository at this point in the history
  • Loading branch information
Dzianis Dashkevich committed Oct 19, 2023
1 parent fdf9ead commit 5da7483
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 45 deletions.
1 change: 1 addition & 0 deletions packages/web-media-box/src/dash-parser/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class Parser {
options.transformTagAttributes || ((tagKey, tagAttributes): Record<string, string> => tagAttributes);

this.parsedManifest = {
type: 'static',
segments: [],
custom: {},
};
Expand Down
6 changes: 4 additions & 2 deletions packages/web-media-box/src/dash-parser/pendingProcessors.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { TagInfo } from '@/dash-parser/stateMachine.ts';
import { TagProcessor } from '@/dash-parser/tags/base.ts';
import { ParsedManifest } from '@/dash-parser/types/parsedManifest';
import { SharedState } from '@/dash-parser/types/sharedState';

export class PendingProcessors {
private readonly pendingMap: Map<TagInfo, PendingProcess> = new Map();
Expand Down Expand Up @@ -57,7 +59,7 @@ export class PendingProcess {
return true;
}

public process(): void {
this.tagProcessor.processPending(this.tagInfo, this.parentTagInfo, this.waitingForMap);
public process(parsedManifest: ParsedManifest, sharedState: SharedState): void {
this.tagProcessor.processPending(this.tagInfo, this.parentTagInfo, this.waitingForMap, parsedManifest, sharedState);
}
}
61 changes: 25 additions & 36 deletions packages/web-media-box/src/dash-parser/stateMachine.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { testString } from '@/dash-parser/examples/mpd.ts';

const PARSE_EMPTY_SPACE_STATE = 1;
const PARSE_TAG_KEY_STATE = 2;
const PARSE_ATTRIBUTE_KEY_STATE = 3;
Expand All @@ -11,6 +9,7 @@ export interface TagInfo {
tagKey: string;
tagValue: string | null;
tagAttributes: Record<string, string>;
hasChildren: boolean;
}

type TagInfoCallback = (tagInfo: TagInfo, parentTagInfo: TagInfo | null) => void;
Expand All @@ -20,42 +19,33 @@ export type StateMachineTransition = (char: string) => void;
export default function createStateMachine(tagInfoCallback: TagInfoCallback): StateMachineTransition {
let currentState = PARSE_EMPTY_SPACE_STATE;

let lastChar: string | null = null;
let currentTagKey = '';
let currentTagValue = '';
let currentAttributeKey = '';
let currentAttributeValue = '';
let hasChildren = true;
let currentTagAttributeKeyValueMap: Record<string, string> = {};

// start = parse-empty-space-state
// parse-empty-space-state -> parse-tag-name (if encounter '<')
// parse-empty-space-state -> parse-empty-space-state (if encounter any char)

// parse-tag-name -> parse-empty-space-state (if encounter '?') // xml declaration
// parse-tag-name -> parse-empty-space-state (if encounter '!') // comment
// parse-tag-name -> parse-empty-space-state (if encounter '/') // closing tag
// parse-tag-name -> parse-tag-attribute-key (if encounter ' ')
// parse-tag-name -> parse-tag-body (if encounter '>')
// if encounter new tag name and we have previous value, just expose it and clear internal state

// parse-tag-attribute-key --> parse-tag-attribute-value (if encounter '=')

// parse-tag-attribute-value --> parse-tag-attribute-quoted-string-value (if encounter '"')

// parse-tag-attribute-quoted-string-value -> parse-tag-name (if encounter '"')

// parse-tag-body --> parse-empty-space-state (if encounter ' ')
// parse-tag-body --> parse-tag-name (if encounter '<')

const stateMachine: Record<number, (char: string) => void> = {
[PARSE_EMPTY_SPACE_STATE]: (char) => {
if (char === '<') {
currentState = PARSE_TAG_KEY_STATE;

if (currentTagKey) {
tagInfoCallback(currentTagKey, currentTagValue || null, currentTagAttributeKeyValueMap);
tagInfoCallback(
{
tagKey: currentTagKey,
tagValue: currentTagValue,
tagAttributes: currentTagAttributeKeyValueMap,
hasChildren,
},
null
); // TODO: parent
currentTagKey = '';
currentTagValue = '';
currentTagAttributeKeyValueMap = {};
hasChildren = true;
}
}
},
Expand All @@ -72,6 +62,7 @@ export default function createStateMachine(tagInfoCallback: TagInfoCallback): St

if (char === '/') {
currentState = PARSE_EMPTY_SPACE_STATE;
hasChildren = lastChar === '<';
return;
}

Expand Down Expand Up @@ -121,10 +112,19 @@ export default function createStateMachine(tagInfoCallback: TagInfoCallback): St
currentState = PARSE_TAG_KEY_STATE;

if (currentTagKey) {
tagInfoCallback(currentTagKey, currentTagValue || null, currentTagAttributeKeyValueMap);
tagInfoCallback(
{
tagKey: currentTagKey,
tagValue: currentTagValue,
tagAttributes: currentTagAttributeKeyValueMap,
hasChildren,
},
null
); // TODO: parent
currentTagKey = '';
currentTagValue = '';
currentTagAttributeKeyValueMap = {};
hasChildren = true;
}
return;
}
Expand All @@ -135,20 +135,9 @@ export default function createStateMachine(tagInfoCallback: TagInfoCallback): St

return (char: string) => {
stateMachine[currentState](char);
lastChar = char;
};
}

// TODO: add paren node support

const test = (): void => {
const stateMachine = createStateMachine((tagName, tagValue, tagAttributes) => {
// eslint-disable-next-line no-console
console.log('tagName: ', tagName, ' tagValue: ', tagValue, ' tagAttributes: ', tagAttributes);
});

for (const char of testString) {
stateMachine(char);
}
};

test();
6 changes: 4 additions & 2 deletions packages/web-media-box/src/dash-parser/tags/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ export abstract class TagProcessor {
public processPending(
tagInfo: TagInfo,
parentTagInfo: TagInfo | null,
requiredChildren: Map<string, TagInfo | null>
requiredChildren: Map<string, TagInfo | null>,
parsedManifest: ParsedManifest,
sharedState: SharedState
): void {
// specific processor will override
}
Expand All @@ -47,7 +49,7 @@ export class Mpd extends TagProcessor {
pendingProcessors: PendingProcessors
): void {
const id = tagInfo.tagAttributes[Mpd.ID];
const type = (tagInfo.tagAttributes[Mpd.TYPE] || 'static') as ManifestType;
const type = (tagInfo.tagAttributes[Mpd.TYPE] || parsedManifest.type) as ManifestType;
const availabilityStartTime = tagInfo.tagAttributes[Mpd.AVAILABILITY_START_TIME];
const availabilityEndTime = tagInfo.tagAttributes[Mpd.AVAILABILITY_END_TIME];

Expand Down
7 changes: 2 additions & 5 deletions packages/web-media-box/test/dash-parser/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import parse from '@/dash-parser/parse';
import { testString } from '@/dash-parser/examples/mpd';
import { describe, it, expect } from 'bun:test';

describe('dash-parser spec', () => {
// TODO: create valid tests
it('testString should give us JSON', () => {
const parsed = parse(testString);
expect(parsed.segments.length).toBe(0);
it('mock spec', () => {
expect(true).toBe(true);
});
});

0 comments on commit 5da7483

Please sign in to comment.