Skip to content

Commit

Permalink
Merge pull request #6 from fearthecowboy/master
Browse files Browse the repository at this point in the history
Progress on LLC#
  • Loading branch information
olydis authored Mar 23, 2018
2 parents 8d1c71b + 446663f commit f43535c
Show file tree
Hide file tree
Showing 71 changed files with 1,487 additions and 658 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ scope-remodeler/emitter:
``` yaml $(llcsharp)
pipeline:
llcsharp/inferrer:
llcsharp/csinferrer:
scope: llcsharp
input: remodeler
output-artifact: code-model-v2
llcsharp/csnamer:
scope: llcsharp
input: inferrer
input: csinferrer
output-artifact: code-model-v2
llcsharp/generate:
Expand Down
6 changes: 3 additions & 3 deletions inferrer/inferrer.ts → common/process-code-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ModelState } from "#common/model-state";
import { Model } from "remodeler/code-model";
import { deconstruct, fixLeadingNumber, pascalCase, camelCase } from "#common/text-manipulation";

export async function process(service: Host) {
export async function processCodeModel(processExtension: (input: Model, service: Host) => Promise<Model>, service: Host) {
try {
// Get the list of files
const files = await service.ListInputs();
Expand All @@ -17,9 +17,9 @@ export async function process(service: Host) {
const original = await service.ReadFile(files[0]);

// deserialize
const codeModel = await deserialize<Model>(await service.ReadFile(files[0]), files[0]);

let codeModel = await deserialize<Model>(await service.ReadFile(files[0]), files[0]);

codeModel = await processExtension(codeModel, service);

// output the model
await service.WriteFile("code-model-v2.yaml", serialize(codeModel), undefined/*,"code-model-v2"*/);
Expand Down
12 changes: 11 additions & 1 deletion common/text-manipulation.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Dictionary } from "#remodeler/common";

let indentation = " ";

Expand Down Expand Up @@ -112,6 +113,15 @@ export function fixEOL(content: string) {
return content.replace(/\r\n/g, EOL);
}

export function map<T, U>(dictionary: Dictionary<T>, callbackfn: (key: string, value: T) => U, thisArg?: any): U[] {
return Object.getOwnPropertyNames(dictionary).map((key) => callbackfn(key, dictionary[key]));
}
export function selectMany<T>(multiArray: T[][]): T[] {
const result = new Array<T>();
multiArray.map(v => result.push(...v));
return result;
}

export function indent(content: string, factor: number = 1): string {
const i = indentation.repeat(factor);
content = i + fixEOL(content.trim());
Expand All @@ -131,7 +141,7 @@ export function deconstruct(identifier: string): Array<string> {
}

export function fixLeadingNumber(identifier: Array<string>): Array<string> {
if (identifier.length > 0 && /\d+/.exec(identifier[0])) {
if (identifier.length > 0 && /^\d+/.exec(identifier[0])) {
return [...convert(parseInt(identifier[0])), ...identifier.slice(1)];
}
return identifier;
Expand Down
1 change: 1 addition & 0 deletions csharp/code-dom/access-modifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export enum AccessModifier {
ProtectedInternal = "protected internal",
PrivateProtected = "private protected",
Private = "private",
Default = "",
}

const order = [AccessModifier.Public, AccessModifier.Internal, AccessModifier.Protected, AccessModifier.ProtectedInternal, AccessModifier.PrivateProtected, AccessModifier.Private];
Expand Down
23 changes: 14 additions & 9 deletions csharp/code-dom/class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,38 @@ import { Property } from "./property";
import { Namespace } from "./namespace";

export class Class extends Type {
protected classOrStruct: "class" | "struct" = "class";
public isStatic: boolean = false;

protected fields = new Array<Field>();
public partial: boolean = false;

constructor(parent: Namespace, name: string, public parentClass?: Class, interfaces = new Array<Interface>(), genericParameters = new Array<string>(), where?: string) {
super(parent, name, interfaces, genericParameters, where);
parent.addClass(this);
constructor(namespace: Namespace, name: string, public parent?: Class, objectIntializer?: Partial<Class>) {
super(namespace, name);
this.apply(objectIntializer);
namespace.addClass(this);
}

public get declaration(): string {
const colon = (this.parentClass || this.interfaces.length > 0) ? ' : ' : '';
const comma = (this.parentClass && this.interfaces.length > 0) ? ", " : '';
const colon = (this.parent || this.interfaces.length > 0) ? ' : ' : '';
const comma = (this.parent && this.interfaces.length > 0) ? ", " : '';

const extendsClass = this.parentClass ? this.parentClass.fullName : '';
const extendsClass = this.parent ? this.parent.fullName : '';
const implementsInterfaces = this.interfaces.map(v => v.fullName).join(', ');
const description = comment(this.description, docCommentPrefix);
const partial = this.partial ? "partial " : "";
const stat = this.isStatic ? "static " : "";

return `
${description}
${this.accessModifier} ${partial}class ${this.name}${colon}${extendsClass}${comma}${implementsInterfaces}
${this.accessModifier} ${stat}${partial}${this.classOrStruct} ${this.name}${colon}${extendsClass}${comma}${implementsInterfaces}
`.trim();
}

public get implementation(): string {
const fields = this.fields.sort(sortByName).map(m => m.declaration).join(EOL);
const methods = this.methods.sort(sortByName).map(m => m.implementation).join(EOL);
const properties = this.properties.sort(sortByName).map(m => m.declaration).join(EOL);

return `
${this.declaration} {
${indent(fields, 1)}
Expand All @@ -53,4 +58,4 @@ ${indent(methods, 1)}
${this.fullName}
`.trim()
}
}
}
20 changes: 20 additions & 0 deletions csharp/code-dom/constructor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Method } from "./method";
import { docComment, EOL, CommaChar, indent } from "#common/text-manipulation";
import { Class } from "./class";

export class Constructor extends Method {
constructor(protected containingClass: Class, objectIntializer?: Partial<Method>) {
super(containingClass.name);
this.apply(objectIntializer);
}

public get declaration(): string {
const parameterDeclaration = this.parameters.joinWith(p => p.declaration, CommaChar);

return `
${this.summaryDocumentation}
${this.parameterDocumentation}
${this.accessModifier} ${this.name}(${parameterDeclaration})
`.trim();
}
}
18 changes: 18 additions & 0 deletions csharp/code-dom/doc-comments.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { EOL } from "#common/text-manipulation";

export const summary = (text: string): string => xmlize("summary", text);


export function xmlize(element: string, text: string): string {
if (text) {
if (text.length < 80 && text.indexOf(EOL) === -1) {
return `<${element}>${text.trim()}</${element}>`;
}
return `
<${element}>
${text}
</${element}>
`.trim();
}
return text;
}
7 changes: 7 additions & 0 deletions csharp/code-dom/expression.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
export interface Expression {
value: string;
}

export class StringExpression implements Expression {
public value: string;
constructor(value: string) {
this.value = `@"${value.replace(/"/g, '""')}"`;
}
}
27 changes: 23 additions & 4 deletions csharp/code-dom/field.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import { TypeDeclaration } from "./type-declaration";
import { Expression } from "#csharp/code-dom/expression";
import { AccessModifier } from "#csharp/code-dom/access-modifier";
import { Initializer } from "#csharp/code-dom/initializer";

export class Field implements Expression {
public visibility = "public";
export class Field extends Initializer implements Expression {
public visibility: AccessModifier = AccessModifier.Public;
public isStatic: boolean = false;
public description: string = "";

constructor(public name: string, public type: TypeDeclaration) {
constructor(public name: string, public type: TypeDeclaration, objectInitializer?: Partial<Field>) {
super();
this.apply(objectInitializer);
}

public get declaration(): string {
Expand All @@ -14,4 +20,17 @@ export class Field implements Expression {
public get value(): string {
return `${this.name}`;
}
}
}

export class ConstantField extends Field {
constructor(name: string, type: TypeDeclaration, public valueExpression: Expression, objectInitializer?: Partial<ConstantField>) {
super(name, type);
this.apply(objectInitializer);
}

public get declaration(): string {
const stat = this.isStatic ? " static " : " ";
return `${this.visibility}${stat}${this.type.use} ${this.name} = ${this.valueExpression.value};`
}
}

12 changes: 12 additions & 0 deletions csharp/code-dom/initializer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export class Initializer {

protected apply<T>(initializer?: Partial<T>) {
if (initializer) {
for (const i in (<any>initializer)) {
//if ((<any>initializer)[i]) {
(<any>this)[i] = (<any>initializer)[i]
//};
}
}
}
}
4 changes: 3 additions & 1 deletion csharp/code-dom/interface-property.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { TypeDeclaration } from "./type-declaration";
import { AccessModifier, highestAccess } from "#csharp/code-dom/access-modifier";
import { Property } from "./property";
import { Interface } from "#csharp/code-dom/interface";

export class InterfaceProperty extends Property {
public readVisibility = AccessModifier.Public;
public writeVisibility = AccessModifier.Public;

constructor(name: string, type: TypeDeclaration) {
constructor(name: string, type: TypeDeclaration, objectInitializer?: Partial<InterfaceProperty>) {
super(name, type);
this.apply(objectInitializer);
}

public get declaration(): string {
Expand Down
5 changes: 3 additions & 2 deletions csharp/code-dom/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import { comment, docCommentPrefix, sortByName, indent, EOL } from "#common/text
import { Namespace } from "./namespace";

export class Interface extends Type {
constructor(parent: Namespace, name: string, interfaces = new Array<Interface>(), genericParameters = new Array<string>(), where?: string) {
super(parent, name, interfaces, genericParameters, where);
constructor(parent: Namespace, name: string, objectIntializer?: Partial<Interface>) {
super(parent, name);
this.apply(objectIntializer);
parent.addInterface(this);
}

Expand Down
57 changes: 47 additions & 10 deletions csharp/code-dom/method.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,79 @@ import { TypeDeclaration } from "./type-declaration";
import * as mscorlib from "./mscorlib";
import { AccessModifier } from "#csharp/code-dom/access-modifier";
import { docCommentPrefix, comment, indent, EOL, CommaChar, join, joinComma, docComment } from "#common/text-manipulation";
import { xmlize, summary } from "#csharp/code-dom/doc-comments";


export class Method extends Statements {
private parameters = new Array<Parameter>();
public parameters = new Array<Parameter>();
public accessModifier = AccessModifier.Public;
public isAsync = false;
public isOverride = false;
public isNew = false;
public description: string = "";
public isStatic: boolean = false;

constructor(public name: string, private returnType: TypeDeclaration = mscorlib.Void) {
constructor(public name: string, protected returnType: TypeDeclaration = mscorlib.Void, objectIntializer?: Partial<Method>) {
super();
this.apply(objectIntializer);
}

public addParameter(parameter: Parameter): Parameter {
this.parameters.push(parameter);
return parameter;
}

protected get summaryDocumentation(): string {
return docComment(summary(this.description));
}

protected get parameterDocumentation(): string {
return docComment(this.parameters.joinWith(p => p.comment, EOL));
}

public get declaration(): string {
const description = docComment(this.description);
const params = docComment(this.parameters.joinWith(p => p.comment, EOL));
const parameterDeclaration = this.parameters.joinWith(p => p.declaration, CommaChar);

const overrideOrNew = this.isOverride ? " override " : this.isNew ? " new " : " ";
const stat = this.isStatic ? " static" : "";
const asynch = this.isAsync ? "async " : "";
return `
${description}
${params}
${this.accessModifier} ${asynch}${this.returnType.use} ${this.name}(${parameterDeclaration}) {
${indent(super.implementation, 1)}
}
${this.summaryDocumentation}
${this.parameterDocumentation}
${this.accessModifier}${stat}${overrideOrNew}${asynch}${this.returnType.use} ${this.name}(${parameterDeclaration})
`.trim();
}

public get implementation(): string {
return `
${this.declaration}
{
${indent(super.implementation)}
}`.trim();

}
}

export class PartialMethod extends Method {
constructor(name: string, returnType: TypeDeclaration = mscorlib.Void, objectIntializer?: Partial<PartialMethod>) {
super(name, returnType);
this.apply(objectIntializer);
}

public get declaration(): string {
const parameterDeclaration = this.parameters.joinWith(p => p.declaration, CommaChar);
const stat = this.isStatic ? "static " : "";
const asynch = this.isAsync ? "async " : "";
return `
${this.summaryDocumentation}
${this.parameterDocumentation}
partial ${stat}${asynch}${this.returnType.use} ${this.name}(${parameterDeclaration})
`.trim();
}

public get implementation(): string {
return `
${this.declaration};`.trim();

}

}
9 changes: 3 additions & 6 deletions csharp/code-dom/mscorlib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ export class LibraryType implements TypeDeclaration {
return `${this.fullName}`;
}

public validation(propertyName: string): string {
return '';
}

public get implementation(): string {
return ``;
}
Expand All @@ -28,8 +24,9 @@ export const Float: TypeDeclaration = new LibraryType("float");
export const Date: TypeDeclaration = new LibraryType("DateTime");
export const Duration: TypeDeclaration = new LibraryType("TimeSpan");
export const Binary: TypeDeclaration = new LibraryType("byte[]");

export const Bool: TypeDeclaration = new LibraryType("bool");
export const Object: TypeDeclaration = new LibraryType("object");
export const ThisObject: TypeDeclaration = new LibraryType("this object");

export const Task: TypeDeclaration = new LibraryType("System.Threading.Tasks.Task");
export const CancellationToken: TypeDeclaration = new LibraryType("System.Threading.CancellationToken");
export const EventListener: TypeDeclaration = new LibraryType("Microsoft.Rest.EventListener");
Loading

0 comments on commit f43535c

Please sign in to comment.