Skip to content

Commit

Permalink
Add Sway language (#6275)
Browse files Browse the repository at this point in the history
* Add sway language

* Heuristics for .sw

* Add XML regex and sample

* Add space after abi in regex

* Update samples

* XML heuristic test

* Update regex
  • Loading branch information
sdankel committed Mar 2, 2023
1 parent 89f4df5 commit 592bbed
Show file tree
Hide file tree
Showing 14 changed files with 653 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -1121,6 +1121,9 @@
[submodule "vendor/grammars/svelte-atom"]
path = vendor/grammars/svelte-atom
url = https://github.com/sebastinez/svelte-atom.git
[submodule "vendor/grammars/sway-vscode-plugin"]
path = vendor/grammars/sway-vscode-plugin
url = https://github.com/FuelLabs/sway-vscode-plugin.git
[submodule "vendor/grammars/swift.tmbundle"]
path = vendor/grammars/swift.tmbundle
url = https://github.com/textmate/swift.tmbundle
Expand Down
2 changes: 2 additions & 0 deletions grammars.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1007,6 +1007,8 @@ vendor/grammars/sublimetext-nasl:
- source.tnsaudit
vendor/grammars/svelte-atom:
- source.svelte
vendor/grammars/sway-vscode-plugin:
- source.sway
vendor/grammars/swift.tmbundle:
- source.swift
vendor/grammars/tcl.tmbundle:
Expand Down
6 changes: 6 additions & 0 deletions lib/linguist/heuristics.yml
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,12 @@ disambiguations:
rules:
- language: STL
pattern: '\A\s*solid(?=$|\s)(?m:.*?)\Rendsolid(?:$|\s)'
- extensions: ['.sw']
rules:
- language: Sway
pattern: '^\s*(?:(?:abi|dep|fn|impl|mod|pub|trait)\s|#\[)'
- language: XML
pattern: '^\s*<\?xml\s+version'
- extensions: ['.t']
rules:
- language: Perl
Expand Down
11 changes: 11 additions & 0 deletions lib/linguist/languages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6659,6 +6659,16 @@ Svelte:
extensions:
- ".svelte"
language_id: 928734530
Sway:
type: programming
color: "#dea584"
extensions:
- ".sw"
tm_scope: source.sway
ace_mode: rust
codemirror_mode: rust
codemirror_mime_type: text/x-rustsrc
language_id: 271471144
Swift:
type: programming
color: "#F05138"
Expand Down Expand Up @@ -7554,6 +7564,7 @@ XML:
- ".srdf"
- ".storyboard"
- ".sublime-snippet"
- ".sw"
- ".targets"
- ".tml"
- ".ts"
Expand Down
34 changes: 34 additions & 0 deletions samples/Sway/arrays.sw
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
script;

struct Foo {
f1: u32,
f2: b256,
}

fn main() {
// Array of integers with type ascription
let array_of_integers: [u8; 5] = [1, 2, 3, 4, 5];

// Array of strings
let array_of_strings = ["Bob", "Jan", "Ron"];

// Array of structs
let array_of_structs: [Foo; 2] = [
Foo {
f1: 11,
f2: 0x1111111111111111111111111111111111111111111111111111111111111111,
},
Foo {
f1: 22,
f2: 0x2222222222222222222222222222222222222222222222222222222222222222,
},
];

// Accessing an element of an array
let mut array_of_bools: [bool; 2] = [true, false];
assert(array_of_bools[0]);

// Mutating the element of an array
array_of_bools[1] = true;
assert(array_of_bools[1]);
}
220 changes: 220 additions & 0 deletions samples/Sway/generic_traits.sw
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
script;

dep my_double;
dep my_point;
dep my_triple;

use my_point::MyPoint;
use my_triple::MyTriple;

trait Setter<T> {
fn set(self, new_value: T) -> Self;
}

struct FooBarData<T> {
value: T
}

impl<T> Setter<T> for FooBarData<T> {
fn set(self, new_value: T) -> Self {
FooBarData {
value: new_value,
}
}
}

trait Returner<T> {
fn return_it(self, the_value: T) -> T;
}

impl<T, F> Returner<T> for FooBarData<F> {
fn return_it(self, the_value: T) -> T {
the_value
}
}

trait MyAdd<T> {
fn my_add(self, a: T, b: T) -> T;
}

// impl<T> MyAdd<u8> for FooBarData<T> {
// fn my_add(self, a: u8, b: u8) -> u8 {
// a + b
// }
// }

impl<T> MyAdd<u64> for FooBarData<T> {
fn my_add(self, a: u64, b: u64) -> u64 {
a + b
}
}

trait MySub<T> {
fn my_sub(a: T, b: T) -> T;
}

// impl<T> MySub<u8> for FooBarData<T> {
// fn my_sub(a: u8, b: u8) -> u8 {
// if a >= b {
// a - b
// } else {
// b - a
// }
// }
// }

impl<T> MySub<u64> for FooBarData<T> {
fn my_sub(a: u64, b: u64) -> u64 {
if a >= b {
a - b
} else {
b - a
}
}
}

struct OtherData<T> {
a: T,
b: T,
}

// impl<T> MyAdd<u8> for OtherData<T> {
// fn my_add(self, a: u8, b: u8) -> u8 {
// a + b
// }
// }

impl<T> MyAdd<u64> for OtherData<T> {
fn my_add(self, a: u64, b: u64) -> u64 {
a + b
}
}

// impl<T> MySub<u8> for OtherData<T> {
// fn my_sub(a: u8, b: u8) -> u8 {
// if a >= b {
// a - b
// } else {
// b - a
// }
// }
// }

impl<T> MySub<u64> for OtherData<T> {
fn my_sub(a: u64, b: u64) -> u64 {
if a >= b {
a - b
} else {
b - a
}
}
}

impl MyTriple<u64> for MyPoint<u64> {
fn my_triple(self, value: u64) -> u64 {
(self.x*3) + (self.y*3) + (value*3)
}
}

struct MyU64 {
inner: u64
}

impl MyTriple<u64> for MyU64 {
fn my_triple(self, value: u64) -> u64 {
(self.inner*3) + (value*3)
}
}

pub struct A {
a: u64,
}

pub struct B {
b: u64,
}

pub struct C {
c: u64,
}

pub trait Convert<T> {
fn convert(t: T) -> Self;
}

impl Convert<B> for A {
fn convert(t: B) -> Self {
A {
a: t.b
}
}
}

impl Convert<C> for A {
fn convert(t: C) -> Self {
A {
a: t.c
}
}
}

fn main() -> u64 {
let a = FooBarData {
value: 1u8
};
let b = a.set(42);
let c = b.value;
let d = b.return_it(true);
let e = b.return_it(9u64);
let f = FooBarData {
value: 1u64
};
let g = f.my_add(a.my_add(1u8, 2u8), a.my_add(3u8, 4u8));
let h = FooBarData::<u64>::my_sub(
FooBarData::<u8>::my_sub(100, 10),
FooBarData::<u8>::my_sub(50, 10),
);
let i = OtherData {
a: true,
b: false,
};
let j = OtherData {
a: 10u32,
b: 11u32,
};
let k = j.my_add(i.my_add(1u8, 2u8), i.my_add(3u8, 4u8));
let l = FooBarData::<u16>::my_sub(
FooBarData::<u32>::my_sub(100, 10),
FooBarData::<u32>::my_sub(50, 10),
);
let m = MyPoint {
x: 10u64,
y: 10u64,
};
let n = m.my_double(100);
let o = m.my_triple(100);
let p = MyU64 {
inner: 30u64
};
let q = p.my_triple(1);

let r_b = B { b: 42 };
let r_c = C { c: 42 };

if c == 42u8
&& d
&& e == 9u64
&& g == 10
&& h == 50
&& k == 10
&& l == 50
&& n == 240
&& o == 360
&& q == 93
&& A::convert(r_b).a == 42
&& A::convert(r_c).a == 42 {
42
} else {
7
}
}
81 changes: 81 additions & 0 deletions samples/Sway/struct_fns.sw
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
library utils;

dep data_structures;
use data_structures::{Foo, Line, Point, TupleInStruct};

fn hardcoded_instantiation() -> Foo {
// Instantiate `foo` as `Foo`
let mut foo = Foo {
bar: 42,
baz: false,
};

// Access and write to "baz"
foo.baz = true;

// Return the struct
foo
}

fn variable_instantiation() -> Foo {
// Declare variables with the same names as the fields in `Foo`
let number = 42;
let truthness = false;

// Instantiate `foo` as `Foo`
let mut foo = Foo {
bar: number,
baz: truthness,
};

// Access and write to "baz"
foo.baz = true;

// Return the struct
foo
}

fn shorthand_instantiation() -> Foo {
// Declare variables with the same names as the fields in `Foo`
let bar = 42;
let baz = false;

// Instantiate `foo` as `Foo`
let mut foo = Foo { bar, baz };

// Access and write to "baz"
foo.baz = true;

// Return the struct
foo
}

fn struct_destructuring() {
let point1 = Point { x: 0, y: 0 };
// Destructure the values from the struct into variables
let Point { x, y } = point1;

let point2 = Point { x: 1, y: 1 };
// If you do not care about specific struct fields then use ".." at the end of your variable list
let Point { x, .. } = point2;

let line = Line {
p1: point1,
p2: point2,
};
// Destructure the values from the nested structs into variables
let Line {
p1: Point { x: x0, y: y0 },
p2: Point { x: x1, y: y1 },
} = line;
// You may also destructure tuples nested in structs and structs nested in tuples
let tuple_in_struct = TupleInStruct {
nested_tuple: (42u64, (42u32, (true, "ok"))),
};
let TupleInStruct {
nested_tuple: (a, (b, (c, d))),
} = tuple_in_struct;

let struct_in_tuple = (Point { x: 2, y: 4 }, Point { x: 3, y: 6 });
let (Point { x: x0, y: y0 }, Point { x: x1, y: y1 }) = struct_in_tuple;
}

0 comments on commit 592bbed

Please sign in to comment.