Implement list, map and string indexing
This commit is contained in:
parent
4eabd61ea8
commit
9f571a0bfb
2
Cargo.lock
generated
2
Cargo.lock
generated
@ -316,7 +316,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "dust-lang"
|
||||
version = "0.3.2"
|
||||
version = "0.3.3"
|
||||
dependencies = [
|
||||
"ansi_term",
|
||||
"cc",
|
||||
|
@ -1,7 +1,7 @@
|
||||
numbers = [1, 2, 3]
|
||||
|
||||
x = numbers.0
|
||||
y = numbers.1
|
||||
z = numbers.2
|
||||
x = numbers.{0}
|
||||
y = numbers.{1}
|
||||
z = numbers.{2}
|
||||
|
||||
(assert_equal x + y, z)
|
||||
|
11
examples/map.ds
Normal file
11
examples/map.ds
Normal file
@ -0,0 +1,11 @@
|
||||
dictionary = {
|
||||
dust = "awesome"
|
||||
answer = 42
|
||||
}
|
||||
|
||||
(output
|
||||
'Dust is '
|
||||
+ dictionary.dust
|
||||
+ '! The answer is '
|
||||
+ dictionary.answer
|
||||
)
|
@ -15,10 +15,10 @@ impl AbstractTree for Index {
|
||||
let collection_node = node.child(0).unwrap();
|
||||
let collection = Expression::from_syntax_node(source, collection_node)?;
|
||||
|
||||
let index_node = node.child(3).unwrap();
|
||||
let index_node = node.child(2).unwrap();
|
||||
let index = Expression::from_syntax_node(source, index_node)?;
|
||||
|
||||
let index_end_node = node.child(5);
|
||||
let index_end_node = node.child(4);
|
||||
let index_end = if let Some(index_end_node) = index_end_node {
|
||||
Some(Expression::from_syntax_node(source, index_end_node)?)
|
||||
} else {
|
||||
@ -52,10 +52,8 @@ impl AbstractTree for Index {
|
||||
}
|
||||
Value::Map(mut map) => {
|
||||
let value = self.index.run(source, &mut map)?;
|
||||
let index = value.as_string()?;
|
||||
let item = map.variables().get(index).cloned().unwrap_or_default();
|
||||
|
||||
Ok(item)
|
||||
Ok(value)
|
||||
}
|
||||
Value::String(string) => {
|
||||
let index = self.index.run(source, context)?.as_integer()? as usize;
|
||||
|
@ -86,39 +86,3 @@ for list in list_of_lists {
|
||||
(expression
|
||||
(value
|
||||
(string))))))))))))))
|
||||
|
||||
==================
|
||||
Async For Loop
|
||||
==================
|
||||
|
||||
asnyc for list in list_of_lists {
|
||||
async for item in list {
|
||||
(output item)
|
||||
}
|
||||
}
|
||||
|
||||
---
|
||||
|
||||
(root
|
||||
(item
|
||||
(statement
|
||||
(expression
|
||||
(identifier))))
|
||||
(item
|
||||
(statement
|
||||
(for
|
||||
(identifier)
|
||||
(expression
|
||||
(identifier))
|
||||
(item
|
||||
(statement
|
||||
(for
|
||||
(identifier)
|
||||
(expression
|
||||
(identifier))
|
||||
(item
|
||||
(statement
|
||||
(expression
|
||||
(tool
|
||||
(expression
|
||||
(identifier)))))))))))))
|
||||
|
@ -21,52 +21,3 @@ __xyz__
|
||||
(statement
|
||||
(expression
|
||||
(identifier)))))
|
||||
|
||||
==================
|
||||
Dot Notation
|
||||
==================
|
||||
|
||||
dust_data.{1}.{name}
|
||||
|
||||
creature.{total_clams}
|
||||
|
||||
foobar.{1}.{42}
|
||||
|
||||
---
|
||||
|
||||
(root
|
||||
(item
|
||||
(statement
|
||||
(expression
|
||||
(index
|
||||
(expression
|
||||
(index
|
||||
(expression
|
||||
(identifier))
|
||||
(expression
|
||||
(value
|
||||
(integer)))))
|
||||
(expression
|
||||
(identifier))))))
|
||||
(item
|
||||
(statement
|
||||
(expression
|
||||
(index
|
||||
(expression
|
||||
(identifier))
|
||||
(expression
|
||||
(identifier))))))
|
||||
(item
|
||||
(statement
|
||||
(expression
|
||||
(index
|
||||
(expression
|
||||
(index
|
||||
(expression
|
||||
(identifier))
|
||||
(expression
|
||||
(value
|
||||
(integer)))))
|
||||
(expression
|
||||
(value
|
||||
(integer))))))))
|
||||
|
80
tree-sitter-dust/corpus/index.txt
Normal file
80
tree-sitter-dust/corpus/index.txt
Normal file
@ -0,0 +1,80 @@
|
||||
==================
|
||||
Indexing
|
||||
==================
|
||||
|
||||
dust_data:1:name
|
||||
|
||||
creature:total_clams
|
||||
|
||||
foobar:1:42
|
||||
|
||||
---
|
||||
|
||||
(root
|
||||
(item
|
||||
(statement
|
||||
(expression
|
||||
(index
|
||||
(expression
|
||||
(index
|
||||
(expression
|
||||
(identifier))
|
||||
(expression
|
||||
(value
|
||||
(integer)))))
|
||||
(expression
|
||||
(identifier))))))
|
||||
(item
|
||||
(statement
|
||||
(expression
|
||||
(index
|
||||
(expression
|
||||
(identifier))
|
||||
(expression
|
||||
(identifier))))))
|
||||
(item
|
||||
(statement
|
||||
(expression
|
||||
(index
|
||||
(expression
|
||||
(index
|
||||
(expression
|
||||
(identifier))
|
||||
(expression
|
||||
(value
|
||||
(integer)))))
|
||||
(expression
|
||||
(value
|
||||
(integer))))))))
|
||||
|
||||
==================
|
||||
Sublist
|
||||
==================
|
||||
|
||||
['answers', 42, 666]:1..2
|
||||
|
||||
---
|
||||
|
||||
(root
|
||||
(item
|
||||
(statement
|
||||
(expression
|
||||
(index
|
||||
(expression
|
||||
(value
|
||||
(list
|
||||
(expression
|
||||
(value
|
||||
(string)))
|
||||
(expression
|
||||
(value
|
||||
(integer)))
|
||||
(expression
|
||||
(value
|
||||
(integer))))))
|
||||
(expression
|
||||
(value
|
||||
(integer)))
|
||||
(expression
|
||||
(value
|
||||
(integer))))))))
|
@ -80,64 +80,3 @@ List Nesting
|
||||
(expression
|
||||
(value
|
||||
(integer)))))))))))))))
|
||||
|
||||
==================
|
||||
List Index
|
||||
==================
|
||||
|
||||
['answers', 42, 666].{1}
|
||||
|
||||
---
|
||||
|
||||
(root
|
||||
(item
|
||||
(statement
|
||||
(expression
|
||||
(index
|
||||
(expression
|
||||
(value
|
||||
(list
|
||||
(expression
|
||||
(value
|
||||
(string)))
|
||||
(expression
|
||||
(value
|
||||
(integer)))
|
||||
(expression
|
||||
(value
|
||||
(integer))))))
|
||||
(expression
|
||||
(value
|
||||
(integer))))))))
|
||||
|
||||
==================
|
||||
Sublist
|
||||
==================
|
||||
|
||||
['answers', 42, 666].{1..2}
|
||||
|
||||
---
|
||||
|
||||
(root
|
||||
(item
|
||||
(statement
|
||||
(expression
|
||||
(index
|
||||
(expression
|
||||
(value
|
||||
(list
|
||||
(expression
|
||||
(value
|
||||
(string)))
|
||||
(expression
|
||||
(value
|
||||
(integer)))
|
||||
(expression
|
||||
(value
|
||||
(integer))))))
|
||||
(expression
|
||||
(value
|
||||
(integer)))
|
||||
(expression
|
||||
(value
|
||||
(integer))))))))
|
||||
|
@ -43,21 +43,3 @@ x = {
|
||||
(expression
|
||||
(value
|
||||
(integer)))))))))))
|
||||
|
||||
==================
|
||||
Map Access
|
||||
==================
|
||||
|
||||
x.{answer}
|
||||
|
||||
---
|
||||
|
||||
(root
|
||||
(item
|
||||
(statement
|
||||
(expression
|
||||
(index
|
||||
(expression
|
||||
(identifier))
|
||||
(expression
|
||||
(identifier)))))))
|
@ -91,14 +91,12 @@ module.exports = grammar({
|
||||
|
||||
index: $ => prec.left(seq(
|
||||
$.expression,
|
||||
'.',
|
||||
'{',
|
||||
':',
|
||||
$.expression,
|
||||
optional(seq(
|
||||
'..',
|
||||
$.expression,
|
||||
)),
|
||||
'}'
|
||||
)),
|
||||
|
||||
function: $ => seq(
|
||||
@ -109,13 +107,13 @@ module.exports = grammar({
|
||||
'}',
|
||||
),
|
||||
|
||||
table: $ => prec.right(seq(
|
||||
table: $ => prec.left(seq(
|
||||
'table',
|
||||
seq('<', repeat1(seq($.identifier, optional(','))), '>'),
|
||||
$.expression,
|
||||
)),
|
||||
|
||||
math: $ => prec.left(1, seq(
|
||||
math: $ => prec.left(seq(
|
||||
$.expression,
|
||||
$.math_operator,
|
||||
$.expression,
|
||||
@ -129,7 +127,7 @@ module.exports = grammar({
|
||||
'%',
|
||||
),
|
||||
|
||||
logic: $ => prec.right(1, seq(
|
||||
logic: $ => prec.right(seq(
|
||||
$.expression,
|
||||
$.logic_operator,
|
||||
$.expression,
|
||||
@ -161,7 +159,7 @@ module.exports = grammar({
|
||||
if_else: $ => prec.left(seq(
|
||||
$.if,
|
||||
repeat(seq($.else_if)),
|
||||
optional(seq($.else)),
|
||||
optional($.else),
|
||||
)),
|
||||
|
||||
if: $ => seq(
|
||||
@ -203,7 +201,7 @@ module.exports = grammar({
|
||||
),
|
||||
|
||||
for: $ => seq(
|
||||
choice('for', 'async for'),
|
||||
'for',
|
||||
$.identifier,
|
||||
'in',
|
||||
$.expression,
|
||||
|
@ -407,11 +407,7 @@
|
||||
},
|
||||
{
|
||||
"type": "STRING",
|
||||
"value": "."
|
||||
},
|
||||
{
|
||||
"type": "STRING",
|
||||
"value": "{"
|
||||
"value": ":"
|
||||
},
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
@ -437,10 +433,6 @@
|
||||
"type": "BLANK"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"type": "STRING",
|
||||
"value": "}"
|
||||
}
|
||||
]
|
||||
}
|
||||
@ -512,7 +504,7 @@
|
||||
]
|
||||
},
|
||||
"table": {
|
||||
"type": "PREC_RIGHT",
|
||||
"type": "PREC_LEFT",
|
||||
"value": 0,
|
||||
"content": {
|
||||
"type": "SEQ",
|
||||
@ -567,7 +559,7 @@
|
||||
},
|
||||
"math": {
|
||||
"type": "PREC_LEFT",
|
||||
"value": 1,
|
||||
"value": 0,
|
||||
"content": {
|
||||
"type": "SEQ",
|
||||
"members": [
|
||||
@ -613,7 +605,7 @@
|
||||
},
|
||||
"logic": {
|
||||
"type": "PREC_RIGHT",
|
||||
"value": 1,
|
||||
"value": 0,
|
||||
"content": {
|
||||
"type": "SEQ",
|
||||
"members": [
|
||||
@ -733,13 +725,8 @@
|
||||
"type": "CHOICE",
|
||||
"members": [
|
||||
{
|
||||
"type": "SEQ",
|
||||
"members": [
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "else"
|
||||
}
|
||||
]
|
||||
"type": "SYMBOL",
|
||||
"name": "else"
|
||||
},
|
||||
{
|
||||
"type": "BLANK"
|
||||
@ -894,17 +881,8 @@
|
||||
"type": "SEQ",
|
||||
"members": [
|
||||
{
|
||||
"type": "CHOICE",
|
||||
"members": [
|
||||
{
|
||||
"type": "STRING",
|
||||
"value": "for"
|
||||
},
|
||||
{
|
||||
"type": "STRING",
|
||||
"value": "async for"
|
||||
}
|
||||
]
|
||||
"type": "STRING",
|
||||
"value": "for"
|
||||
},
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
|
@ -714,6 +714,10 @@
|
||||
"type": "/",
|
||||
"named": false
|
||||
},
|
||||
{
|
||||
"type": ":",
|
||||
"named": false
|
||||
},
|
||||
{
|
||||
"type": "<",
|
||||
"named": false
|
||||
@ -762,10 +766,6 @@
|
||||
"type": "async",
|
||||
"named": false
|
||||
},
|
||||
{
|
||||
"type": "async for",
|
||||
"named": false
|
||||
},
|
||||
{
|
||||
"type": "bash",
|
||||
"named": false
|
||||
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user