Skip to content

Handle Int nodes. #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"homepage": "https://github.com/indutny/llparse-frontend#readme",
"dependencies": {
"debug": "^3.2.6",
"llparse-builder": "^1.3.2"
"llparse-builder": "git+https://github.com/arthurschreiber/llparse-builder.git#arthur/binary-parsing"
},
"devDependencies": {
"@types/debug": "0.0.30",
Expand Down
1 change: 1 addition & 0 deletions src/container/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export class Container {
Consume: this.combine((impl) => impl.node.Consume),
Empty: this.combine((impl) => impl.node.Empty),
Error: this.combine((impl) => impl.node.Error),
Int: this.combine((impl) => impl.node.Int),
Invoke: this.combine((impl) => impl.node.Invoke),
Pause: this.combine((impl) => impl.node.Pause),
Sequence: this.combine((impl) => impl.node.Sequence),
Expand Down
64 changes: 49 additions & 15 deletions src/frontend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ export class Frontend {
} else if (node instanceof source.node.Consume) {
result = new nodeImpl.Consume(
new frontend.node.Consume(id(), node.field));
} else if (node instanceof source.node.Int) {
result = this.translateInt(node);
} else if (node instanceof source.node.SpanStart) {
result = new nodeImpl.SpanStart(
new frontend.node.SpanStart(id(), this.spanMap.get(node.span)!,
Expand All @@ -184,27 +186,37 @@ export class Frontend {

// Initialize result
const otherwise = node.getOtherwiseEdge();

if (Array.isArray(result)) {
assert(node instanceof source.node.Match);
const match = node as source.node.Match;
assert(node instanceof source.node.Match || node instanceof source.node.Int);

// TODO(indutny): move this to llparse-builder?
assert.notStrictEqual(otherwise, undefined,
`Node "${node.name}" has no \`.otherwise()\``);
if (node instanceof source.node.Match) {
// TODO(indutny): move this to llparse-builder?
assert.notStrictEqual(otherwise, undefined,
`Node "${node.name}" has no \`.otherwise()\``);

// Assign otherwise to every node of Trie
if (otherwise !== undefined) {
// Assign otherwise to every node of Trie
if (otherwise !== undefined) {
for (const child of result) {
child.ref.setOtherwise(this.translate(otherwise.node),
otherwise.noAdvance);
}
}

// Assign transform to every node of Trie
const transform = this.translateTransform(node.getTransform());
for (const child of result) {
child.ref.setOtherwise(this.translate(otherwise.node),
otherwise.noAdvance);
child.ref.setTransform(transform);
}
}
} else if (node instanceof source.node.Int) {
// TODO(indutny): move this to llparse-builder?
assert.notStrictEqual(otherwise, undefined,
`Node "${node.name}" has no \`.otherwise()\``);

// Assign transform to every node of Trie
const transform = this.translateTransform(match.getTransform());
for (const child of result) {
child.ref.setTransform(transform);
// Assign otherwise to last node of int expansion
if (otherwise !== undefined) {
result[result.length - 1].ref.setOtherwise(this.translate(otherwise.node),
otherwise.noAdvance)
}
}

assert(result.length >= 1);
Expand Down Expand Up @@ -256,6 +268,28 @@ export class Frontend {
}
}

private translateInt(node: source.node.Int) : ReadonlyArray<IWrap<frontend.node.Node>> {
const { name, field, bytes, signed, littleEndian } = node;

const inner = new frontend.node.Int(this.id.id(name), field, bytes, signed, littleEndian, 0);
let result = [ new this.implementation.node.Int(inner) ];

// Break loops
this.map.set(node, result[0]);

let next = result[0];
for (let offset = 1; offset < node.bytes; offset++) {
const uniqueName = this.id.id(`${name}_byte${offset + 1}`);
const inner = new frontend.node.Int(uniqueName, field, bytes, signed, littleEndian, offset);
result[offset] = new this.implementation.node.Int(inner);

next.ref.setOtherwise(result[offset], false);
next = result[offset];
}

return result;
}

private translateMatch(node: source.node.Match): MatchResult {
const trie = new Trie(node.name);

Expand Down
1 change: 1 addition & 0 deletions src/implementation/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export interface INodeImplementation {
readonly Consume: new(n: node.Consume) => IWrap<node.Consume>;
readonly Empty: new(n: node.Empty) => IWrap<node.Empty>;
readonly Error: new(n: node.Error) => IWrap<node.Error>;
readonly Int: new(n: node.Int) => IWrap<node.Int>;
readonly Invoke: new(n: node.Invoke) => IWrap<node.Invoke>;
readonly Pause: new(n: node.Pause) => IWrap<node.Pause>;
readonly Sequence: new(n: node.Sequence) => IWrap<node.Sequence>;
Expand Down
1 change: 1 addition & 0 deletions src/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export * from './base';
export * from './consume';
export * from './empty';
export * from './error';
export * from './int';
export * from './invoke';
export * from './match';
export * from './pause';
Expand Down
8 changes: 8 additions & 0 deletions src/node/int.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { IUniqueName } from '../utils';
import { Node } from './base';

export class Int extends Node {
constructor(id: IUniqueName, readonly field: string, public readonly bytes: number, public readonly signed: boolean, public readonly littleEndian: boolean, public readonly byteOffset: number) {
super(id);
}
}
8 changes: 8 additions & 0 deletions test/fixtures/a-implementation/node/byte.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { node } from '../../../../src/frontend';
import { Node } from './base';

export class Byte extends Node<node.Byte> {
protected doBuild(out: string[]): void {
out.push(this.format(''));
}
}
3 changes: 2 additions & 1 deletion test/fixtures/a-implementation/node/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Consume } from './consume';
import { Empty } from './empty';
import { Error } from './error';
import { Int } from './int';
import { Invoke } from './invoke';
import { Pause } from './pause';
import { Sequence } from './sequence';
Expand All @@ -10,6 +11,6 @@ import { SpanStart } from './span-start';
import { TableLookup } from './table-lookup';

export default {
Consume, Empty, Error, Invoke, Pause, Sequence, Single, SpanEnd,
Consume, Empty, Error, Invoke, Int, Pause, Sequence, Single, SpanEnd,
SpanStart, TableLookup,
};
8 changes: 8 additions & 0 deletions test/fixtures/a-implementation/node/int.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { node } from '../../../../src/frontend';
import { Node } from './base';

export class Int extends Node<node.Int> {
protected doBuild(out: string[]): void {
out.push(this.format(`byteOffset=${this.ref.byteOffset}`));
}
}
8 changes: 8 additions & 0 deletions test/fixtures/implementation/node/byte.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { node } from '../../../../src/frontend';
import { Node } from './base';

export class Byte extends Node<node.Byte> {
protected doBuild(out: string[]): void {
out.push(this.format(''));
}
}
3 changes: 2 additions & 1 deletion test/fixtures/implementation/node/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Consume } from './consume';
import { Empty } from './empty';
import { Error } from './error';
import { Int } from './int';
import { Invoke } from './invoke';
import { Pause } from './pause';
import { Sequence } from './sequence';
Expand All @@ -10,6 +11,6 @@ import { SpanStart } from './span-start';
import { TableLookup } from './table-lookup';

export default {
Consume, Empty, Error, Invoke, Pause, Sequence, Single, SpanEnd,
Consume, Empty, Error, Int, Invoke, Pause, Sequence, Single, SpanEnd,
SpanStart, TableLookup,
};
8 changes: 8 additions & 0 deletions test/fixtures/implementation/node/int.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { node } from '../../../../src/frontend';
import { Node } from './base';

export class Int extends Node<node.Int> {
protected doBuild(out: string[]): void {
out.push(this.format(`byteOffset=${this.ref.byteOffset}`));
}
}
32 changes: 32 additions & 0 deletions test/frontend-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,38 @@ describe('llparse-frontend', () => {
]);
});

it('should expand int nodes based on their number of required bytes', () => {
b.property('i8', 'byte');

const byte = b.intLE('byte', 1);
byte.otherwise(b.error(123, 'hello'));

checkNodes(f, byte, [
'<Int name=llparse__n_byte_int_8 byteOffset=0 otherwise-no_adv=llparse__n_error/>',
'<ErrorNode name=llparse__n_error code=123 reason="hello"/>'
]);

const short = b.intLE('short', 2);
short.otherwise(b.error(123, 'hello'));

checkNodes(f, short, [
'<Int name=llparse__n_short_int_16_le byteOffset=0 otherwise=llparse__n_short_int_16_le_byte2/>',
'<Int name=llparse__n_short_int_16_le_byte2 byteOffset=1 otherwise-no_adv=llparse__n_error_1/>',
'<ErrorNode name=llparse__n_error_1 code=123 reason="hello"/>'
]);

const word = b.intLE('word', 4);
word.otherwise(b.error(123, 'hello'));

checkNodes(f, word, [
'<Int name=llparse__n_word_int_32_le byteOffset=0 otherwise=llparse__n_word_int_32_le_byte2/>',
'<Int name=llparse__n_word_int_32_le_byte2 byteOffset=1 otherwise=llparse__n_word_int_32_le_byte3/>',
'<Int name=llparse__n_word_int_32_le_byte3 byteOffset=2 otherwise=llparse__n_word_int_32_le_byte4/>',
'<Int name=llparse__n_word_int_32_le_byte4 byteOffset=3 otherwise-no_adv=llparse__n_error_2/>',
'<ErrorNode name=llparse__n_error_2 code=123 reason="hello"/>'
]);
});

it('should do peephole optimization', () => {
const root = b.node('root');
const root1 = b.node('a');
Expand Down