2008-04-19 11:10:15 -04:00
|
|
|
import color, method, mode
|
2009-02-16 23:40:11 -05:00
|
|
|
from lex import Grammar, Rule, PatternRule, RegionRule
|
|
|
|
|
|
|
|
class PatternGroupRule(PatternRule):
|
|
|
|
def __init__(self, name, *args):
|
|
|
|
assert args and len(args) % 2 == 0
|
|
|
|
i = 0
|
|
|
|
pairs = []
|
|
|
|
while i < len(args):
|
|
|
|
tokname, pattern = args[i], args[i+1]
|
|
|
|
pairs.append((tokname, re.compile(pattern, self.reflags)))
|
|
|
|
i += 2
|
|
|
|
Rule.__init__(self, name)
|
|
|
|
self.pairs = tuple(pairs)
|
|
|
|
def match(self, lexer, parent):
|
|
|
|
(x, y) = (lexer.x, lexer.y)
|
|
|
|
matches = []
|
|
|
|
for (tokname, tokre) in self.pairs:
|
|
|
|
if y >= len(lexer.lines):
|
|
|
|
return []
|
|
|
|
line = self.get_line(lexer, y)
|
|
|
|
m = tokre.match(line, x)
|
|
|
|
if m:
|
|
|
|
x += len(m.group(0))
|
|
|
|
if x >= len(line):
|
|
|
|
x = 0
|
|
|
|
y += 1
|
|
|
|
matches.append((tokname, m))
|
|
|
|
else:
|
|
|
|
return []
|
|
|
|
assert len(matches) == len(self.pairs)
|
|
|
|
return matches
|
|
|
|
def lex(self, lexer, parent, matches):
|
|
|
|
if matches:
|
|
|
|
for (tokname, m) in matches:
|
|
|
|
yield self.make_token(lexer, m.group(0), tokname, parent, m.groupdict())
|
|
|
|
raise StopIteration
|
2007-07-21 11:40:53 -04:00
|
|
|
|
2008-09-30 17:04:35 -04:00
|
|
|
class StringGrammar1(Grammar):
|
2008-09-30 17:11:50 -04:00
|
|
|
rules = [
|
|
|
|
PatternRule(r'data', r'[^"&]+'),
|
|
|
|
PatternRule(r'escaped', r'&[a-z]+;'),
|
|
|
|
]
|
2008-09-30 17:04:35 -04:00
|
|
|
class StringGrammar2(Grammar):
|
2008-09-30 17:11:50 -04:00
|
|
|
rules = [
|
|
|
|
PatternRule(r'data', r"[^'&]+"),
|
|
|
|
PatternRule(r'escaped', r'&[a-z]+;'),
|
|
|
|
]
|
2008-09-30 17:04:35 -04:00
|
|
|
|
2009-02-16 23:40:11 -05:00
|
|
|
class CDataGrammar(Grammar):
|
|
|
|
rules = [PatternRule(r'data', r'(?:[^\]]|\](?!\])|\]\](?!>))+')]
|
2008-09-30 17:04:35 -04:00
|
|
|
class CommentGrammar(Grammar):
|
|
|
|
rules = [PatternRule(r'data', r'(?:[^-]|-(?!-)|--(?!>))+')]
|
|
|
|
class MetadataGrammar(Grammar):
|
|
|
|
rules = [PatternRule(r'data', r'(?:[^?]|\?(?!>))+')]
|
|
|
|
|
2007-07-21 11:40:53 -04:00
|
|
|
class TagGrammar(Grammar):
|
|
|
|
rules = [
|
2008-09-30 17:04:35 -04:00
|
|
|
PatternRule(r'attrname', r'[a-zA-Z_][a-zA-Z0-9_]+(?==)'),
|
|
|
|
PatternRule(r'namespace', r'[a-zA-Z_]+(?=:)'),
|
2008-11-08 19:15:33 -05:00
|
|
|
PatternRule(r'name', r'[a-zA-Z_][a-zA-Z0-9_]*'),
|
2008-09-30 17:04:35 -04:00
|
|
|
PatternRule(r'delimiter', r'[/=]'),
|
|
|
|
RegionRule(r'string', r'"', StringGrammar1, r'"'),
|
|
|
|
RegionRule(r'string', r"'", StringGrammar2, r"'"),
|
|
|
|
PatternRule(r'spaces', r' +'),
|
|
|
|
PatternRule(r'eol', r'\n'),
|
2007-07-21 11:40:53 -04:00
|
|
|
]
|
|
|
|
|
|
|
|
class XMLGrammar(Grammar):
|
|
|
|
rules = [
|
|
|
|
# TODO: how does cdata work again?
|
2008-09-30 17:04:35 -04:00
|
|
|
PatternRule(r'data', r'[^<& \n]+'),
|
|
|
|
PatternRule(r'spaces', r' +'),
|
|
|
|
RegionRule(r'comment', r'<!--', CommentGrammar, r'-->'),
|
2009-02-16 23:40:11 -05:00
|
|
|
RegionRule(r'xml_cdata', r'<!\[CDATA\[', CDataGrammar, r'\]\]>'),
|
|
|
|
RegionRule(r'xml_tag', r'<(?![?!])', TagGrammar, r'/?>'),
|
2008-09-30 17:04:35 -04:00
|
|
|
PatternRule(r'xml_entity', r'&[a-z]+;'),
|
|
|
|
PatternRule(r'eol', r'\n'),
|
2008-09-30 17:11:50 -04:00
|
|
|
RegionRule(r'xml_metadata', r'<\?', MetadataGrammar, r'\?>'),
|
2007-07-21 11:40:53 -04:00
|
|
|
]
|
|
|
|
|
2008-04-19 11:10:15 -04:00
|
|
|
class XmlValidate(method.shell.Exec):
|
|
|
|
'''Valid the buffer's contents as valid XML.'''
|
|
|
|
show_success = True
|
|
|
|
args = []
|
|
|
|
def _execute(self, w, **vargs):
|
|
|
|
self._doit(w, w.buffer.path, 'xmlwf %(path)r', cmdname='xml-validate')
|
|
|
|
|
2008-05-06 01:23:58 -04:00
|
|
|
class XmlCreateTag(method.Method):
|
2009-01-28 16:28:33 -05:00
|
|
|
'''Create an opening and closing tag'''
|
2008-05-06 01:23:58 -04:00
|
|
|
args = [method.Argument('tagname', prompt="Tag Name: ", help="Create an open/close tag pair")]
|
|
|
|
def _execute(self, w, **vargs):
|
|
|
|
t = vargs['tagname']
|
|
|
|
w.insert_string_at_cursor("<%s>" % t)
|
|
|
|
p = w.logical_cursor()
|
|
|
|
w.insert_string_at_cursor("</%s>" % t)
|
|
|
|
w.goto(p)
|
|
|
|
|
2009-02-16 23:40:11 -05:00
|
|
|
class XmlCreateComment(method.Method):
|
|
|
|
'''Create an opening and closing tag'''
|
|
|
|
def _execute(self, w, **vargs):
|
|
|
|
w.insert_string_at_cursor("<!-- ")
|
|
|
|
p = w.logical_cursor()
|
|
|
|
w.insert_string_at_cursor(" -->")
|
|
|
|
w.goto(p)
|
|
|
|
|
|
|
|
class XmlCreateCdata(method.Method):
|
|
|
|
'''Create an opening and closing tag'''
|
|
|
|
def _execute(self, w, **vargs):
|
|
|
|
w.insert_string_at_cursor("<![CDATA[")
|
|
|
|
p = w.logical_cursor()
|
|
|
|
w.insert_string_at_cursor("]]>")
|
|
|
|
w.goto(p)
|
|
|
|
|
2007-10-21 20:55:29 -04:00
|
|
|
class XML(mode.Fundamental):
|
2009-03-17 15:24:10 -04:00
|
|
|
name = 'XML'
|
2008-06-16 09:45:13 -04:00
|
|
|
extensions = ['.xml', '.xml.in', '.xsl', '.xsd']
|
2007-10-18 17:07:35 -04:00
|
|
|
grammar = XMLGrammar
|
|
|
|
colors = {
|
2008-09-30 17:11:50 -04:00
|
|
|
'xml_metadata.start': ('magenta', 'default', 'bold'),
|
|
|
|
'xml_metadata.data': ('magenta', 'default', 'bold'),
|
|
|
|
'xml_metadata.end': ('magenta', 'default', 'bold'),
|
2008-05-03 13:31:30 -04:00
|
|
|
'xml_tag.start': ('default', 'default', 'bold'),
|
|
|
|
'xml_tag.namespace': ('magenta', 'default', 'bold'),
|
|
|
|
'xml_tag.name': ('blue', 'default', 'bold'),
|
|
|
|
'xml_tag.attrname': ('cyan', 'default', 'bold'),
|
|
|
|
'xml_tag.string.start': ('green', 'default', 'bold'),
|
|
|
|
'xml_tag.string.null': ('green', 'default', 'bold'),
|
|
|
|
'xml_tag.string.end': ('green', 'default', 'bold'),
|
|
|
|
'xml_tag.end': ('default', 'default', 'bold'),
|
2008-05-06 02:03:47 -04:00
|
|
|
'xml_entity': ('magenta', 'default', 'bold'),
|
2009-02-16 23:40:11 -05:00
|
|
|
'xml_cdata.start': ('magenta', 'default', 'bold'),
|
|
|
|
'xml_cdata.data': ('green', 'default', 'bold'),
|
|
|
|
'xml_cdata.end': ('magenta', 'default', 'bold'),
|
2007-07-21 11:40:53 -04:00
|
|
|
}
|
2009-02-16 23:40:11 -05:00
|
|
|
actions = [XmlValidate, XmlCreateTag, XmlCreateComment, XmlCreateCdata]
|
2009-02-15 12:06:35 -05:00
|
|
|
_bindings = {
|
|
|
|
'xml-create-tag': ('M-t',),
|
|
|
|
}
|
2007-10-19 02:41:33 -04:00
|
|
|
install = XML.install
|