SlideShare a Scribd company logo
Anna Herlihy
Senior Software Engineer
Stockholm
BSON-Transpilers
or
How To Add Your Favorite Language to Compass
@annaisworking
1. Compass Features
2. Technical Requirements
3. How to contribute!
Compass Tour
Export To Language (query)
Export To Language (aggregation)
But wait...
Wouldn’t it be great if you could just write
whatever language you want, directly into
Compass?
Language-Modes
BSON is a large enough
subset to treat the
problem as if we’re
supporting the entire
language syntax
Accept query or aggregation in any language
Export query or aggregation to any language
+
=
Requirements
Any language
to
any language
translation!
MongoDB World 2019: BSON Transpilers: Transpiling from Any Language to Any Language!
Intermediate representation
Intermediate representation
???
MongoDB World 2019: BSON Transpilers: Transpiling from Any Language to Any Language!
MongoDB World 2019: BSON Transpilers: Transpiling from Any Language to Any Language!
MongoDB World 2019: BSON Transpilers: Transpiling from Any Language to Any Language!
Complexity
For every input language we support:
1. We want to only have to do the work once
2. We want to define the input language without
knowing or caring how many output languages exist
We do not want to have to
rewrite the translation for
every possible
combination, that would be
O(n2) and we want O(n)
For every input language we support:
1. We want to only have to do the work once
2. We want to define the input language without
knowing or caring how many output languages exist
MongoDB World 2019: BSON Transpilers: Transpiling from Any Language to Any Language!
For every input language we support:
1. We want to only have to do the work once
2. We want to define the input language without
knowing or caring how many output languages exist
Same principle for target languages
MongoDB World 2019: BSON Transpilers: Transpiling from Any Language to Any Language!
Encouraging Community Involvement
1. Many communities are small + passionate
2. Open source ethos :)
3. How many people are compiler experts?
How to add your own
output language
Compiler 101
Illustrations by @Irina!
Tree Building
{ today: Date() }
START
Object
Literal
{}
Symbol
today
Function
Call
Args
()
Symbol
Date
key
funcname
value
args
The tree-building stage
includes lexing, parsing,
syntax analysis, and more!
MongoDB World 2019: BSON Transpilers: Transpiling from Any Language to Any Language!
Parser Generator
● Parsers are hard!
● Support for most languages
● We apply ANTLR to our input
and we get a tree. The tree
building stage is completely
handled!
● Bonus: ANTLR generates a
visitor class!
OUR SAVIOR
MongoDB World 2019: BSON Transpilers: Transpiling from Any Language to Any Language!
The Visitor Pattern
Visitors traverse trees by “visiting” each node
For each type of node, the visitor calls the corresponding
function.
➢ When the visitor sees a node that is a “string” type, it
will call the visitString method and expect the
generated code to be returned.
Extremely Simple Visitor Example
‘testing…testing’
START
JavaScript Code
Extremely Simple Visitor Example
‘testing…testing’
START
JavaScript Code
“testing...testing”
END
Java Code
Extremely Simple Visitor Example
‘testing…testing’
START
ANTLR
“testing...testing”
END
string
’‘
testing...testing
value
Extremely Simple Visitor Example
‘testing…testing’
START
ANTLR
“testing...testing”
END
string
’‘
testing...testing
value
Extremely Simple Visitor Example
‘testing…testing’
START
“testing...testing”
END
VISITOR
string
’‘
testing...testing
value
Extremely Simple Visitor Example
class Visitor() extends ANTLRVisitor {
visitString(node) {
const val = node.value;
return ‘“‘ + val + ‘”‘;
}
}
string
’‘
testing...testing
‘testing…testing’
START
“testing...testing”
END
value
What about functions or variables?
● We need to support native language features as well
as BSON-specific types.
● ObjectId, Date, Decimal128, Timestamp, etc…
● How can we tell the difference between variables?
ObjectId()
START
Date()
START JavaScript Input JavaScript Input
new java.util.Date() new ObjectId()
ENDEND
JavaScript Input
Java Output
ObjectId()
START
Date()
START
Function
Call
Args
()
Symbol
Date
Function
Call
Args
()
Symbol
ObjectId
new java.util.Date() new ObjectId()
ENDEND
ObjectId()
START
Date()
START
Function
Call
Args
()
Symbol
Date
Function
Call
Args
()
Symbol
ObjectId
The same visit methods are going to be called for both...
To the visitor, these two trees look the same...
We need a
Symbol Table!
Symbol Table
● Need a place to keep track of all the symbols, i.e. variable or
function names.
● When the visitor reaches a Symbol node, it looks it up in the
Symbol Table
● This is also a convenient place to differentiate between output
languages...
Visiting a Symbol
visitSymbol(node) {
const name = node.symbol;
node.type = this.Symbols[name];
if (node.type === undefined) {
throw new ReferenceError(`Symbol ${name} is undefined`);
}
return name;
}
First get the symbol
itself from the node
Visiting a Symbol
visitSymbol(node) {
const name = node.symbol;
node.type = this.Symbols[name];
if (node.type === undefined) {
throw new ReferenceError(`Symbol ${name} is undefined`);
}
return name;
}
Look up the name of
the symbol in the table
and “decorate” the
node with the results
Visiting a Symbol
visitSymbol(node) {
const name = node.symbol;
node.type = this.Symbols[name];
if (node.type === undefined) {
throw new ReferenceError(`Symbol ${name} is undefined`);
}
return name;
}
If it’s not in the table,
throw an exception
Visiting a Symbol
visitSymbol(node) {
const name = node.symbol;
node.type = this.Symbols[name];
if (node.type === undefined) {
throw new ReferenceError(`Symbol ${name} is undefined`);
}
return name;
}
Return the name of
the function to the
parent
What’s in the Symbol Table?
ObjectId:
id: "ObjectId"
callable: *constructor
args:
- [ *StringType, *NumericType, null ]
type: *ObjectIdType
attr: {}
template: *ObjectIdSymbolTemplate
argsTemplate: *ObjectIdSymbolArgsTemplate
What’s in the Symbol Table?
ObjectId:
id: "ObjectId"
The name of the attribute. Mostly
used for error reporting.
What’s in the Symbol Table?
ObjectId:
id: "ObjectId"
callable: *constructor
There are 3 types of symbol:
*func: a function name
*constructor: also a function name, but may require a “new“
*var: a variable. Indicates that the symbol cannot be called.
What’s in a Symbol?
ObjectId:
id: "ObjectId"
callable: *constructor
args:
- [ *StringType, *NumericType, null ]
If the symbol is callable, this is where the arguments are defined. Each
element in the array is a positional argument and contains the list of
acceptable types. So ObjectId accepts one string or number argument, or no
arguments at all.
What’s in a Symbol?
ObjectId:
id: "ObjectId"
callable: *constructor
args:
- [ *StringType, *NumericType, null ]
type: *ObjectIdType
The return type of the function, or
if the symbol is a variable, the type
of the variable.
What’s in a Symbol?
ObjectId:
id: "ObjectId"
callable: *constructor
args:
- [ *StringType, *NumericType, null ]
type: *ObjectIdType
attr: {...}
Any attributes of the symbol. This is a sub-symbol table, i.e. a
mapping of names to symbols. Ex: ObjectId.fromDate()
What’s in a Symbol?
ObjectId:
id: "ObjectId"
callable: *constructor
args:
- [ *StringType, *NumericType, null ]
type: *ObjectIdType
attr: {}
template: *ObjectIdSymbolTemplate
These are functions that accept strings and return strings, and are responsible for doing the string
transformations from one language syntax to another language's syntax. template is the symbol
itself, and argsTemplate is applied to the args. These are specific to the output language and
defined in a separate file.
Each output language has a file
(in YAML)
where the templates are defined.
To Recap
ANTLR creates a tree from the user input
We visit the ANTLR-generated tree using visitors
When the visitor reaches a symbol, it looks up metadata in the
Symbol Table.
§ The metadata includes template functions that specify what
code should be generated
So how do I add my own output language to Compass?
Add your own template file!!
All you need to do to add an output
language is fill out the templates!
symbols/sample_template.yaml
There is a skeleton template file available
To add a new output language:
§ fill out each template with the correct translation to your language.
Templates mostly apply to symbols, but there are also templates for
literals and other syntax.
NumberLong(‘1’)
START
Visitor
NumberLong(‘1’)
START
Visitor
symbols/python/templates.yaml
LongTemplate(arg) {
return `Int64(${arg})`;
}
NumberLong(‘1’)
START
Int64(‘1’)
END
Visitor
symbols/python/templates.yaml
LongTemplate(arg) {
return `Int64(${arg})`;
}
NumberLong(‘1’)
START
Int64(‘1’)
END
Visitor
symbols/python/templates.yaml
LongTemplate(arg) {
return `Int64(${arg})`;
}
symbols/java/templates.yaml
LongTemplate(arg) {
return `Long.parseLong(${arg})`;
}
NumberLong(‘1’)
START
Long.parseLong(
"1"
)
Int64(‘1’)
END
Visitor
symbols/python/templates.yaml
LongTemplate(arg) {
return `Int64(${arg})`;
}
symbols/java/templates.yaml
LongTemplate(arg) {
return `Long.parseLong(${arg})`;
}
NumberLong(‘1’)
START
Long.parseLong(
"1"
)
Int64(‘1’)
END
Visitor
symbols/python/templates.yaml
LongTemplate(arg) {
return `Int64(${arg})`;
}
symbols/java/templates.yaml
LongTemplate(arg) {
return `Long.parseLong(${arg})`;
}
symbols/csharp/templates.yaml
LongTemplate(arg) {
return `Convert.ToInt64(${arg})``;
}
NumberLong(‘1’)
START
Long.parseLong(
"1"
)
Int64(‘1’)
Convert.ToInt64(
"1"
)
END
Visitor
symbols/python/templates.yaml
LongTemplate(arg) {
return `Int64(${arg})`;
}
symbols/java/templates.yaml
LongTemplate(arg) {
return `Long.parseLong(${arg})`;
}
symbols/csharp/templates.yaml
LongTemplate(arg) {
return `Convert.ToInt64(${arg})``;
}
Go forth and write
templates!
Ruby, PHP, Go, R, Rust, C & more!
❖ Want to add an output language?
➢ Just fill out a symbol table file!
❖ Want to write an input language?
➢ Write a visitor
Future Features!
We now have a pluggable transpiler
from any language BSON to any
language BSON….what can we do with
it?
Import pipelines in whatever language you like
Expand the syntax to include driver usage
Generate examples for MongoDB University
Put it in front of
the shell!
Expand it to support 100%
language syntax!
Use it for anything at all...
Thanks to the Compass Team!
★ Alena Khineika
★ Irina Shestak
★ Durran Jordan
Thank you!
Everything I said, in much more detail:
github.com/mongodb-js/bson-transpilers
> CONTRIBUTING.md
Questions?
compass@mongodb.com or anna@mongodb.com
MongoDB World 2019: BSON Transpilers: Transpiling from Any Language to Any Language!

More Related Content

PDF
MongoDB.local Berlin: How to add your favorite language to MongoDB Compass
PDF
Working with Cocoa and Objective-C
PDF
Quick swift tour
PDF
Swift Basics
PDF
Effective Scala: Programming Patterns
PPTX
Javascript Basics
PDF
Real-World Scala Design Patterns
PDF
Introduction to web programming with JavaScript
MongoDB.local Berlin: How to add your favorite language to MongoDB Compass
Working with Cocoa and Objective-C
Quick swift tour
Swift Basics
Effective Scala: Programming Patterns
Javascript Basics
Real-World Scala Design Patterns
Introduction to web programming with JavaScript

What's hot (20)

PPT
JavaScript: The Language
PPTX
All You Need to Know About Type Script
PDF
JavaScript For CSharp Developer
PDF
Basics of JavaScript
PDF
Why Java Sucks and C# Rocks (Final)
PPTX
Lab #2: Introduction to Javascript
PDF
Hourglass Interfaces for C++ APIs - CppCon 2014
PPTX
Scala’s implicits
PDF
Effective Scala (JavaDay Riga 2013)
PDF
Exploring Ceylon with Gavin King - JUG BB Talk - Belrin 2014
PDF
Sorbet at Grailed
PDF
Javascript essentials
PPT
Javascript
PDF
Gunosy.go#7 reflect
PPSX
Esoft Metro Campus - Certificate in java basics
PPT
P H P Part I, By Kian
PDF
Denis Lebedev, Swift
PPT
Javascript
PPTX
002. Introducere in type script
PDF
Php Crash Course - Macq Electronique 2010
JavaScript: The Language
All You Need to Know About Type Script
JavaScript For CSharp Developer
Basics of JavaScript
Why Java Sucks and C# Rocks (Final)
Lab #2: Introduction to Javascript
Hourglass Interfaces for C++ APIs - CppCon 2014
Scala’s implicits
Effective Scala (JavaDay Riga 2013)
Exploring Ceylon with Gavin King - JUG BB Talk - Belrin 2014
Sorbet at Grailed
Javascript essentials
Javascript
Gunosy.go#7 reflect
Esoft Metro Campus - Certificate in java basics
P H P Part I, By Kian
Denis Lebedev, Swift
Javascript
002. Introducere in type script
Php Crash Course - Macq Electronique 2010
Ad

Similar to MongoDB World 2019: BSON Transpilers: Transpiling from Any Language to Any Language! (20)

PPTX
Type script - advanced usage and practices
PDF
Back to the Future with TypeScript
PPTX
Introduction to Kotlin for Android developers
PPTX
Complete Notes on Angular 2 and TypeScript
PDF
TypeScript: coding JavaScript without the pain
PDF
The Swift Compiler and Standard Library
PPTX
Code is not text! How graph technologies can help us to understand our code b...
PPTX
What’s new in .NET
PDF
From android/java to swift (3)
PDF
Kotlin for Android Developers - 3
PPTX
Advanced javascript from zero to hero in this PPT
PPTX
“Insulin” for Scala’s Syntactic Diabetes
PPT
04 Console input output-
PDF
Kotlin: A pragmatic language by JetBrains
PDF
Crystal internals (part 1)
PDF
Crystal internals (part 1)
PDF
Crystal internals (part 1)
PPTX
TypeScript Presentation - Jason Haffey
PDF
IronSmalltalk
PDF
Kotlin for Android devs
Type script - advanced usage and practices
Back to the Future with TypeScript
Introduction to Kotlin for Android developers
Complete Notes on Angular 2 and TypeScript
TypeScript: coding JavaScript without the pain
The Swift Compiler and Standard Library
Code is not text! How graph technologies can help us to understand our code b...
What’s new in .NET
From android/java to swift (3)
Kotlin for Android Developers - 3
Advanced javascript from zero to hero in this PPT
“Insulin” for Scala’s Syntactic Diabetes
04 Console input output-
Kotlin: A pragmatic language by JetBrains
Crystal internals (part 1)
Crystal internals (part 1)
Crystal internals (part 1)
TypeScript Presentation - Jason Haffey
IronSmalltalk
Kotlin for Android devs
Ad

More from MongoDB (20)

PDF
MongoDB SoCal 2020: Migrate Anything* to MongoDB Atlas
PDF
MongoDB SoCal 2020: Go on a Data Safari with MongoDB Charts!
PDF
MongoDB SoCal 2020: Using MongoDB Services in Kubernetes: Any Platform, Devel...
PDF
MongoDB SoCal 2020: A Complete Methodology of Data Modeling for MongoDB
PDF
MongoDB SoCal 2020: From Pharmacist to Analyst: Leveraging MongoDB for Real-T...
PDF
MongoDB SoCal 2020: Best Practices for Working with IoT and Time-series Data
PDF
MongoDB SoCal 2020: MongoDB Atlas Jump Start
PDF
MongoDB .local San Francisco 2020: Powering the new age data demands [Infosys]
PDF
MongoDB .local San Francisco 2020: Using Client Side Encryption in MongoDB 4.2
PDF
MongoDB .local San Francisco 2020: Using MongoDB Services in Kubernetes: any ...
PDF
MongoDB .local San Francisco 2020: Go on a Data Safari with MongoDB Charts!
PDF
MongoDB .local San Francisco 2020: From SQL to NoSQL -- Changing Your Mindset
PDF
MongoDB .local San Francisco 2020: MongoDB Atlas Jumpstart
PDF
MongoDB .local San Francisco 2020: Tips and Tricks++ for Querying and Indexin...
PDF
MongoDB .local San Francisco 2020: Aggregation Pipeline Power++
PDF
MongoDB .local San Francisco 2020: A Complete Methodology of Data Modeling fo...
PDF
MongoDB .local San Francisco 2020: MongoDB Atlas Data Lake Technical Deep Dive
PDF
MongoDB .local San Francisco 2020: Developing Alexa Skills with MongoDB & Golang
PDF
MongoDB .local Paris 2020: Realm : l'ingrédient secret pour de meilleures app...
PDF
MongoDB .local Paris 2020: Upply @MongoDB : Upply : Quand le Machine Learning...
MongoDB SoCal 2020: Migrate Anything* to MongoDB Atlas
MongoDB SoCal 2020: Go on a Data Safari with MongoDB Charts!
MongoDB SoCal 2020: Using MongoDB Services in Kubernetes: Any Platform, Devel...
MongoDB SoCal 2020: A Complete Methodology of Data Modeling for MongoDB
MongoDB SoCal 2020: From Pharmacist to Analyst: Leveraging MongoDB for Real-T...
MongoDB SoCal 2020: Best Practices for Working with IoT and Time-series Data
MongoDB SoCal 2020: MongoDB Atlas Jump Start
MongoDB .local San Francisco 2020: Powering the new age data demands [Infosys]
MongoDB .local San Francisco 2020: Using Client Side Encryption in MongoDB 4.2
MongoDB .local San Francisco 2020: Using MongoDB Services in Kubernetes: any ...
MongoDB .local San Francisco 2020: Go on a Data Safari with MongoDB Charts!
MongoDB .local San Francisco 2020: From SQL to NoSQL -- Changing Your Mindset
MongoDB .local San Francisco 2020: MongoDB Atlas Jumpstart
MongoDB .local San Francisco 2020: Tips and Tricks++ for Querying and Indexin...
MongoDB .local San Francisco 2020: Aggregation Pipeline Power++
MongoDB .local San Francisco 2020: A Complete Methodology of Data Modeling fo...
MongoDB .local San Francisco 2020: MongoDB Atlas Data Lake Technical Deep Dive
MongoDB .local San Francisco 2020: Developing Alexa Skills with MongoDB & Golang
MongoDB .local Paris 2020: Realm : l'ingrédient secret pour de meilleures app...
MongoDB .local Paris 2020: Upply @MongoDB : Upply : Quand le Machine Learning...

Recently uploaded (20)

PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
cuic standard and advanced reporting.pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPT
Teaching material agriculture food technology
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPTX
MYSQL Presentation for SQL database connectivity
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Encapsulation theory and applications.pdf
Review of recent advances in non-invasive hemoglobin estimation
Dropbox Q2 2025 Financial Results & Investor Presentation
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
The AUB Centre for AI in Media Proposal.docx
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
gpt5_lecture_notes_comprehensive_20250812015547.pdf
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
cuic standard and advanced reporting.pdf
Per capita expenditure prediction using model stacking based on satellite ima...
Teaching material agriculture food technology
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
MYSQL Presentation for SQL database connectivity
“AI and Expert System Decision Support & Business Intelligence Systems”
The Rise and Fall of 3GPP – Time for a Sabbatical?
Assigned Numbers - 2025 - Bluetooth® Document
Digital-Transformation-Roadmap-for-Companies.pptx
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Spectral efficient network and resource selection model in 5G networks
MIND Revenue Release Quarter 2 2025 Press Release
Encapsulation theory and applications.pdf

MongoDB World 2019: BSON Transpilers: Transpiling from Any Language to Any Language!