Skip to content

Add the Placeable node #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 30, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions fluent/migrate/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,12 @@ def replace(acc, cur):
parts, rest = acc
before, key, after = rest.value.partition(cur)

placeable = FTL.Placeable(replacements[key])

# Return the elements found and converted so far, and the remaining
# text which hasn't been scanned for placeables yet.
return (
parts + [FTL.TextElement(before), replacements[key]],
parts + [FTL.TextElement(before), placeable],
FTL.TextElement(after)
)

Expand Down Expand Up @@ -240,7 +242,8 @@ def createVariant(zipped_enum):
variants=map(createVariant, enumerate(zip(keys, variants)))
)

return FTL.Pattern([select])
placeable = FTL.Placeable(select)
return FTL.Pattern([placeable])


class CONCAT(Transform):
Expand All @@ -256,7 +259,7 @@ def concat_elements(acc, cur):
acc.extend(cur.elements)
return acc
elif (isinstance(cur, FTL.TextElement) or
isinstance(cur, FTL.Expression)):
isinstance(cur, FTL.Placeable)):
acc.append(cur)
return acc

Expand Down
5 changes: 5 additions & 0 deletions fluent/syntax/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,11 @@ def __init__(self, value, **kwargs):
super(TextElement, self).__init__(**kwargs)
self.value = value

class Placeable(SyntaxNode):
def __init__(self, expression, **kwargs):
super(Placeable, self).__init__(**kwargs)
self.expression = expression

class Expression(SyntaxNode):
def __init__(self, **kwargs):
super(Expression, self).__init__(**kwargs)
Expand Down
39 changes: 15 additions & 24 deletions fluent/syntax/ftlstream.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@
from .errors import ParseError


INLINE_WS = (' ', '\t')


class FTLParserStream(ParserStream):
def peek_line_ws(self):
def peek_inline_ws(self):
ch = self.current_peek()
while ch:
if ch != ' ' and ch != '\t':
if ch not in INLINE_WS:
break
ch = self.peek()

def skip_ws_lines(self):
def skip_blank_lines(self):
while True:
self.peek_line_ws()
self.peek_inline_ws()

if self.current_peek_is('\n'):
self.skip_to_peek()
Expand All @@ -22,9 +25,9 @@ def skip_ws_lines(self):
self.reset_peek()
break

def skip_line_ws(self):
def skip_inline_ws(self):
while self.ch:
if self.ch != ' ' and self.ch != '\t':
if self.ch not in INLINE_WS:
break
self.next()

Expand Down Expand Up @@ -67,19 +70,6 @@ def is_number_start(self):

return (cc >= 48 and cc <= 57) or cc == 45

def is_peek_next_line_indented(self):
if not self.current_peek_is('\n'):
return False

self.peek()

if self.current_peek_is(' '):
self.reset_peek()
return True

self.reset_peek()
return False

def is_peek_next_line_variant_start(self):
if not self.current_peek_is('\n'):
return False
Expand All @@ -88,7 +78,7 @@ def is_peek_next_line_variant_start(self):

ptr = self.get_peek_index()

self.peek_line_ws()
self.peek_inline_ws()

if (self.get_peek_index() - ptr == 0):
self.reset_peek()
Expand All @@ -112,7 +102,7 @@ def is_peek_next_line_attribute_start(self):

ptr = self.get_peek_index()

self.peek_line_ws()
self.peek_inline_ws()

if (self.get_peek_index() - ptr == 0):
self.reset_peek()
Expand All @@ -133,7 +123,7 @@ def is_peek_next_line_pattern(self):

ptr = self.get_peek_index()

self.peek_line_ws()
self.peek_inline_ws()

if (self.get_peek_index() - ptr == 0):
self.reset_peek()
Expand All @@ -159,7 +149,7 @@ def is_peek_next_line_tag_start(self):

ptr = self.get_peek_index()

self.peek_line_ws()
self.peek_inline_ws()

if (self.get_peek_index() - ptr == 0):
self.reset_peek()
Expand All @@ -173,14 +163,15 @@ def is_peek_next_line_tag_start(self):
return False

def skip_to_next_entry_start(self):
while self.next():
while self.ch:
if self.current_is('\n') and not self.peek_char_is('\n'):
self.next()

if self.ch is None or self.is_id_start() or \
(self.current_is('/') and self.peek_char_is('/')) or \
(self.current_is('[') and self.peek_char_is('[')):
break
self.next()

def take_id_start(self):
if self.is_id_start():
Expand Down
Loading