diff --git a/examples/guessing_game.ds b/examples/guessing_game.ds new file mode 100644 index 0000000..acb6b3d --- /dev/null +++ b/examples/guessing_game.ds @@ -0,0 +1,28 @@ +# This is a Dust version of an example from the Rust Book. +# +# https://doc.rust-lang.org/book/ch02-00-guessing-game-tutorial.html + +output("Guess the number.") + +secret_number = int:random_range(0..=100); + +loop { + output("Please input your guess.") + + input = io::stdin():expect("Failed to read line.") + guess = int:parse(input); + + output("You guessed: " + guess) + + match cmp(guess, secret_number) { + Ordering::Less -> output("Too small!"), + Ordering::Greater -> output("Too big!"), + Ordering::Equal -> { + output("You win!"); + break; + } + } +} + + + diff --git a/tree-sitter-dust/grammar.js b/tree-sitter-dust/grammar.js index 5221183..50a0c97 100644 --- a/tree-sitter-dust/grammar.js +++ b/tree-sitter-dust/grammar.js @@ -31,6 +31,7 @@ module.exports = grammar({ $.for, $.if_else, $.index_assignment, + $.break_loop, $.match, $.pipe, $.while, @@ -327,6 +328,12 @@ module.exports = grammar({ ), ), + break_loop: $ => + seq( + 'loop', + $.block, + ), + while: $ => seq( 'while', diff --git a/tree-sitter-dust/highlights.scm b/tree-sitter-dust/highlights.scm index ba9a6ae..f3b7925 100644 --- a/tree-sitter-dust/highlights.scm +++ b/tree-sitter-dust/highlights.scm @@ -1,20 +1,33 @@ -(expression) @expression +(statement) @statement +[ + (expression) + (function_expression) + (index_expression) +] @expression (value) @value (identifier) @variable + (value) @value (string) @string - [ (integer) (float) ] @number - -(function) @function - +[ + (command) + (function) +] @function +(range) @range (boolean) @boolean (list) @list +(map) @map -["," ":" ";"] @punctuation.delimiter +(struct_definition) @struct +(enum_definition) @enum + +(block) @block + +["," ";"] @punctuation.delimiter [ "[" @@ -27,30 +40,33 @@ ")" ] @punctuation.bracket -[ - (type) - (type_specification) -] @type +(type) @type (assignment_operator) @operator.assignment (logic_operator) @operator.logic (math_operator) @operator.math [ + "as" "async" + "break" "else" "else if" + "enum" "false" "for" "if" "in" + "loop" "match" - "self" + "return" + "struct" "true" "while" "->" - "=>" + ":" + "::" + "^" ] @keyword -(built_in_function) @function.builtin (function_call) @function.call