-
Notifications
You must be signed in to change notification settings - Fork 40
General purpose web IDE for MLscript #367
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
Draft
chengluyu
wants to merge
5
commits into
hkust-taco:hkmc2
Choose a base branch
from
chengluyu:web-ide
base: hkmc2
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e1146fa
Prototype the Web IDE
chengluyu ecb5737
Move web IDE to apps and create a runner for apps
chengluyu ec96a8d
Add path resolution logic
chengluyu 94fe366
Support module resolution
chengluyu fc93faa
Commit the unstable `uid` changes to make CI happy
chengluyu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,6 +9,7 @@ import utils.* | |
| import hkmc2.semantics.* | ||
| import hkmc2.syntax.Keyword.`override` | ||
| import semantics.Elaborator.{Ctx, State} | ||
| import hkmc2.io.Path | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unused? |
||
|
|
||
|
|
||
| class ParserSetup(file: io.Path, dbgParsing: Bool)(using state: Elaborator.State, raise: Raise, cctx: CompilerCtx): | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| package hkmc2 | ||
|
|
||
| import mlscript.utils.*, shorthands.* | ||
|
|
||
| trait ModuleResolver: | ||
| /** | ||
| * Try to resolve path if it refers to a module or a source file in a module. | ||
| * | ||
| * @param path the import path | ||
| * @return the resolved path and module identifier, if any. | ||
| */ | ||
| def tryResolveModulePath(path: Str): Opt[(Str \/ io.Path, Opt[Str])] | ||
|
|
||
| /** | ||
| * For local tests, including `CompileTestRunner`, `DiffTestRunner`, etc. | ||
| * | ||
| * @param stdPath the path to the standard library folder | ||
| * @param nodeModulesPath the optional path to the `node_modules` folder. | ||
| * If provided, we will do a simple check to see if | ||
| * the requested Node.js built-in module exists. | ||
| */ | ||
| class LocalTestModuleResolver(stdPath: io.Path, nodeModulesPath: Opt[io.Path])(using fs: io.FileSystem) extends ModuleResolver: | ||
| import LocalTestModuleResolver.* | ||
|
|
||
| private def existsNodeModule(orgNameOpt: Opt[Str], moduleName: Str): Bool = | ||
| nodeModulesPath match | ||
| case S(basePath) => | ||
| val packagePath = orgNameOpt match | ||
| case S(orgName) => basePath / s"@$orgName" / moduleName | ||
| case N => basePath / moduleName | ||
| fs.exists(packagePath) | ||
| case N => false | ||
|
|
||
| /** | ||
| * Resolve an imported module name to a directory (or file) path. This method | ||
| * should be overridden by subclasses to provide module resolution logic. | ||
| * | ||
| * @param orgName the optional organization name of the module | ||
| * @param moduleName the name of the module being imported | ||
| * @param noSubPath if the import does not have a sub-path (i.e., only the module name) | ||
| * @return the pure module name or the resolved path | ||
| */ | ||
| private def resolveModule(orgName: Opt[Str], moduleName: Str, noSubPath: Bool): Opt[(Str \/ io.Path, Opt[Str])] = | ||
| // Currently, there is only one std module, so the implementation here is | ||
| // hard-coded. If one day we implement the mechanism of a module manifest | ||
| // (for example, through `.mlson` file or `.witton` file), this part will | ||
| // need to be updated accordingly. | ||
| if orgName.isEmpty then | ||
| if noSubPath then | ||
| val realName = if moduleName.startsWith("node:") then moduleName.drop(5) else moduleName | ||
| if allowedNodeJsModules contains realName then S(L(moduleName) -> S(realName)) | ||
| else if existsNodeModule(orgName, moduleName) then S(L(moduleName) -> N) | ||
| else N | ||
| else if moduleName == "std" then S(R(stdPath) -> N) | ||
| else N | ||
| else N | ||
|
|
||
| def tryResolveModulePath(path: Str): Opt[(Str \/ io.Path, Opt[Str])] = path match | ||
| case r(orgName, modName, subPath) => | ||
| val orgNameOpt = Opt(orgName) | ||
| if subPath is null then | ||
| resolveModule(orgNameOpt, modName, true) | ||
| else | ||
| val res = resolveModule(orgNameOpt, modName, false).map: | ||
| case (R(p), id) => (R(p / io.RelPath(subPath)), id) | ||
| case other => other | ||
| res | ||
| case _ => N | ||
|
|
||
| object LocalTestModuleResolver: | ||
| def apply(stdPath: io.Path, nodeModulesPath: Opt[io.Path] = N)(using fs: io.FileSystem): LocalTestModuleResolver = | ||
| new LocalTestModuleResolver(stdPath, nodeModulesPath) | ||
|
|
||
| /** | ||
| * The pattern of valid Node.js module specifiers. | ||
| * | ||
| * 1. **Group 1:** Scope (no `@`) | ||
| * + Example: `@my-org/foo/bar` → `"my-org"` | ||
| * 2. **Group 2:** Package name | ||
| * + Example: `@my-org/foo/bar` → `"foo"` | ||
| * + Example: `mypkg/test` → `"mypkg"` | ||
| * 3. **Group 3:** The remaining text after the first slash | ||
| * + Example: `@my-org/foo/bar/baz` → `"bar/baz"` | ||
| * + Example: `mypkg/sub/path` → `"sub/path"` | ||
| * + Example: `mypkg` → `null` | ||
| */ | ||
| private val r = """^(?:@([a-z0-9-~][a-z0-9-._~]*)\/)?((?:node:)?[a-z0-9-~][a-z0-9-._~]*)(?:\/(.*))?$""".r | ||
|
|
||
| /** | ||
| * An incomplete list of allowed Node.js built-in modules. Add more as needed. | ||
| */ | ||
| private val allowedNodeJsModules = Set( | ||
| "fs", "path", "http", "https", "url", "util", "events", "stream", "buffer", | ||
| "os", "child_process", "vm", "assert", "tty", "process") | ||
|
|
||
| // TODO: WebModuleResolver that can resolve URL imports. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any reason to move all of these in the companion? Doing so means they can no longer be overridden by other users of the test suite. (There is just one example with
BenchTestStatefor now.)