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

Allow can and canc to read from stdin, check for errors, cleaner output

This commit is contained in:
Étienne Fildadut 2018-12-30 13:56:10 +01:00
parent ab7472152f
commit 09ac497aed
7 changed files with 92 additions and 62 deletions

View file

@ -1,4 +1,4 @@
Copyright (c) 2017 Étienne "Reuh" Fildadut
Copyright (c) 2017-2019 Étienne "Reuh" Fildadut
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:

View file

@ -322,12 +322,18 @@ The library can be used standalone through the ```canc``` and ```can``` utility:
You can choose to use another directory where files should be written using the ```dest=destinationDirectory``` argument.
You can choose the output filename using ```out=filename```. By default, compiled files have the same name as their input file, but with a ```.lua``` extension. _(not in latest release)_
```canc``` can write to the standard output instead of creating files using the ```-print``` argument.
You can choosed to run only the preprocessor or compile using the ```-preprocess``` and ```-compile``` flags.
You can choose to run only the preprocessor or compile using the ```-preprocess``` and ```-compile``` flags.
You can choose to only parse the file and check it for syntaxic errors using the ```-parse``` flag. Errors will be printed to stderr in a similar format to ```luac -p```. _(not in latest release)_
The ```-ast``` flag is also available for debugging, and will disable preprocessing, compiling and file writing, and instead directly dump the AST generated from the input file(s) to stdout.
Instead of providing filenames, you can use ```-``` to read from standard input. _(not in latest release)_
* example uses :
````canc foo.can````
@ -342,6 +348,10 @@ The library can be used standalone through the ```canc``` and ```can``` utility:
preprocess _foo.can_ with _verbose_ set to _true_, compile it and execute it.
````canc -parse foo.can````
checks foo.can for syntaxic errors. _(not in latest release)_
* ```can```
Start a simplisitic Candran REPL.
@ -354,6 +364,8 @@ The library can be used standalone through the ```canc``` and ```can``` utility:
This command will use error rewriting if enabled.
Instead of providing a filename, you can use ```-``` to read from standard input. _(not in latest release)_
### Library usage
Candran can also be used as a Lua library. For example,
````lua

19
bin/can
View file

@ -5,12 +5,25 @@ local cmdline = require("lib.cmdline")
local args = cmdline(arg)
if args.help or args.h then
print("Candran interpreter version "..candran.VERSION.." by Reuh")
print("Usage: "..arg[0].." [target=<target>] [options] filename")
print("Candran "..candran.VERSION.." interpreter by Reuh")
print("Usage: "..arg[0].." [options] filename")
print("Use - instead of a filename to read from the standard input.")
print("Default options:")
for opt, val in pairs(candran.default) do
if type(val) == "string" then val = val:gsub("\n", "\\n") end
print((" %s=%q"):format(opt, val))
end
return
end
if #args >= 1 then
if arg[#arg] == "-" then
local f, err = candran.load(io.read("*a"), "stdin", nil, args)
if not f then
io.stderr:write("can: "..err.."\n")
os.exit(1)
end
f()
elseif #args >= 1 then
candran.dofile(args[1], args)
else -- REPL
print("Candran " .. candran.VERSION)

View file

@ -5,33 +5,74 @@ local parse = require("lib.lua-parser.parser").parse
local pp = require("lib.lua-parser.pp")
if #arg < 1 then
print("Candran compiler version "..candran.VERSION.." by Reuh")
print("Usage: "..arg[0].." [target=<target>] [dest=<destination directory>] [-print] [-preprocess] [-compile] [-ast] [options] filename...")
print("Candran "..candran.VERSION.." compiler by Reuh")
print("Usage: "..arg[0].." [options] filenames...")
print("Use - instead of filenames to read from the standard input. The output file will be named stdin.lua by default.")
print("Compiler options:")
print(" dest=\"directory\" where compiled files should be written")
print(" out=\"name.lua\" output filename. By default, will use the same name as the input file with a .lua extension.")
print(" -print write to the standard output instead of creating files")
print(" -preprocess only run the preprocessor")
print(" -compile only run the compiler")
print(" -parse only parse the file and prints errors to stdout")
print(" -ast (for debugging purposes) only parse the files and dump the AST to stdout")
print("Default options:")
for opt, val in pairs(candran.default) do
if type(val) == "string" then val = val:gsub("\n", "\\n") end
print((" %s=%q"):format(opt, val))
end
return
end
local args = cmdline(arg)
if arg[#arg] == "-" then
table.insert(args, io.stdin)
end
for _, file in ipairs(args) do
local dest = file:gsub("%.can$", "")..".lua"
-- Read
local dest, input
if file == io.stdin then
dest = args.out or "stdin.lua"
input = io.read("*a")
args.chunkname = "stdin"
else
dest = args.out or (file:gsub("%.can$", "")..".lua")
local inputFile, err = io.open(file, "r")
if not inputFile then
io.stderr:write("canc: cannot open "..file..": "..err)
os.exit(1)
end
input = inputFile:read("*a")
inputFile:close()
args.chunkname = file
end
-- Parse-only situations
if args.parse or args.ast then
local ast, err = parse(input, args.chunkname)
if not ast then
io.stderr:write("canc: "..err.."\n")
os.exit(1)
end
if args.ast then
pp.dump(ast)
end
return
end
-- Compile and output
if args.dest then
dest = args.dest .. "/" .. dest
end
if not args.print then
print("Compiling "..file.." in "..dest)
end
local inputFile, err = io.open(file, "r")
if not inputFile then error("Error while opening input file: "..err) end
local input = inputFile:read("*a")
inputFile:close()
args.chunkname = file
if args.ast then
pp.dump(assert(parse(input, args.chunkname)))
return
print("Compiling "..args.chunkname.." in "..dest)
end
local out = input
@ -48,12 +89,13 @@ for _, file in ipairs(args) do
if args.print then
print(out)
else
local outFile = io.open(dest, "w")
local outFile, err = io.open(dest, "w")
if not outFile then
os.execute("mkdir -p "..dest:gsub("[^/]+%.lua$", ""))
outFile, err = io.open(dest, "w")
if not outFile then
error("Error while writing output file: "..err)
io.stderr:write("canc: cannot open "..dest..": "..err)
os.exit(1)
end
end
outFile:write(out)

View file

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

View file

@ -2931,7 +2931,7 @@ return parser -- ./lib/lua-parser/parser.lua:685
end -- ./lib/lua-parser/parser.lua:685
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
local candran = { ["VERSION"] = "0.7.0" } -- candran.can:13
local candran = { ["VERSION"] = "0.7.0-dev" } -- candran.can:13
candran["default"] = { -- candran.can:17
["target"] = "lua53", -- candran.can:18
["indentation"] = "", -- candran.can:19

View file

@ -1,37 +0,0 @@
package = "candran"
version = "0.6.2-1"
description = {
summary = "A simple Lua dialect and preprocessor.",
detailed = [[
Candran is a dialect of the Lua 5.3 programming language which compiles to Lua 5.3 and Lua 5.1/LuaJit. It adds several useful syntax additions which aims to make Lua faster and easier to write, and a simple preprocessor.
Unlike Moonscript, Candran tries to stay close to the Lua syntax, and existing Lua code can run on Candran unmodified.
]],
license = "MIT",
homepage = "https://github.com/Reuh/candran",
--issues_url = "https://github.com/Reuh/candran", -- LuaRocks 3.0
maintainer = "Étienne 'Reuh' Fildadut <fildadut@reuh.eu>",
--labels = {} -- LuaRocks 3.0
}
source = {
url = "git://github.com/Reuh/candran",
tag = "v0.6.2"
}
dependencies = {
"lua >= 5.1",
"lpeglabel >= 1.5.0"
}
build = {
type = "builtin",
modules = {
candran = "candran.lua"
},
install = {
bin = { "bin/can", "bin/canc" }
}
--copy_directories = { "doc", "test" }
}