Skip to content

Commit

Permalink
add support for async iterable, fixes #41
Browse files Browse the repository at this point in the history
  • Loading branch information
plinss committed Aug 9, 2019
1 parent 368c99b commit 2d94815
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 4 deletions.
1 change: 1 addition & 0 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ def test_difference(input, output):
serializer cereal(short one);
iterable<Foo>;
iterable<Foo, Bar>;
async iterable<Foo, Bar>;
readonly maplike<Foo, Bar>;
setlike<Uint8ClampedArray>;
attribute boolean required;
Expand Down
9 changes: 6 additions & 3 deletions widlparser/constructs.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,15 +336,16 @@ def __repr__(self):
return output + ((' [default: ' + repr(self.default) + ']]') if (self.default) else ']')


class InterfaceMember(Construct): # [ExtendedAttributes] Const | Operation | SpecialOperation | Stringifier | StaticMember | Iterable | Attribute | Maplike | Setlike
class InterfaceMember(Construct): # [ExtendedAttributes] Const | Operation | SpecialOperation | Stringifier | StaticMember | AsyncIterable | Iterable | Attribute | Maplike | Setlike
@classmethod
def peek(cls, tokens):
tokens.pushPosition(False)
Construct.peek(tokens)
return tokens.popPosition(Const.peek(tokens) or
Stringifier.peek(tokens) or StaticMember.peek(tokens) or
Iterable.peek(tokens) or Maplike.peek(tokens) or
Setlike.peek(tokens) or Attribute.peek(tokens) or
AsyncIterable.peek(tokens) or Iterable.peek(tokens) or
Maplike.peek(tokens) or Setlike.peek(tokens) or
Attribute.peek(tokens) or
SpecialOperation.peek(tokens) or Operation.peek(tokens))

def __init__(self, tokens, parent):
Expand All @@ -355,6 +356,8 @@ def __init__(self, tokens, parent):
self.member = Stringifier(tokens, parent)
elif (StaticMember.peek(tokens)):
self.member = StaticMember(tokens, parent)
elif (AsyncIterable.peek(tokens)):
self.member = AsyncIterable(tokens, parent)
elif (Iterable.peek(tokens)):
self.member = Iterable(tokens, parent)
elif (Maplike.peek(tokens)):
Expand Down
59 changes: 59 additions & 0 deletions widlparser/productions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1531,6 +1531,65 @@ def __repr__(self):
return output + ']'


class AsyncIterable(ChildProduction): # "async iterable" "<" TypeWithExtendedAttributes "," TypeWithExtendedAttributes ">" ";"
@classmethod
def peek(cls, tokens):
tokens.pushPosition(False)
if (Symbol.peek(tokens, 'async')):
if (Symbol.peek(tokens, 'iterable')):
if (Symbol.peek(tokens, '<')):
if (TypeWithExtendedAttributes.peek(tokens)):
if (Symbol.peek(tokens, ',')):
if (TypeWithExtendedAttributes.peek(tokens)):
token = tokens.peek()
return tokens.popPosition(token and token.isSymbol('>'))
return tokens.popPosition(False)

def __init__(self, tokens, parent):
ChildProduction.__init__(self, tokens, parent)
self._async = Symbol(tokens)
self._iterable = Symbol(tokens)
self._openType = Symbol(tokens, '<')
self.keyType = TypeWithExtendedAttributes(tokens)
self._comma = Symbol(tokens)
self.valueType = TypeWithExtendedAttributes(tokens)
self._closeType = Symbol(tokens, '>')
self._consumeSemicolon(tokens)
self._didParse(tokens)

@property
def idlType(self):
return 'async-iterable'

@property
def name(self):
return '__async_iterable__'

@property
def arguments(self):
return None

def _unicode(self):
output = unicode(self._async) + unicode(self._iterable) + unicode(self._openType)
output += unicode(self.keyType) + unicode(self._comma) + unicode(self.valueType)
return output + unicode(self._closeType)

def _markup(self, generator):
self._async.markup(generator)
self._iterable.markup(generator)
generator.addText(self._openType)
generator.addType(self.keyType)
generator.addText(self._comma)
generator.addType(self.valueType)
generator.addText(self._closeType)
return self

def __repr__(self):
output = '[AsyncIterable: '
output += repr(self.keyType) + ' ' + repr(self.valueType)
return output + ']'


class Maplike(ChildProduction): # ["readonly"] "maplike" "<" TypeWithExtendedAttributes "," TypeWithExtendedAttributes ">" ";"
@classmethod
def peek(cls, tokens):
Expand Down
2 changes: 1 addition & 1 deletion widlparser/tokenizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def __repr__(self):

class Tokenizer(object):
SymbolIdents = frozenset((
'any', 'attribute', 'ArrayBuffer', 'boolean', 'byte', 'ByteString', 'callback', 'const', 'creator', 'DataView',
'any', 'async', 'attribute', 'ArrayBuffer', 'boolean', 'byte', 'ByteString', 'callback', 'const', 'creator', 'DataView',
'deleter', 'dictionary', 'DOMString', 'double', 'enum', 'Error', 'exception', 'false', 'float',
'Float32Array', 'Float64Array', 'FrozenArray', 'getter', 'implements', 'includes', 'Infinity', '-Infinity', 'inherit', 'Int8Array',
'Int16Array', 'Int32Array', 'interface', 'iterable', 'legacycaller', 'legacyiterable', 'long', 'maplike', 'mixin',
Expand Down

0 comments on commit 2d94815

Please sign in to comment.