diff --git a/README.md b/README.md index a715320..b5c2283 100644 --- a/README.md +++ b/README.md @@ -72,11 +72,11 @@ end Candran is released under the MIT License (see ```LICENSE``` for details). #### Quick setup -Install Candran automatically using LuaRocks: ```sudo luarocks install rockspec/candran-0.11.0-1.rockspec```. +Install Candran automatically using LuaRocks: ```sudo luarocks install rockspec/candran-0.12.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```. -You can optionally install lua-linenoise (```luarocks install linenoise```, version 0.9 or above) for an improved REPL. The rockspec will install linenoise by default. +You can optionally install lua-linenoise (```luarocks install linenoise```, version 0.9 or above) for an improved REPL, and luacheck (```luarocks install luacheck```, version 0.23.0 or above) to be able to use ```cancheck```. Installing Candran using LuaRocks will install linenoise and luacheck by default. You can register the Candran package searcher in your main Lua file (`require("candran").setup()`) and any subsequent `require` call in your project will automatically search for Candran modules. @@ -87,6 +87,8 @@ Most editors should be able to use their existing Lua support for Candran code. * [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 +For linting, if your editor support [luacheck](https://github.com/luarocks/luacheck), you should be able to replace it with ```cancheck``` (in this repository ```bin/cancheck```, or installed automatically if Candran was installed using LuaRocks), which is a wrapper around luacheck that monkey-patch it to support Candran. + The language ------------ ### Syntax additions @@ -495,7 +497,7 @@ The library can be used standalone through the ```canc``` and ```can``` utility: Start a simplisitic Candran REPL. - If you want a better REPL (autocompletion, history, ability to move the cursor), install lua-linenoise: ```luarocks install linenoise```. + If you want a better REPL (autocompletion, history, ability to move the cursor), install lua-linenoise: ```luarocks install linenoise``` (automatically installed if Candran was installed using LuaRocks). * ````can [options] filename```` @@ -509,6 +511,12 @@ The library can be used standalone through the ```canc``` and ```can``` utility: Use the ```-h``` or ```-help``` option to display a short help text. +* ```cancheck``` + + Provides a linter and static analyzer with the exact same interface as [luacheck](https://github.com/luarocks/luacheck). + + This requires luacheck: ```luarocks install luacheck``` (automatically installed if Candran was installed through LuaRocks). + ### Library usage Candran can also be used as a Lua library: ````lua @@ -534,9 +542,9 @@ The table returned by _require("candran")_ gives you access to: ##### Compiler & preprocessor * ````candran.VERSION````: Candran's version string (e.g. `"0.10.0"`). -* ````candran.preprocess(code[, options])````: return the Candran code _code_, preprocessed with the _options_ options table. -* ````candran.compile(code[, options])````: return the Candran code compiled to Lua with the _options_ option table. -* ````candran.make(code[, options])````: return the Candran code, preprocessed and compiled with the _options_ options table. +* ````candran.preprocess(code[, options])````: return the Candran code _code_, preprocessed with the _options_ options table; or nil, err in case of error. +* ````candran.compile(code[, options])````: return the Candran code compiled to Lua with the _options_ option table; or nil, err in case of error. +* ````candran.make(code[, options])````: return the Candran code, preprocessed and compiled with the _options_ options table; or nil, err in case of error. ##### Code loading helpers * ```candran.loadfile(filepath, env, options)```: Candran equivalent to the Lua 5.3's loadfile funtion. Will rewrite errors by default. diff --git a/bin/cancheck b/bin/cancheck index 5354d40..27315a1 100644 --- a/bin/cancheck +++ b/bin/cancheck @@ -1,17 +1,60 @@ #!/usr/bin/env lua +--[[ +Based on luacheck: https://github.com/luarocks/luacheck + +The MIT License (MIT) + +Copyright (c) 2014 - 2018 Peter Melnichenko + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +]] + -- Monkey patch Luacheck (tested against version 0.23.0) to support Candran files local candran = require("candran") local util = require("candran.util") +-- set a function upvalues (if several names are given, will go up the upvalue chain) +local function setupvalue(fn, val, name, ...) + for i=1, debug.getinfo(fn, "u").nups do + local n, v = debug.getupvalue(fn, i) + if n == name then + if not ... then + debug.setupvalue(fn, i, val) + else + setupvalue(v, val, ...) + end + end + end +end + +-- escape a string to be used as a pattern local function escape(str) return str:gsub("[^%w]", "%%%0") end +-- returns a pattern that find start and stop position of a token local function pattern(token) return "()"..escape(token).."()" end +-- token aliases local tokenAlias = { ["self"] = { "@", ":" } } @@ -135,18 +178,6 @@ local function format_location(file, location, opts) end return res end -local function setupvalue(fn, val, name, ...) - for i=1, debug.getinfo(fn, "u").nups do - local n, v = debug.getupvalue(fn, i) - if n == name then - if not ... then - debug.setupvalue(fn, i, val) - else - setupvalue(v, val, ...) - end - end - end -end setupvalue(format.builtin_formatters.plain, format_location, "format_event", "format_location") -- Fix some Luacheck messages and run diff --git a/rockspec/candran-0.11.0-1.rockspec b/rockspec/candran-0.12.0-1.rockspec similarity index 87% rename from rockspec/candran-0.11.0-1.rockspec rename to rockspec/candran-0.12.0-1.rockspec index fb04283..6e2588d 100644 --- a/rockspec/candran-0.11.0-1.rockspec +++ b/rockspec/candran-0.12.0-1.rockspec @@ -2,7 +2,7 @@ rockspec_format = "3.0" package = "candran" -version = "0.11.0-1" +version = "0.12.0-1" description = { summary = "A simple Lua dialect and preprocessor.", @@ -19,13 +19,14 @@ description = { source = { url = "git://github.com/Reuh/candran", - tag = "v0.11.0" + tag = "v0.12.0" } dependencies = { "lua >= 5.1", "lpeglabel >= 1.5.0", - "linenoise >= 0.9" + "linenoise >= 0.9", + "luacheck >= 0.23.0" } build = { @@ -34,6 +35,6 @@ build = { candran = "candran.lua" }, install = { - bin = { "bin/can", "bin/canc" } + bin = { "bin/can", "bin/canc", "bin/cancheck" } } }