UNPKG

@gram-data/gram-parse

Version:
3 lines (2 loc) 11.8 kB
"use strict";function e(e){return e&&"object"==typeof e&&"default"in e?e.default:e}Object.defineProperty(exports,"__esModule",{value:!0});var s=e(require("unified")),t=e(require("nearley")),n=e(require("moo")),o=require("@gram-data/gram-builder"),r=require("@gram-data/gram-ast");function a(){return(a=Object.assign||function(e){for(var s=1;s<arguments.length;s++){var t=arguments[s];for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(e[n]=t[n])}return e}).apply(this,arguments)}function i(e){return e[0]}var l=n.compile({whitespace:{match:/\s+/,lineBreaks:!0},lineComment:{match:/\/\/.*?\n?$/},hexadecimal:r.tokens.hexadecimal,octal:r.tokens.octal,measurement:r.tokens.measurement,decimal:r.tokens.decimal,integer:r.tokens.integer,taggedString:{match:r.tokens.taggedString},boolean:["true","TRUE","True","false","FALSE","False"],symbol:r.tokens.symbol,identifier:r.tokens.identifier,doubleQuotedString:{match:r.tokens.doubleQuotedString,value:function(e){return e.slice(1,-1)}},singleQuotedString:{match:r.tokens.singleQuotedString,value:function(e){return e.slice(1,-1)}},tickedString:{match:r.tokens.tickedString,value:function(e){return e.slice(1,-1)}},"--\x3e":"--\x3e","--":"--","<--":"<--","-[]->":"-[]->","-[]-":"-[]-","<-[]-":"<-[]-","<-[":"<-[","]->":"]->","-[":"-[","]-":"]-","{":"{","}":"}","[":"[","]":"]","(":"(",")":")",",":",",":":":","`":"`","'":"'","ø":"ø"}),m=function(){return null},u=function(e){return e[0].text},c={Lexer:l,ParserRules:[{name:"GramSeq$ebnf$1$subexpression$1",symbols:["Path"],postprocess:function(e){return e[0]}},{name:"GramSeq$ebnf$1",symbols:["GramSeq$ebnf$1$subexpression$1"]},{name:"GramSeq$ebnf$1$subexpression$2",symbols:["Path"],postprocess:function(e){return e[0]}},{name:"GramSeq$ebnf$1",symbols:["GramSeq$ebnf$1","GramSeq$ebnf$1$subexpression$2"],postprocess:function(e){return e[0].concat([e[1]])}},{name:"GramSeq",symbols:["_","GramSeq$ebnf$1"],postprocess:function(e){return o.seq(o.flatten(e[1]))}},{name:"Path",symbols:["NodePattern"],postprocess:i},{name:"Path",symbols:["PathComposition"],postprocess:i},{name:"Path",symbols:["PathPair"],postprocess:i},{name:"NodePattern",symbols:["Node","_","Edge","_","NodePattern"],postprocess:function(e){var s=e[2];return o.cons([e[0],e[4]],{kind:s.kind,id:s.id,labels:s.labels,record:s.record})}},{name:"NodePattern",symbols:["Node"],postprocess:i},{name:"Node$ebnf$1",symbols:["Attributes"],postprocess:i},{name:"Node$ebnf$1",symbols:[],postprocess:function(){return null}},{name:"Node",symbols:[{literal:"("},"_","Node$ebnf$1",{literal:")"},"_"],postprocess:function(e){var s=e[2];return s?o.node(s.id,s.labels,s.record):o.node()}},{name:"Edge$ebnf$1",symbols:["Attributes"],postprocess:i},{name:"Edge$ebnf$1",symbols:[],postprocess:function(){return null}},{name:"Edge",symbols:[{literal:"-["},"_","Edge$ebnf$1",{literal:"]->"},"_"],postprocess:function(e){return a({kind:"right"},e[2])}},{name:"Edge$ebnf$2",symbols:["Attributes"],postprocess:i},{name:"Edge$ebnf$2",symbols:[],postprocess:function(){return null}},{name:"Edge",symbols:[{literal:"-["},"_","Edge$ebnf$2",{literal:"]-"},"_"],postprocess:function(e){return a({kind:"either"},e[2])}},{name:"Edge$ebnf$3",symbols:["Attributes"],postprocess:i},{name:"Edge$ebnf$3",symbols:[],postprocess:function(){return null}},{name:"Edge",symbols:[{literal:"<-["},"_","Edge$ebnf$3",{literal:"]-"},"_"],postprocess:function(e){return a({kind:"left"},e[2])}},{name:"Edge",symbols:[{literal:"-[]->"},"_"],postprocess:function(){return{kind:"right"}}},{name:"Edge",symbols:[{literal:"-[]-"},"_"],postprocess:function(){return{kind:"either"}}},{name:"Edge",symbols:[{literal:"<-[]-"},"_"],postprocess:function(){return{kind:"left"}}},{name:"Edge",symbols:[{literal:"--\x3e"},"_"],postprocess:function(){return{kind:"right"}}},{name:"Edge",symbols:[{literal:"--"},"_"],postprocess:function(){return{kind:"either"}}},{name:"Edge",symbols:[{literal:"<--"},"_"],postprocess:function(){return{kind:"left"}}},{name:"PathComposition",symbols:["PathPoint"],postprocess:i},{name:"PathComposition",symbols:["PathAnnotation"],postprocess:i},{name:"PathComposition",symbols:["PathExpression"],postprocess:i},{name:"PathPoint$ebnf$1",symbols:["Attributes"],postprocess:i},{name:"PathPoint$ebnf$1",symbols:[],postprocess:function(){return null}},{name:"PathPoint",symbols:[{literal:"["},"_","PathPoint$ebnf$1",{literal:"]"},"_"],postprocess:function(e){var s=e[2];return s&&(s.id||s.labels||s.record)&&"ø"!==s.id?o.node(s.id,s.labels,s.record):o.empty()}},{name:"PathAnnotation$ebnf$1",symbols:["Attributes"],postprocess:i},{name:"PathAnnotation$ebnf$1",symbols:[],postprocess:function(){return null}},{name:"PathAnnotation",symbols:[{literal:"["},"_","PathAnnotation$ebnf$1","Path",{literal:"]"},"_"],postprocess:function(e){var s=e[2];return o.cons([e[3]],s?{id:s.id,labels:s.labels,record:s.record}:{})}},{name:"PathExpression$ebnf$1",symbols:["Attributes"],postprocess:i},{name:"PathExpression$ebnf$1",symbols:[],postprocess:function(){return null}},{name:"PathExpression$ebnf$2",symbols:["Kind"],postprocess:i},{name:"PathExpression$ebnf$2",symbols:[],postprocess:function(){return null}},{name:"PathExpression",symbols:[{literal:"["},"_","PathExpression$ebnf$1","PathExpression$ebnf$2","Path","Path",{literal:"]"},"_"],postprocess:function(e){var s=e[2];return o.cons([e[4],e[5]],{kind:e[3],id:s.id,labels:s.labels,record:s.record})}},{name:"PathPair$subexpression$1",symbols:["NodePattern"]},{name:"PathPair$subexpression$1",symbols:["PathComposition"]},{name:"PathPair",symbols:["PathPair$subexpression$1",{literal:","},"_","Path"],postprocess:function(e){return o.pair([e[0][0],e[3]])}},{name:"Kind",symbols:[{literal:","},"_"],postprocess:function(){return"pair"}},{name:"Kind",symbols:[{literal:"--\x3e"},"_"],postprocess:function(){return"right"}},{name:"Kind",symbols:[{literal:"--"},"_"],postprocess:function(){return"either"}},{name:"Kind",symbols:[{literal:"<--"},"_"],postprocess:function(){return"left"}},{name:"Attributes$ebnf$1",symbols:["Identity"],postprocess:i},{name:"Attributes$ebnf$1",symbols:[],postprocess:function(){return null}},{name:"Attributes$ebnf$2",symbols:["LabelList"],postprocess:i},{name:"Attributes$ebnf$2",symbols:[],postprocess:function(){return null}},{name:"Attributes$ebnf$3",symbols:["Record"],postprocess:i},{name:"Attributes$ebnf$3",symbols:[],postprocess:function(){return null}},{name:"Attributes",symbols:["Attributes$ebnf$1","Attributes$ebnf$2","Attributes$ebnf$3"],postprocess:function(e,s,t){var n=e[0],o=e[1],r=e[2];return n||o||r?{id:n,labels:o,record:r}:t}},{name:"LabelList$ebnf$1",symbols:["Label"]},{name:"LabelList$ebnf$1",symbols:["LabelList$ebnf$1","Label"],postprocess:function(e){return e[0].concat([e[1]])}},{name:"LabelList",symbols:["LabelList$ebnf$1"],postprocess:function(e){return e[0]}},{name:"Label",symbols:[{literal:":"},"Symbol"],postprocess:function(e){return e[1]}},{name:"Identity",symbols:[l.has("identifier")?{type:"identifier"}:identifier,"_"],postprocess:u},{name:"Identity",symbols:[{literal:"ø"},"_"],postprocess:u},{name:"Identity",symbols:[l.has("symbol")?{type:"symbol"}:symbol,"_"],postprocess:u},{name:"Identity",symbols:[l.has("integer")?{type:"integer"}:integer,"_"],postprocess:u},{name:"Identity",symbols:[l.has("octal")?{type:"octal"}:octal,"_"],postprocess:u},{name:"Identity",symbols:[l.has("hexadecimal")?{type:"hexadecimal"}:hexadecimal,"_"],postprocess:u},{name:"Identity",symbols:[l.has("measurement")?{type:"measurement"}:measurement,"_"],postprocess:u},{name:"Identity",symbols:[l.has("tickedString")?{type:"tickedString"}:tickedString,"_"],postprocess:function(e){return e[0].text.slice(1,-1)}},{name:"Symbol",symbols:[l.has("symbol")?{type:"symbol"}:symbol,"_"],postprocess:u},{name:"Symbol",symbols:[l.has("tickedString")?{type:"tickedString"}:tickedString,"_"],postprocess:function(e){return e[0].text.slice(1,-1)}},{name:"Record",symbols:[{literal:"{"},"_",{literal:"}"},"_"],postprocess:m},{name:"Record$ebnf$1",symbols:[]},{name:"Record$ebnf$1$subexpression$1",symbols:[{literal:","},"_","Property"],postprocess:function(e){return e[2]}},{name:"Record$ebnf$1",symbols:["Record$ebnf$1","Record$ebnf$1$subexpression$1"],postprocess:function(e){return e[0].concat([e[1]])}},{name:"Record",symbols:[{literal:"{"},"_","Property","Record$ebnf$1",{literal:"}"},"_"],postprocess:function(e){return o.propertiesToRecord([e[2]].concat(e[3]))}},{name:"Property",symbols:["Symbol",{literal:":"},"_","Value"],postprocess:function(e){return o.property(e[0],e[3])}},{name:"Value",symbols:["StringLiteral","_"],postprocess:i},{name:"Value",symbols:["NumericLiteral","_"],postprocess:i},{name:"Value",symbols:[l.has("boolean")?{type:"boolean"}:boolean,"_"],postprocess:function(e){return o.boolean(JSON.parse(e[0].value.toLowerCase()))}},{name:"Value$ebnf$1",symbols:[]},{name:"Value$ebnf$1$subexpression$1",symbols:[{literal:","},"_","Value"],postprocess:function(e){return e[2]}},{name:"Value$ebnf$1",symbols:["Value$ebnf$1","Value$ebnf$1$subexpression$1"],postprocess:function(e){return e[0].concat([e[1]])}},{name:"Value",symbols:[{literal:"["},"_","Value","Value$ebnf$1",{literal:"]"},"_"],postprocess:function(e){return[e[2]].concat(e[3])}},{name:"StringLiteral",symbols:[l.has("singleQuotedString")?{type:"singleQuotedString"}:singleQuotedString],postprocess:function(e){return o.string(e[0].value)}},{name:"StringLiteral",symbols:[l.has("doubleQuotedString")?{type:"doubleQuotedString"}:doubleQuotedString],postprocess:function(e){return o.string(e[0].value)}},{name:"StringLiteral",symbols:[l.has("tickedString")?{type:"tickedString"}:tickedString],postprocess:function(e){return o.string(e[0].value)}},{name:"StringLiteral",symbols:[l.has("taggedString")?{type:"taggedString"}:taggedString],postprocess:function(e){var s=function(e){var s=e.match(/([^`]+)`(.+)`$/);if(null==s)throw Error("Malformed tagged string: "+e);return{tag:s[1],value:s[2]}}(e[0].value);return o.tagged(s.tag,s.value)}},{name:"NumericLiteral",symbols:[l.has("integer")?{type:"integer"}:integer],postprocess:function(e){return o.integer(e[0].value)}},{name:"NumericLiteral",symbols:[l.has("decimal")?{type:"decimal"}:decimal],postprocess:function(e){return o.decimal(e[0].value)}},{name:"NumericLiteral",symbols:[l.has("hexadecimal")?{type:"hexadecimal"}:hexadecimal],postprocess:function(e){return o.hexadecimal(e[0].value)}},{name:"NumericLiteral",symbols:[l.has("octal")?{type:"octal"}:octal],postprocess:function(e){return o.octal(e[0].value)}},{name:"NumericLiteral",symbols:[l.has("measurement")?{type:"measurement"}:measurement],postprocess:function(e){var s=function(e){var s=e.match(/(-?[0-9.]+)([a-zA-Z]+)/);if(null==s)throw Error("Malformed measurement : "+e);return{value:s[1],unit:s[2]}}(e[0].value);return o.measurement(s.unit,s.value)}},{name:"_$ebnf$1",symbols:[l.has("whitespace")?{type:"whitespace"}:whitespace],postprocess:i},{name:"_$ebnf$1",symbols:[],postprocess:function(){return null}},{name:"_",symbols:["_$ebnf$1"],postprocess:m},{name:"Comment",symbols:[l.has("lineComment")?{type:"lineComment"}:lineComment],postprocess:m},{name:"EOL",symbols:[{literal:"\n"}],postprocess:m}],ParserStart:"GramSeq"},p=function(e,s){var n,o,r=new t.Parser(t.Grammar.fromCompiled(c));try{var a=r.feed(e);if(void 0===a.results[0]&&a.lexerState){var i={line:(o=a.lexerState).line,column:o.col};s.fail("Incomplete parse.",i)}return a.results.length>1&&s.info("[WARN] parsing is ambiguous"),a.results[0]||{type:"error"}}catch(e){var l=e.token?{line:(n=e.token).line,column:n.col}:{line:0,column:0};s.fail(e.message,l)}},b=function(){this.Parser=p};exports.errors={__proto__:null,INCOMPLETE_PARSE:"Incomplete parse.",SYNTAX_ERROR:"Syntax error at"},exports.gramParserPlugin=b,exports.toAST=function(e){return s().use(b).freeze().parse(e)}; //# sourceMappingURL=gram-parse.cjs.production.min.js.map