@mrleebo/prisma-ast
Advanced tools
Comparing version 0.1.2 to 0.1.3
@@ -6,7 +6,7 @@ export declare function getSchema(source: string): Schema; | ||
} | ||
export declare type Block = Model | Datasource | Generator | Enum | Comment; | ||
export declare type Block = Model | Datasource | Generator | Enum | Comment | Break; | ||
export interface Model { | ||
type: 'model'; | ||
name: string; | ||
properties: Array<Property | Comment>; | ||
properties: Array<Property | Comment | Break>; | ||
} | ||
@@ -16,3 +16,3 @@ export interface Datasource { | ||
name: string; | ||
assignments: Array<Assignment | Comment>; | ||
assignments: Array<Assignment | Comment | Break>; | ||
} | ||
@@ -22,3 +22,3 @@ export interface Generator { | ||
name: string; | ||
assignments: Array<Assignment | Comment>; | ||
assignments: Array<Assignment | Comment | Break>; | ||
} | ||
@@ -28,3 +28,3 @@ export interface Enum { | ||
name: string; | ||
enumerators: Array<Enumerator | Comment>; | ||
enumerators: Array<Enumerator | Comment | Break>; | ||
} | ||
@@ -35,2 +35,5 @@ export interface Comment { | ||
} | ||
export interface Break { | ||
type: 'break'; | ||
} | ||
export declare type Property = GroupedModelAttribute | ModelAttribute | Field; | ||
@@ -37,0 +40,0 @@ export interface Assignment { |
import { CstParser } from 'chevrotain'; | ||
export declare class PrismaParser extends CstParser { | ||
constructor(); | ||
private break; | ||
private keyedArg; | ||
@@ -5,0 +6,0 @@ private array; |
@@ -181,2 +181,7 @@ 'use strict'; | ||
_this = _CstParser.call(this, allTokens) || this; | ||
_this["break"] = _this.RULE('break', function () { | ||
_this.CONSUME1(LineBreak); | ||
_this.CONSUME2(LineBreak); | ||
}); | ||
_this.keyedArg = _this.RULE('keyedArg', function () { | ||
@@ -388,2 +393,8 @@ _this.CONSUME(Identifier, { | ||
ALT: function ALT() { | ||
return _this.SUBRULE(_this["break"], { | ||
LABEL: 'list' | ||
}); | ||
} | ||
}, { | ||
ALT: function ALT() { | ||
return _this.CONSUME2(LineBreak); | ||
@@ -538,2 +549,8 @@ } | ||
ALT: function ALT() { | ||
return _this.SUBRULE(_this["break"], { | ||
LABEL: 'list' | ||
}); | ||
} | ||
}, { | ||
ALT: function ALT() { | ||
return _this.CONSUME(LineBreak); | ||
@@ -624,2 +641,8 @@ } | ||
_proto["break"] = function _break() { | ||
return { | ||
type: 'break' | ||
}; | ||
}; | ||
_proto.comment = function comment(ctx) { | ||
@@ -797,2 +820,5 @@ var _ctx$text = ctx.text, | ||
case 'break': | ||
return printBreak(); | ||
default: | ||
@@ -807,2 +833,6 @@ throw new Error("Unrecognized block type"); | ||
function printBreak() { | ||
return '\n'; | ||
} | ||
function printDatasource(db) { | ||
@@ -819,10 +849,29 @@ var keyLength = db.assignments.reduce(function (max, current) { | ||
function printEnum(enumerator) { | ||
var children = enumerator.enumerators.map(function (enumerator) { | ||
return enumerator.type === 'comment' ? printComment(enumerator) : enumerator.name; | ||
}).filter(Boolean).join('\n '); | ||
var children = enumerator.enumerators.map(printEnumerator).filter(Boolean).join('\n '); | ||
return "\nenum " + enumerator.name + " {\n " + children + "\n}"; | ||
} | ||
function printEnumerator(enumerator) { | ||
switch (enumerator.type) { | ||
case 'enumerator': | ||
return enumerator.name; | ||
case 'comment': | ||
return printComment(enumerator); | ||
case 'break': | ||
return printBreak(); | ||
default: | ||
throw new Error("Unexpected enumerator type"); | ||
} | ||
} | ||
function printGenerator(generator) { | ||
var children = generator.assignments.map(printAssignment).filter(Boolean).join('\n '); | ||
var keyLength = generator.assignments.reduce(function (max, current) { | ||
return Math.max(max, current.type === 'assignment' ? current.key.length : 0); | ||
}, 0); | ||
var children = generator.assignments.map(function (assignment) { | ||
return printAssignment(assignment, keyLength); | ||
}).filter(Boolean).join('\n '); | ||
return "\ngenerator " + generator.name + " {\n " + children + "\n}"; | ||
@@ -849,4 +898,15 @@ } | ||
if (node.type === 'comment') return printComment(node); | ||
return node.key.padEnd(keyLength) + " = " + printValue(node.value); | ||
switch (node.type) { | ||
case 'comment': | ||
return printComment(node); | ||
case 'break': | ||
return printBreak(); | ||
case 'assignment': | ||
return node.key.padEnd(keyLength) + " = " + printValue(node.value); | ||
default: | ||
throw new Error("Unexpected assignment type"); | ||
} | ||
} | ||
@@ -873,2 +933,5 @@ | ||
case 'break': | ||
return printBreak(); | ||
default: | ||
@@ -875,0 +938,0 @@ throw new Error("Unrecognized property type"); |
@@ -1,2 +0,2 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("chevrotain"),t=e.createToken({name:"Identifier",pattern:/[a-zA-Z]\w*/}),n=e.createToken({name:"Datasource",pattern:/datasource/,longer_alt:t}),r=e.createToken({name:"Generator",pattern:/generator/,longer_alt:t}),a=e.createToken({name:"Model",pattern:/model/,longer_alt:t}),u=e.createToken({name:"Enum",pattern:/enum/,longer_alt:t}),o=e.createToken({name:"True",pattern:/true/,longer_alt:t}),i=e.createToken({name:"False",pattern:/false/,longer_alt:t}),c=e.createToken({name:"Null",pattern:/null/,longer_alt:t}),l=e.createToken({name:"Comment",pattern:e.Lexer.NA}),m=e.createToken({name:"DocComment",pattern:/\/\/\/\s*(.+)/,categories:[l]}),L=e.createToken({name:"LineComment",pattern:/\/\/\s*(.+)/,categories:[l]}),s=e.createToken({name:"Attribute",pattern:e.Lexer.NA}),f=e.createToken({name:"ModelAttribute",pattern:/@@/,label:"'@@'",categories:[s]}),p=e.createToken({name:"FieldAttribute",pattern:/@/,label:"'@'",categories:[s]}),E=e.createToken({name:"Dot",pattern:/\./,label:"'.'"}),A=e.createToken({name:"QuestionMark",pattern:/\?/,label:"'?'"}),U=e.createToken({name:"Array",pattern:/\[\]/,label:"'[]'"}),v=e.createToken({name:"LCurly",pattern:/{/,label:"'{'"}),y=e.createToken({name:"RCurly",pattern:/}/,label:"'}'"}),d=e.createToken({name:"LRound",pattern:/\(/,label:"'('"}),S=e.createToken({name:"RRound",pattern:/\)/,label:"')'"}),T=e.createToken({name:"LSquare",pattern:/\[/,label:"'['"}),g=e.createToken({name:"RSquare",pattern:/\]/,label:"']'"}),N=e.createToken({name:"Comma",pattern:/,/,label:"','"}),B=e.createToken({name:"Colon",pattern:/:/,label:"':'"}),b=e.createToken({name:"Equals",pattern:/=/,label:"'='"}),O=e.createToken({name:"StringLiteral",pattern:/"(:?[^\\"\n\r]+|\\(:?[bfnrtv"\\/]|u[0-9a-fA-F]{4}))*"/}),k=e.createToken({name:"NumberLiteral",pattern:/-?(0|[1-9]\d*)(\.\d+)?([eE][+-]?\d+)?/}),M=e.createToken({name:"WhiteSpace",pattern:/\s+/,group:e.Lexer.SKIPPED}),C=e.createToken({name:"LineBreak",pattern:"\n",line_breaks:!0,label:"LineBreak"}),R=[l,m,L,C,M,s,f,p,E,A,U,v,y,T,g,d,S,N,B,b,o,i,c,n,r,a,u,O,k,t],h=new e.Lexer(R);function w(e,t){e.prototype=Object.create(t.prototype),e.prototype.constructor=e,(Object.setPrototypeOf||function(e,t){return e.__proto__=t,e})(e,t)}var x=new(function(e){function m(){var m;return(m=e.call(this,R)||this).keyedArg=m.RULE("keyedArg",(function(){m.CONSUME(t,{LABEL:"keyName"}),m.CONSUME(B),m.SUBRULE(m.value)})),m.array=m.RULE("array",(function(){m.CONSUME(T),m.MANY_SEP({SEP:N,DEF:function(){m.SUBRULE(m.value)}}),m.CONSUME(g)})),m.func=m.RULE("func",(function(){m.CONSUME(t,{LABEL:"funcName"}),m.CONSUME(d),m.MANY_SEP({SEP:N,DEF:function(){m.SUBRULE(m.value)}}),m.CONSUME(S)})),m.value=m.RULE("value",(function(){m.OR([{ALT:function(){return m.CONSUME(O,{LABEL:"value"})}},{ALT:function(){return m.CONSUME(k,{LABEL:"value"})}},{ALT:function(){return m.SUBRULE(m.array,{LABEL:"value"})}},{ALT:function(){return m.SUBRULE(m.func,{LABEL:"value"})}},{ALT:function(){return m.CONSUME(o,{LABEL:"value"})}},{ALT:function(){return m.CONSUME(i,{LABEL:"value"})}},{ALT:function(){return m.CONSUME(c,{LABEL:"value"})}},{ALT:function(){return m.CONSUME(t,{LABEL:"value"})}}])})),m.property=m.RULE("property",(function(){m.CONSUME(t,{LABEL:"propertyName"}),m.CONSUME(b),m.SUBRULE(m.value,{LABEL:"propertyValue"})})),m.assignment=m.RULE("assignment",(function(){m.CONSUME(t,{LABEL:"assignmentName"}),m.CONSUME(b),m.SUBRULE(m.value,{LABEL:"assignmentValue"})})),m.field=m.RULE("field",(function(){m.CONSUME(t,{LABEL:"fieldName"}),m.SUBRULE(m.value,{LABEL:"fieldType"}),m.OPTION((function(){m.OR([{ALT:function(){return m.CONSUME(U,{LABEL:"array"})}},{ALT:function(){return m.CONSUME(A,{LABEL:"optional"})}}])})),m.MANY((function(){m.SUBRULE(m.attribute,{LABEL:"attributeList"})}))})),m.block=m.RULE("block",(function(e){var t=(void 0===e?{}:e).componentType,n="enum"===t,r="model"===t;m.CONSUME(v),m.CONSUME1(C),m.MANY((function(){m.OR([{ALT:function(){return m.SUBRULE(m.comment,{LABEL:"list"})}},{GATE:function(){return r},ALT:function(){return m.SUBRULE(m.property,{LABEL:"list"})}},{GATE:function(){return r},ALT:function(){return m.SUBRULE(m.attribute,{LABEL:"list"})}},{GATE:function(){return r},ALT:function(){return m.SUBRULE(m.field,{LABEL:"list"})}},{GATE:function(){return n},ALT:function(){return m.SUBRULE(m.enum,{LABEL:"list"})}},{GATE:function(){return!r},ALT:function(){return m.SUBRULE(m.assignment,{LABEL:"list"})}},{ALT:function(){return m.CONSUME2(C)}}])})),m.CONSUME(y)})),m.enum=m.RULE("enum",(function(){m.CONSUME(t,{LABEL:"enumName"})})),m.attribute=m.RULE("attribute",(function(){m.OR1([{ALT:function(){return m.CONSUME(f,{LABEL:"modelAttribute"})}},{ALT:function(){return m.CONSUME(p,{LABEL:"fieldAttribute"})}}]),m.OR2([{ALT:function(){m.CONSUME1(t,{LABEL:"groupName"}),m.CONSUME(E),m.CONSUME2(t,{LABEL:"attributeName"})}},{ALT:function(){return m.CONSUME(t,{LABEL:"attributeName"})}}]),m.OPTION((function(){m.CONSUME(d),m.MANY_SEP({SEP:N,DEF:function(){m.SUBRULE(m.attributeArg)}}),m.CONSUME(S)}))})),m.attributeArg=m.RULE("attributeArg",(function(){m.OR([{ALT:function(){return m.SUBRULE(m.keyedArg,{LABEL:"value"})}},{ALT:function(){return m.SUBRULE(m.value,{LABEL:"value"})}}])})),m.component=m.RULE("component",(function(){var e=m.OR1([{ALT:function(){return m.CONSUME(n,{LABEL:"type"})}},{ALT:function(){return m.CONSUME(r,{LABEL:"type"})}},{ALT:function(){return m.CONSUME(a,{LABEL:"type"})}},{ALT:function(){return m.CONSUME(u,{LABEL:"type"})}}]);m.OR2([{ALT:function(){m.CONSUME1(t,{LABEL:"groupName"}),m.CONSUME(E),m.CONSUME2(t,{LABEL:"componentName"})}},{ALT:function(){return m.CONSUME(t,{LABEL:"componentName"})}}]),m.SUBRULE(m.block,{ARGS:[{componentType:e.image}]})})),m.comment=m.RULE("comment",(function(){m.CONSUME(l,{LABEL:"text"})})),m.schema=m.RULE("schema",(function(){m.MANY((function(){m.OR([{ALT:function(){return m.SUBRULE(m.comment,{LABEL:"list"})}},{ALT:function(){return m.SUBRULE(m.component,{LABEL:"list"})}},{ALT:function(){return m.CONSUME(C)}}])}))})),m.performSelfAnalysis(),m}return w(m,e),m}(e.CstParser)),_=function(e){function t(){var t;return(t=e.call(this)||this).validateVisitor(),t}w(t,e);var n=t.prototype;return n.schema=function(e){var t,n=this;return{type:"schema",list:null==(t=e.list)?void 0:t.map((function(e){return n.visit([e])}))}},n.component=function(e){var t=e.type[0].image,n=e.componentName[0].image,r=this.visit(e.block);switch(t){case"datasource":return{type:"datasource",name:n,assignments:r};case"generator":return{type:"generator",name:n,assignments:r};case"model":return{type:"model",name:n,properties:r};case"enum":return{type:"enum",name:n,enumerators:r};default:throw new Error("Unexpected block type: "+t)}},n.comment=function(e){return{type:"comment",text:e.text[0].image}},n.block=function(e){var t,n=this;return null==(t=e.list)?void 0:t.map((function(e){return n.visit([e])}))},n.assignment=function(e){var t=this.visit(e.assignmentValue);return{type:"assignment",key:e.assignmentName[0].image,value:t}},n.field=function(e){var t=this,n=this.visit(e.fieldType),r=e.fieldName[0].image,a=e.attributeList&&e.attributeList.map((function(e){return t.visit([e])}));return{type:"field",name:r,fieldType:n,array:null!=e.array,optional:null!=e.optional,attributes:a}},n.attribute=function(e){var t=this,n=e.attributeName[0].image,r=(e.groupName||[{}])[0].image,a=e.attributeArg&&e.attributeArg.map((function(e){return t.visit(e)}));return{type:"attribute",name:n,kind:null!=e.modelAttribute?"model":"field",group:r,args:a}},n.attributeArg=function(e){return{type:"attributeArgument",value:this.visit(e.value)}},n.func=function(e){var t=this;return{type:"function",name:e.funcName[0].image,params:e.value&&e.value.map((function(e){return t.visit([e])}))}},n.array=function(e){var t=this;return{type:"array",args:e.value&&e.value.map((function(e){return t.visit([e])}))}},n.keyedArg=function(e){return{type:"keyValue",key:e.keyName[0].image,value:this.visit(e.value)}},n.value=function(e){return"image"in e.value[0]?e.value[0].image:this.visit(e.value)},n.enum=function(e){return{type:"enumerator",name:e.enumName[0].image}},t}(x.getBaseCstVisitorConstructorWithDefaults());function j(e){switch(e.type){case"comment":return P(e);case"datasource":return n=(t=e).assignments.reduce((function(e,t){return Math.max(e,"assignment"===t.type?t.key.length:0)}),0),r=t.assignments.map((function(e){return D(e,n)})).filter(Boolean).join("\n "),"\ndatasource "+t.name+" {\n "+r+"\n}";case"enum":return function(e){var t=e.enumerators.map((function(e){return"comment"===e.type?P(e):e.name})).filter(Boolean).join("\n ");return"\nenum "+e.name+" {\n "+t+"\n}"}(e);case"generator":return function(e){var t=e.assignments.map(D).filter(Boolean).join("\n ");return"\ngenerator "+e.name+" {\n "+t+"\n}"}(e);case"model":return function(e){var t=e.properties.reduce((function(e,t){return Math.max(e,"field"===t.type?t.name.length:0)}),0),n=e.properties.reduce((function(e,t){return Math.max(e,"field"===t.type?F(t).length:0)}),0),r=e.properties.map((function(e){return function(e,t,n){switch(void 0===t&&(t=0),void 0===n&&(n=0),e.type){case"attribute":return G(e);case"field":return function(e,t,n){void 0===t&&(t=0),void 0===n&&(n=0);var r=e.name.padEnd(t),a=F(e).padEnd(n),u=e.attributes?e.attributes.map(G):[];return[r,a].concat(u).filter(Boolean).join(" ").trim()}(e,t,n);case"comment":return P(e);default:throw new Error("Unrecognized property type")}}(e,t,n)})).filter(Boolean).join("\n ");return"\nmodel "+e.name+" {\n "+r+"\n}"}(e);default:throw new Error("Unrecognized block type")}var t,n,r}function P(e){return e.text}function D(e,t){return void 0===t&&(t=0),"comment"===e.type?P(e):e.key.padEnd(t)+" = "+Y(e.value)}function G(e){var t=e.args&&e.args.length>0?"("+e.args.map(V).filter(Boolean).join(", ")+")":"";return("field"===e.kind?"@":"@@")+e.name+t}function V(e){return Y(e.value)}function F(e){var t=e.array?"[]":e.optional?"?":"";if("object"==typeof e.fieldType)switch(e.fieldType.type){case"function":var n=e.fieldType.params.map(Y);return e.fieldType.name+"("+n+")"+t;default:throw new Error("Unexpected field type")}return""+e.fieldType+t}function Y(e){switch(typeof e){case"object":if("type"in e)switch(e.type){case"keyValue":return e.key+": "+Y(e.value);case"function":return e.name+"("+(e.params?e.params.map(Y):"")+")";case"array":return"["+e.args.join(", ")+"]";default:throw new Error("Unexpected value type")}throw new Error("Unexpected object value");default:return String(e)}}exports.getSchema=function(e){var t=h.tokenize(e);x.input=t.tokens;var n=x.schema();if(x.errors.length>0)throw x.errors[0];return(new _).visit(n)},exports.printSchema=function(e){return e.list.map(j).filter(Boolean).join("\n")}; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("chevrotain"),t=e.createToken({name:"Identifier",pattern:/[a-zA-Z]\w*/}),n=e.createToken({name:"Datasource",pattern:/datasource/,longer_alt:t}),r=e.createToken({name:"Generator",pattern:/generator/,longer_alt:t}),a=e.createToken({name:"Model",pattern:/model/,longer_alt:t}),u=e.createToken({name:"Enum",pattern:/enum/,longer_alt:t}),i=e.createToken({name:"True",pattern:/true/,longer_alt:t}),o=e.createToken({name:"False",pattern:/false/,longer_alt:t}),c=e.createToken({name:"Null",pattern:/null/,longer_alt:t}),l=e.createToken({name:"Comment",pattern:e.Lexer.NA}),m=e.createToken({name:"DocComment",pattern:/\/\/\/\s*(.+)/,categories:[l]}),s=e.createToken({name:"LineComment",pattern:/\/\/\s*(.+)/,categories:[l]}),L=e.createToken({name:"Attribute",pattern:e.Lexer.NA}),f=e.createToken({name:"ModelAttribute",pattern:/@@/,label:"'@@'",categories:[L]}),p=e.createToken({name:"FieldAttribute",pattern:/@/,label:"'@'",categories:[L]}),E=e.createToken({name:"Dot",pattern:/\./,label:"'.'"}),A=e.createToken({name:"QuestionMark",pattern:/\?/,label:"'?'"}),U=e.createToken({name:"Array",pattern:/\[\]/,label:"'[]'"}),y=e.createToken({name:"LCurly",pattern:/{/,label:"'{'"}),d=e.createToken({name:"RCurly",pattern:/}/,label:"'}'"}),v=e.createToken({name:"LRound",pattern:/\(/,label:"'('"}),S=e.createToken({name:"RRound",pattern:/\)/,label:"')'"}),g=e.createToken({name:"LSquare",pattern:/\[/,label:"'['"}),T=e.createToken({name:"RSquare",pattern:/\]/,label:"']'"}),B=e.createToken({name:"Comma",pattern:/,/,label:"','"}),N=e.createToken({name:"Colon",pattern:/:/,label:"':'"}),b=e.createToken({name:"Equals",pattern:/=/,label:"'='"}),k=e.createToken({name:"StringLiteral",pattern:/"(:?[^\\"\n\r]+|\\(:?[bfnrtv"\\/]|u[0-9a-fA-F]{4}))*"/}),O=e.createToken({name:"NumberLiteral",pattern:/-?(0|[1-9]\d*)(\.\d+)?([eE][+-]?\d+)?/}),M=e.createToken({name:"WhiteSpace",pattern:/\s+/,group:e.Lexer.SKIPPED}),C=e.createToken({name:"LineBreak",pattern:"\n",line_breaks:!0,label:"LineBreak"}),h=[l,m,s,C,M,L,f,p,E,A,U,y,d,g,T,v,S,B,N,b,i,o,c,n,r,a,u,k,O,t],R=new e.Lexer(h);function w(e,t){e.prototype=Object.create(t.prototype),e.prototype.constructor=e,(Object.setPrototypeOf||function(e,t){return e.__proto__=t,e})(e,t)}var x=new(function(e){function m(){var m;return(m=e.call(this,h)||this).break=m.RULE("break",(function(){m.CONSUME1(C),m.CONSUME2(C)})),m.keyedArg=m.RULE("keyedArg",(function(){m.CONSUME(t,{LABEL:"keyName"}),m.CONSUME(N),m.SUBRULE(m.value)})),m.array=m.RULE("array",(function(){m.CONSUME(g),m.MANY_SEP({SEP:B,DEF:function(){m.SUBRULE(m.value)}}),m.CONSUME(T)})),m.func=m.RULE("func",(function(){m.CONSUME(t,{LABEL:"funcName"}),m.CONSUME(v),m.MANY_SEP({SEP:B,DEF:function(){m.SUBRULE(m.value)}}),m.CONSUME(S)})),m.value=m.RULE("value",(function(){m.OR([{ALT:function(){return m.CONSUME(k,{LABEL:"value"})}},{ALT:function(){return m.CONSUME(O,{LABEL:"value"})}},{ALT:function(){return m.SUBRULE(m.array,{LABEL:"value"})}},{ALT:function(){return m.SUBRULE(m.func,{LABEL:"value"})}},{ALT:function(){return m.CONSUME(i,{LABEL:"value"})}},{ALT:function(){return m.CONSUME(o,{LABEL:"value"})}},{ALT:function(){return m.CONSUME(c,{LABEL:"value"})}},{ALT:function(){return m.CONSUME(t,{LABEL:"value"})}}])})),m.property=m.RULE("property",(function(){m.CONSUME(t,{LABEL:"propertyName"}),m.CONSUME(b),m.SUBRULE(m.value,{LABEL:"propertyValue"})})),m.assignment=m.RULE("assignment",(function(){m.CONSUME(t,{LABEL:"assignmentName"}),m.CONSUME(b),m.SUBRULE(m.value,{LABEL:"assignmentValue"})})),m.field=m.RULE("field",(function(){m.CONSUME(t,{LABEL:"fieldName"}),m.SUBRULE(m.value,{LABEL:"fieldType"}),m.OPTION((function(){m.OR([{ALT:function(){return m.CONSUME(U,{LABEL:"array"})}},{ALT:function(){return m.CONSUME(A,{LABEL:"optional"})}}])})),m.MANY((function(){m.SUBRULE(m.attribute,{LABEL:"attributeList"})}))})),m.block=m.RULE("block",(function(e){var t=(void 0===e?{}:e).componentType,n="enum"===t,r="model"===t;m.CONSUME(y),m.CONSUME1(C),m.MANY((function(){m.OR([{ALT:function(){return m.SUBRULE(m.comment,{LABEL:"list"})}},{GATE:function(){return r},ALT:function(){return m.SUBRULE(m.property,{LABEL:"list"})}},{GATE:function(){return r},ALT:function(){return m.SUBRULE(m.attribute,{LABEL:"list"})}},{GATE:function(){return r},ALT:function(){return m.SUBRULE(m.field,{LABEL:"list"})}},{GATE:function(){return n},ALT:function(){return m.SUBRULE(m.enum,{LABEL:"list"})}},{GATE:function(){return!r},ALT:function(){return m.SUBRULE(m.assignment,{LABEL:"list"})}},{ALT:function(){return m.SUBRULE(m.break,{LABEL:"list"})}},{ALT:function(){return m.CONSUME2(C)}}])})),m.CONSUME(d)})),m.enum=m.RULE("enum",(function(){m.CONSUME(t,{LABEL:"enumName"})})),m.attribute=m.RULE("attribute",(function(){m.OR1([{ALT:function(){return m.CONSUME(f,{LABEL:"modelAttribute"})}},{ALT:function(){return m.CONSUME(p,{LABEL:"fieldAttribute"})}}]),m.OR2([{ALT:function(){m.CONSUME1(t,{LABEL:"groupName"}),m.CONSUME(E),m.CONSUME2(t,{LABEL:"attributeName"})}},{ALT:function(){return m.CONSUME(t,{LABEL:"attributeName"})}}]),m.OPTION((function(){m.CONSUME(v),m.MANY_SEP({SEP:B,DEF:function(){m.SUBRULE(m.attributeArg)}}),m.CONSUME(S)}))})),m.attributeArg=m.RULE("attributeArg",(function(){m.OR([{ALT:function(){return m.SUBRULE(m.keyedArg,{LABEL:"value"})}},{ALT:function(){return m.SUBRULE(m.value,{LABEL:"value"})}}])})),m.component=m.RULE("component",(function(){var e=m.OR1([{ALT:function(){return m.CONSUME(n,{LABEL:"type"})}},{ALT:function(){return m.CONSUME(r,{LABEL:"type"})}},{ALT:function(){return m.CONSUME(a,{LABEL:"type"})}},{ALT:function(){return m.CONSUME(u,{LABEL:"type"})}}]);m.OR2([{ALT:function(){m.CONSUME1(t,{LABEL:"groupName"}),m.CONSUME(E),m.CONSUME2(t,{LABEL:"componentName"})}},{ALT:function(){return m.CONSUME(t,{LABEL:"componentName"})}}]),m.SUBRULE(m.block,{ARGS:[{componentType:e.image}]})})),m.comment=m.RULE("comment",(function(){m.CONSUME(l,{LABEL:"text"})})),m.schema=m.RULE("schema",(function(){m.MANY((function(){m.OR([{ALT:function(){return m.SUBRULE(m.comment,{LABEL:"list"})}},{ALT:function(){return m.SUBRULE(m.component,{LABEL:"list"})}},{ALT:function(){return m.SUBRULE(m.break,{LABEL:"list"})}},{ALT:function(){return m.CONSUME(C)}}])}))})),m.performSelfAnalysis(),m}return w(m,e),m}(e.CstParser)),_=function(e){function t(){var t;return(t=e.call(this)||this).validateVisitor(),t}w(t,e);var n=t.prototype;return n.schema=function(e){var t,n=this;return{type:"schema",list:null==(t=e.list)?void 0:t.map((function(e){return n.visit([e])}))}},n.component=function(e){var t=e.type[0].image,n=e.componentName[0].image,r=this.visit(e.block);switch(t){case"datasource":return{type:"datasource",name:n,assignments:r};case"generator":return{type:"generator",name:n,assignments:r};case"model":return{type:"model",name:n,properties:r};case"enum":return{type:"enum",name:n,enumerators:r};default:throw new Error("Unexpected block type: "+t)}},n.break=function(){return{type:"break"}},n.comment=function(e){return{type:"comment",text:e.text[0].image}},n.block=function(e){var t,n=this;return null==(t=e.list)?void 0:t.map((function(e){return n.visit([e])}))},n.assignment=function(e){var t=this.visit(e.assignmentValue);return{type:"assignment",key:e.assignmentName[0].image,value:t}},n.field=function(e){var t=this,n=this.visit(e.fieldType),r=e.fieldName[0].image,a=e.attributeList&&e.attributeList.map((function(e){return t.visit([e])}));return{type:"field",name:r,fieldType:n,array:null!=e.array,optional:null!=e.optional,attributes:a}},n.attribute=function(e){var t=this,n=e.attributeName[0].image,r=(e.groupName||[{}])[0].image,a=e.attributeArg&&e.attributeArg.map((function(e){return t.visit(e)}));return{type:"attribute",name:n,kind:null!=e.modelAttribute?"model":"field",group:r,args:a}},n.attributeArg=function(e){return{type:"attributeArgument",value:this.visit(e.value)}},n.func=function(e){var t=this;return{type:"function",name:e.funcName[0].image,params:e.value&&e.value.map((function(e){return t.visit([e])}))}},n.array=function(e){var t=this;return{type:"array",args:e.value&&e.value.map((function(e){return t.visit([e])}))}},n.keyedArg=function(e){return{type:"keyValue",key:e.keyName[0].image,value:this.visit(e.value)}},n.value=function(e){return"image"in e.value[0]?e.value[0].image:this.visit(e.value)},n.enum=function(e){return{type:"enumerator",name:e.enumName[0].image}},t}(x.getBaseCstVisitorConstructorWithDefaults());function j(e){switch(e.type){case"comment":return P(e);case"datasource":return n=(t=e).assignments.reduce((function(e,t){return Math.max(e,"assignment"===t.type?t.key.length:0)}),0),r=t.assignments.map((function(e){return G(e,n)})).filter(Boolean).join("\n "),"\ndatasource "+t.name+" {\n "+r+"\n}";case"enum":return function(e){var t=e.enumerators.map(D).filter(Boolean).join("\n ");return"\nenum "+e.name+" {\n "+t+"\n}"}(e);case"generator":return function(e){var t=e.assignments.reduce((function(e,t){return Math.max(e,"assignment"===t.type?t.key.length:0)}),0),n=e.assignments.map((function(e){return G(e,t)})).filter(Boolean).join("\n ");return"\ngenerator "+e.name+" {\n "+n+"\n}"}(e);case"model":return function(e){var t=e.properties.reduce((function(e,t){return Math.max(e,"field"===t.type?t.name.length:0)}),0),n=e.properties.reduce((function(e,t){return Math.max(e,"field"===t.type?Y(t).length:0)}),0),r=e.properties.map((function(e){return function(e,t,n){switch(void 0===t&&(t=0),void 0===n&&(n=0),e.type){case"attribute":return V(e);case"field":return function(e,t,n){void 0===t&&(t=0),void 0===n&&(n=0);var r=e.name.padEnd(t),a=Y(e).padEnd(n),u=e.attributes?e.attributes.map(V):[];return[r,a].concat(u).filter(Boolean).join(" ").trim()}(e,t,n);case"comment":return P(e);case"break":return"\n";default:throw new Error("Unrecognized property type")}}(e,t,n)})).filter(Boolean).join("\n ");return"\nmodel "+e.name+" {\n "+r+"\n}"}(e);case"break":return"\n";default:throw new Error("Unrecognized block type")}var t,n,r}function P(e){return e.text}function D(e){switch(e.type){case"enumerator":return e.name;case"comment":return P(e);case"break":return"\n";default:throw new Error("Unexpected enumerator type")}}function G(e,t){switch(void 0===t&&(t=0),e.type){case"comment":return P(e);case"break":return"\n";case"assignment":return e.key.padEnd(t)+" = "+q(e.value);default:throw new Error("Unexpected assignment type")}}function V(e){var t=e.args&&e.args.length>0?"("+e.args.map(F).filter(Boolean).join(", ")+")":"";return("field"===e.kind?"@":"@@")+e.name+t}function F(e){return q(e.value)}function Y(e){var t=e.array?"[]":e.optional?"?":"";if("object"==typeof e.fieldType)switch(e.fieldType.type){case"function":var n=e.fieldType.params.map(q);return e.fieldType.name+"("+n+")"+t;default:throw new Error("Unexpected field type")}return""+e.fieldType+t}function q(e){switch(typeof e){case"object":if("type"in e)switch(e.type){case"keyValue":return e.key+": "+q(e.value);case"function":return e.name+"("+(e.params?e.params.map(q):"")+")";case"array":return"["+e.args.join(", ")+"]";default:throw new Error("Unexpected value type")}throw new Error("Unexpected object value");default:return String(e)}}exports.getSchema=function(e){var t=R.tokenize(e);x.input=t.tokens;var n=x.schema();if(x.errors.length>0)throw x.errors[0];return(new _).visit(n)},exports.printSchema=function(e){return e.list.map(j).filter(Boolean).join("\n")}; | ||
//# sourceMappingURL=prisma-ast.cjs.production.min.js.map |
@@ -177,2 +177,7 @@ import { Lexer, createToken, CstParser } from 'chevrotain'; | ||
_this = _CstParser.call(this, allTokens) || this; | ||
_this["break"] = _this.RULE('break', function () { | ||
_this.CONSUME1(LineBreak); | ||
_this.CONSUME2(LineBreak); | ||
}); | ||
_this.keyedArg = _this.RULE('keyedArg', function () { | ||
@@ -384,2 +389,8 @@ _this.CONSUME(Identifier, { | ||
ALT: function ALT() { | ||
return _this.SUBRULE(_this["break"], { | ||
LABEL: 'list' | ||
}); | ||
} | ||
}, { | ||
ALT: function ALT() { | ||
return _this.CONSUME2(LineBreak); | ||
@@ -534,2 +545,8 @@ } | ||
ALT: function ALT() { | ||
return _this.SUBRULE(_this["break"], { | ||
LABEL: 'list' | ||
}); | ||
} | ||
}, { | ||
ALT: function ALT() { | ||
return _this.CONSUME(LineBreak); | ||
@@ -620,2 +637,8 @@ } | ||
_proto["break"] = function _break() { | ||
return { | ||
type: 'break' | ||
}; | ||
}; | ||
_proto.comment = function comment(ctx) { | ||
@@ -793,2 +816,5 @@ var _ctx$text = ctx.text, | ||
case 'break': | ||
return printBreak(); | ||
default: | ||
@@ -803,2 +829,6 @@ throw new Error("Unrecognized block type"); | ||
function printBreak() { | ||
return '\n'; | ||
} | ||
function printDatasource(db) { | ||
@@ -815,10 +845,29 @@ var keyLength = db.assignments.reduce(function (max, current) { | ||
function printEnum(enumerator) { | ||
var children = enumerator.enumerators.map(function (enumerator) { | ||
return enumerator.type === 'comment' ? printComment(enumerator) : enumerator.name; | ||
}).filter(Boolean).join('\n '); | ||
var children = enumerator.enumerators.map(printEnumerator).filter(Boolean).join('\n '); | ||
return "\nenum " + enumerator.name + " {\n " + children + "\n}"; | ||
} | ||
function printEnumerator(enumerator) { | ||
switch (enumerator.type) { | ||
case 'enumerator': | ||
return enumerator.name; | ||
case 'comment': | ||
return printComment(enumerator); | ||
case 'break': | ||
return printBreak(); | ||
default: | ||
throw new Error("Unexpected enumerator type"); | ||
} | ||
} | ||
function printGenerator(generator) { | ||
var children = generator.assignments.map(printAssignment).filter(Boolean).join('\n '); | ||
var keyLength = generator.assignments.reduce(function (max, current) { | ||
return Math.max(max, current.type === 'assignment' ? current.key.length : 0); | ||
}, 0); | ||
var children = generator.assignments.map(function (assignment) { | ||
return printAssignment(assignment, keyLength); | ||
}).filter(Boolean).join('\n '); | ||
return "\ngenerator " + generator.name + " {\n " + children + "\n}"; | ||
@@ -845,4 +894,15 @@ } | ||
if (node.type === 'comment') return printComment(node); | ||
return node.key.padEnd(keyLength) + " = " + printValue(node.value); | ||
switch (node.type) { | ||
case 'comment': | ||
return printComment(node); | ||
case 'break': | ||
return printBreak(); | ||
case 'assignment': | ||
return node.key.padEnd(keyLength) + " = " + printValue(node.value); | ||
default: | ||
throw new Error("Unexpected assignment type"); | ||
} | ||
} | ||
@@ -869,2 +929,5 @@ | ||
case 'break': | ||
return printBreak(); | ||
default: | ||
@@ -871,0 +934,0 @@ throw new Error("Unrecognized property type"); |
@@ -14,2 +14,3 @@ import { CstNode, IToken } from '@chevrotain/types'; | ||
}): Types.Block; | ||
break(): Types.Break; | ||
comment(ctx: CstNode & { | ||
@@ -16,0 +17,0 @@ text: [IToken]; |
{ | ||
"version": "0.1.2", | ||
"version": "0.1.3", | ||
"license": "MIT", | ||
@@ -4,0 +4,0 @@ "main": "dist/index.js", |
@@ -19,3 +19,3 @@ import { PrismaLexer } from './lexer'; | ||
export type Block = Model | Datasource | Generator | Enum | Comment; | ||
export type Block = Model | Datasource | Generator | Enum | Comment | Break; | ||
@@ -25,3 +25,3 @@ export interface Model { | ||
name: string; | ||
properties: Array<Property | Comment>; | ||
properties: Array<Property | Comment | Break>; | ||
} | ||
@@ -32,3 +32,3 @@ | ||
name: string; | ||
assignments: Array<Assignment | Comment>; | ||
assignments: Array<Assignment | Comment | Break>; | ||
} | ||
@@ -39,3 +39,3 @@ | ||
name: string; | ||
assignments: Array<Assignment | Comment>; | ||
assignments: Array<Assignment | Comment | Break>; | ||
} | ||
@@ -46,3 +46,3 @@ | ||
name: string; | ||
enumerators: Array<Enumerator | Comment>; | ||
enumerators: Array<Enumerator | Comment | Break>; | ||
} | ||
@@ -55,2 +55,6 @@ | ||
export interface Break { | ||
type: 'break'; | ||
} | ||
export type Property = GroupedModelAttribute | ModelAttribute | Field; | ||
@@ -57,0 +61,0 @@ |
@@ -10,2 +10,7 @@ import { CstParser } from 'chevrotain'; | ||
private break = this.RULE('break', () => { | ||
this.CONSUME1(lexer.LineBreak); | ||
this.CONSUME2(lexer.LineBreak); | ||
}); | ||
private keyedArg = this.RULE('keyedArg', () => { | ||
@@ -108,2 +113,3 @@ this.CONSUME(lexer.Identifier, { LABEL: 'keyName' }); | ||
}, | ||
{ ALT: () => this.SUBRULE(this.break, { LABEL: 'list' }) }, | ||
{ ALT: () => this.CONSUME2(lexer.LineBreak) }, | ||
@@ -193,2 +199,3 @@ ]); | ||
{ ALT: () => this.SUBRULE(this.component, { LABEL: 'list' }) }, | ||
{ ALT: () => this.SUBRULE(this.break, { LABEL: 'list' }) }, | ||
{ ALT: () => this.CONSUME(lexer.LineBreak) }, | ||
@@ -195,0 +202,0 @@ ]); |
@@ -22,2 +22,4 @@ import * as Types from './getSchema'; | ||
return printModel(block); | ||
case 'break': | ||
return printBreak(); | ||
default: | ||
@@ -32,3 +34,9 @@ throw new Error(`Unrecognized block type`); | ||
function printBreak() { | ||
return '\n'; | ||
} | ||
function printDatasource(db: Types.Datasource) { | ||
// FIXME: indent reset on 'break' | ||
// not super high priority because the IntrospectionEngine will autoformat | ||
const keyLength = db.assignments.reduce( | ||
@@ -52,5 +60,3 @@ (max, current) => | ||
const children = enumerator.enumerators | ||
.map(enumerator => | ||
enumerator.type === 'comment' ? printComment(enumerator) : enumerator.name | ||
) | ||
.map(printEnumerator) | ||
.filter(Boolean) | ||
@@ -65,5 +71,26 @@ .join('\n '); | ||
function printEnumerator( | ||
enumerator: Types.Enumerator | Types.Comment | Types.Break | ||
) { | ||
switch (enumerator.type) { | ||
case 'enumerator': | ||
return enumerator.name; | ||
case 'comment': | ||
return printComment(enumerator); | ||
case 'break': | ||
return printBreak(); | ||
default: | ||
throw new Error(`Unexpected enumerator type`); | ||
} | ||
} | ||
function printGenerator(generator: Types.Generator) { | ||
const keyLength = generator.assignments.reduce( | ||
(max, current) => | ||
Math.max(max, current.type === 'assignment' ? current.key.length : 0), | ||
0 | ||
); | ||
const children = generator.assignments | ||
.map(printAssignment) | ||
.map(assignment => printAssignment(assignment, keyLength)) | ||
.filter(Boolean) | ||
@@ -106,11 +133,19 @@ .join('\n '); | ||
function printAssignment( | ||
node: Types.Assignment | Types.Comment, | ||
node: Types.Assignment | Types.Comment | Types.Break, | ||
keyLength = 0 | ||
) { | ||
if (node.type === 'comment') return printComment(node); | ||
return `${node.key.padEnd(keyLength)} = ${printValue(node.value)}`; | ||
switch (node.type) { | ||
case 'comment': | ||
return printComment(node); | ||
case 'break': | ||
return printBreak(); | ||
case 'assignment': | ||
return `${node.key.padEnd(keyLength)} = ${printValue(node.value)}`; | ||
default: | ||
throw new Error(`Unexpected assignment type`); | ||
} | ||
} | ||
function printProperty( | ||
node: Types.Property | Types.Comment, | ||
node: Types.Property | Types.Comment | Types.Break, | ||
nameLength = 0, | ||
@@ -126,2 +161,4 @@ typeLength = 0 | ||
return printComment(node); | ||
case 'break': | ||
return printBreak(); | ||
default: | ||
@@ -128,0 +165,0 @@ throw new Error(`Unrecognized property type`); |
@@ -42,2 +42,6 @@ import { CstNode, IToken } from '@chevrotain/types'; | ||
break(): Types.Break { | ||
return { type: 'break' }; | ||
} | ||
comment(ctx: CstNode & { text: [IToken] }): Types.Comment { | ||
@@ -52,12 +56,2 @@ const [{ image: comment }] = ctx.text; | ||
/* | ||
property( | ||
ctx: CstNode & { propertyName: [IToken]; propertyValue: [CstNode] } | ||
): Types.Property { | ||
const value = this.visit(ctx.propertyValue); | ||
const [{ image: name }] = ctx.propertyName; | ||
return { type: 'property', name, value }; | ||
} | ||
*/ | ||
assignment( | ||
@@ -64,0 +58,0 @@ ctx: CstNode & { assignmentName: [IToken]; assignmentValue: [CstNode] } |
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
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
228030
3080