Skip to content

Commit

Permalink
Merge c593ccb into f0fd7fe
Browse files Browse the repository at this point in the history
  • Loading branch information
mbovel committed Jul 12, 2020
2 parents f0fd7fe + c593ccb commit b0fae40
Show file tree
Hide file tree
Showing 189 changed files with 1,219 additions and 8,168 deletions.
2 changes: 1 addition & 1 deletion .prettierrc.json
@@ -1,5 +1,5 @@
{
"useTabs": true,
"tabWidth": 4,
"printWidth": 100
"printWidth": 120
}
23 changes: 13 additions & 10 deletions benchmark/parseBenchmark.ts
@@ -1,44 +1,47 @@
/* tslint:disable:no-console */
import * as Benchmark from "benchmark";
import { makeParser } from "../src/parser/parse";
import { getTestSchema } from "../test/schemas";
import { parse } from "../src/parser/parse";
import { formatBytes, generateBenchmarkInput } from "./utils";

const parse = makeParser(getTestSchema(), console.error);
const input = generateBenchmarkInput();
console.log("Size of test input: " + formatBytes(input.length));
const jsonAst = JSON.stringify(parse(input));
const jsonStr = JSON.stringify(parse(input, true));
new Benchmark.Suite("compare")
.add("Parse Hashml", () => {
parse(input);
return parse(input);
})
.add("Parse JSON", () => {
JSON.parse(jsonAst);
return JSON.parse(jsonStr);
})
.add("String.matchAll", () => {
for (const _ of input.matchAll(
/(?:((?:\r\n|\n|\r|^)(\t*)[\t ]*(?:#([^ \[\r\n]+)(?: |$))?)|(#([^ \[]+)(\[)?)|(]\[)|(\[)|(\\(.)))/g
));
return [
...input.matchAll(
/(?:((?:\r\n|\n|\r|^)(\t*)[\t ]*(?:#([^ \[\r\n]+)(?: |$))?)|(#([^ \[]+)(\[)?)|(]\[)|(\[)|(\\(.)))/g
)
];
})
.add("Iterate through chars", () => {
const SHARP = "#";
const OPENING_BRACKET = "[";
let n = 0;
const end = input.length;
for (let i = 0; i < end; ++i) {
switch (input.charAt(i)) {
case SHARP:
break;
case OPENING_BRACKET:
++n;
break;
}
}
return n;
})
// add listeners
.on("cycle", (event: Event) => {
console.log(String(event.target));
})
.on("complete", function (this: Benchmark[]) {
console.log("JSON/Hashml ratio: " + (this[1].stats.mean / this[0].stats.mean).toFixed(2));
console.log("Hashml throughput: " + formatBytes(input.length / this[0].stats.mean) + "/s");
})
// run async
.run({ async: true });
31 changes: 0 additions & 31 deletions benchmark/xmlBenchmark.ts

This file was deleted.

44 changes: 44 additions & 0 deletions src/ast/BlockElement.ts
@@ -0,0 +1,44 @@
import { InlineContent } from "./InlineContent";
import { parseInline } from "../parser/parseInline";

export class BlockElement {
public tagError?: string;
public labelError?: string;
private _parsedLabel?: InlineContent;

constructor(
readonly tag?: string,
readonly label?: string,
readonly lineDelta: number = 0,
readonly children: BlockElement[] = []
) {}

toJSON() {
return { tag: this.tag, label: this.label, lineDelta: this.lineDelta, children: this.children };
}

get(tag: string | undefined) {
return this.children.find(el => el.tag === tag);
}

getAll(tag: string | undefined) {
return this.children.filter(el => el.tag === tag);
}

get parsedLabel(): InlineContent {
if (!this._parsedLabel) this._parsedLabel = parseInline(this.label || "");
return this._parsedLabel;
}

get source(): string {
return this.tagSource + this.labelSource;
}

get tagSource(): string {
return this.tag ? "#" + this.tag + " " : "";
}

get labelSource(): string {
return this.label || "";
}
}
19 changes: 19 additions & 0 deletions src/ast/InlineContent.ts
@@ -0,0 +1,19 @@
import { SpanElement } from "./SpanElement";

export class InlineContent {
public error?: string;

constructor(readonly content: Array<string | SpanElement> = []) {}

get(tag: string | undefined) {
return this.content.find(el => typeof el === "object" && el.tag === tag) as SpanElement | undefined;
}

getAll(tag: string | undefined) {
return this.content.filter(el => typeof el === "object" && el.tag === tag) as SpanElement[];
}

get source() {
return this.content.map(el => (typeof el === "string" ? el : el.source)).join("");
}
}
18 changes: 18 additions & 0 deletions src/ast/SpanElement.ts
@@ -0,0 +1,18 @@
import { InlineContent } from "./InlineContent";

export class SpanElement {
public error?: string;

constructor(readonly tag: string, readonly sugar: string | null = null, readonly args: InlineContent[] = []) {}

addArg(): InlineContent {
const arg = new InlineContent();
this.args.push(arg);
return arg;
}

get source(): string {
if (this.sugar) return this.sugar + this.args[0].source + this.sugar;
return "#" + this.tag + this.args.map(arg => "[" + arg.source + "]").join("");
}
}
16 changes: 0 additions & 16 deletions src/index.ts

This file was deleted.

135 changes: 0 additions & 135 deletions src/ir/IRBlockHandler.ts

This file was deleted.

0 comments on commit b0fae40

Please sign in to comment.