parent
9998febf3d
commit
050f68bc69
63
mode/xml.py
63
mode/xml.py
|
@ -1,5 +1,40 @@
|
|||
import color, method, mode
|
||||
from lex import Grammar, PatternRule, RegionRule
|
||||
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
|
||||
|
||||
class StringGrammar1(Grammar):
|
||||
rules = [
|
||||
|
@ -12,6 +47,8 @@ class StringGrammar2(Grammar):
|
|||
PatternRule(r'escaped', r'&[a-z]+;'),
|
||||
]
|
||||
|
||||
class CDataGrammar(Grammar):
|
||||
rules = [PatternRule(r'data', r'(?:[^\]]|\](?!\])|\]\](?!>))+')]
|
||||
class CommentGrammar(Grammar):
|
||||
rules = [PatternRule(r'data', r'(?:[^-]|-(?!-)|--(?!>))+')]
|
||||
class MetadataGrammar(Grammar):
|
||||
|
@ -35,7 +72,8 @@ class XMLGrammar(Grammar):
|
|||
PatternRule(r'data', r'[^<& \n]+'),
|
||||
PatternRule(r'spaces', r' +'),
|
||||
RegionRule(r'comment', r'<!--', CommentGrammar, r'-->'),
|
||||
RegionRule(r'xml_tag', r'<(?!\?)', TagGrammar, r'/?>'),
|
||||
RegionRule(r'xml_cdata', r'<!\[CDATA\[', CDataGrammar, r'\]\]>'),
|
||||
RegionRule(r'xml_tag', r'<(?![?!])', TagGrammar, r'/?>'),
|
||||
PatternRule(r'xml_entity', r'&[a-z]+;'),
|
||||
PatternRule(r'eol', r'\n'),
|
||||
RegionRule(r'xml_metadata', r'<\?', MetadataGrammar, r'\?>'),
|
||||
|
@ -58,6 +96,22 @@ class XmlCreateTag(method.Method):
|
|||
w.insert_string_at_cursor("</%s>" % t)
|
||||
w.goto(p)
|
||||
|
||||
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)
|
||||
|
||||
class XML(mode.Fundamental):
|
||||
modename = 'XML'
|
||||
extensions = ['.xml', '.xml.in', '.xsl', '.xsd']
|
||||
|
@ -75,8 +129,11 @@ class XML(mode.Fundamental):
|
|||
'xml_tag.string.end': ('green', 'default', 'bold'),
|
||||
'xml_tag.end': ('default', 'default', 'bold'),
|
||||
'xml_entity': ('magenta', 'default', 'bold'),
|
||||
'xml_cdata.start': ('magenta', 'default', 'bold'),
|
||||
'xml_cdata.data': ('green', 'default', 'bold'),
|
||||
'xml_cdata.end': ('magenta', 'default', 'bold'),
|
||||
}
|
||||
actions = [XmlValidate, XmlCreateTag]
|
||||
actions = [XmlValidate, XmlCreateTag, XmlCreateComment, XmlCreateCdata]
|
||||
_bindings = {
|
||||
'xml-create-tag': ('M-t',),
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue