Comparing version 3.0.0-3 to 3.0.0-4
@@ -1,9 +0,9 @@ | ||
import Decorate from "./decorate"; | ||
import { NodeDefinition } from "./interfaces"; | ||
import { Decorators, NodeDefinition } from "./interfaces"; | ||
import Tree from "./tree"; | ||
export default class Highlight extends Decorate { | ||
export default class Highlight<R> { | ||
private static findNodeDefinition; | ||
private mode; | ||
constructor(decorators: any, mode: string); | ||
source(source: string, tree: Tree, definitions: NodeDefinition[]): string | string[]; | ||
private decorators; | ||
constructor(decorators: Decorators<R>, mode: string); | ||
source(source: string, tree: Tree, definitions: NodeDefinition[]): R[]; | ||
private decorateNode; | ||
@@ -10,0 +10,0 @@ private decorateText; |
@@ -7,6 +7,5 @@ "use strict"; | ||
const astNodePoint_1 = __importDefault(require("./astNodePoint")); | ||
const decorate_1 = __importDefault(require("./decorate")); | ||
class Highlight extends decorate_1.default { | ||
class Highlight { | ||
constructor(decorators, mode) { | ||
super(decorators); | ||
this.decorators = decorators; | ||
this.mode = mode; | ||
@@ -37,3 +36,3 @@ } | ||
if (char === `\n`) { | ||
decoratedStrings.push(`\n`); | ||
decoratedStrings.push(this.decorators.newLine()); | ||
wordInRange = ""; | ||
@@ -71,12 +70,7 @@ if (nodes[currentToken].type === "\n") { | ||
// if the current char is not part of any token, then append it to the array | ||
decoratedStrings.push(char); | ||
decoratedStrings.push(this.decorators.word(char)); | ||
} | ||
columnAtLine++; | ||
} | ||
if (this.mode === "console") { | ||
return decoratedStrings.join(""); | ||
} | ||
else { | ||
return decoratedStrings; | ||
} | ||
return decoratedStrings; | ||
} | ||
@@ -135,8 +129,3 @@ decorateNode(text, node, definitions) { | ||
} | ||
if (this.mode === "console") { | ||
return decoratedStrings.join(""); | ||
} | ||
else { | ||
return decoratedStrings; | ||
} | ||
return decoratedStrings; | ||
} | ||
@@ -146,52 +135,50 @@ decorateText(text, type, definition) { | ||
case "`": | ||
return super.backtick(text); | ||
return this.decorators.backtick(text); | ||
case "|": | ||
return super.pipeline(text, definition); | ||
return this.decorators.pipeline(text, definition); | ||
case "&&": | ||
return super.operator(text, definition); | ||
return this.decorators.operator(text, definition); | ||
case "(": | ||
case "$(": | ||
return super.openingParens(text); | ||
case ")": | ||
return this.decorators.parens(text); | ||
case "{": | ||
return super.openingBraces(text); | ||
case "}": | ||
return super.closingBraces(text); | ||
case ")": | ||
return super.closingParens(text, definition); | ||
return this.decorators.braces(text); | ||
case ">": | ||
case ">&": | ||
return super.redirect(text, definition); | ||
return this.decorators.redirect(text, definition); | ||
case ";": | ||
return super.semicolon(text, definition); | ||
return this.decorators.semicolon(text, definition); | ||
case "=": | ||
return super.equal(text, definition); | ||
return this.decorators.equal(text, definition); | ||
case "command_name": | ||
case "program": | ||
return super.program(text, definition); | ||
return this.decorators.program(text, definition); | ||
case "comment": | ||
return super.comment(text, definition); | ||
return this.decorators.comment(text, definition); | ||
case "do": | ||
return super.do(text, definition); | ||
return this.decorators.do(text, definition); | ||
case "done": | ||
return super.done(text, definition); | ||
return this.decorators.done(text, definition); | ||
case "file_descriptor": | ||
return super.fileDescriptor(text, definition); | ||
return this.decorators.fileDescriptor(text, definition); | ||
case "for": | ||
return super.for(text, definition); | ||
return this.decorators.for(text, definition); | ||
case "function": | ||
return super.fn(text, definition); | ||
return this.decorators.fn(text, definition); | ||
case "in": | ||
return super.in(text, definition); | ||
return this.decorators.in(text, definition); | ||
case "option": | ||
return super.option(text, definition); | ||
return this.decorators.option(text, definition); | ||
case "optionArg": | ||
return super.optionArg(text, definition); | ||
return this.decorators.optionArg(text, definition); | ||
case "subcommand": | ||
return super.subcommand(text, definition); | ||
return this.decorators.subcommand(text, definition); | ||
case "variable_name": | ||
return super.variableName(text, definition); | ||
return this.decorators.variableName(text, definition); | ||
case "while": | ||
return super.while(text, definition); | ||
return this.decorators.while(text, definition); | ||
default: | ||
return super.word(text, definition); | ||
return this.decorators.word(text, definition); | ||
} | ||
@@ -198,0 +185,0 @@ } |
import ASTNode from "./astNode"; | ||
import ASTNodePoint from "./astNodePoint"; | ||
import Highlight from "./highlight"; | ||
import { NodeDefinition } from "./interfaces"; | ||
import { NodeDefinition, Decorators } from "./interfaces"; | ||
import Tree from "./tree"; | ||
import TreeCursor from "./treeCursor"; | ||
import { OptionArgNodeDefinition, OptionNodeDefinition, ProgramNodeDefinition, SubcommandNodeDefinition } from "./nodeDefinition"; | ||
export { ASTNode, OptionArgNodeDefinition, OptionNodeDefinition, ProgramNodeDefinition, SubcommandNodeDefinition, Tree, TreeCursor, ASTNodePoint, Highlight, NodeDefinition }; | ||
export { ASTNode, OptionArgNodeDefinition, OptionNodeDefinition, ProgramNodeDefinition, SubcommandNodeDefinition, Tree, TreeCursor, ASTNodePoint, Highlight, NodeDefinition, Decorators }; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -23,2 +23,28 @@ import { ArgumentSchema, OptionSchema, ProgramSchema, SubcommandSchema } from "kmdr-parser"; | ||
} | ||
export interface Decorators<T> { | ||
backtick(text: string, definition?: NodeDefinition): T; | ||
braces(text: string, definition?: NodeDefinition): T; | ||
comment(text: string, definition?: NodeDefinition): T; | ||
do(text: string, definition?: NodeDefinition): T; | ||
done(text: string, definition?: NodeDefinition): T; | ||
equal(text: string, definition?: NodeDefinition): T; | ||
fileDescriptor(text: string, definition?: NodeDefinition): T; | ||
fn(text: string, definition?: NodeDefinition): T; | ||
for(text: string, definition?: NodeDefinition): T; | ||
in(text: string, definition?: NodeDefinition): T; | ||
newLine(): T; | ||
option(text: string, definition?: NodeDefinition): T; | ||
optionArg(text: string, definition?: NodeDefinition): T; | ||
operator(text: string, definition?: NodeDefinition): T; | ||
parens(test: string, definition?: NodeDefinition): T; | ||
pipeline(text: string, definition?: NodeDefinition): T; | ||
program(text: string, definition?: NodeDefinition): T; | ||
redirect(text: string, definition?: NodeDefinition): T; | ||
semicolon(text: string, definition?: NodeDefinition): T; | ||
space(): T; | ||
subcommand(text: string, definition?: NodeDefinition): T; | ||
variableName(text: string, definition?: NodeDefinition): T; | ||
while(text: string, definition?: NodeDefinition): T; | ||
word(text: string, definition?: NodeDefinition): T; | ||
} | ||
//# sourceMappingURL=interfaces.d.ts.map |
{ | ||
"name": "kmdr-ast", | ||
"version": "3.0.0-3", | ||
"version": "3.0.0-4", | ||
"description": "Traverse the AST of an explanation by kmdr", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
import { ASTNode } from "."; | ||
import ASTNodePoint from "./astNodePoint"; | ||
import Decorate from "./decorate"; | ||
import { NodeDefinition } from "./interfaces"; | ||
import { Decorators, NodeDefinition } from "./interfaces"; | ||
import Tree from "./tree"; | ||
export default class Highlight extends Decorate { | ||
export default class Highlight<R> { | ||
private static findNodeDefinition( | ||
@@ -29,5 +28,6 @@ node: ASTNode, | ||
private mode: string; | ||
private decorators: Decorators<R>; | ||
constructor(decorators: any, mode: string) { | ||
super(decorators); | ||
constructor(decorators: Decorators<R>, mode: string) { | ||
this.decorators = decorators; | ||
this.mode = mode; | ||
@@ -40,3 +40,3 @@ } | ||
definitions: NodeDefinition[] | ||
): string | string[] { | ||
): R[] { | ||
const nodes = tree.flatten(); | ||
@@ -49,3 +49,3 @@ | ||
const decoratedStrings: string[] = []; | ||
const decoratedStrings: R[] = []; | ||
let columnAtLine = 0; | ||
@@ -57,3 +57,3 @@ | ||
if (char === `\n`) { | ||
decoratedStrings.push(`\n`); | ||
decoratedStrings.push(this.decorators.newLine()); | ||
wordInRange = ""; | ||
@@ -101,11 +101,8 @@ | ||
// if the current char is not part of any token, then append it to the array | ||
decoratedStrings.push(char); | ||
decoratedStrings.push(this.decorators.word(char)); | ||
} | ||
columnAtLine++; | ||
} | ||
if (this.mode === "console") { | ||
return decoratedStrings.join(""); | ||
} else { | ||
return decoratedStrings; | ||
} | ||
return decoratedStrings; | ||
} | ||
@@ -117,3 +114,3 @@ | ||
definitions: NodeDefinition[] | ||
) { | ||
): R[] { | ||
const decoratedStrings = []; | ||
@@ -191,68 +188,63 @@ const { startPosition } = node; | ||
} | ||
if (this.mode === "console") { | ||
return decoratedStrings.join(""); | ||
} else { | ||
return decoratedStrings; | ||
} | ||
return decoratedStrings; | ||
} | ||
private decorateText<T>( | ||
private decorateText( | ||
text: string, | ||
type: string, | ||
definition?: NodeDefinition | ||
): T | string { | ||
): R { | ||
switch (type) { | ||
case "`": | ||
return super.backtick(text); | ||
return this.decorators.backtick(text); | ||
case "|": | ||
return super.pipeline(text, definition); | ||
return this.decorators.pipeline(text, definition); | ||
case "&&": | ||
return super.operator(text, definition); | ||
return this.decorators.operator(text, definition); | ||
case "(": | ||
case "$(": | ||
return super.openingParens(text); | ||
case ")": | ||
return this.decorators.parens(text); | ||
case "{": | ||
return super.openingBraces(text); | ||
case "}": | ||
return super.closingBraces(text); | ||
case ")": | ||
return super.closingParens(text, definition); | ||
return this.decorators.braces(text); | ||
case ">": | ||
case ">&": | ||
return super.redirect(text, definition); | ||
return this.decorators.redirect(text, definition); | ||
case ";": | ||
return super.semicolon(text, definition); | ||
return this.decorators.semicolon(text, definition); | ||
case "=": | ||
return super.equal(text, definition); | ||
return this.decorators.equal(text, definition); | ||
case "command_name": | ||
case "program": | ||
return super.program(text, definition); | ||
return this.decorators.program(text, definition); | ||
case "comment": | ||
return super.comment(text, definition); | ||
return this.decorators.comment(text, definition); | ||
case "do": | ||
return super.do(text, definition); | ||
return this.decorators.do(text, definition); | ||
case "done": | ||
return super.done(text, definition); | ||
return this.decorators.done(text, definition); | ||
case "file_descriptor": | ||
return super.fileDescriptor(text, definition); | ||
return this.decorators.fileDescriptor(text, definition); | ||
case "for": | ||
return super.for(text, definition); | ||
return this.decorators.for(text, definition); | ||
case "function": | ||
return super.fn(text, definition); | ||
return this.decorators.fn(text, definition); | ||
case "in": | ||
return super.in(text, definition); | ||
return this.decorators.in(text, definition); | ||
case "option": | ||
return super.option(text, definition); | ||
return this.decorators.option(text, definition); | ||
case "optionArg": | ||
return super.optionArg(text, definition); | ||
return this.decorators.optionArg(text, definition); | ||
case "subcommand": | ||
return super.subcommand(text, definition); | ||
return this.decorators.subcommand(text, definition); | ||
case "variable_name": | ||
return super.variableName(text, definition); | ||
return this.decorators.variableName(text, definition); | ||
case "while": | ||
return super.while(text, definition); | ||
return this.decorators.while(text, definition); | ||
default: | ||
return super.word(text, definition); | ||
return this.decorators.word(text, definition); | ||
} | ||
} | ||
} |
import ASTNode from "./astNode"; | ||
import ASTNodePoint from "./astNodePoint"; | ||
import Highlight from "./highlight"; | ||
import { NodeDefinition } from "./interfaces"; | ||
import { NodeDefinition, Decorators } from "./interfaces"; | ||
import Tree from "./tree"; | ||
@@ -25,3 +25,4 @@ import TreeCursor from "./treeCursor"; | ||
Highlight, | ||
NodeDefinition | ||
NodeDefinition, | ||
Decorators | ||
}; |
@@ -31,1 +31,28 @@ import { | ||
} | ||
export interface Decorators<T> { | ||
backtick(text: string, definition?: NodeDefinition): T; | ||
braces(text: string, definition?: NodeDefinition): T; | ||
comment(text: string, definition?: NodeDefinition): T; | ||
do(text: string, definition?: NodeDefinition): T; | ||
done(text: string, definition?: NodeDefinition): T; | ||
equal(text: string, definition?: NodeDefinition): T; | ||
fileDescriptor(text: string, definition?: NodeDefinition): T; | ||
fn(text: string, definition?: NodeDefinition): T; | ||
for(text: string, definition?: NodeDefinition): T; | ||
in(text: string, definition?: NodeDefinition): T; | ||
newLine(): T; | ||
option(text: string, definition?: NodeDefinition): T; | ||
optionArg(text: string, definition?: NodeDefinition): T; | ||
operator(text: string, definition?: NodeDefinition): T; | ||
parens(test: string, definition?: NodeDefinition): T; | ||
pipeline(text: string, definition?: NodeDefinition): T; | ||
program(text: string, definition?: NodeDefinition): T; | ||
redirect(text: string, definition?: NodeDefinition): T; | ||
semicolon(text: string, definition?: NodeDefinition): T; | ||
space(): T; | ||
subcommand(text: string, definition?: NodeDefinition): T; | ||
variableName(text: string, definition?: NodeDefinition): T; | ||
while(text: string, definition?: NodeDefinition): T; | ||
word(text: string, definition?: NodeDefinition): T; | ||
} |
@@ -1,76 +0,99 @@ | ||
import { NodeDefinition } from "../src/interfaces"; | ||
import { Decorators, NodeDefinition } from "../src/interfaces"; | ||
export const consoleDecorators = { | ||
backtick: (text: string) => { | ||
export default class ConsoleDecorators implements Decorators<string> { | ||
public backtick(text: string) { | ||
return `\u001b[97m${text}\u001b[0m`; | ||
}, | ||
closingBraces: (text: string) => { | ||
} | ||
public braces(text: string) { | ||
return `\u001b[92m${text}\u001b[0m`; | ||
}, | ||
closingParens: (text: string) => { | ||
return `\u001b[35m${text}\u001b[0m`; | ||
}, | ||
comment: (text: string, definition?: NodeDefinition) => { | ||
} | ||
public comment(text: string, definition?: NodeDefinition) { | ||
return `\u001b[90m${text}\u001b[0m`; | ||
}, | ||
do: (text: string, definition?: NodeDefinition) => { | ||
} | ||
public do(text: string, definition?: NodeDefinition) { | ||
return `\u001b[35m${text}\u001b[0m`; | ||
}, | ||
done: (text: string, definition?: NodeDefinition) => { | ||
} | ||
public done(text: string, definition?: NodeDefinition) { | ||
return `\u001b[35m${text}\u001b[0m`; | ||
}, | ||
equal: (text: string, definition?: NodeDefinition) => { | ||
} | ||
public equal(text: string, definition?: NodeDefinition) { | ||
return `\u001b[35m${text}\u001b[0m`; | ||
}, | ||
fileDescriptor: (text: string, definition?: NodeDefinition) => { | ||
} | ||
public fileDescriptor(text: string, definition?: NodeDefinition) { | ||
return `\u001b[96m${text}\u001b[0m`; | ||
}, | ||
fn: (text: string, definition?: NodeDefinition) => { | ||
} | ||
public fn(text: string, definition?: NodeDefinition) { | ||
return `\u001b[35m${text}\u001b[0m`; | ||
}, | ||
for: (text: string, definition?: NodeDefinition) => { | ||
} | ||
public for(text: string, definition?: NodeDefinition) { | ||
return `\u001b[35m${text}\u001b[0m`; | ||
}, | ||
in: (text: string, definition?: NodeDefinition) => { | ||
} | ||
public in(text: string, definition?: NodeDefinition) { | ||
return `\u001b[35m${text}\u001b[0m`; | ||
}, | ||
openingBraces: (text: string) => { | ||
return `\u001b[92m${text}\u001b[0m`; | ||
}, | ||
openingParens: (text: string) => { | ||
return `\u001b[35m${text}\u001b[0m`; | ||
}, | ||
operator: (text: string, definition?: NodeDefinition) => { | ||
} | ||
public newLine() { | ||
return "\n"; | ||
} | ||
public operator(text: string, definition?: NodeDefinition) { | ||
return `\u001b[34m${text}\u001b[0m`; | ||
}, | ||
option: (text: string, definition?: NodeDefinition) => { | ||
} | ||
public option(text: string, definition?: NodeDefinition) { | ||
return `\u001b[35m${text}\u001b[0m`; | ||
}, | ||
optionArg: (text: string, definition?: NodeDefinition) => { | ||
} | ||
public optionArg(text: string, definition?: NodeDefinition) { | ||
return `\u001b[31m${text}\u001b[0m`; | ||
}, | ||
pipeline: (text: string, definition?: NodeDefinition) => { | ||
} | ||
public parens(text: string) { | ||
return `\u001b[35m${text}\u001b[0m`; | ||
} | ||
public pipeline(text: string, definition?: NodeDefinition) { | ||
return `\u001b[91m${text}\u001b[0m`; | ||
}, | ||
program: (text: string, definition?: NodeDefinition) => { | ||
} | ||
public program(text: string, definition?: NodeDefinition) { | ||
return `\u001b[32;1m${text}\u001b[0m`; | ||
}, | ||
redirect: (text: string, definition?: NodeDefinition) => { | ||
} | ||
public redirect(text: string, definition?: NodeDefinition) { | ||
return `\u001b[34m${text}\u001b[0m`; | ||
}, | ||
semicolon: (text: string, definition?: NodeDefinition) => { | ||
} | ||
public semicolon(text: string, definition?: NodeDefinition) { | ||
return `\u001b[94m${text}\u001b[0m`; | ||
}, | ||
subcommand: (text: string, definition?: NodeDefinition) => { | ||
} | ||
public space() { | ||
return " "; | ||
} | ||
public subcommand(text: string, definition?: NodeDefinition) { | ||
return `\u001b[36;1m${text}\u001b[0m`; | ||
}, | ||
variableName: (text: string, definition?: NodeDefinition) => { | ||
} | ||
public variableName(text: string, definition?: NodeDefinition) { | ||
return `\u001b[36;1m${text}\u001b[0m`; | ||
}, | ||
while: (text: string) => { | ||
} | ||
public while(text: string) { | ||
return `\u001b[35m${text}\u001b[0m`; | ||
}, | ||
word: (text: string, definition?: NodeDefinition) => { | ||
} | ||
public word(text: string, definition?: NodeDefinition) { | ||
return text; | ||
} | ||
}; | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
709236
114
4893