Permalink
Browse files

allow string literal as table key

  • Loading branch information...
1 parent bc13b1b commit 43c1e0a4fbca3fb3d68ac538de0d472a04fe2053 @leafo committed Sep 9, 2012
Showing with 25 additions and 1 deletion.
  1. +13 −0 docs/reference.md
  2. +1 −1 moonscript/parse.lua
  3. +6 −0 tests/inputs/tables.moon
  4. +5 −0 tests/outputs/tables.lua
View
@@ -345,6 +345,19 @@ same as the variable names, then the `:` prefix operator can be used:
print_table :hair, :height
```
+If you want the key of a field in the table to to be result of an expression,
+then you can wrap it in `[` `]`, just like in Lua. You can also use a string
+literal directly as a key, leaving out the square brackets. This is useful if
+your key has any special chacters.
+
+ ```moon
+ t = {
+ [1 + 2]: "hello"
+ "hello world": true
+ }
+
+ ```
+
## Comprehensions
Compiling provide a convenient syntax for constructing a new table by iterating
View
@@ -457,7 +457,7 @@ local build_grammar = wrap_env(function()
op"*" + op"^" +
Ct(NameList) * (sym"=" * Ct(ExpListLow))^-1) / mark"export",
- KeyValue = (sym":" * Name) / self_assign + Ct((KeyName + sym"[" * Exp * sym"]") * symx":" * (Exp + TableBlock)),
+ KeyValue = (sym":" * Name) / self_assign + Ct((KeyName + sym"[" * Exp * sym"]" + DoubleString + SingleString) * symx":" * (Exp + TableBlock)),
KeyValueList = KeyValue * (sym"," * KeyValue)^0,
KeyValueLine = CheckIndent * KeyValueList * sym","^-1,
View
@@ -95,4 +95,10 @@ t = {
[b]: 'b'
}
+xam = {
+ hello: 1234
+ "hello": 12354
+ ["hello"]: 12354
+}
+
View
@@ -120,4 +120,9 @@ call_me("hello", {
local t = {
a = 'a',
[b] = 'b'
+}
+local xam = {
+ hello = 1234,
+ ["hello"] = 12354,
+ ["hello"] = 12354
}

0 comments on commit 43c1e0a

Please sign in to comment.