-
Notifications
You must be signed in to change notification settings - Fork 22
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
Introduce SanLang language - Lexer, Parser and Interpreter #4002
Merged
Conversation
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
ed188d9
to
a2a2692
Compare
Don't forget to add an Academy article about Sanlang |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Changes
Overview
SanLang
is a a small interpreted language that can execute one-liners likeflat_map(map_keys(@projects), fn slug -> @projects[slug]["github_organizations"]
.To improve the templating engine capabilities for Queries 2.0 we introduce SanLang -- an interpreted language inspired by the Elixir syntax.
We want to provide the ability for small code snippets (one-liners in most cases) that extract and manipulate some data.
For example, if a user wants to add a text widget with
About the author
information, the user doesn't have to hardcode the email/twitter/telegram/etc. links, but can use code likeEmail: {{@owner["email"}}, Twitter: {{@owner.twitter_handle}}
The identifiers starting with
@
are provided as environment bindings by the backend and the user has access to it without doing anything else.Why a language?
We want to allow the users to write code that is executed on the backend. To allow this, we need to be very careful in what and how we allow it to be executed.
Doing
String.split
/Regex.scan
/etc. parsing won't suffice, or it will be much more complicated and hard to maintain and debug.Allowing users to write Elixir code will force either to analyze all the code for un-safe operations
System.cmd
/http calls
/etc. and it can be hard to verify that it is indeed safe.Using a separate language like python/lua/etc. will require us to add this language compiler/interpreter as a dependency and support inter-language compatibility.
Executing Elixir in a safe environment (container/jail/etc.) will also induce complexity.
Considering all these precautions, developing a new small language does not sound so terrible.
Technologies used
The
SanLang
language has three main components: lexer, parser, and interpreter.lex
andyacc
tools for LALR(1) parsing.Language overview
The following are valid SanLang expressions:
1
,"string"
,3.14
;true
andfalse
;1 + 2*3 + 10
evaluates to 17;pow(10,18)
,div(6,4)
(for integer division);@projects
map
function and lambda functions for working with this environment variables. See below for more examples.@projects["santiment"]["main_contract_address"]["decimals"]
1 == 1
,1 != 2
,1 > 2
,1 < 2
,1 >= 2
,1 <= 2
;and
andor
:true and false
,true or false
;5 + 6 < 10
,pow(2, 10) - 1 < 1024 and pow(2,10) + 1 > 1024
.Examples:
@projects
map:map_keys(@projects)
`@projects["sentiment"]["main_contract_address"]["decimals"]
flat_map(map_keys(@projects), fn slug -> @projects[slug]["github_organizations"] end)
@owner["email"]
filter(@data, fn x -> x > 1 and x < 10 end)
san_lang_test.exs
for more examples.Ticket
Checklist: