-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add namespaces (declare + nested) (#4)
- Loading branch information
Showing
8 changed files
with
144 additions
and
71 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
@@ -1,6 +1,12 @@ | ||
import "./en-gb.lang"; | ||
|
||
namespace mynamespace; | ||
|
||
// Comment | ||
/* Comment */ | ||
example.key "Hello world!"; | ||
example.key2(a1, a2) "abc " a1 a2 " def " a1; | ||
|
||
namespace hi { | ||
key "mynamespace.hi.key"; | ||
} |
This file contains 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 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 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 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 |
---|---|---|
@@ -1,36 +1,69 @@ | ||
import { Compiler } from "./compiler"; | ||
import { Parser } from "./parser"; | ||
import { TokensEmitter } from "./tokensemitter"; | ||
import { Translations, TranslationsEngine } from "./translations"; | ||
import * as path from "node:path"; | ||
import * as fs from "node:fs"; | ||
|
||
let te = new TokensEmitter(); | ||
te.accept(` | ||
import "./hello_world.lang"; | ||
import "./sus.lang"; | ||
// Comment | ||
/* Comment */ | ||
example.first(playerName, arg2) "Hello " /* Comment in da middle! */ playerName " and welcome to our server!"; | ||
example.second "cool"; | ||
example.third; | ||
`); | ||
console.log(te); | ||
async function compile(input: string) { | ||
const compiler = new (class extends Compiler { | ||
override async resolveImport(p: string, importFrom: string): Promise<Translations> { | ||
const target = path.resolve(importFrom, "..", p); | ||
return this.compileFromText(await fs.promises.readFile(target, "utf-8"), "", target); | ||
} | ||
})(); | ||
const result = await compiler.compileFromText(input, "", __filename); | ||
return new TranslationsEngine(result); | ||
} | ||
|
||
let parser = new Parser(); | ||
parser.accept(te); | ||
console.log(parser.statements); | ||
function assert(input: string, expected: string) { | ||
if (input != expected) { | ||
process.stderr.write(`\x1b[91mTEST ERROR: \x1b[0m'${input}' != '${expected}'\x1b[0m\n`); | ||
process.exit(1); | ||
} | ||
} | ||
|
||
let compiler = new(class extends Compiler { | ||
override async resolveImport(path: string): Promise<Translations> { | ||
return {}; | ||
function exists(a: any) { | ||
if (a == null) { | ||
process.stderr.write(`\x1b[91mTEST ERROR: \x1b[0mNon-existent/null/undefined object\x1b[0m\n`); | ||
process.exit(1); | ||
} | ||
})(); | ||
} | ||
|
||
async function main() { | ||
let translations = await compiler.compileFromStatements(parser); | ||
let engine = new TranslationsEngine(translations); | ||
console.log(engine.translate("example.first", "nahkd123")); | ||
console.log(engine.convertToMC()["example.first"]); | ||
let all = await Promise.all([ | ||
compile(` | ||
test "a"; | ||
test.nothing; | ||
test.single "a"; | ||
test.one_argument(a) "Value is " a "!"; | ||
test.two_arguments(a, b) "We got " a " then " b " and back to " a " and " a " again."; | ||
`).then(t => { | ||
assert(t.translate("test"), "a"); | ||
assert(t.translate("test.nothing"), ""); | ||
assert(t.translate("test.single"), "a"); | ||
assert(t.translate("test.one_argument", "12345"), "Value is 12345!"); | ||
assert(t.translate("test.two_arguments", "123", "abc"), "We got 123 then abc and back to 123 and 123 again."); | ||
}), | ||
compile(` | ||
import "../example/en-us.lang"; | ||
namespace separateNamespace.abcdef; | ||
key "separateNamespace.abcdef"; | ||
key2(a) "Value is " a "!"; | ||
key3(a, b) a " then " b " and then " a " and " a " again"; | ||
`).then(t => { | ||
exists(t.translations["separateNamespace.abcdef.key"]); | ||
exists(t.translations["mynamespace.example.key"]); | ||
exists(t.translations["mynamespace.hi.key"]); | ||
|
||
assert(t.translate("example.engb"), "12345"); | ||
assert(t.translate("mynamespace.example.key2", "A", "B"), "abc AB def A"); | ||
assert(t.translate("mynamespace.hi.key"), "mynamespace.hi.key"); | ||
}) | ||
]); | ||
|
||
process.stdout.write(`\x1b[92mTests passed! (${all.length} tests)\x1b[0m\n`); | ||
process.exit(0); | ||
} | ||
|
||
main(); |
This file contains 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 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