Skip to content
Transpilers for the export to language / language modes Compass feature.
JavaScript ANTLR
Branch: master
Clone or download
Latest commit 0507537 Oct 15, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
codegeneration Add tests Oct 15, 2019
config Add RangeError Jun 5, 2018
grammars Add named arguments to python (#128) Oct 31, 2018
helper Rename Bson-Compilers to Bson-Transpilers Aug 6, 2018
img-docs add tree visiting diagram Jun 25, 2018
lib Test updates Oct 15, 2019
printers
symbols minor fixes for driver syntax Oct 15, 2019
test Test updates Oct 15, 2019
.eslintrc Error tests Apr 6, 2018
.gitignore Adding package.json Aug 1, 2018
.jsfmtrc Start ECMAScript Mar 5, 2018
.npmignore Need config module May 6, 2019
.travis.yml bump node to 9.8.0 Jun 15, 2018
CONTRIBUTING.md Add support for translating to javascript objects (#129) Dec 18, 2018
LICENSE document the API a bit more; add back a section for installing tools … May 10, 2018
README.md Update README with supported langs Jul 4, 2019
compile-symbol-table.js Add support for translating to javascript objects (#129) Dec 18, 2018
index.js
make-test-file.js Fix up make-test-file (#132) May 7, 2019
package-lock.json 0.13.1 Oct 15, 2019
package.json

README.md

BSON-Transpilers

npm version build status downloads

Transpilers for building BSON documents in any language. Current support provided for shell javascript and python as inputs. java, c#, node, shell and python as outputs.

Usage

const transpiler = require('bson-transpilers');

const input = 'javascript';
const output = 'java';

const string =`
{ item: "book", qty: Int32(10), tags: ["red", "blank"], dim_cm: [14, Int32("81")] }`;

try {
  const compiledString = transpiler[input][output].compile(string);
  console.log(compiledCode);
  // new Document("item", "book").append("qty", 10)
  // .append("tags", Arrays.asList("red", "blank"))
  // .append("dim_cm", Arrays.asList(14L, 81")))
} catch (error) {
  console.error(error);
}

API

compiledString = transpiler[inputLang][outputLang].compile(codeString)

Output a compiled string given input and output languages.

  • inputLang: Input language of the code string. shell and javascript are currently supported.
  • outputLang: The language you would like the output to be. java, python, shell, javascript, and csharp are currently supported.
  • codeString: The code string you would like to be compiled to your selected output language.

importsString = transpiler[inputLang][outputLang].getImports()

Output a string containing the set of import statements for the generated code to compile. These are all the packages that the compiled code could use so that the transpiler output will be runnable.

catch (error)

Any transpiler errors that occur will be thrown. To catch them, wrap the transpiler in a try/catch block.

  • error.message: Message bson-transpilers will send back letting you know the transpiler error.
  • error.stack: The usual error stacktrace.
  • error.code: Error code that bson-transpilers adds to the error object to help you distinguish error types.
  • error.line: If it is a syntax error, will have the line.
  • error.column: If it is a syntax error, will have the column.
  • error.symbol: If it is a syntax error, will have the symbol associated with the error.

Errors

There are a few different error classes thrown by bson-transpilers, each with their own error code:

BsonTranspilersArgumentError

code: E_BSONTRANSPILERS_ARGUMENT

This will occur when you're using a method with a wrong number of arguments, or the arguments are of the wrong type. For example, ObjectId().equals() requires one argument and it will throw if anything other than one argument is provided:

// ✘: this will throw a BsonTranspilersArgumentError.
ObjectId().equals(ObjectId(), ObjectId());

// ✔: this won't throw
ObjectId().equals(ObjectId());
// ✘: this will throw a BsonTranspilersArgumentError.
ObjectId({});

// ✔: this won't throw
ObjectId();

BsonTranspilersAttributeError

code: E_BSONTRANSPILERS_ATTRIBUTE

Will be thrown if an invalid method or property is used on a BSON object. For example, since new DBRef() doesn't have a method .foo(), transpiler will throw:

// ✘: method foo doesn't exist, so this will throw a BsonTranspilersAttributeError .
new DBRef('newCollection', new ObjectId()).foo()

// ✔: this won't throw, since .toString() method exists
new DBRef('newCollection', new ObjectId()).toString(10)

BsonTranspilersSyntaxError

code: E_BSONTRANSPILERS_SYNTAX

This will throw if you have a syntax error. For example missing a colon in Object assignment, or forgetting a comma in array definition:

// ✘: this is not a proper object definition; will throw E_SYNTAX_GENERIC
{ key 'beep' }

// ✘: this is not a proper array definition, will throw E_SYNTAX_GENERIC
[ 'beep'; 'boop' 'beepBoop' ]

// ✔: neither of these will throw 
{ key: 'beep' }
[ 'beep', 'boop', 'beepBoop' ]

BsonTranspilersTypeError

code: E_BSONTRANSPILERS_TYPE

This error will occur if a symbol is treated as the wrong type. For example, if a non-function is called:

// ✘: MAX_VALUE is a constant, not a function
Long.MAX_VALUE()

// ✔: MAX_VALUE without a call will not throw
Long.MAX_VALUE

BsonTranspilersUnimplementedError

code: E_BSONTRANSPILERS_UNIMPLEMENTED

If there is a feature in the input code that is not currently supported by the transpiler.

BsonTranspilersRuntimeError

code: E_BSONTRANSPILERS_RUNTIME

A generic runtime error will be thrown for all errors that are not covered by the above list of errors. These are usually constructor requirements, for example when using a RegExp() an unsupported flag is given:

// ✘: these are not proper 'RegExp()' flags, a BsonTranspilersRuntimeError will be thrown.
new RegExp('ab+c', 'beep')

// ✔: 'im' are proper 'RegExp()' flags
new RegExp('ab+c', 'im')

BsonTranspilersInternalError

code: E_BSONTRANSPILERS_INTERNAL

In the case where something has gone wrong within compilation, and an error has occured. If you see this error, please create an issue on Github!

Install

npm install -S bson-transpilers

Contributing

Head over to the readme on contributing to find out more information on project structure and setting up your environment.

Authors

License

Apache-2.0

You can’t perform that action at this time.