import{tokenize as e}from"../tokenizer/index.mjs";import{generateSourceMap as o}from"./sourceMap.mjs";import*as t from"./token-before.mjs";import{safe as n}from"./token-before.mjs";import{Delim as r,WhiteSpace as i}from"../tokenizer/types.mjs";function f(e,o){if("function"==typeof o){let t=null;return void e.children.forEach(e=>{null!==t&&o.call(this,t),this.node(e),t=e})}e.children.forEach(this.node,this)}function s(o){e(o,(e,t,n)=>{this.token(e,o.slice(t,n))})}function c(e){const c=new Map;for(let[o,t]of Object.entries(e.node)){"function"==typeof(t.generate||t)&&c.set(o,t.generate||t)}return function(e,m){let d="",l=0,p={node(e){if(!c.has(e.type))throw new Error("Unknown node type: "+e.type);c.get(e.type).call(h,e)},tokenBefore:n,token(e,o){l=this.tokenBefore(l,e,o),this.emit(o,e,!1),e===r&&92===o.charCodeAt(0)&&this.emit("\n",i,!0)},emit(e){d+=e},result:()=>d};m&&("function"==typeof m.decorator&&(p=m.decorator(p)),m.sourceMap&&(p=o(p)),m.mode in t&&(p.tokenBefore=t[m.mode]));const h={node:e=>p.node(e),children:f,token:(e,o)=>p.token(e,o),tokenize:s};return p.node(e),p.result()}}export{c as createGenerator};