Skip to content
This repository has been archived by the owner on Nov 4, 2023. It is now read-only.

Commit

Permalink
feat: improve concept parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
cdaringe committed Jan 17, 2021
1 parent 94e9803 commit 63881f2
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 3 deletions.
9 changes: 8 additions & 1 deletion src/ir/ir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ export type Type =
| Property
| Struct
| Sym
| Union;
| Union
| TypeDecl;

export type Param = {
name: string;
Expand Down Expand Up @@ -138,6 +139,12 @@ export type Struct = StructLike & IRCommon<"struct">;

export const struct = factory<Struct>("struct");

export type TypeDecl = {
name: string;
type: Type;
} & IRCommon<"typedecl">;
export const typdecl = factory<TypeDecl>("typedecl");

export type Intf = {
name: string;
membersByName: Record<string, Property>;
Expand Down
4 changes: 3 additions & 1 deletion src/ir/printer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { intf, testIsType, Type } from "./ir";
import { Type } from "./ir";

export const printInner = (t: Type): string => {
switch (t.__type) {
Expand Down Expand Up @@ -56,6 +56,8 @@ export const printInner = (t: Type): string => {
case "boolean":
case "any":
return t.__type;
case "typedecl":
return `type ${t.name} = ${print(t.type)}`;
default:
// exhaustive match check
return ((arg: never) => {
Expand Down
69 changes: 68 additions & 1 deletion src/scrape/concepts.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,70 @@
import { IDocument, IElement } from "happy-dom";
import { query } from "../batteries/dom/dom-extensions";
import { asMarkdown } from "../batteries/markdown";
import { fn, property, struct, testIsType } from "../ir/ir";
import {
fn,
intf,
num,
ofLua,
property,
struct,
testIsType,
typdecl,
Type,
union,
} from "../ir/ir";
import { parseParam } from "./classes";

/**
*
* As a SimpleItemStack
* As a LuaTechnologyPrototype: If you have a reference to LuaTechnologyPrototype, you may pass it directly.
* As a LuaForce: If you have a reference to LuaForce, you may pass it directly.
*/
export const parseSpecificationConceptOption = (el: IElement) => {
const noAsA = el.textContent.replace(/As an? /, "");
const [t, ...descriptionParts] = noAsA.split(":");
const description = descriptionParts.join(":");
const type = ofLua(t);
type.description = description;
return type;
};

export const parseSpecificationConcept = ({
name,
description,
el,
}: {
name: string;
description: string;
el: IElement;
}) => {
let members: Type[] = [];
// edge-case - Position
// position only has examples. damn it! but it's so ubiquitous we need to support it
if (name === "Position") {
members = [
intf({
name: "Position",
membersByName: {
x: property({ name: "x", type: num({ description: "x coord" }) }),
y: property({ name: "y", type: num({ description: "x coord" }) }),
},
}),
];
} else {
const ul = query(el, ".element-content ul", "failed to find content el");
members = ul.children.map(parseSpecificationConceptOption);
}
return typdecl({
name,
description,
type: union({
members,
}),
});
};

export const scrapeConcept = (el: IElement) => {
const content = query(el, ".element-content", "missing content el");
let description = "";
Expand All @@ -19,6 +80,12 @@ export const scrapeConcept = (el: IElement) => {
const fields = fieldListEl
? fieldListEl.children.filter((el) => el.textContent).map(parseParam)
: [];
if (description.match(/may be specified/))
return parseSpecificationConcept({
name,
description,
el,
});
return struct({
name,
description,
Expand Down

0 comments on commit 63881f2

Please sign in to comment.