Skip to content

Commit

Permalink
improve testing and cli
Browse files Browse the repository at this point in the history
  • Loading branch information
TheFireBlast committed Jun 30, 2022
1 parent f78b820 commit 06e41f9
Show file tree
Hide file tree
Showing 20 changed files with 401 additions and 776 deletions.
6 changes: 4 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
node_modules/
dist/
out/
/dist/
/out/
/test/out/
# for running with @swc/register
src/rion.js
6 changes: 3 additions & 3 deletions .mocharc.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"extension": ["ts"],
"spec": "test/**/*.test.ts",
"require": "@swc/register"
}
"spec": "test/**/*.spec.ts",
"require": ["@swc/register"]
}
2 changes: 2 additions & 0 deletions bin/cli.js → bin/iro.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/env node

const { createCommand } = require("../dist/cli");

createCommand(true).parse();
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "0.1.0",
"main": "dist/index.js",
"bin": {
"iro": "dist/cli.js"
"iro": "dist/iro.js"
},
"author": "FireBlast",
"license": "MIT",
Expand Down
2 changes: 0 additions & 2 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#!/usr/bin/env node

import * as fs from "fs";
import { parse, compile, IroError, nodes, getParser } from "./index";
import * as chalk from "chalk";
Expand Down
14 changes: 9 additions & 5 deletions src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,16 @@ function getRegexGroups(regex: string) {
return list;
}

interface CompileOptions {
textmate?: boolean;
ace?: boolean;
aceDefaultToken?: boolean;
export interface CompileOptions {
/**Compíle to textmate format */
textmate: boolean;
/**Compíle to ace format */
ace: boolean;
/**Emit default token when compiling to ace */
aceDefaultToken: boolean;
}

export function compile(ast: N.Grammar, options?: CompileOptions) {
export function compile(ast: N.Grammar, options?: Partial<CompileOptions>) {
options = Object.assign(
<CompileOptions>{
textmate: true,
Expand Down Expand Up @@ -640,6 +643,7 @@ export function compile(ast: N.Grammar, options?: CompileOptions) {
}

traverse(ast);

if (!errors.find((e) => e.fatal)) {
// Expand includes
for (let r in aceGrammar.rules) {
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as nearley from "nearley";
import { compile as _compile, IroError } from "./compiler";
import { compile as _compile, CompileOptions, IroError } from "./compiler";
import { Grammar, SourceLocationPosition } from "./nodes";
import { NearleyError, isNearleyError } from "./utils";

Expand Down Expand Up @@ -36,7 +36,7 @@ export function parse(input: string): Grammar {
}
export function compile(
input: string | Grammar,
options?: Parameters<typeof _compile>[1]
options?: Partial<CompileOptions>
): ReturnType<typeof _compile> {
var ast: Grammar;
if (typeof input == "string") {
Expand Down
2 changes: 1 addition & 1 deletion src/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as fs from "fs";
import { parse, compile, IroError, nodes } from "./index";
import { prettyError } from "./utils";

var input = fs.readFileSync("./test/test.iro", "utf8");
var input = fs.readFileSync("./test/grammars/test.iro", "utf8");
var parsingError = false;
console.time("parsing took");
try {
Expand Down
53 changes: 53 additions & 0 deletions test/cli.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { expect } from "chai";
import * as fs from "fs/promises";
import * as path from "path";
import { createCommand } from "../src/cli";

const OUT_PATH = path.join(__dirname, "out");

//TODO: use in-memory fs?

const noop = () => {};
function runCLI(args: string[], output: (out: string) => void = noop) {
const _log = console.log;
console.log = output;

var cli = createCommand();
cli.parse([process.argv[0], process.argv[1], ...args]);

console.log = _log;
}

async function cleanOutput() {
await Promise.all(
(await fs.readdir(OUT_PATH)).map((fname) => fs.rm(path.join(OUT_PATH, fname), { recursive: true, force: true }))
);
}

before(async () => {
await fs.mkdir(OUT_PATH, { recursive: true });
await cleanOutput();
});

describe("CLI", function () {
beforeEach(async function () {
await cleanOutput();
});
it("should output all targets", async function () {
runCLI(["compile", path.join(__dirname, "grammars/rion.iro"), "-o", OUT_PATH, "-t", "all"]);
expect(await fs.readdir(OUT_PATH)).to.have.members(
[
//
"rion.ace.js",
"rion.ace.json",
"rion.tmlanguage",
"rion.tmlanguage.json",
],
"expected output directory to have all target files"
);
});
it("should not output files on fail", async function () {
runCLI(["compile", path.join(__dirname, "grammars/should_fail/fail_parser.iro"), "-o", OUT_PATH, "-t", "all"]);
expect(await fs.readdir(OUT_PATH)).to.be.empty;
});
});
130 changes: 65 additions & 65 deletions test/inline_style.iro → test/grammars/inline_style.iro
Original file line number Diff line number Diff line change
@@ -1,66 +1,66 @@
name = mylang
file_extensions []= hello, world;
#__rec1 \= $${__rec2}
#__rec2 \= $${__rec1}
__Identifier \= [a-z0-1_]+
contexts [] {
main : context {
: push {
regex \= (")
styles [] = .punctuation;
context [] = inside_quotes;
}
}
inside_quotes : context {
: pop {
regex \= (")
styles [] = .punctuation;
}
: pattern {
regex \= (\\(?:\\|"))
styles [] = .escaped_text;
}
: pattern {
regex \= ([^"\\]+)
styles [] = .quoted_text;
}
}
}
styles [] {
.punctuation : style {
color = brown
ace_scope = punctuation
textmate_scope = punctuation
pygments_scope = Punctuation
}
.escaped_text : style {
color = white
ace_scope = punctuation
textmate_scope = punctuation
pygments_scope = Punctuation
}
.keyword : style {
color = cyan
ace_scope = keyword
textmate_scope = keywordd
pygments_scope = Keyword
}
.quoted_text : style {
color = yellow
ace_scope = text
textmate_scope = text
pygments_scope = String
}
name = mylang
file_extensions []= hello, world;
#__rec1 \= $${__rec2}
#__rec2 \= $${__rec1}

__Identifier \= [a-z0-1_]+

contexts [] {



main : context {
: push {
regex \= (")
styles [] = .punctuation;
context [] = inside_quotes;
}
}

inside_quotes : context {
: pop {
regex \= (")
styles [] = .punctuation;
}
: pattern {
regex \= (\\(?:\\|"))
styles [] = .escaped_text;
}
: pattern {
regex \= ([^"\\]+)
styles [] = .quoted_text;
}
}



}

styles [] {

.punctuation : style {
color = brown
ace_scope = punctuation
textmate_scope = punctuation
pygments_scope = Punctuation
}
.escaped_text : style {
color = white
ace_scope = punctuation
textmate_scope = punctuation
pygments_scope = Punctuation
}
.keyword : style {
color = cyan
ace_scope = keyword
textmate_scope = keywordd
pygments_scope = Keyword
}
.quoted_text : style {
color = yellow
ace_scope = text
textmate_scope = text
pygments_scope = String
}

}
96 changes: 48 additions & 48 deletions test/push_pop.iro → test/grammars/push_pop.iro
Original file line number Diff line number Diff line change
@@ -1,49 +1,49 @@
name = mylang
file_extensions []= hello, world;
__rec1 \= $${__rec2}
__rec2 \= $${__rec1}
__Identifier \= [a-z0-1_]+
contexts [] {
main : context {
: push {
regex \= (")
styles [] = .punctuation;
context [] = inside_quotes;
}
}
inside_quotes : context {
: pop {
regex \= (")
styles [] = .punctuation;
}
: pattern {
regex \= (\\(?:\\|"))
styles [] = .punctuation;
}
: pattern {
regex \= ([^"\\]+)
styles [] = .quoted_text;
}
}
}
styles [] {
.punctuation : style {
color = brown
}
.quoted_text : style {
color = yellow
pygments_scope = String
}
name = mylang
file_extensions []= hello, world;
__rec1 \= $${__rec2}
__rec2 \= $${__rec1}

__Identifier \= [a-z0-1_]+

contexts [] {



main : context {
: push {
regex \= (")
styles [] = .punctuation;
context [] = inside_quotes;
}
}

inside_quotes : context {
: pop {
regex \= (")
styles [] = .punctuation;
}
: pattern {
regex \= (\\(?:\\|"))
styles [] = .punctuation;
}
: pattern {
regex \= ([^"\\]+)
styles [] = .quoted_text;
}
}



}

styles [] {

.punctuation : style {
color = brown
}
.quoted_text : style {
color = yellow
pygments_scope = String
}

}
File renamed without changes.
Loading

0 comments on commit 06e41f9

Please sign in to comment.