Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add support for priority check + a few small fixes

--HG--
branch : sneakylang
  • Loading branch information...
commit 2944fd4453b8ba005f9e50c7fad91dec65ff1aa9 1 parent 95a537f
andros authored
View
4 sneakylang/node.py
@@ -46,7 +46,7 @@ def add_child(self, node, position=None):
if isinstance(node, TextNode):
if self.actual_text_content is not None:
- raise ValueError, 'Adding a text node, but one is alread present'
+ raise ValueError('Adding a text node, but one is already present')
self.actual_text_content = node
else:
self.actual_text_content = None
@@ -65,7 +65,7 @@ def insert_child(self, node, index):
""" Insert child on given position """
if isinstance(node, TextNode):
if self.actual_text_content is not None:
- raise ValueError, 'Adding a text node, but one is alread present'
+ raise ValueError('Adding a text node, but one is already present')
self.actual_text_content = node
else:
self.actual_text_content = None
View
8 sneakylang/parser.py
@@ -164,14 +164,14 @@ def parse(stream, register_map, register=None, parsers=None, state=None, builder
stream = stream_new
opened_text_node = None
else:
- # macro not resolved, add text node
+ logging.debug('Macro not resolved, add text node')
node, stream = _get_text_node(stream, register, register_map, builder, state, opened_text_node=opened_text_node, whole_stream=whole_stream)
if opened_text_node is None:
builder.append(node, move_actual=False)
- opened_text_node=node
+ opened_text_node = node
except (ParserRollback, MacroCallError):
- #badly resolved macro
- logging.debug('Catched ParseRollback, forcing text char')
+ # badly resolved macro
+ logging.debug('ParserRollback caught, forcing text char')
node, stream = _get_text_node(stream, register, register_map, builder, state, True, opened_text_node=opened_text_node, whole_stream=whole_stream)
if opened_text_node is None:
builder.append(node, move_actual=False)
View
16 sneakylang/register.py
@@ -92,9 +92,20 @@ def _most_matching(self, matching):
most = None
length = 0
for m in matching:
- if len(m.string[m.start():m.end()]) > length:
+ mlen = len(m.string[m.start():m.end()])
+
+ if mlen > length:
most = m
length = len(m.string[m.start():m.end()])
+ elif mlen == length:
+ logging.debug('Two or more parsers are matching, ' \
+ 'performing the priority check')
+ m_parser = self.parser_start[m.re.pattern[1:]][1]
+ most_parser = self.parser_start[most.re.pattern[1:]][1]
+ if getattr(m_parser, 'priority', 0) > \
+ getattr(most_parser, 'priority', 0):
+ most = m
+
if most is None:
return (None, None)
return (self.parser_start[most.re.pattern[1:]][1], most.string[most.start():most.end()])
@@ -110,7 +121,7 @@ def resolve_parser(self, stream, register, whole_stream=None):
for start in self.parser_start:
compiled, parser = self.parser_start[start]
if start.find('^') != -1:
- if compiled.match(whole_stream):
+ if compiled.match(whole_stream) and stream == whole_stream:
matching.append(compiled.match(whole_stream))
else:
if compiled.match(stream):
@@ -191,6 +202,7 @@ def resolve_macro(self, stream, builder, state=None, whole_stream=None):
whole_stream = stream
parser = self.parser_register.resolve_parser(stream, self, whole_stream)
+
if parser is not None:
# Macro resolved in alternate syntax, use parser to get pacro
macro, stream_new = parser.get_macro(builder, state)
View
2  sneakylang/treebuilder.py
@@ -106,4 +106,4 @@ def set_actual_node(self, node):
else:
raise ValueError, 'Node %s not found in tree' % node
- actual_node = property(fget=get_actual_node)
+ actual_node = property(fget=get_actual_node)
Please sign in to comment.
Something went wrong with that request. Please try again.