Skip to content
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

Add JsLIGO support #5908

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,6 @@
[submodule "vendor/grammars/JSyntax"]
path = vendor/grammars/JSyntax
url = https://github.com/tikkanz/JSyntax
[submodule "vendor/grammars/Ligo-grammar"]
path = vendor/grammars/Ligo-grammar
url = https://github.com/pewulfman/Ligo-grammar.git
[submodule "vendor/grammars/LiveScript.tmbundle"]
path = vendor/grammars/LiveScript.tmbundle
url = https://github.com/paulmillr/LiveScript.tmbundle
Expand Down Expand Up @@ -779,6 +776,9 @@
[submodule "vendor/grammars/latex.tmbundle"]
path = vendor/grammars/latex.tmbundle
url = https://github.com/textmate/latex.tmbundle
[submodule "vendor/grammars/ligo"]
path = vendor/grammars/ligo
url = https://gitlab.com/ligolang/ligo.git
[submodule "vendor/grammars/linter-lilypond"]
path = vendor/grammars/linter-lilypond
url = https://github.com/nwhetsell/linter-lilypond
Expand Down
9 changes: 5 additions & 4 deletions grammars.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@ vendor/grammars/Isabelle.tmbundle:
- source.isabelle.theory
vendor/grammars/JSyntax:
- source.j
vendor/grammars/Ligo-grammar:
- source.ligo
- source.mligo
- source.religo
vendor/grammars/LiveScript.tmbundle:
- source.livescript
vendor/grammars/MATLAB-Language-grammar:
Expand Down Expand Up @@ -725,6 +721,11 @@ vendor/grammars/latex.tmbundle:
- text.tex.latex
- text.tex.latex.beamer
- text.tex.latex.memoir
vendor/grammars/ligo:
- source.jsligo
- source.ligo
- source.mligo
- source.religo
vendor/grammars/linter-lilypond:
- source.lilypond
vendor/grammars/liquid-tm-grammar:
Expand Down
13 changes: 12 additions & 1 deletion lib/linguist/languages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -869,7 +869,7 @@ Cairo:
language_id: 620599567
CameLIGO:
type: programming
color: "#3be133"
color: "#f08707"
extensions:
- ".mligo"
tm_scope: source.mligo
Expand Down Expand Up @@ -3101,6 +3101,17 @@ Jolie:
ace_mode: text
tm_scope: source.jolie
language_id: 998078858
JsLIGO:
type: programming
color: "#f1e05a"
extensions:
- ".jsligo"
tm_scope: source.jsligo
ace_mode: javascript
codemirror_mode: javascript
codemirror_mime_type: text/javascript
group: LigoLANG
language_id: 588633658
Jsonnet:
color: "#0064bd"
type: programming
Expand Down
181 changes: 181 additions & 0 deletions samples/JsLIGO/FA1.2.jsligo
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
/* Errors */
namespace Errors {
export let notEnoughBalance = "NotEnoughBalance";
export let notEnoughAllowance = "NotEnoughAllowance";
/* Extra error, see: https://docs.google.com/document/d/1YLPtQxZu1UAvO9cZ1O2RPXBbT0mooh4DYKjA_jp-RLM/edit */
export let vulnerable_operation = "Switching allowances from N to M is a vulnerability";
};

namespace Allowance {
type spender = address;
type allowed_amount = nat;
export type t = map<spender, allowed_amount>;

export let get_allowed_amount = (a:t, spender:spender) : nat => {
return match(Map.find_opt(spender,a), {
Some: (v : allowed_amount) => v,
None: () => 0 as nat
});
};

export let set_allowed_amount = (a:t, spender:spender, allowed_amount:allowed_amount) : t => {
if (allowed_amount > (0 as nat)) {
return Map.add(spender, allowed_amount, a)
} else {
return a;
}
}
};


namespace Ledger {
type owner = address;
type spender = address;
type amount_ = nat;
export type t = big_map<owner, [amount_, Allowance.t]>;

export let get_for_user = (ledger:t, owner: owner) : [amount_,Allowance.t] => {
return match(Big_map.find_opt(owner, ledger), {
Some: (tokens : [amount_, Allowance.t]) => tokens,
None: () => [0 as nat,Map.empty as Allowance.t]
});
};

export let update_for_user = (ledger:t, owner: owner, amount_ : amount_, allowances : Allowance.t) : t => {
return Big_map.update(owner, (Some ([amount_,allowances])), ledger)
};

export let set_approval = (ledger:t, owner: owner, spender : spender, allowed_amount: amount_) : t => {
let [tokens, allowances] = get_for_user(ledger, owner);
let previous_allowances = Allowance.get_allowed_amount(allowances, spender);
let _ = assert_with_error((previous_allowances == (0 as nat) || allowed_amount == (0 as nat)), Errors.vulnerable_operation);
let allowances = Allowance.set_allowed_amount(allowances, spender, allowed_amount);
let ledger = update_for_user(ledger, owner, tokens, allowances);
return ledger
};

export let decrease_token_amount_for_user = (ledger : t, spender : spender, from_ : owner, amount_ : amount_) : t => {
let [tokens, allowances] = get_for_user(ledger, from_);
let allowed_amount = Allowance.get_allowed_amount(allowances, spender);
if (spender == from_) {
allowed_amount = tokens;
};
let _ = assert_with_error((allowed_amount >= amount_), Errors.notEnoughAllowance);
let _ = assert_with_error((tokens >= amount_), Errors.notEnoughBalance);
let tokens = abs(tokens - amount_);
let ledger = update_for_user(ledger, from_, tokens, allowances);
return ledger
};

export let increase_token_amount_for_user = (ledger : t, to_ : owner, amount_ : amount_) : t => {
let [tokens, allowances] = get_for_user(ledger, to_);
let tokens = tokens + amount_;
let ledger = update_for_user(ledger, to_, tokens, allowances);
return ledger
};
};

namespace TokenMetadata {
/**
This should be initialized at origination, conforming to either
TZIP-12 : https://gitlab.com/tezos/tzip/-/blob/master/proposals/tzip-12/tzip-12.md#token-metadata
or TZIP-16 : https://gitlab.com/tezos/tzip/-/blob/master/proposals/tzip-12/tzip-12.md#contract-metadata-tzip-016
*/
type data = {
token_id : nat,
token_info : map<string, bytes>
};
export type t = data;
};

namespace Storage {
export type t = {
ledger : Ledger.t,
token_metadata : TokenMetadata.t,
totalSupply : nat,
/* Note: memoizing the sum of all participant balance reduce the cost of getTotalSupply entrypoint.
However, with this pattern the value has to be manually set at origination which can lead to consistency issues.
*/
};

export let get_amount_for_owner = (s:t, owner : address) : nat => {
let [amount_, _] = Ledger.get_for_user(s.ledger, owner);
return amount_
};

export let get_allowances_for_owner = (s:t, owner : address) : Allowance.t => {
let [_, allowances] = Ledger.get_for_user(s.ledger, owner);
return allowances
};

export let get_ledger = (s:t) : Ledger.t => s.ledger;
export let set_ledger = (s:t, ledger:Ledger.t) : t => {
return {...s, ledger: ledger}
}

};

type storage = Storage.t;


/** transfer entrypoint */
type transfer = [address, [address, nat]];
const transfer = ([from_, to_value]:transfer, s:storage) : [list<operation>, Storage.t] => {
let [to_, value] = to_value;
let ledger1 = Storage.get_ledger(s);
let ledger2 = Ledger.decrease_token_amount_for_user(ledger1, Tezos.sender, from_, value);
let ledger = Ledger.increase_token_amount_for_user(ledger2, to_, value);
let s1 = Storage.set_ledger(s, ledger);
return [(list([]) as list<operation>), s1]
};

/** approve */
type approve = [address, nat];
const approve = ([spender,value] : approve, s:storage) : [list<operation>, Storage.t] => {
let ledger1 = Storage.get_ledger(s);
let ledger = Ledger.set_approval(ledger1, Tezos.sender, spender, value);
let s1 = Storage.set_ledger(s, ledger);
return [list([]) as list<operation>, s1]
};

/** getBalance entrypoint */
type getAllowance = [[address, address], contract<nat>];
const getAllowance = ([owner_spender,callback]: getAllowance, s: storage) : [list<operation>, Storage.t] => {
let [owner,spender] = owner_spender;
let a = Storage.get_allowances_for_owner(s, owner);
let allowed_amount = Allowance.get_allowed_amount(a, spender);
let operation = Tezos.transaction(allowed_amount, 0 as tez, callback);
return [list([operation]) as list<operation>, s]
};

/** getBalance entrypoint */
type getBalance = [address, contract<nat>];
const getBalance = ([owner,callback]: getBalance, s: storage) : [list<operation>, Storage.t] => {
let balance_ = Storage.get_amount_for_owner(s, owner);
let operation = Tezos.transaction(balance_, 0 as tez, callback);
return [list([operation]) as list<operation>, s]
};

/** getTotalSupply entrypoint */
type getTotalSupply = [unit, contract<nat>];
const getTotalSupply = ([_,callback] : getTotalSupply, s:storage) : [list<operation>, Storage.t] => {
let operation = Tezos.transaction(s.totalSupply, 0 as tez, callback);
return [list([operation]) as list<operation>, s]
};

type parameter =
["Transfer", transfer]
| ["Approve", approve]
| ["GetAllowance", getAllowance]
| ["GetBalance", getBalance]
| ["GetTotalSupply", getTotalSupply];

let main = ([p,s]:[parameter, storage]) : [list<operation>, Storage.t] => {
return match(p, {
Transfer: (p : transfer) => transfer(p, s),
Approve: (p : approve) => approve(p, s),
GetAllowance: (p : getAllowance) => getAllowance(p, s),
GetBalance: (p : getBalance) => getBalance(p, s),
GetTotalSupply: (p : getTotalSupply) => getTotalSupply(p, s)
});
}
26 changes: 15 additions & 11 deletions tools/grammars/compiler/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,19 @@ var GrammarsInNonStdPath = map[string]bool{

// IgnoredFiles is a list of files that look like syntax files but aren't, or are known to be broken and never likely to be fixed.
var IgnoredFiles = map[string]bool{
"ballerina-grammar/syntaxes/ballerina.monarch.json": true,
"oz-tmbundle/Originals/Oz.tmLanguage": true,
"abl-tmlanguage/package-lock.json": true,
"abl-tmlanguage/package.json": true,
"avro.tmLanguage/package-lock.json": true,
"avro.tmLanguage/package.json": true,
"avro.tmLanguage/avro-avsc-json-schema.json": true,
"liquid-tm-grammar/tests/syntaxes/css.tmLanguage.json": true,
"liquid-tm-grammar/tests/syntaxes/html.tmLanguage.json": true,
"liquid-tm-grammar/tests/syntaxes/javascript.tmLanguage.json": true,
"liquid-tm-grammar/tests/syntaxes/json.tmLanguage.json": true,
"ballerina-grammar/syntaxes/ballerina.monarch.json": true,
"oz-tmbundle/Originals/Oz.tmLanguage": true,
"abl-tmlanguage/package-lock.json": true,
"abl-tmlanguage/package.json": true,
"avro.tmLanguage/package-lock.json": true,
"avro.tmLanguage/package.json": true,
"avro.tmLanguage/avro-avsc-json-schema.json": true,
"liquid-tm-grammar/tests/syntaxes/css.tmLanguage.json": true,
"liquid-tm-grammar/tests/syntaxes/html.tmLanguage.json": true,
"liquid-tm-grammar/tests/syntaxes/javascript.tmLanguage.json": true,
"liquid-tm-grammar/tests/syntaxes/json.tmLanguage.json": true,
"ligo/tools/lsp/vscode-plugin/syntaxes/religo.configuration.json": true,
"ligo/tools/lsp/vscode-plugin/syntaxes/jsligo.configuration.json": true,
"ligo/tools/lsp/vscode-plugin/syntaxes/mligo.configuration.json": true,
"ligo/tools/lsp/vscode-plugin/syntaxes/ligo.configuration.json": true,
}
7 changes: 4 additions & 3 deletions vendor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ This is a list of grammars that Linguist selects to provide syntax highlighting
- **Cabal Config:** [atom-haskell/language-haskell](https://github.com/atom-haskell/language-haskell)
- **Cadence:** [onflow/vscode-cadence](https://github.com/onflow/vscode-cadence)
- **Cairo:** [xshitaka/atom-language-cairo](https://github.com/xshitaka/atom-language-cairo)
- **CameLIGO:** [pewulfman/Ligo-grammar](https://github.com/pewulfman/Ligo-grammar)
- **CameLIGO:** [gitlab:ligolang/ligo](https://gitlab.com/ligolang/ligo)
- **Cap'n Proto:** [textmate/capnproto.tmbundle](https://github.com/textmate/capnproto.tmbundle)
- **CartoCSS:** [yohanboniface/carto-atom](https://github.com/yohanboniface/carto-atom)
- **Ceylon:** [jeancharles-roger/ceylon-sublimetext](https://github.com/jeancharles-roger/ceylon-sublimetext)
Expand Down Expand Up @@ -252,6 +252,7 @@ This is a list of grammars that Linguist selects to provide syntax highlighting
- **Jison:** [cdibbs/language-jison](https://github.com/cdibbs/language-jison)
- **Jison Lex:** [cdibbs/language-jison](https://github.com/cdibbs/language-jison)
- **Jolie:** [fmontesi/language-jolie](https://github.com/fmontesi/language-jolie)
- **JsLIGO:** [gitlab:ligolang/ligo](https://gitlab.com/ligolang/ligo)
- **Jsonnet:** [google/language-jsonnet](https://github.com/google/language-jsonnet)
- **Julia:** [JuliaEditorSupport/atom-language-julia](https://github.com/JuliaEditorSupport/atom-language-julia)
- **Jupyter Notebook:** [Nixinova/NovaGrammars](https://github.com/Nixinova/NovaGrammars)
Expand All @@ -274,7 +275,7 @@ This is a list of grammars that Linguist selects to provide syntax highlighting
- **Lean:** [leanprover/vscode-lean](https://github.com/leanprover/vscode-lean)
- **Less:** [atom/language-less](https://github.com/atom/language-less)
- **Lex:** [Alhadis/language-grammars](https://github.com/Alhadis/language-grammars)
- **LigoLANG:** [pewulfman/Ligo-grammar](https://github.com/pewulfman/Ligo-grammar)
- **LigoLANG:** [gitlab:ligolang/ligo](https://gitlab.com/ligolang/ligo)
- **LilyPond:** [nwhetsell/linter-lilypond](https://github.com/nwhetsell/linter-lilypond)
- **Liquid:** [Shopify/liquid-tm-grammar](https://github.com/Shopify/liquid-tm-grammar)
- **Literate CoffeeScript:** [atom/language-coffee-script](https://github.com/atom/language-coffee-script)
Expand Down Expand Up @@ -417,7 +418,7 @@ This is a list of grammars that Linguist selects to provide syntax highlighting
- **ReScript:** [rescript-lang/rescript-vscode](https://github.com/rescript-lang/rescript-vscode)
- **Readline Config:** [Alhadis/language-etc](https://github.com/Alhadis/language-etc)
- **Reason:** [reasonml-editor/language-reason](https://github.com/reasonml-editor/language-reason)
- **ReasonLIGO:** [pewulfman/Ligo-grammar](https://github.com/pewulfman/Ligo-grammar)
- **ReasonLIGO:** [gitlab:ligolang/ligo](https://gitlab.com/ligolang/ligo)
- **Rebol:** [Oldes/Sublime-REBOL](https://github.com/Oldes/Sublime-REBOL)
- **Record Jar:** [Alhadis/language-etc](https://github.com/Alhadis/language-etc)
- **Red:** [Oldes/Sublime-Red](https://github.com/Oldes/Sublime-Red)
Expand Down
1 change: 0 additions & 1 deletion vendor/grammars/Ligo-grammar
Submodule Ligo-grammar deleted from d1338b
1 change: 1 addition & 0 deletions vendor/grammars/ligo
Submodule ligo added at f901f3
31 changes: 0 additions & 31 deletions vendor/licenses/git_submodule/Ligo-grammar.dep.yml

This file was deleted.

17 changes: 17 additions & 0 deletions vendor/licenses/git_submodule/ligo.dep.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
name: ligo
version: f901f3f382785e38a008b165d106f79f30116446
type: git_submodule
homepage: https://gitlab.com/ligolang/ligo.git
license: mit
licenses:
- sources: LICENSE.md
text: |
Copyright 2019 contributors

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
notices: []