Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support mjs loading #13

Merged
merged 2 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions build/compile
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,5 @@ fi

# build hera esbuild-plugin
civet < source/esbuild-plugin.civet > esbuild-plugin.js

cp source/esm.mjs dist/
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@danielx/hera",
"version": "0.8.10",
"version": "0.8.11",
"description": "Small and fast parsing expression grammars",
"devDependencies": {
"@babel/core": "^7.17.8",
Expand Down Expand Up @@ -38,7 +38,7 @@
"loader": [
"ts-node/esm",
"./build/coffee-esm.mjs",
"./build/hera-esm.mjs",
"./source/esm.mjs",
"@danielx/civet/esm"
],
"reporter": "spec",
Expand Down Expand Up @@ -90,6 +90,7 @@
"./lib": "./dist/machine.js",
"./esbuild-plugin": "./esbuild-plugin.js",
"./register": "./register.js",
"./esm": "./dist/esm.mjs",
"./*": "./*",
"./dist/*": "./dist/*"
},
Expand Down
48 changes: 26 additions & 22 deletions source/compiler.civet
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
import type { HeraAST, HeraRules, StructuralHandling } from ./hera-types.ts

export type CompilerOptions = {
filename: string
inlineMap: boolean
source: string
types: boolean
filename?: string
inlineMap?: boolean
source?: string
types?: boolean
libPath?: string | undefined
module?: boolean
}

strDefs: string[] := []
Expand Down Expand Up @@ -266,15 +267,15 @@ compileRulesObject := (ruleNames: string[]) ->
${meat}
}`

compileExports := (options: CompilerOptions, ruleNames: string[]) ->
if options.types
compileExports := (ruleNames: string[], module: boolean) ->
if module
meat := ruleNames.map (name) ->
` ${name}`
.join(",\n")

return `export {\n${meat}\n}`
else
ruleNames.map (name) ->

return ruleNames.map (name) ->
`exports.${name} = ${name};`
.join("\n")

Expand Down Expand Up @@ -309,7 +310,10 @@ defaultOptions: CompilerOptions :=
export function compile(rules: HeraRules, maybeOptions?: CompilerOptions)
options := { ...defaultOptions, ...maybeOptions }

{ types } := options
strDefs.length = 0
reDefs.length = 0

{ types, module } := options
/* c8 ignore next */
libPath := options.libPath or defaultOptions.libPath
ruleNames := Object.keys(rules)
Expand All @@ -330,8 +334,9 @@ export function compile(rules: HeraRules, maybeOptions?: CompilerOptions)
sm = SourceMap(options.source)
genOpts.updateSourceMap = sm.updateSourceMap

head := (types ? tsHead : jsHead).replace("\"./machine.js\"", JSON.stringify(libPath))
tail := (types ? tsTail : jsTail).replace("\"./machine.js\"", JSON.stringify(libPath))
head := (module ? mjsHead : cjsHead).replace("\"./machine.js\"", JSON.stringify(libPath))
tail := types ? tsTail : jsTail
exp := (module ? mjsExport : cjsExport)

code := generate [
head
Expand All @@ -344,7 +349,9 @@ export function compile(rules: HeraRules, maybeOptions?: CompilerOptions)
"\n\n"
tail
"\n\n"
compileExports(options, ruleNames)
exp
"\n\n"
compileExports(ruleNames, !!module)
"\n\n"
//@ts-ignore
rules[Symbol.for("code")]
Expand Down Expand Up @@ -409,7 +416,7 @@ generate := (node: ASTNode, options: GenerateOptions): string ->
/* c8 ignore next */
throw new Error "unknown node type"

jsHead := """
cjsHead := """
const {
$C,
$E,
Expand All @@ -429,9 +436,7 @@ jsHead := """
$TS,
$TV,
$Y,
HeraGrammar,
Parser,
ParseState,
ParserContext,
ParserOptions,
Validator
Expand Down Expand Up @@ -468,12 +473,14 @@ jsTail := """
}
}
}())
"""

cjsExport := """
exports.default = parser
exports.parse = parser.parse
"""

tsHead := """
mjsHead := """
import {
$C,
$E,
Expand All @@ -493,13 +500,8 @@ tsHead := """
$TS,
$TV,
$Y,
HeraGrammar,
Parser,
ParseState,
ParserContext,
ParserOptions,
Validator
} from "./machine.ts"
} from "./machine.js"

"""

Expand Down Expand Up @@ -532,7 +534,9 @@ tsTail := """
}
}
}())
"""

mjsExport := """
export default parser
export const { parse } = parser
"""
21 changes: 17 additions & 4 deletions build/hera-esm.mjs → source/esm.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { pathToFileURL } from 'url';
import "../register.js";
import { fileURLToPath, pathToFileURL } from 'url';

import fs from "fs";
import { compile } from "@danielx/hera";

const baseURL = pathToFileURL(process.cwd() + '/').href;
const extensionsRegex = /\.hera$/;
Expand All @@ -20,9 +22,20 @@ export async function resolve(specifier, context, defaultResolve) {

export async function load(url, context, next) {
if (extensionsRegex.test(url)) {
return next(url.replace(extensionsRegex, ".cjs"), {
format: "commonjs"
const filename = fileURLToPath(url);
const source = compile(fs.readFileSync(filename, 'utf8'), {
filename,
inlineMap: true,
module: true,
});

// TODO: how to avoid shortCircuit?
// We may want to pass the module to babel or whatever in the future
return {
format: "module",
source,
shortCircuit: true,
};
}

// Let Node.js handle all other URLs.
Expand Down
26 changes: 26 additions & 0 deletions source/main.civet
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,35 @@ generate := <T extends HeraGrammar>(src: string) => {
}
}

modImport := async <T extends HeraGrammar>(src: string) => {
fs := await import('node:fs/promises')
{ fileURLToPath } := await import "url"
{ dirname } := await import "path"

__dirname := dirname fileURLToPath(import.meta.url)

js := compile(parse(src), {
module: true,
libPath: `${ __dirname }/machine.js`,
})

// random temp file
tmpPath := `${ __dirname }/hera-tmp-${ Math.random().toString(36).slice(2) }.mjs`
await fs.writeFile(tmpPath, js)

try
(await import tmpPath) as {
parse: (input: string, options?: ParserOptions<T>) => unknown
}
finally
await fs.unlink(tmpPath)
}

export {
parse
compile
execMod
modImport
generate
grammarToEBNF
}
Expand Down
11 changes: 10 additions & 1 deletion test/main.coffee
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import assert from "assert"
import hera, { compile, generate } from "../source/main.civet"
import hera, { compile, generate, modImport } from "../source/main.civet"

test = it

Expand Down Expand Up @@ -787,3 +787,12 @@ describe "Hera", ->
value: "a"
}, "the data"]
]

describe "modImport", ->
it "should generate module", ->
{parse} = await modImport """
Rule
"a"
"""

assert parse("a")
Loading