pmacs3/mode/lua.py

100 lines
3.6 KiB
Python
Raw Normal View History

import subprocess
2009-04-06 02:20:43 -04:00
import time
from tab import StackTabber
from method import Method
from mode import Fundamental
from lex import Grammar, PatternRule, RegionRule, PatternMatchRule
2008-12-07 00:02:28 -05:00
from mode.python import StringGrammar1, StringGrammar2
2009-04-06 02:20:43 -04:00
from mode.pipe import Pipe
from method.shell import Interact
2008-12-07 00:02:28 -05:00
chr1 = '[a-zA-Z_]'
2015-09-29 08:48:35 -04:00
chr2 = '[a-zA-Z_0-9_]'
word = '[a-zA-Z_]' + chr2 + '*'
capword = '[a-zA-Z]' + chr2 + '*'
2008-12-07 00:02:28 -05:00
class LuaGrammar(Grammar):
rules = [
2015-09-29 08:48:35 -04:00
RegionRule('comment', r'--\[\[', Grammar, r'\]\]'),
PatternRule('comment', '--.*$'),
PatternRule('spaces', ' +'),
2009-04-06 02:20:43 -04:00
PatternRule('eol', r'\n'),
2008-12-07 00:02:28 -05:00
2015-09-29 08:48:35 -04:00
RegionRule('lua.string', "'", StringGrammar1, "'"), #fixme
RegionRule('lua.string', '"', StringGrammar2, '"'), #fixme
PatternMatchRule('', '(function)( +)([A-Za-z_][A-Za-z0-9_]*)(:)([A-Za-z_][A-Za-z0-9_]*)', 'lua.keyword', 'spaces', 'lua.class', 'delimiter', 'lua.function'),
PatternMatchRule('', '(function)( +)([A-Za-z_][A-Za-z0-9_]*)', 'lua.keyword', 'spaces', 'lua.function'),
2008-12-07 00:02:28 -05:00
2015-09-29 08:48:35 -04:00
PatternRule('lua.keyword', '(?:while|until|then|return|repeat|or|not|local|in|if|function|for|end|elseif|else|done|do|break|and)(?!' + chr2 + ')'),
PatternRule('lua.reserved', '(?:true|false|nil|self)(?![a-zA-Z0-9_])'),
2009-04-06 02:20:43 -04:00
PatternRule('lua.internal', '_[A-Z]+'),
2015-09-29 08:48:35 -04:00
PatternRule('lua.class', '[A-Z][a-zA-Z0-9_]*'),
2009-04-06 02:20:43 -04:00
PatternRule('lua.identifier', word),
2008-12-07 00:02:28 -05:00
2009-04-06 02:20:43 -04:00
PatternRule('delimiter', r'(?:[=(){}\[\];:,.])'),
PatternRule('lua.operator', r'(?:\.\.\.|\.\.|==|~=|<=|>=|<|>)'),
PatternRule('lua.operator', r'(?:\+|-|/|\*|%|\^)'),
2008-12-07 00:02:28 -05:00
2009-04-06 02:20:43 -04:00
PatternRule('lua.integer', r"(?<![\.0-9a-zA-Z_])(?:0|-?[1-9][0-9]*|0[0-7]+|0[xX][0-9a-fA-F]+)[lL]?(?![\.0-9a-zA-Z_])"),
PatternRule('lua.float', r"(?<![\.0-9a-zA-Z_])(?:-?[0-9]+\.[0-9]*|-?\.[0-9]+|(?:[0-9]|[0-9]+\.[0-9]*|-?\.[0-9]+)[eE][\+-]?[0-9]+)(?![\.0-9a-zA-Z_])"),
2008-12-07 00:02:28 -05:00
]
class LuaCheckSyntax(Method):
2008-12-07 00:02:28 -05:00
'''Check the syntax of a lua file'''
def _execute(self, w, **vargs):
app = w.application
cmd = "luac -p %r" % (w.buffer.path)
(status, output) = subprocess.getstatusoutput(cmd)
2008-12-07 00:02:28 -05:00
if status == 0:
app.set_error("Syntax OK")
app.data_buffer("*Lua-Check-Syntax*", output, switch_to=False)
else:
app.data_buffer("*Lua-Check-Syntax*", output)
2009-04-06 02:20:43 -04:00
class LuaStart(Interact):
args = []
modename = 'luapipe'
2009-04-06 02:23:41 -04:00
reuse = True
2009-04-06 02:20:43 -04:00
def _execute(self, w, **vargs):
2009-04-06 02:23:41 -04:00
Interact._execute(self, w, bname='*Lua*', cmd='lua')
2009-04-06 02:20:43 -04:00
class LuaLoadFile(Interact):
args = []
modename = 'luapipe'
2009-04-06 02:23:41 -04:00
reuse = True
2009-04-06 02:20:43 -04:00
def _execute(self, w, **vargs):
2009-04-06 02:23:41 -04:00
Interact._execute(self, w, bname='*Lua*', cmd='lua')
b = w.application.get_buffer_by_name('*Lua*')
2009-04-06 02:20:43 -04:00
path = w.buffer.path
b.pipe_write('dofile("' + path + '");\n')
class Lua(Fundamental):
name = 'Lua'
2008-12-07 00:02:28 -05:00
extensions = ['.lua']
grammar = LuaGrammar
2009-02-15 12:06:35 -05:00
commentc = '--'
2008-12-07 00:02:28 -05:00
opentokens = ('delimiter',)
opentags = {'(': ')', '[': ']', '{': '}'}
closetokens = ('delimiter',)
closetags = {')': '(', ']': '[', '}': '{'}
2015-09-29 08:48:35 -04:00
colors = {
'lua.class': ('blue225', 'default'),
'lua.reserved': ('magenta505', 'default'),
}
2009-04-06 02:20:43 -04:00
actions = [LuaCheckSyntax, LuaStart, LuaLoadFile]
_bindings = {
2009-02-15 12:06:35 -05:00
'close-paren': (')',),
'close-brace': ('}',),
'close-bracket': (']',),
'lua-check-syntax': ('C-c s',),
}
2008-12-07 00:02:28 -05:00
2009-04-06 02:20:43 -04:00
class LuaPipe(Pipe):
name = 'luapipe'
grammar = LuaGrammar
def install(*args):
Lua.install(*args)
LuaPipe.install(*args)