@scaffdog/engine
Advanced tools
Comparing version 1.1.0 to 1.2.0
@@ -7,2 +7,8 @@ import type { SourceLocation } from '@scaffdog/types'; | ||
}; | ||
export declare class CommentExpr implements Node { | ||
body: string; | ||
loc: SourceLocation; | ||
constructor(body: string, loc: SourceLocation); | ||
toString(): string; | ||
} | ||
export declare class Ident implements Node { | ||
@@ -14,12 +20,35 @@ name: string; | ||
} | ||
export declare class Literal implements Node { | ||
value: string | number | boolean | undefined | null; | ||
export declare class StringLiteral implements Node { | ||
value: string; | ||
quote: string; | ||
loc: SourceLocation; | ||
constructor(value: string | number | boolean | undefined | null, loc: SourceLocation); | ||
constructor(value: string, quote: string, loc: SourceLocation); | ||
toString(): string; | ||
} | ||
export declare class NumberLiteral implements Node { | ||
value: number; | ||
loc: SourceLocation; | ||
constructor(value: number, loc: SourceLocation); | ||
toString(): string; | ||
} | ||
export declare class BooleanLiteral implements Node { | ||
value: boolean; | ||
loc: SourceLocation; | ||
constructor(value: boolean, loc: SourceLocation); | ||
toString(): string; | ||
} | ||
export declare class UndefinedLiteral implements Node { | ||
loc: SourceLocation; | ||
constructor(loc: SourceLocation); | ||
toString(): string; | ||
} | ||
export declare class NullLiteral implements Node { | ||
loc: SourceLocation; | ||
constructor(loc: SourceLocation); | ||
toString(): string; | ||
} | ||
export declare class RawExpr implements Node { | ||
value: string; | ||
body: string; | ||
loc: SourceLocation; | ||
constructor(value: string, loc: SourceLocation); | ||
constructor(body: string, loc: SourceLocation); | ||
toString(): string; | ||
@@ -33,2 +62,3 @@ } | ||
} | ||
export declare type Literal = StringLiteral | NumberLiteral | BooleanLiteral | UndefinedLiteral | NullLiteral; | ||
export declare class LiteralExpr implements Node { | ||
@@ -40,7 +70,10 @@ literal: Literal; | ||
} | ||
export declare type MemberObject = IdentExpr | MemberExpr; | ||
export declare type MemberProperty = IdentExpr | LiteralExpr | MemberExpr; | ||
export declare class MemberExpr implements Node { | ||
object: Node; | ||
property: Node; | ||
object: MemberObject; | ||
property: MemberProperty; | ||
computed: boolean; | ||
loc: SourceLocation; | ||
constructor(object: Node, property: Node); | ||
constructor(object: MemberObject, property: MemberProperty, computed: boolean); | ||
toString(): string; | ||
@@ -51,4 +84,5 @@ } | ||
args: Node[]; | ||
pipe: boolean; | ||
loc: SourceLocation; | ||
constructor(name: string, args: Node[], loc: SourceLocation); | ||
constructor(name: string, args: Node[], pipe: boolean, loc: SourceLocation); | ||
toString(): string; | ||
@@ -55,0 +89,0 @@ } |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.TagExpr = exports.CallExpr = exports.MemberExpr = exports.LiteralExpr = exports.IdentExpr = exports.RawExpr = exports.Literal = exports.Ident = void 0; | ||
exports.TagExpr = exports.CallExpr = exports.MemberExpr = exports.LiteralExpr = exports.IdentExpr = exports.RawExpr = exports.NullLiteral = exports.UndefinedLiteral = exports.BooleanLiteral = exports.NumberLiteral = exports.StringLiteral = exports.Ident = exports.CommentExpr = void 0; | ||
class CommentExpr { | ||
constructor(body, loc) { | ||
this.body = body; | ||
this.loc = loc; | ||
} | ||
toString() { | ||
return `/* ${this.body} */`; | ||
} | ||
} | ||
exports.CommentExpr = CommentExpr; | ||
class Ident { | ||
@@ -14,3 +24,14 @@ constructor(name, loc) { | ||
exports.Ident = Ident; | ||
class Literal { | ||
class StringLiteral { | ||
constructor(value, quote, loc) { | ||
this.value = value; | ||
this.quote = quote; | ||
this.loc = loc; | ||
} | ||
toString() { | ||
return `${this.quote}${this.value}${this.quote}`; | ||
} | ||
} | ||
exports.StringLiteral = StringLiteral; | ||
class NumberLiteral { | ||
constructor(value, loc) { | ||
@@ -21,7 +42,7 @@ this.value = value; | ||
toString() { | ||
return typeof this.value === 'string' ? `"${this.value}"` : `${this.value}`; | ||
return this.value.toString(); | ||
} | ||
} | ||
exports.Literal = Literal; | ||
class RawExpr { | ||
exports.NumberLiteral = NumberLiteral; | ||
class BooleanLiteral { | ||
constructor(value, loc) { | ||
@@ -32,5 +53,33 @@ this.value = value; | ||
toString() { | ||
return this.value; | ||
return this.value.toString(); | ||
} | ||
} | ||
exports.BooleanLiteral = BooleanLiteral; | ||
class UndefinedLiteral { | ||
constructor(loc) { | ||
this.loc = loc; | ||
} | ||
toString() { | ||
return 'undefined'; | ||
} | ||
} | ||
exports.UndefinedLiteral = UndefinedLiteral; | ||
class NullLiteral { | ||
constructor(loc) { | ||
this.loc = loc; | ||
} | ||
toString() { | ||
return 'null'; | ||
} | ||
} | ||
exports.NullLiteral = NullLiteral; | ||
class RawExpr { | ||
constructor(body, loc) { | ||
this.body = body; | ||
this.loc = loc; | ||
} | ||
toString() { | ||
return this.body; | ||
} | ||
} | ||
exports.RawExpr = RawExpr; | ||
@@ -58,5 +107,6 @@ class IdentExpr { | ||
class MemberExpr { | ||
constructor(object, property) { | ||
constructor(object, property, computed) { | ||
this.object = object; | ||
this.property = property; | ||
this.computed = computed; | ||
this.loc = { | ||
@@ -68,3 +118,5 @@ start: object.loc.start, | ||
toString() { | ||
return `${this.object.toString()}[${this.property.toString()}]`; | ||
const obj = this.object.toString(); | ||
const prop = this.property.toString(); | ||
return this.computed ? `${obj}[${prop}]` : `${obj}.${prop}`; | ||
} | ||
@@ -74,5 +126,6 @@ } | ||
class CallExpr { | ||
constructor(name, args, loc) { | ||
constructor(name, args, pipe, loc) { | ||
this.name = name; | ||
this.args = args; | ||
this.pipe = pipe; | ||
this.loc = loc; | ||
@@ -79,0 +132,0 @@ } |
@@ -28,6 +28,6 @@ "use strict"; | ||
if (cur[1] === true) { | ||
acc = acc.trimRight(); | ||
acc = acc.trimEnd(); | ||
} | ||
if (self[index - 1] != null && self[index - 1][2] === true) { | ||
acc += cur[0].trimLeft(); | ||
acc += cur[0].trimStart(); | ||
} | ||
@@ -57,6 +57,6 @@ else { | ||
if (tagExpr.isOpenTrim()) { | ||
output = output.trimLeft(); | ||
output = output.trimStart(); | ||
} | ||
if (tagExpr.isCloseTrim()) { | ||
output = output.trimRight(); | ||
output = output.trimEnd(); | ||
} | ||
@@ -86,13 +86,17 @@ return output; | ||
if (expr.property instanceof ast_1.IdentExpr) { | ||
property = this._compileIdentExpr(expr.property); | ||
if (expr.computed) { | ||
property = this._compileIdentExpr(expr.property); | ||
} | ||
else { | ||
property = expr.property.ident.name; | ||
} | ||
} | ||
else if (expr.property instanceof ast_1.LiteralExpr) { | ||
switch (typeof expr.property.literal.value) { | ||
case 'string': | ||
case 'number': | ||
property = expr.property.literal.value; | ||
break; | ||
default: | ||
throw this._error(`[${expr.property.literal}] is invalid property`, expr.property); | ||
if (expr.property.literal instanceof ast_1.StringLiteral || | ||
expr.property.literal instanceof ast_1.NumberLiteral) { | ||
property = expr.property.literal.value; | ||
} | ||
else { | ||
throw this._error(`[${expr.property.literal}] is invalid property`, expr.property); | ||
} | ||
} | ||
@@ -116,9 +120,9 @@ else if (expr.property instanceof ast_1.MemberExpr) { | ||
_compileLiteralExpr(expr) { | ||
switch (typeof expr.literal.value) { | ||
case 'string': | ||
case 'number': | ||
return `${expr.literal.value}`; | ||
default: | ||
return ''; | ||
if (expr.literal instanceof ast_1.StringLiteral || | ||
expr.literal instanceof ast_1.NumberLiteral) { | ||
return `${expr.literal.value}`; | ||
} | ||
else { | ||
return ''; | ||
} | ||
} | ||
@@ -133,3 +137,13 @@ _compileCallExpr(expr) { | ||
if (expr instanceof ast_1.LiteralExpr) { | ||
return expr.literal.value; | ||
if (expr.literal instanceof ast_1.StringLiteral || | ||
expr.literal instanceof ast_1.NumberLiteral || | ||
expr.literal instanceof ast_1.BooleanLiteral) { | ||
return expr.literal.value; | ||
} | ||
else if (expr.literal instanceof ast_1.NullLiteral) { | ||
return null; | ||
} | ||
else if (expr.literal instanceof ast_1.UndefinedLiteral) { | ||
return undefined; | ||
} | ||
} | ||
@@ -145,3 +159,3 @@ else if (expr instanceof ast_1.IdentExpr) { | ||
} | ||
throw this._error(`"${expr}" is invalid argument`, expr); | ||
throw this._error(`${expr} is invalid argument`, expr); | ||
}); | ||
@@ -148,0 +162,0 @@ return helper(this._context, ...args); |
@@ -5,2 +5,3 @@ export * from './ast'; | ||
export * from './context'; | ||
export * from './formatter'; | ||
export * from './helpers'; | ||
@@ -7,0 +8,0 @@ export * from './parser'; |
@@ -17,2 +17,3 @@ "use strict"; | ||
__exportStar(require("./context"), exports); | ||
__exportStar(require("./formatter"), exports); | ||
__exportStar(require("./helpers"), exports); | ||
@@ -19,0 +20,0 @@ __exportStar(require("./parser"), exports); |
@@ -18,3 +18,5 @@ import type { Node } from './ast'; | ||
private _parseCallExpr; | ||
private _parseLiteralExpr; | ||
private _parseLiteral; | ||
private _error; | ||
} |
@@ -29,3 +29,3 @@ "use strict"; | ||
case 'STRING': | ||
ast.push(new ast_1.RawExpr(this._current.literal, this._current.loc)); | ||
ast.push(new ast_1.RawExpr(this._current.literal.value, this._current.loc)); | ||
break; | ||
@@ -70,3 +70,3 @@ } | ||
case 'IDENT': | ||
expressions.push(this._parseCallExpr()); | ||
expressions.push(this._parseCallExpr(passedPipe)); | ||
break; | ||
@@ -82,3 +82,3 @@ case 'DOT': | ||
if (passedPipe) { | ||
expressions.push(new ast_1.CallExpr(this._current.literal, [], this._current.loc)); | ||
expressions.push(new ast_1.CallExpr(this._current.literal, [], true, this._current.loc)); | ||
} | ||
@@ -93,6 +93,9 @@ else { | ||
case 'BOOLEAN': | ||
case 'NUMBER': | ||
case 'STRING': | ||
case 'NUMBER': | ||
expressions.push(new ast_1.LiteralExpr(new ast_1.Literal(this._current.literal, this._current.loc))); | ||
expressions.push(this._parseLiteralExpr()); | ||
break; | ||
case 'COMMENT': | ||
expressions.push(new ast_1.CommentExpr(this._current.literal, this._current.loc)); | ||
break; | ||
case 'PIPE': | ||
@@ -124,13 +127,19 @@ passedPipe = true; | ||
_parseMemberExpr(object) { | ||
const isBracket = this._current.type === 'OPEN_BRACKET'; | ||
const computed = this._current.type === 'OPEN_BRACKET'; | ||
let property = null; | ||
this._bump(); | ||
if (isBracket) { | ||
if (computed) { | ||
switch (this._current.type) { | ||
case 'NUMBER': | ||
case 'STRING': | ||
case 'NUMBER': | ||
property = new ast_1.LiteralExpr(new ast_1.Literal(this._current.literal, this._current.loc)); | ||
property = this._parseLiteralExpr(); | ||
break; | ||
case 'IDENT': | ||
property = new ast_1.IdentExpr(new ast_1.Ident(this._current.literal, this._current.loc)); | ||
switch (this._next.type) { | ||
case 'DOT': | ||
case 'OPEN_BRACKET': | ||
this._bump(); | ||
return new ast_1.MemberExpr(object, this._parseMemberExpr(property), true); | ||
} | ||
break; | ||
@@ -140,8 +149,2 @@ default: | ||
} | ||
switch (this._next.type) { | ||
case 'DOT': | ||
case 'OPEN_BRACKET': | ||
this._bump(); | ||
return new ast_1.MemberExpr(object, this._parseMemberExpr(property)); | ||
} | ||
if (this._next.type !== 'CLOSE_BRACKET') { | ||
@@ -151,6 +154,2 @@ throw this._error('"]" expected', this._next); | ||
this._bump(); | ||
if (this._next.type === 'OPEN_BRACKET') { | ||
this._bump(); | ||
return this._parseMemberExpr(new ast_1.MemberExpr(object, property)); | ||
} | ||
} | ||
@@ -160,3 +159,3 @@ else { | ||
case 'IDENT': | ||
property = new ast_1.LiteralExpr(new ast_1.Literal(this._current.literal, this._current.loc)); | ||
property = new ast_1.IdentExpr(new ast_1.Ident(this._current.literal, this._current.loc)); | ||
break; | ||
@@ -166,13 +165,15 @@ default: | ||
} | ||
if (this._next.type === 'DOT') { | ||
} | ||
switch (this._next.type) { | ||
case 'OPEN_BRACKET': | ||
case 'DOT': | ||
this._bump(); | ||
return this._parseMemberExpr(new ast_1.MemberExpr(object, property)); | ||
} | ||
return this._parseMemberExpr(new ast_1.MemberExpr(object, property, computed)); | ||
} | ||
if (property != null) { | ||
return new ast_1.MemberExpr(object, property); | ||
return new ast_1.MemberExpr(object, property, computed); | ||
} | ||
throw this._error(`unexpected syntax`, this._current); | ||
} | ||
_parseCallExpr() { | ||
_parseCallExpr(passedPipe) { | ||
const name = this._current.literal; | ||
@@ -183,12 +184,29 @@ const loc = this._current.loc; | ||
while (!this._endOfTokens()) { | ||
if (this._current.type === 'IDENT') { | ||
args.push(new ast_1.IdentExpr(new ast_1.Ident(this._current.literal, this._current.loc))); | ||
switch (this._current.type) { | ||
case 'IDENT': { | ||
const ident = new ast_1.IdentExpr(new ast_1.Ident(this._current.literal, this._current.loc)); | ||
switch (this._next.type) { | ||
case 'DOT': | ||
case 'OPEN_BRACKET': | ||
this._bump(); | ||
args.push(this._parseMemberExpr(ident)); | ||
break; | ||
default: | ||
args.push(ident); | ||
break; | ||
} | ||
break; | ||
} | ||
case 'NULL': | ||
case 'UNDEFINED': | ||
case 'BOOLEAN': | ||
case 'NUMBER': | ||
case 'STRING': | ||
args.push(this._parseLiteralExpr()); | ||
break; | ||
} | ||
else { | ||
args.push(new ast_1.LiteralExpr(new ast_1.Literal(this._current.literal, this._current.loc))); | ||
} | ||
switch (this._next.type) { | ||
case 'CLOSE_TAG': | ||
case 'PIPE': | ||
return new ast_1.CallExpr(name, args, loc); | ||
return new ast_1.CallExpr(name, args, passedPipe, loc); | ||
default: | ||
@@ -200,2 +218,20 @@ this._bump(); | ||
} | ||
_parseLiteralExpr() { | ||
return new ast_1.LiteralExpr(this._parseLiteral()); | ||
} | ||
_parseLiteral() { | ||
switch (this._current.type) { | ||
case 'NULL': | ||
return new ast_1.NullLiteral(this._current.loc); | ||
case 'UNDEFINED': | ||
return new ast_1.UndefinedLiteral(this._current.loc); | ||
case 'BOOLEAN': | ||
return new ast_1.BooleanLiteral(this._current.literal, this._current.loc); | ||
case 'NUMBER': | ||
return new ast_1.NumberLiteral(this._current.literal, this._current.loc); | ||
case 'STRING': | ||
return new ast_1.StringLiteral(this._current.literal.value, this._current.literal.quote, this._current.loc); | ||
} | ||
throw this._error('unexpected syntax', this._current); | ||
} | ||
_error(msg, token) { | ||
@@ -202,0 +238,0 @@ return (0, error_1.error)(msg, { |
@@ -55,3 +55,6 @@ "use strict"; | ||
try { | ||
return esprima.tokenize(input, { loc: true }); | ||
return esprima.tokenize(input, { | ||
loc: true, | ||
comment: true, | ||
}); | ||
} | ||
@@ -90,2 +93,5 @@ catch (e) { | ||
switch (token.type) { | ||
case 'BlockComment': | ||
output.push((0, tokens_1.createToken)('COMMENT', token.value, loc)); | ||
break; | ||
case 'Null': | ||
@@ -95,3 +101,6 @@ output.push((0, tokens_1.createToken)('NULL', null, loc)); | ||
case 'String': | ||
output.push((0, tokens_1.createToken)('STRING', token.value.slice(1, token.value.length - 1), loc)); | ||
output.push((0, tokens_1.createToken)('STRING', { | ||
quote: token.value[0], | ||
value: token.value.slice(1, token.value.length - 1), | ||
}, loc)); | ||
break; | ||
@@ -193,3 +202,6 @@ case 'Boolean': | ||
if (buf.length > 0) { | ||
output.push((0, tokens_1.createToken)('STRING', buf2str(), { | ||
output.push((0, tokens_1.createToken)('STRING', { | ||
quote: '', | ||
value: buf2str(), | ||
}, { | ||
start: bufPos != null | ||
@@ -196,0 +208,0 @@ ? bufPos |
@@ -5,6 +5,10 @@ import type { SourceLocation } from '@scaffdog/types'; | ||
EOF: null; | ||
COMMENT: string; | ||
NULL: null; | ||
UNDEFINED: undefined; | ||
BOOLEAN: boolean; | ||
STRING: string; | ||
STRING: { | ||
quote: string; | ||
value: string; | ||
}; | ||
NUMBER: number; | ||
@@ -11,0 +15,0 @@ IDENT: string; |
{ | ||
"name": "@scaffdog/engine", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "A module of scaffdog template engine.", | ||
@@ -32,4 +32,4 @@ "keywords": [ | ||
"dependencies": { | ||
"@scaffdog/error": "1.1.0", | ||
"@scaffdog/types": "1.1.0", | ||
"@scaffdog/error": "1.2.0", | ||
"@scaffdog/types": "1.2.0", | ||
"change-case": "^4.1.2", | ||
@@ -47,3 +47,3 @@ "dayjs": "^1.10.4", | ||
}, | ||
"gitHead": "43845a86f39e25fdd969bb95affc7e7294cc9ea6" | ||
"gitHead": "be44fc43d7b771c9464b780fc4c8ca18acebfb0c" | ||
} |
46314
32
1285
+ Added@scaffdog/error@1.2.0(transitive)
+ Added@scaffdog/types@1.2.0(transitive)
- Removed@scaffdog/error@1.1.0(transitive)
- Removed@scaffdog/types@1.1.0(transitive)
Updated@scaffdog/error@1.2.0
Updated@scaffdog/types@1.2.0