1
0
Fork 0
mirror of https://github.com/Reuh/candran.git synced 2025-10-27 17:59:30 +00:00

Candran 0.8.0

This commit is contained in:
Étienne Fildadut 2019-08-11 17:18:43 +02:00
parent ea19956f45
commit ff2f3a8feb
5 changed files with 60 additions and 79 deletions

106
README.md
View file

@ -20,7 +20,7 @@ end
let a = { let a = {
hey = true, hey = true,
newHop = :(foo, thing) -- short function declaration, with self method = :(foo, thing) -- short function declaration, with self
@hey = thing(foo) -- @ as an alias for self @hey = thing(foo) -- @ as an alias for self
end, end,
@ -29,11 +29,11 @@ let a = {
end end
} }
a:newHop(42, (foo) a:method(42, (foo)
return "something " .. foo return "something " .. foo
end) end)
local list = [ -- table comprehension (kind of) local odd = [ -- table comprehension (kind of)
for i=1, 10 do for i=1, 10 do
if i%2 == 0 then if i%2 == 0 then
continue -- continue keyword continue -- continue keyword
@ -53,7 +53,7 @@ print("Hello %s":format("world")) -- methods calls on strings (and tables) litte
Candran is released under the MIT License (see ```LICENSE``` for details). Candran is released under the MIT License (see ```LICENSE``` for details).
#### Quick setup #### Quick setup
Install Candran automatically using LuaRocks: ```sudo luarocks install rockspec/candran-0.7.0-1.rockspec```. Install Candran automatically using LuaRocks: ```sudo luarocks install rockspec/candran-0.8.0-1.rockspec```.
Or manually install LPegLabel (```luarocks install LPegLabel```, version 1.5 or above), download this repository and use Candran through the scripts in ```bin/``` or use it as a library with the self-contained ```candran.lua```. Or manually install LPegLabel (```luarocks install LPegLabel```, version 1.5 or above), download this repository and use Candran through the scripts in ```bin/``` or use it as a library with the self-contained ```candran.lua```.
@ -61,8 +61,10 @@ You can register the Candran package searcher in your main Lua file (`require("c
#### Editor support #### Editor support
Most editors should be able to use their existing Lua support for Candran code. If you want full support for the additional syntax in your editor: Most editors should be able to use their existing Lua support for Candran code. If you want full support for the additional syntax in your editor:
* **Sublime Text 3**:
* [sublime-candran](https://github.com/Reuh/sublime-candran) support the full Candran syntax
* [SublimeLinter-candran-contrib](https://github.com/Reuh/SublimeLinter-contrib-candran) SublimeLinter plugin for Candran
* **Atom**: [language-candran](https://atom.io/packages/language-candran) support the full Candran syntax * **Atom**: [language-candran](https://atom.io/packages/language-candran) support the full Candran syntax
* **Sublime Text 3**: [sublime-candran](https://github.com/Reuh/sublime-candran) support the full Candran syntax
The language The language
------------ ------------
@ -105,6 +107,20 @@ It is equivalent to doing ```if arg == nil then arg = default end``` for each ar
The default values can be complete Lua expressions, and will be evaluated each time the function is run. The default values can be complete Lua expressions, and will be evaluated each time the function is run.
##### Short anonymous function declaration
```lua
a = (arg1, arg2)
print(arg1)
end
b = :(hop)
print(self, hop)
end
```
Anonymous function (functions values) can be created in a more concise way by omitting the ```function``` keyword.
A ```:``` can prefix the parameters paranthesis to automatically add a ```self``` parameter.
##### `@` self aliases ##### `@` self aliases
```lua ```lua
a = { a = {
@ -121,20 +137,6 @@ When a variable name is prefied with ```@```, the name will be accessed in ```se
When used by itself, ```@``` is an alias for ```self```. When used by itself, ```@``` is an alias for ```self```.
##### Short anonymous function declaration
```lua
a = (arg1, arg2)
print(arg1)
end
b = :(hop)
print(self, hop)
end
```
Anonymous function (functions values) can be created in a more concise way by omitting the ```function``` keyword.
A ```:``` can prefix the parameters paranthesis to automatically add a ```self``` parameter.
##### `let` variable declaration ##### `let` variable declaration
```lua ```lua
let a = { let a = {
@ -197,21 +199,6 @@ Any list of expressions placed *at the end of a block* will be converted into a
**Please note** that this doesn't work with `v()` function calls, because these are already valid statements. Use `push v()` instead. **Please note** that this doesn't work with `v()` function calls, because these are already valid statements. Use `push v()` instead.
##### Statement expressions
```lua
a = if false then
"foo" -- i.e. push "foo", i.e. return "foo"
else
"bar"
end
print(a) -- bar
a, b, c = for i=1,2 do i end
print(a, b, c) -- 1, 2, nil
```
Candran allows to use `if`, `do`, `while`, `repeat` and `for` statements as expressions. Their content will be run as if they were run in a separate function which is immediatly run.
##### Table comprehension ##### Table comprehension
```lua ```lua
a = [ a = [
@ -239,22 +226,6 @@ Values returned by the function will be inserted in the generated table in the o
The table generation function also have access to the `self` (or its alias `@`) variable, which is the table which is being created, so you can set arbitrary fields of the table. The table generation function also have access to the `self` (or its alias `@`) variable, which is the table which is being created, so you can set arbitrary fields of the table.
##### One line statements
```lua
if condition()
a()
elseif foo()
b()
if other()
a()
else -- "end" is always needed for else!
c()
end
```
`if`, `elseif`, `for`, and `while` statements can be writtent without `do`, `then` or `end`, in which case they contain a single statement.
##### Suffixable string and table litterals ##### Suffixable string and table litterals
```lua ```lua
"some text":upper() -- same as ("some text"):upper() in Lua "some text":upper() -- same as ("some text"):upper() in Lua
@ -271,6 +242,37 @@ String litterals, table litterals, and comprehensions can be suffixed with `:` m
**Please note**, that "normal" functions calls have priority over this syntax, in order to maintain Lua compatibility. **Please note**, that "normal" functions calls have priority over this syntax, in order to maintain Lua compatibility.
##### Statement expressions
```lua
a = if false then
"foo" -- i.e. push "foo", i.e. return "foo"
else
"bar"
end
print(a) -- bar
a, b, c = for i=1,2 do i end
print(a, b, c) -- 1, 2, nil
```
Candran allows to use `if`, `do`, `while`, `repeat` and `for` statements as expressions. Their content will be run as if they were run in a separate function which is immediatly run.
##### One line statements
```lua
if condition()
a()
elseif foo()
b()
if other()
a()
else -- "end" is always needed for else!
c()
end
```
`if`, `elseif`, `for`, and `while` statements can be writtent without `do`, `then` or `end`, in which case they contain a single statement.
### Preprocessor ### Preprocessor
Before compiling, Candran's preprocessor is run. It execute every line starting with a _#_ (ignoring prefixing whitespace, long strings and comments) as Candran code. Before compiling, Candran's preprocessor is run. It execute every line starting with a _#_ (ignoring prefixing whitespace, long strings and comments) as Candran code.
For example, For example,
@ -437,7 +439,7 @@ at the top of your main Lua file. If a Candran file is found when you call ```re
You can give arbitrary options which will be gived to the preprocessor, but Candran already provide and uses these with their associated default values: You can give arbitrary options which will be gived to the preprocessor, but Candran already provide and uses these with their associated default values:
```lua ```lua
target = "lua53" -- Compiler target. "lua53" or "luajit". target = "lua53" -- Compiler target. "lua53" or "luajit" (default is automatically selected based on the Lua version used).
indentation = "" -- Character(s) used for indentation in the compiled file. indentation = "" -- Character(s) used for indentation in the compiled file.
newline = "\n" -- Character(s) used for newlines in the compiled file. newline = "\n" -- Character(s) used for newlines in the compiled file.
variablePrefix = "__CAN_" -- Prefix used when Candran needs to set a local variable to provide some functionality (example: to load LuaJIT's bit lib when using bitwise operators). variablePrefix = "__CAN_" -- Prefix used when Candran needs to set a local variable to provide some functionality (example: to load LuaJIT's bit lib when using bitwise operators).
@ -464,6 +466,6 @@ canc candran.can
You can then run the tests on your build : You can then run the tests on your build :
```` ````
cd tests cd test
lua test.lua ../candran.lua lua test.lua ../candran.lua
```` ````

View file

@ -10,7 +10,7 @@
#import("lib.lua-parser.parser") #import("lib.lua-parser.parser")
local candran = { local candran = {
VERSION = "0.7.0-dev" VERSION = "0.8.0"
} }
--- Default options. --- Default options.

View file

@ -2931,7 +2931,7 @@ return parser -- ./lib/lua-parser/parser.lua:685
end -- ./lib/lua-parser/parser.lua:685 end -- ./lib/lua-parser/parser.lua:685
local parser = _() or parser -- ./lib/lua-parser/parser.lua:689 local parser = _() or parser -- ./lib/lua-parser/parser.lua:689
package["loaded"]["lib.lua-parser.parser"] = parser or true -- ./lib/lua-parser/parser.lua:690 package["loaded"]["lib.lua-parser.parser"] = parser or true -- ./lib/lua-parser/parser.lua:690
local candran = { ["VERSION"] = "0.7.0-dev" } -- candran.can:13 local candran = { ["VERSION"] = "0.8.0" } -- candran.can:13
candran["default"] = { -- candran.can:17 candran["default"] = { -- candran.can:17
["target"] = _VERSION == "Lua 5.1" and "luajit" or "lua53", -- candran.can:18 ["target"] = _VERSION == "Lua 5.1" and "luajit" or "lua53", -- candran.can:18
["indentation"] = "", -- candran.can:19 ["indentation"] = "", -- candran.can:19

View file

@ -1,6 +1,6 @@
package = "candran" package = "candran"
version = "0.7.0-1" version = "0.8.0-1"
description = { description = {
summary = "A simple Lua dialect and preprocessor.", summary = "A simple Lua dialect and preprocessor.",
@ -10,14 +10,14 @@ description = {
]], ]],
license = "MIT", license = "MIT",
homepage = "https://github.com/Reuh/candran", homepage = "https://github.com/Reuh/candran",
--issues_url = "https://github.com/Reuh/candran", -- LuaRocks 3.0 issues_url = "https://github.com/Reuh/candran",
maintainer = "Étienne 'Reuh' Fildadut <fildadut@reuh.eu>", maintainer = "Étienne 'Reuh' Fildadut <fildadut@reuh.eu>",
--labels = {} -- LuaRocks 3.0 labels = {"lpeg", "commandline"}
} }
source = { source = {
url = "git://github.com/Reuh/candran", url = "git://github.com/Reuh/candran",
tag = "v0.7.0" tag = "v0.8.0"
} }
dependencies = { dependencies = {
@ -33,5 +33,4 @@ build = {
install = { install = {
bin = { "bin/can", "bin/canc" } bin = { "bin/can", "bin/canc" }
} }
--copy_directories = { "doc", "test" }
} }

View file

@ -544,14 +544,6 @@ local s = function() return {len=4} end
local a = s"foo"["len"] local a = s"foo"["len"]
return a return a
]], 4) ]], 4)
test("suffixable string litteral call", [[
local r, e = pcall(function() "foo"() end)
return not r and e:match("attempt to call a string value")
]], "attempt to call a string value")
test("suffixable string litteral call lua conflict", [[
local s = function() return function() return 4 end end
return s"foo"()
]], 4)
test("suffixable table litteral method", [[ test("suffixable table litteral method", [[
return {a=3,len=function(t) return t.a end}:len() return {a=3,len=function(t) return t.a end}:len()
@ -574,14 +566,6 @@ test("suffixable table litteral dot index lua conflict", [[
local s = function() return {len=4} end local s = function() return {len=4} end
return s{len=3}["len"] return s{len=3}["len"]
]], 4) ]], 4)
test("suffixable table litteral call", [[
local r, e = pcall(function() {}() end)
return not r and e:match("attempt to call a table value")
]], "attempt to call a table value")
test("suffixable table litteral call lua conflict", [[
local s = function() return function() return 4 end end
return s{}()
]], 4)
test("suffixable table comprehension method", [[ test("suffixable table comprehension method", [[
return [@len = function() return 3 end]:len() return [@len = function() return 3 end]:len()
@ -592,10 +576,6 @@ return [@len = 3].len
test("suffixable table comprehension array index", [[ test("suffixable table comprehension array index", [[
return [@len=3]["len"] return [@len=3]["len"]
]], 3) ]], 3)
test("suffixable table comprehension call", [[
local r, e = pcall(function() []() end)
return not r and e:match("attempt to call a table value")
]], "attempt to call a table value")
-- results -- results
local resultCounter = {} local resultCounter = {}