Conflicts:
	bin/moonc
This commit is contained in:
AlissaSquared 2016-01-07 17:40:51 -06:00
commit 6580353537
19 changed files with 105 additions and 289 deletions

View File

@ -1,15 +1,36 @@
.PHONY: test local compile compile_system watch lint count LUA ?= lua5.1
LUAROCKS ?= luarocks
ifneq ($(LUA),lua)
LUA_VERSION = $(shell echo $(LUA) | sed -e "s/lua\(.*\)/\1/")
LUAROCKS = luarocks-$(LUA_VERSION)
LUA_PATH_MAKE = $(shell echo "$$LUA_PATH" | sed -e "s/[0-9]\.[0-9]/$(LUA_VERSION)/g")
LUA_CPATH_MAKE = $(shell echo "$$LUA_CPATH" | sed -e "s/[0-9]\.[0-9]/$(LUA_VERSION)/g")
endif
ifeq ($(LUA),luajit)
LUAROCKS = luarocks-5.1
endif
.PHONY: test local compile compile_system watch lint count show
test: test:
busted busted
show:
# LUA $(LUA)
# LUA_VERSION $(LUA_VERSION)
# LUAROCKS $(LUAROCKS)
# LUA_PATH_MAKE $(LUA_PATH_MAKE)
# LUA_CPATH_MAKE $(LUA_CPATH_MAKE)
local: compile local: compile
luarocks make --local moonscript-dev-1.rockspec LUA_PATH='$(LUA_PATH_MAKE)' LUA_CPATH='$(LUA_CPATH_MAKE)' $(LUAROCKS) make --local moonscript-dev-1.rockspec
compile: compile:
lua5.1 bin/moonc moon/ moonscript/ LUA_PATH='$(LUA_PATH_MAKE)' LUA_CPATH='$(LUA_CPATH_MAKE)' $(LUA) bin/moonc moon/ moonscript/
echo "#!/usr/bin/env lua" > bin/moon echo "#!/usr/bin/env lua" > bin/moon
lua5.1 bin/moonc -p bin/moon.moon >> bin/moon $(LUA) bin/moonc -p bin/moon.moon >> bin/moon
echo "-- vim: set filetype=lua:" >> bin/moon echo "-- vim: set filetype=lua:" >> bin/moon
compile_system: compile_system:

View File

@ -27,6 +27,13 @@ busted
Writing specs is a bit more complicated. Check out [the spec writing guide](spec/README.md). Writing specs is a bit more complicated. Check out [the spec writing guide](spec/README.md).
## Editor Support
* [Vim](https://github.com/leafo/moonscript-vim)
* [Textadept](https://github.com/leafo/moonscript-textadept)
* [Sublime/Textmate](https://github.com/leafo/moonscript-tmbundle)
## License (MIT) ## License (MIT)
Copyright (C) 2015 by Leaf Corcoran Copyright (C) 2015 by Leaf Corcoran

View File

@ -264,16 +264,17 @@ local function create_watcher(files)
end end
end end
-- build function to check for lint or compile in watch
local check_compile_or_lint
if opts.l then
local lint = require "moonscript.cmd.lint"
check_compile_or_lint = lint.lint_file
else
check_compile_or_lint = compile_and_write
end
if opts.w then if opts.w then
-- build function to check for lint or compile in watch
local handle_file
if opts.l then
local lint = require "moonscript.cmd.lint"
handle_file = lint.lint_file
else
handle_file = compile_and_write
end
local watcher = create_watcher(files) local watcher = create_watcher(files)
-- catches interrupt error for ctl-c -- catches interrupt error for ctl-c
local protected = function() local protected = function()
@ -292,7 +293,7 @@ if opts.w then
target = opts.o target = opts.o
end end
local success, err = check_compile_or_lint(fname, target) local success, err = handle_file(fname, target)
if opts.l then if opts.l then
if success then if success then
io.stderr:write(success .. "\n\n") io.stderr:write(success .. "\n\n")
@ -314,9 +315,10 @@ if opts.w then
io.stderr:write("\nQuitting...\n") io.stderr:write("\nQuitting...\n")
elseif opts.l then elseif opts.l then
local has_linted_with_error; local has_linted_with_error;
local lint = require "moonscript.cmd.lint"
for _, tuple in pairs(files) do for _, tuple in pairs(files) do
local fname = tuple[1] local fname = tuple[1]
local res, err = check_compile_or_lint(fname) local res, err = lint.lint_file(fname)
if res then if res then
has_linted_with_error = true has_linted_with_error = true
io.stderr:write(res .. "\n\n") io.stderr:write(res .. "\n\n")

View File

@ -1,8 +1,9 @@
target: reference/api {
template: reference target: "reference/api"
title: Compiler API template: "reference"
short_name: api title: "Compiler API"
-- short_name: "api"
}
# MoonScript Compiler API # MoonScript Compiler API

View File

@ -1,8 +1,9 @@
target: reference/command_line {
template: reference target: "reference/command_line"
title: Command Line Tools template: "reference"
short_name: command_line title: "Command Line Tools"
-- short_name: "command_line"
}
# Command Line Tools # Command Line Tools

View File

@ -1,8 +1,9 @@
target: reference/index {
template: reference target: "reference/index"
title: Language Guide template: "reference"
short_name: lang title: "Language Guide"
-- short_name: "lang"
}
MoonScript is a programming language that compiles to MoonScript is a programming language that compiles to
[Lua](http://www.lua.org). This guide expects the reader to have basic [Lua](http://www.lua.org). This guide expects the reader to have basic

View File

@ -1,8 +1,9 @@
target: reference/standard_lib {
template: reference target: "reference/standard_lib"
title: Standard Library template: "reference"
short_name: stdlib title: "Standard Library"
-- short_name: "stdlib"
}
The MoonScript installation comes with a small kernel of functions that can be The MoonScript installation comes with a small kernel of functions that can be
used to perform various common things. used to perform various common things.

View File

@ -1,36 +0,0 @@
# MoonScript for [scintillua][1]
MoonScript syntax file for [SciTE][2] written in Lua for [scintillua][1].
## Windows Binary
Windows users can get a all-included package ready for MoonScript Development:
<http://moonscript.org/scite/>
If you already have a ScITE installation, or are on another platform, follow
the directions below.
## Installation
Install SciTE, then [install scintillua][1].
Put `moonscript.properties` in in your ScITE installation folder or user
properties folder.
Copy the entire contents of the `lexers` folder in this repository into your
scintillua `lexers` folder.
In your `lexers` folder edit `lpeg.properties`, add to the end:
file.patterns.moonscript=*.moon
lexer.$(file.patterns.moonscript)=lpeg_moonscript
Optionally, enable the Moon theme, find `lexer.peg.color.theme` in the same
file and change it to:
lexer.lpeg.color.theme=moon
[1]: http://foicica.com/scintillua/ "scintillua"
[2]: http://www.scintilla.org/SciTE.html "SciTE"

View File

@ -1,124 +0,0 @@
-- Copyright 2006-2011 Mitchell mitchell<att>caladbolg.net. See LICENSE.
-- Moonscript lexer by leaf corcoran <http://leafo.net>
local l = lexer
local token, word_match = l.token, l.word_match
local P, S, R = lpeg.P, lpeg.S, lpeg.R
local M = { _NAME = 'moonscript' }
-- Whitespace.
local ws = token(l.WHITESPACE, l.space^1)
local longstring = #('[[' + ('[' * P('=')^0 * '['))
local longstring = longstring * P(function(input, index)
local level = input:match('^%[(=*)%[', index)
if level then
local _, stop = input:find(']'..level..']', index, true)
return stop and stop + 1 or #input + 1
end
end)
-- Comments.
local line_comment = '--' * l.nonnewline^0
local block_comment = '--' * longstring
local comment = token(l.COMMENT, block_comment + line_comment)
-- Strings.
local sq_str = l.delimited_range("'", '\\', true)
local dq_str = l.delimited_range('"', '\\', true)
local string = token(l.STRING, sq_str + dq_str + longstring)
-- Numbers.
local number = token(l.NUMBER, l.float + l.integer)
-- Keywords.
local keyword = token(l.KEYWORD, word_match {
'return', 'break', 'for', 'while',
'if', 'else', 'elseif', 'then', 'export',
'import', 'from', 'with', 'in', 'and',
'or', 'not', 'class', 'extends', 'super', 'do',
'using', 'switch', 'when',
})
local special = token("special", word_match { "true", "false", "nil" })
-- Functions.
local builtin = token(l.FUNCTION, word_match({
"_G","_VERSION","assert","collectgarbage","dofile","error","getfenv","getmetatable","ipairs","load",
"loadfile","loadstring","module","next","pairs","pcall","print","rawequal","rawget","rawset","require",
"select","setfenv","setmetatable","tonumber","tostring","type","unpack","xpcall",
"coroutine.create","coroutine.resume","coroutine.running","coroutine.status","coroutine.wrap","coroutine.yield",
"debug.debug","debug.getfenv","debug.gethook","debug.getinfo","debug.getlocal","debug.getmetatable",
"debug.getregistry","debug.getupvalue","debug.setfenv","debug.sethook","debug.setlocal","debug.setmetatable",
"debug.setupvalue","debug.traceback",
"io.close","io.flush","io.input","io.lines","io.open","io.output","io.popen","io.read","io.stderr","io.stdin",
"io.stdout","io.tmpfile","io.type","io.write",
"math.abs","math.acos","math.asin","math.atan","math.atan2","math.ceil","math.cos","math.cosh","math.deg",
"math.exp","math.floor","math.fmod","math.frexp","math.huge","math.ldexp","math.log","math.log10","math.max",
"math.min","math.modf","math.pi","math.pow","math.rad","math.random","math.randomseed","math.sin","math.sinh",
"math.sqrt","math.tan","math.tanh",
"os.clock","os.date","os.difftime","os.execute","os.exit","os.getenv","os.remove","os.rename","os.setlocale",
"os.time","os.tmpname",
"package.cpath","package.loaded","package.loaders","package.loadlib","package.path","package.preload",
"package.seeall",
"string.byte","string.char","string.dump","string.find","string.format","string.gmatch","string.gsub",
"string.len","string.lower","string.match","string.rep","string.reverse","string.sub","string.upper",
"table.concat","table.insert","table.maxn","table.remove","table.sort"
}, "%."))
-- Identifiers.
local identifier = token(l.IDENTIFIER, l.word)
local fndef = token("fndef", P"->" + P"=>")
local err = token(l.ERROR, word_match { "function", "end" })
-- Operators.
local symbol = token("symbol", S("(){}[]"))
local operator = token(l.OPERATOR, '~=' + S('+-*!\\/%^#=<>;:,.'))
-- self ref
local self_var = token("self_ref", "@" * l.word + "self")
local proper_ident = token("proper_ident", R("AZ") * l.word)
local tbl_key = token("tbl_key", l.word * ":" + ":" * l.word )
M._rules = {
{ 'whitespace', ws },
{ 'error', err },
{ 'self', self_var },
{ 'special', special },
{ 'keyword', keyword },
{ 'builtin', builtin },
{ 'identifier', proper_ident + tbl_key + identifier },
{ 'comment', comment },
{ 'number', number },
{ 'string', string },
{ 'fndef', fndef },
{ 'symbol', symbol },
{ 'operator', operator },
{ 'any_char', l.any_char },
}
local style_special = { fore = l.colors.light_blue }
local style_fndef = { fore = l.colors.green }
M._tokenstyles = {
{ 'self_ref', style_special },
{ 'proper_ident', l.style_class },
{ 'fndef', style_fndef },
{ 'symbol', style_fndef },
{ 'special', style_special },
{ 'tbl_key', { fore = l.colors.red } },
}
return M

View File

@ -1,61 +0,0 @@
-- Copyright 2006-2011 Mitchell mitchell<att>caladbolg.net. See LICENSE.
-- moon lexer theme for Scintillua.
module('lexer', package.seeall)
colors = {
green = color('9F', 'FF', '98'), --
blue = color('94', '95', 'FF'), --
light_blue = color('98', 'D9', 'FF'), --
red = color('FF', '98', '98'), --
bright_red = color("F9", "26", "32"), --
yellow = color('FF', 'E8', '98'), --
teal = color('4D', '99', '99'),
white = color('FF', 'FF', 'FF'), --
black = color('2E', '2E', '2E'), --
grey = color('92', '92', '92'), --
purple = color('CB', '98', 'FF'), --
orange = color('FF', '92', '00'), --
pink = color("ED", "4E", "78"), --
}
style_nothing = style { }
style_char = style { fore = colors.red, bold = true }
style_class = style { fore = colors.light_blue, bold = true }
style_comment = style { fore = colors.grey, }
style_constant = style { fore = colors.teal, bold = true }
style_definition = style { fore = colors.red, bold = true }
style_error = style { fore = colors.white, back = colors.bright_red, bold = true}
style_function = style { fore = colors.orange, bold = true }
style_keyword = style { fore = colors.purple, bold = true }
style_number = style { fore = colors.blue }
style_operator = style { fore = colors.red, bold = true }
style_string = style { fore = colors.yellow, bold = true }
style_preproc = style { fore = colors.light_blue }
style_tag = style { fore = colors.teal, bold = true }
style_type = style { fore = colors.green }
style_variable = style { fore = colors.white, italic = true }
style_embedded = style_tag..{ back = color('44', '44', '44') }
style_identifier = style_nothing
-- Default styles.
local font_face = '!Bitstream Vera Sans Mono'
local font_size = 12
if WIN32 then
font_face = not GTK and 'Courier New' or '!Courier New'
elseif OSX then
font_face = '!Monaco'
font_size = 12
end
style_default = style{
font = font_face,
size = font_size,
fore = colors.white,
back = colors.black
}
style_line_number = style { fore = colors.black, back = colors.grey }
style_bracelight = style { fore = color('66', '99', 'FF'), bold = true }
style_bracebad = style { fore = color('FF', '66', '99'), bold = true }
style_controlchar = style_nothing
style_indentguide = style { fore = colors.grey, back = colors.white }
style_calltip = style { fore = colors.white, back = color('44', '44', '44') }

View File

@ -1,11 +0,0 @@
file.patterns.moon=*.moon
shbang.moon=moon
filter.moon=MoonScript (moon)|$(file.patterns.moon)|
command.compile.*.moon=moonc "$(FileNameExt)"
command.go.*.moon=moon "$(FileNameExt)"
tabsize=2
indent.size=2
use.tabs=0

View File

@ -102,7 +102,7 @@ compile_file_text = function(text, opts)
"Compile time\t" .. format_time(compile_time), "Compile time\t" .. format_time(compile_time),
"" ""
}, "\n")) }, "\n"))
return nil return true
end end
return code return code
end end

View File

@ -105,7 +105,7 @@ compile_file_text = (text, opts={}) ->
"Compile time\t" .. format_time(compile_time), "Compile time\t" .. format_time(compile_time),
"" ""
}, "\n" }, "\n"
return nil return true
code code

View File

@ -286,6 +286,9 @@ return {
number = function(self, node) number = function(self, node)
return node[2] return node[2]
end, end,
bitnot = function(self, node)
return self:line("~", self:value(node[2]))
end,
length = function(self, node) length = function(self, node)
return self:line("#", self:value(node[2])) return self:line("#", self:value(node[2]))
end, end,

View File

@ -173,6 +173,9 @@ string_chars = {
number: (node) => number: (node) =>
node[2] node[2]
bitnot: (node) =>
@line "~", @value node[2]
length: (node) => length: (node) =>
@line "#", @value node[2] @line "#", @value node[2]

View File

@ -15,10 +15,10 @@ local wrap_env
wrap_env = require("moonscript.parse.env").wrap_env wrap_env = require("moonscript.parse.env").wrap_env
local R, S, V, P, C, Ct, Cmt, Cg, Cb, Cc local R, S, V, P, C, Ct, Cmt, Cg, Cb, Cc
R, S, V, P, C, Ct, Cmt, Cg, Cb, Cc = lpeg.R, lpeg.S, lpeg.V, lpeg.P, lpeg.C, lpeg.Ct, lpeg.Cmt, lpeg.Cg, lpeg.Cb, lpeg.Cc R, S, V, P, C, Ct, Cmt, Cg, Cb, Cc = lpeg.R, lpeg.S, lpeg.V, lpeg.P, lpeg.C, lpeg.Ct, lpeg.Cmt, lpeg.Cg, lpeg.Cb, lpeg.Cc
local White, Break, Stop, Comment, Space, SomeSpace, SpaceBreak, EmptyLine, AlphaNum, Num, Shebang, _Name local White, Break, Stop, Comment, Space, SomeSpace, SpaceBreak, EmptyLine, AlphaNum, Num, Shebang, L, _Name
do do
local _obj_0 = require("moonscript.parse.literals") local _obj_0 = require("moonscript.parse.literals")
White, Break, Stop, Comment, Space, SomeSpace, SpaceBreak, EmptyLine, AlphaNum, Num, Shebang, _Name = _obj_0.White, _obj_0.Break, _obj_0.Stop, _obj_0.Comment, _obj_0.Space, _obj_0.SomeSpace, _obj_0.SpaceBreak, _obj_0.EmptyLine, _obj_0.AlphaNum, _obj_0.Num, _obj_0.Shebang, _obj_0.Name White, Break, Stop, Comment, Space, SomeSpace, SpaceBreak, EmptyLine, AlphaNum, Num, Shebang, L, _Name = _obj_0.White, _obj_0.Break, _obj_0.Stop, _obj_0.Comment, _obj_0.Space, _obj_0.SomeSpace, _obj_0.SpaceBreak, _obj_0.EmptyLine, _obj_0.AlphaNum, _obj_0.Num, _obj_0.Shebang, _obj_0.L, _obj_0.Name
end end
local SpaceName = Space * _Name local SpaceName = Space * _Name
Num = Space * (Num / function(v) Num = Space * (Num / function(v)
@ -110,10 +110,10 @@ local build_grammar = wrap_env(debug_grammar, function(root)
File = Shebang ^ -1 * (Block + Ct("")), File = Shebang ^ -1 * (Block + Ct("")),
Block = Ct(Line * (Break ^ 1 * Line) ^ 0), Block = Ct(Line * (Break ^ 1 * Line) ^ 0),
CheckIndent = Cmt(Indent, check_indent), CheckIndent = Cmt(Indent, check_indent),
Line = (CheckIndent * Statement + Space * #Stop), Line = (CheckIndent * Statement + Space * L(Stop)),
Statement = pos(Import + While + With + For + ForEach + Switch + Return + Local + Export + BreakLoop + Ct(ExpList) * (Update + Assign) ^ -1 / format_assign) * Space * ((key("if") * Exp * (key("else") * Exp) ^ -1 * Space / mark("if") + key("unless") * Exp / mark("unless") + CompInner / mark("comprehension")) * Space) ^ -1 / wrap_decorator, Statement = pos(Import + While + With + For + ForEach + Switch + Return + Local + Export + BreakLoop + Ct(ExpList) * (Update + Assign) ^ -1 / format_assign) * Space * ((key("if") * Exp * (key("else") * Exp) ^ -1 * Space / mark("if") + key("unless") * Exp / mark("unless") + CompInner / mark("comprehension")) * Space) ^ -1 / wrap_decorator,
Body = Space ^ -1 * Break * EmptyLine ^ 0 * InBlock + Ct(Statement), Body = Space ^ -1 * Break * EmptyLine ^ 0 * InBlock + Ct(Statement),
Advance = #Cmt(Indent, advance_indent), Advance = L(Cmt(Indent, advance_indent)),
PushIndent = Cmt(Indent, push_indent), PushIndent = Cmt(Indent, push_indent),
PreventIndent = Cmt(Cc(-1), push_indent), PreventIndent = Cmt(Cc(-1), push_indent),
PopIndent = Cmt("", pop_indent), PopIndent = Cmt("", pop_indent),
@ -145,12 +145,12 @@ local build_grammar = wrap_env(debug_grammar, function(root)
CompClause = CompFor + CompForEach + key("when") * Exp / mark("when"), CompClause = CompFor + CompForEach + key("when") * Exp / mark("when"),
Assign = sym("=") * (Ct(With + If + Switch) + Ct(TableBlock + ExpListLow)) / mark("assign"), Assign = sym("=") * (Ct(With + If + Switch) + Ct(TableBlock + ExpListLow)) / mark("assign"),
Update = ((sym("..=") + sym("+=") + sym("-=") + sym("*=") + sym("/=") + sym("%=") + sym("or=") + sym("and=")) / trim) * Exp / mark("update"), Update = ((sym("..=") + sym("+=") + sym("-=") + sym("*=") + sym("/=") + sym("%=") + sym("or=") + sym("and=")) / trim) * Exp / mark("update"),
CharOperators = Space * C(S("+-*/%^><")), CharOperators = Space * C(S("+-*/%^><|&")),
WordOperators = op("or") + op("and") + op("<=") + op(">=") + op("~=") + op("!=") + op("==") + op(".."), WordOperators = op("or") + op("and") + op("<=") + op(">=") + op("~=") + op("!=") + op("==") + op("..") + op("<<") + op(">>") + op("//"),
BinaryOperator = (WordOperators + CharOperators) * SpaceBreak ^ 0, BinaryOperator = (WordOperators + CharOperators) * SpaceBreak ^ 0,
Assignable = Cmt(Chain, check_assignable) + Name + SelfName, Assignable = Cmt(Chain, check_assignable) + Name + SelfName,
Exp = Ct(Value * (BinaryOperator * Value) ^ 0) / flatten_or_mark("exp"), Exp = Ct(Value * (BinaryOperator * Value) ^ 0) / flatten_or_mark("exp"),
SimpleValue = If + Unless + Switch + With + ClassDecl + ForEach + For + While + Cmt(Do, check_do) + sym("-") * -SomeSpace * Exp / mark("minus") + sym("#") * Exp / mark("length") + key("not") * Exp / mark("not") + TblComprehension + TableLit + Comprehension + FunLit + Num, SimpleValue = If + Unless + Switch + With + ClassDecl + ForEach + For + While + Cmt(Do, check_do) + sym("-") * -SomeSpace * Exp / mark("minus") + sym("#") * Exp / mark("length") + sym("~") * Exp / mark("bitnot") + key("not") * Exp / mark("not") + TblComprehension + TableLit + Comprehension + FunLit + Num,
ChainValue = (Chain + Callable) * Ct(InvokeArgs ^ -1) / join_chain, ChainValue = (Chain + Callable) * Ct(InvokeArgs ^ -1) / join_chain,
Value = pos(SimpleValue + Ct(KeyValueList) / mark("table") + ChainValue + String), Value = pos(SimpleValue + Ct(KeyValueList) / mark("table") + ChainValue + String),
SliceValue = SimpleValue + ChainValue, SliceValue = SimpleValue + ChainValue,
@ -170,7 +170,7 @@ local build_grammar = wrap_env(debug_grammar, function(root)
ColonChainItem = symx("\\") * _Name / mark("colon"), ColonChainItem = symx("\\") * _Name / mark("colon"),
ColonChain = ColonChainItem * (Invoke * ChainItems ^ -1) ^ -1, ColonChain = ColonChainItem * (Invoke * ChainItems ^ -1) ^ -1,
Slice = symx("[") * (SliceValue + Cc(1)) * sym(",") * (SliceValue + Cc("")) * (sym(",") * SliceValue) ^ -1 * sym("]") / mark("slice"), Slice = symx("[") * (SliceValue + Cc(1)) * sym(",") * (SliceValue + Cc("")) * (sym(",") * SliceValue) ^ -1 * sym("]") / mark("slice"),
Invoke = FnArgs / mark("call") + SingleString / wrap_func_arg + DoubleString / wrap_func_arg + #P("[") * LuaString / wrap_func_arg, Invoke = FnArgs / mark("call") + SingleString / wrap_func_arg + DoubleString / wrap_func_arg + L(P("[")) * LuaString / wrap_func_arg,
TableValue = KeyValue + Ct(Exp), TableValue = KeyValue + Ct(Exp),
TableLit = sym("{") * Ct(TableValueList ^ -1 * sym(",") ^ -1 * (SpaceBreak * TableLitLine * (sym(",") ^ -1 * SpaceBreak * TableLitLine) ^ 0 * sym(",") ^ -1) ^ -1) * White * sym("}") / mark("table"), TableLit = sym("{") * Ct(TableValueList ^ -1 * sym(",") ^ -1 * (SpaceBreak * TableLitLine * (sym(",") ^ -1 * SpaceBreak * TableLitLine) ^ 0 * sym(",") ^ -1) ^ -1) * White * sym("}") / mark("table"),
TableValueList = TableValue * (sym(",") * TableValue) ^ 0, TableValueList = TableValue * (sym(",") * TableValue) ^ 0,

View File

@ -3,11 +3,10 @@ lpeg = require "lpeg"
lpeg.setmaxstack 10000 -- whoa lpeg.setmaxstack 10000 -- whoa
err_msg = "Failed to parse:%s\n [%d] >> %s" err_msg = "Failed to parse:%s\n [%d] >> %s"
import Stack from require "moonscript.data" import Stack from require "moonscript.data"
import trim, pos_to_line, get_line from require"moonscript.util" import trim, pos_to_line, get_line from require "moonscript.util"
import unpack from require "moonscript.util" import unpack from require "moonscript.util"
import wrap_env from require "moonscript.parse.env" import wrap_env from require "moonscript.parse.env"
@ -17,11 +16,10 @@ import wrap_env from require "moonscript.parse.env"
{ {
:White, :Break, :Stop, :Comment, :Space, :SomeSpace, :SpaceBreak, :EmptyLine, :White, :Break, :Stop, :Comment, :Space, :SomeSpace, :SpaceBreak, :EmptyLine,
:AlphaNum, :Num, :Shebang :AlphaNum, :Num, :Shebang, :L
Name: _Name Name: _Name
} = require "moonscript.parse.literals" } = require "moonscript.parse.literals"
SpaceName = Space * _Name SpaceName = Space * _Name
Num = Space * (Num / (v) -> {"number", v}) Num = Space * (Num / (v) -> {"number", v})
@ -110,7 +108,7 @@ build_grammar = wrap_env debug_grammar, (root) ->
File: Shebang^-1 * (Block + Ct"") File: Shebang^-1 * (Block + Ct"")
Block: Ct(Line * (Break^1 * Line)^0) Block: Ct(Line * (Break^1 * Line)^0)
CheckIndent: Cmt(Indent, check_indent), -- validates line is in correct indent CheckIndent: Cmt(Indent, check_indent), -- validates line is in correct indent
Line: (CheckIndent * Statement + Space * #Stop) Line: (CheckIndent * Statement + Space * L(Stop))
Statement: pos( Statement: pos(
Import + While + With + For + ForEach + Switch + Return + Import + While + With + For + ForEach + Switch + Return +
@ -123,9 +121,9 @@ build_grammar = wrap_env debug_grammar, (root) ->
CompInner / mark"comprehension" CompInner / mark"comprehension"
) * Space)^-1 / wrap_decorator ) * Space)^-1 / wrap_decorator
Body: Space^-1 * Break * EmptyLine^0 * InBlock + Ct(Statement), -- either a statement, or an indented block Body: Space^-1 * Break * EmptyLine^0 * InBlock + Ct(Statement) -- either a statement, or an indented block
Advance: #Cmt(Indent, advance_indent), -- Advances the indent, gives back whitespace for CheckIndent Advance: L Cmt(Indent, advance_indent) -- Advances the indent, gives back whitespace for CheckIndent
PushIndent: Cmt(Indent, push_indent) PushIndent: Cmt(Indent, push_indent)
PreventIndent: Cmt(Cc(-1), push_indent) PreventIndent: Cmt(Cc(-1), push_indent)
PopIndent: Cmt("", pop_indent) PopIndent: Cmt("", pop_indent)
@ -180,8 +178,8 @@ build_grammar = wrap_env debug_grammar, (root) ->
Assign: sym"=" * (Ct(With + If + Switch) + Ct(TableBlock + ExpListLow)) / mark"assign" Assign: sym"=" * (Ct(With + If + Switch) + Ct(TableBlock + ExpListLow)) / mark"assign"
Update: ((sym"..=" + sym"+=" + sym"-=" + sym"*=" + sym"/=" + sym"%=" + sym"or=" + sym"and=") / trim) * Exp / mark"update" Update: ((sym"..=" + sym"+=" + sym"-=" + sym"*=" + sym"/=" + sym"%=" + sym"or=" + sym"and=") / trim) * Exp / mark"update"
CharOperators: Space * C(S"+-*/%^><") CharOperators: Space * C(S"+-*/%^><|&")
WordOperators: op"or" + op"and" + op"<=" + op">=" + op"~=" + op"!=" + op"==" + op".." WordOperators: op"or" + op"and" + op"<=" + op">=" + op"~=" + op"!=" + op"==" + op".." + op"<<" + op">>" + op"//"
BinaryOperator: (WordOperators + CharOperators) * SpaceBreak^0 BinaryOperator: (WordOperators + CharOperators) * SpaceBreak^0
Assignable: Cmt(Chain, check_assignable) + Name + SelfName Assignable: Cmt(Chain, check_assignable) + Name + SelfName
@ -196,6 +194,7 @@ build_grammar = wrap_env debug_grammar, (root) ->
Cmt(Do, check_do) + Cmt(Do, check_do) +
sym"-" * -SomeSpace * Exp / mark"minus" + sym"-" * -SomeSpace * Exp / mark"minus" +
sym"#" * Exp / mark"length" + sym"#" * Exp / mark"length" +
sym"~" * Exp / mark"bitnot" +
key"not" * Exp / mark"not" + key"not" * Exp / mark"not" +
TblComprehension + TblComprehension +
TableLit + TableLit +
@ -251,7 +250,7 @@ build_grammar = wrap_env debug_grammar, (root) ->
Invoke: FnArgs / mark"call" + Invoke: FnArgs / mark"call" +
SingleString / wrap_func_arg + SingleString / wrap_func_arg +
DoubleString / wrap_func_arg + DoubleString / wrap_func_arg +
#P"[" * LuaString / wrap_func_arg L(P"[") * LuaString / wrap_func_arg
TableValue: KeyValue + Ct(Exp) TableValue: KeyValue + Ct(Exp)
@ -282,7 +281,7 @@ build_grammar = wrap_env debug_grammar, (root) ->
op"*" + op"^" + op"*" + op"^" +
Ct(NameList) * (sym"=" * Ct(ExpListLow))^-1) / mark"export" Ct(NameList) * (sym"=" * Ct(ExpListLow))^-1) / mark"export"
KeyValue: (sym":" * -SomeSpace * Name * lpeg.Cp()) / self_assign + Ct((KeyName + sym"[" * Exp * sym"]" + DoubleString + SingleString) * symx":" * (Exp + TableBlock + SpaceBreak^1 * Exp)) KeyValue: (sym":" * -SomeSpace * Name * lpeg.Cp!) / self_assign + Ct((KeyName + sym"[" * Exp * sym"]" + DoubleString + SingleString) * symx":" * (Exp + TableBlock + SpaceBreak^1 * Exp))
KeyValueList: KeyValue * (sym"," * KeyValue)^0 KeyValueList: KeyValue * (sym"," * KeyValue)^0
KeyValueLine: CheckIndent * KeyValueList * sym","^-1 KeyValueLine: CheckIndent * KeyValueList * sym","^-1

View File

@ -5,11 +5,15 @@ do
local _obj_0 = require("lpeg") local _obj_0 = require("lpeg")
S, P, R, C = _obj_0.S, _obj_0.P, _obj_0.R, _obj_0.C S, P, R, C = _obj_0.S, _obj_0.P, _obj_0.R, _obj_0.C
end end
local lpeg = require("lpeg")
local L = lpeg.luversion and lpeg.L or function(v)
return #v
end
local White = S(" \t\r\n") ^ 0 local White = S(" \t\r\n") ^ 0
local plain_space = S(" \t") ^ 0 local plain_space = S(" \t") ^ 0
local Break = P("\r") ^ -1 * P("\n") local Break = P("\r") ^ -1 * P("\n")
local Stop = Break + -1 local Stop = Break + -1
local Comment = P("--") * (1 - S("\r\n")) ^ 0 * #Stop local Comment = P("--") * (1 - S("\r\n")) ^ 0 * L(Stop)
local Space = plain_space * Comment ^ -1 local Space = plain_space * Comment ^ -1
local SomeSpace = S(" \t") ^ 1 * Comment ^ -1 local SomeSpace = S(" \t") ^ 1 * Comment ^ -1
local SpaceBreak = Space * Break local SpaceBreak = Space * Break
@ -19,6 +23,7 @@ local Name = C(R("az", "AZ", "__") * AlphaNum ^ 0)
local Num = P("0x") * R("09", "af", "AF") ^ 1 * (S("uU") ^ -1 * S("lL") ^ 2) ^ -1 + R("09") ^ 1 * (S("uU") ^ -1 * S("lL") ^ 2) + (R("09") ^ 1 * (P(".") * R("09") ^ 1) ^ -1 + P(".") * R("09") ^ 1) * (S("eE") * P("-") ^ -1 * R("09") ^ 1) ^ -1 local Num = P("0x") * R("09", "af", "AF") ^ 1 * (S("uU") ^ -1 * S("lL") ^ 2) ^ -1 + R("09") ^ 1 * (S("uU") ^ -1 * S("lL") ^ 2) + (R("09") ^ 1 * (P(".") * R("09") ^ 1) ^ -1 + P(".") * R("09") ^ 1) * (S("eE") * P("-") ^ -1 * R("09") ^ 1) ^ -1
local Shebang = P("#!") * P(1 - Stop) ^ 0 local Shebang = P("#!") * P(1 - Stop) ^ 0
return safe_module("moonscript.parse.literals", { return safe_module("moonscript.parse.literals", {
L = L,
White = White, White = White,
Break = Break, Break = Break,
Stop = Stop, Stop = Stop,

View File

@ -2,13 +2,16 @@
import safe_module from require "moonscript.util" import safe_module from require "moonscript.util"
import S, P, R, C from require "lpeg" import S, P, R, C from require "lpeg"
lpeg = require "lpeg"
L = lpeg.luversion and lpeg.L or (v) -> #v
White = S" \t\r\n"^0 White = S" \t\r\n"^0
plain_space = S" \t"^0 plain_space = S" \t"^0
Break = P"\r"^-1 * P"\n" Break = P"\r"^-1 * P"\n"
Stop = Break + -1 Stop = Break + -1
Comment = P"--" * (1 - S"\r\n")^0 * #Stop Comment = P"--" * (1 - S"\r\n")^0 * L(Stop)
Space = plain_space * Comment^-1 Space = plain_space * Comment^-1
SomeSpace = S" \t"^1 * Comment^-1 SomeSpace = S" \t"^1 * Comment^-1
@ -29,6 +32,7 @@ Num = P"0x" * R("09", "af", "AF")^1 * (S"uU"^-1 * S"lL"^2)^-1 +
Shebang = P"#!" * P(1 - Stop)^0 Shebang = P"#!" * P(1 - Stop)^0
safe_module "moonscript.parse.literals", { safe_module "moonscript.parse.literals", {
:L
:White, :Break, :Stop, :Comment, :Space, :SomeSpace, :SpaceBreak, :EmptyLine, :White, :Break, :Stop, :Comment, :Space, :SomeSpace, :SpaceBreak, :EmptyLine,
:AlphaNum, :Name, :Num, :Shebang :AlphaNum, :Name, :Num, :Shebang
} }